Specify UTF-8 encoding, since it's not default on Windows

https://peps.python.org/pep-0597/#using-the-default-encoding-is-a-common-mistake
This commit is contained in:
Isaiah Odhner 2023-07-17 19:29:34 -04:00
parent 033e86f4e7
commit fc395e2db9
8 changed files with 21 additions and 20 deletions

View File

@ -97,11 +97,11 @@ def write_ansi_file(file: TextIO) -> None:
# Generate and write to a file
file_path = os.path.join(os.path.dirname(__file__), f'../samples/{box_inner_width}x{box_inner_height}_font_template.ans')
file_path = os.path.abspath(file_path)
with open(file_path, 'w') as file:
with open(file_path, 'w', encoding='utf-8') as file:
write_ansi_file(file)
# Print the art to the terminal
with open(file_path, 'r') as file:
with open(file_path, 'r', encoding='utf-8') as file:
print(file.read())
# Print the path to the file, and resulting file size

View File

@ -65,11 +65,11 @@ height = 24
# Generate and write the ANSI art to a file
file_path = os.path.join(os.path.dirname(__file__), '../samples/gradient_test.ans')
file_path = os.path.abspath(file_path)
with open(file_path, 'w') as file:
with open(file_path, 'w', encoding='utf-8') as file:
generate_ansi_art(width, height, file)
# Print the art to the terminal
with open(file_path, 'r') as file:
with open(file_path, 'r', encoding='utf-8') as file:
print(file.read())
# Print the path to the file, and resulting file size

View File

@ -167,9 +167,9 @@ full_size_flf_output_path = os.path.join(font_folder, 'NanoTiny_v14_4x4.flf')
extracted_image, extracted_text_half, extracted_text_full = extract_textures(image_input_path)
extracted_image.save(image_output_path)
print(f'Wrote extracted textures to {image_output_path}')
with open(full_size_flf_output_path, 'w') as f:
with open(full_size_flf_output_path, 'w', encoding='utf-8') as f:
f.write(extracted_text_full)
print(f'Wrote FIGlet font {full_size_flf_output_path}')
with open(half_size_flf_output_path, 'w') as f:
with open(half_size_flf_output_path, 'w', encoding='utf-8') as f:
f.write(extracted_text_half)
print(f'Wrote FIGlet font {half_size_flf_output_path}')

View File

@ -893,7 +893,8 @@ class NodeInfo(Container):
# parse the python file to find the line number of the widget definition
# could use `ast` module for robustness
# to avoid things like finding DEFAULT_CSS from the wrong widget
with open(path) as f:
# TODO: handle read/decode errors
with open(path, encoding="utf-8") as f:
lines = f.readlines()
for i, line in enumerate(lines):
if f"class {widget_name}" in line:

View File

@ -30,7 +30,7 @@ def load_language(language_code: str):
return
try:
file = os.path.join(localization_folder, language_code, "localizations.js")
with open(file, "r") as f:
with open(file, "r", encoding="utf-8") as f:
# find the JSON object
js = f.read()
start = js.find("{")
@ -49,7 +49,7 @@ def load_language(language_code: str):
if TRACK_UNTRANSLATED:
untranslated: set[str] = set()
try:
with open(untranslated_file, "r") as f:
with open(untranslated_file, "r", encoding="utf-8") as f:
untranslated = set(f.read().splitlines())
except FileNotFoundError:
pass
@ -87,7 +87,7 @@ def get(base_language_str: str, *interpolations: str) -> str:
if base_language_str not in untranslated and current_language != base_language:
untranslated.add(base_language_str)
# append to untranslated strings file
with open(untranslated_file, "a") as f:
with open(untranslated_file, "a", encoding="utf-8") as f:
f.write(base_language_str + "\n")
return base_language_str

View File

@ -90,10 +90,10 @@ loaded_localizations("{target_lang}", {localizations_json});
f.write(js)
# file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "index.html"))
# with open(file_path, "r") as f:
# with open(file_path, "r", encoding="utf-8") as f:
# code = f.read()
# available_langs_json = json.dumps(available_langs, ensure_ascii=False).replace('","', '", "')
# code = re.sub(r"(available_languages\s*=\s*)\[[^\]]*\]", f"$1{available_langs_json}]", code)
# with open(file_path, "w") as f:
# with open(file_path, "w", encoding="utf-8") as f:
# f.write(code)
# print(f'Updated available_languages list in "{file_path}"')

View File

@ -85,7 +85,7 @@ def update_cli_help_on_readme():
readme_help_start = re.compile(r"```\n.*--help\n")
readme_help_end = re.compile(r"```")
readme_file_path = os.path.join(os.path.dirname(__file__), "../../README.md")
with open(readme_file_path, "r+") as f:
with open(readme_file_path, "r+", encoding="utf-8") as f:
# By default, argparse uses the terminal width for formatting help text,
# even when using format_help() to get a string.
# The only way to override that is to override the formatter_class.
@ -150,7 +150,7 @@ class MetaGlyphFont:
# I could install the font, with FigletFont.installFonts,
# maybe with some prefixed name, but I don't want to do that.
with open(self.file_path) as f:
with open(self.file_path, encoding="utf-8") as f:
flf = f.read()
fig_font = FigletFont()
fig_font.data = flf
@ -2746,7 +2746,7 @@ class PaintApp(App[None]):
if os.path.getsize(backup_file_path) > MAX_FILE_SIZE:
self.message_box(_("Open"), _("A backup file was found, but was not recovered.") + "\n" + _("The file is too large to open."), "ok")
return
with open(backup_file_path, "r") as f:
with open(backup_file_path, "r", encoding="utf-8") as f:
backup_content = f.read()
backup_image = AnsiArtDocument.from_text(backup_content)
self.backup_checked_for = backup_file_path
@ -3326,7 +3326,7 @@ class PaintApp(App[None]):
if os.path.getsize(file_path) > MAX_FILE_SIZE:
self.message_box(_("Paste"), _("The file is too large to open."), "ok")
return
with open(file_path, "r") as f:
with open(file_path, "r", encoding="utf-8") as f:
# TODO: handle pasting image files
self.paste(f.read())
window.close()
@ -3484,7 +3484,7 @@ class PaintApp(App[None]):
def handle_selected_file_path(file_path: str) -> None:
try:
with open(file_path, "r") as f:
with open(file_path, "r", encoding="utf-8") as f:
self.load_palette(f.read())
except UnicodeDecodeError:
self.message_box(_("Open"), file_path + "\n" + _("Paint cannot read this file.") + "\n" + _("Unexpected file format."), "ok")
@ -3564,7 +3564,7 @@ Columns: {len(palette) // 2}
os.makedirs(dir, exist_ok=True)
svg = self.image.get_svg()
image_path = os.path.join(dir, "wallpaper.svg")
with open(image_path, "w") as f:
with open(image_path, "w", encoding="utf-8") as f:
f.write(svg)
set_wallpaper(image_path)
except Exception as e:
@ -3916,7 +3916,7 @@ Columns: {len(palette) // 2}
handle_button=handle_button,
)
try:
with open(os.path.join(os.path.dirname(__file__), "stretch_skew_icons.ans")) as f:
with open(os.path.join(os.path.dirname(__file__), "stretch_skew_icons.ans"), encoding="utf-8") as f:
icons_ansi = f.read()
icons_doc = AnsiArtDocument.from_ansi(icons_ansi)
icons_rich_markup = icons_doc.get_rich_console_markup()

View File

@ -576,7 +576,7 @@ get_warning_icon = lambda: Static("""
# def get_question_icon() -> Static:
# global question_icon_ansi
# if not question_icon_ansi:
# with open("question_icon.ans", "r") as f:
# with open("question_icon.ans", "r", encoding="utf-8") as f:
# question_icon_ansi = f.read()
# return Static(question_icon_ansi, classes="question_icon message_box_icon")