Skip to content

Commit

Permalink
Fix whitespace regex replacement and file encoding (#11)
Browse files Browse the repository at this point in the history
* fix: py2gyat whitespace in regex

* use utf-8 encoding
  • Loading branch information
lambda-abstraction authored Dec 17, 2024
1 parent 4a0b258 commit f3eb668
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions pygyat/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def parse_glazes(filename):
list of str: All imported modules, suffixed with '.gyat'. Ie, the name
the imported files must have if they are pygyat files.
"""
infile = open(filename, "r")
infile = open(filename, "r", encoding="utf-8")
infile_str = ""

for line in infile:
Expand Down Expand Up @@ -119,8 +119,8 @@ def parse_file(filepath, filename_prefix, outputname=None, change_imports=None):
"""
filename = os.path.basename(filepath)

infile = open(filepath, "r")
outfile = open(filename_prefix + _change_file_name(filename, outputname), "w")
infile = open(filepath, "r", encoding="utf-8")
outfile = open(filename_prefix + _change_file_name(filename, outputname), "w", encoding="utf-8")

# Read file to string
infile_str_raw = ""
Expand Down
10 changes: 5 additions & 5 deletions scripts/py2gyat
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def reverse_parse(filename, outputname):
outputname (str): Path of destination file
"""
# Open a file as bytes
infile = open(filename, "r")
infile = open(filename, "r", encoding="utf-8")
inlines = infile.readlines()

# Read file to string
Expand Down Expand Up @@ -191,7 +191,7 @@ def reverse_parse(filename, outputname):
# replace anything in mappings.keys() with its value but opposite
for value, key in GYAT2PY_MAPPINGS.items():
if "\\s" in value:
value = value.replace("\\s", "s")
value = value.replace("\\s+", " ")
line = safe_substitute(value, key, line)
line = re.sub(
r'(?<!["\'#])\b{}\b(?!["\'])'.format(re.escape(key)), value, line
Expand All @@ -200,7 +200,7 @@ def reverse_parse(filename, outputname):
infile_str_indented += line + add_comment + "\n"

# Save the file
outfile = open(outputname, "w")
outfile = open(outputname, "w", encoding="utf-8")
outfile.write(infile_str_indented)


Expand Down Expand Up @@ -239,11 +239,11 @@ def main():
else change_file_name(cmd_args.input[0], None)
)

infile = open(cmd_args.input[0], "r")
infile = open(cmd_args.input[0], "r", encoding="utf-8")
infile_string = "".join(infile.readlines())

# pre_parsed = pre_reverse_parse(infile_string)
tempoutfile = open(cmd_args.input[0] + ".py2gyattemp", "w")
tempoutfile = open(cmd_args.input[0] + ".py2gyattemp", "w", encoding="utf-8")
# tempoutfile.write(pre_parsed)
tempoutfile.write(infile_string)
tempoutfile.close()
Expand Down

0 comments on commit f3eb668

Please sign in to comment.