From ed0f55c1d79636040843ae5c5e0d9308e35da65a Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 21 Nov 2022 17:27:39 +0100 Subject: Fix snippets_translate on Windows Force UTF-8 encoding. As a drive-by, use Pathlib methods. Complements 81d81baf0ef6c42ebdb6a2b2293d09031ce6dd5c. Pick-to: 6.4 Change-Id: Iccdb33429a030ec76dc55699b07210cd533ba7c2 Reviewed-by: Christian Tismer --- tools/snippets_translate/main.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tools/snippets_translate/main.py b/tools/snippets_translate/main.py index 0ee0b49ee..22fc292fb 100644 --- a/tools/snippets_translate/main.py +++ b/tools/snippets_translate/main.py @@ -311,10 +311,15 @@ def get_license_from_file(filename): def translate_file(file_path, final_path, qt_path, debug, write): - with open(str(file_path)) as f: - lines = f.read().splitlines() - rel_path = file_path.relative_to(qt_path) - snippets = get_snippets(lines, rel_path) + snippets = [] + try: + with file_path.open("r", encoding="utf-8") as f: + lines = f.read().splitlines() + rel_path = file_path.relative_to(qt_path) + snippets = get_snippets(lines, rel_path) + except Exception as e: + log.error(f"Error reading {file_path}: {e}") + raise if snippets: # TODO: Get license header first license_header = get_license_from_file(str(file_path)) @@ -363,7 +368,7 @@ def translate_file(file_path, final_path, qt_path, debug, write): log.info(f"Creating directories for {target_file.parent}") target_file.parent.mkdir(parents=True, exist_ok=True) - with target_file.open("w") as out_f: + with target_file.open("w", encoding="utf-8") as out_f: out_f.write(license_header) out_f.write("\n\n") -- cgit v1.2.3