Skip to content

Commit

Permalink
Specify utf-8 as the default read encoding for files to avoid problem…
Browse files Browse the repository at this point in the history
…s when Windows defaults to another encoding.
  • Loading branch information
fpereiro committed Apr 26, 2021
1 parent 5549ca5 commit fa65a2b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
TRANSLATIONS = hedyweb.Translations()

# Load main menu (do it once, can be cached)
with open(f'main/menu.json', 'r') as f:
with open(f'main/menu.json', 'r', encoding='utf-8') as f:
main_menu_json = json.load(f)


Expand Down Expand Up @@ -399,7 +399,7 @@ def main_page(page):
effective_lang = 'en'

try:
with open(f'main/{page}-{effective_lang}.md', 'r') as f:
with open(f'main/{page}-{effective_lang}.md', 'r', encoding='utf-8') as f:
contents = f.read()
except IOError:
abort(404)
Expand Down
2 changes: 1 addition & 1 deletion courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Doc:

def load_yaml(filename):
try:
with open(filename, 'r') as f:
with open(filename, 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
except IOError:
return {}
4 changes: 2 additions & 2 deletions docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def add_to_index(self, doc):
class MarkdownDoc:
@staticmethod
def from_file(filename):
with open(filename, 'r') as f:
with open(filename, 'r', encoding='utf-8') as f:
contents = f.read()
parts = re.split('^---+$', contents, maxsplit=1, flags=re.M)
if len(parts) == 1:
Expand All @@ -67,4 +67,4 @@ def from_file(filename):

def __init__(self, front_matter, doc):
self.front_matter = front_matter
self.markdown = doc
self.markdown = doc
12 changes: 6 additions & 6 deletions hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
13: ['print', 'ask', 'is', 'if', 'for', 'elif']
}

#
#
# closest_command() searches for known commands in an invalid command.
#
# It will return the known command which is closest positioned at the beginning.
# It will return '' if the invalid command does not contain any known command.
# It will return '' if the invalid command does not contain any known command.
#

def closest_command(invalid_command, known_commands):

# First search for 100% match of known commands
min_position = len(invalid_command)
min_command = ''
Expand All @@ -42,7 +42,7 @@ def closest_command(invalid_command, known_commands):
if position != -1 and position < min_position:
min_position = position
min_command = known_command

# If not found, search for partial match of know commands
if min_command == '':
min_command = closest_command_with_min_distance(invalid_command, known_commands)
Expand Down Expand Up @@ -171,7 +171,7 @@ def input(self,args):
return args[0].children

def create_parser(level):
with open(f"grammars/level{str(level)}.lark", "r") as file:
with open(f"grammars/level{str(level)}.lark", "r", encoding="utf-8") as file:
grammar = file.read()
return Lark(grammar)

Expand Down Expand Up @@ -745,7 +745,7 @@ def ask(self, children):
def create_grammar(level):
# Load Lark grammars relative to directory of current file
script_dir = path.abspath(path.dirname(__file__))
with open(path.join(script_dir, "grammars", "level" + str(level) + ".lark"), "r") as file:
with open(path.join(script_dir, "grammars", "level" + str(level) + ".lark"), "r", encoding="utf-8") as file:
return file.read()


Expand Down
8 changes: 4 additions & 4 deletions runhedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def print(uitvoer):
sys.stdout.write(uitvoer + '\n')

def main():
# python3 hedy.py <filename(0)> [levelargument(1)] [level(2)]
# python3 hedy.py <filename(0)> [levelargument(1)] [level(2)]

args = sys.argv[1:]
args_length = len(args)
Expand All @@ -30,9 +30,9 @@ def main():
level = 8
except ValueError:
print("Level argument is not an integer, skipping argument.")

lines = ""
with open(filename, 'r') as f:
with open(filename, 'r', encoding='utf-8') as f:
lines = f.readlines()

if level == 0:
Expand All @@ -58,4 +58,4 @@ def main():
sys.exit(1)

if __name__ == '__main__':
main()
main()

0 comments on commit fa65a2b

Please sign in to comment.