From 65d7605623c2ea9aff4a87befb622e2419678311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Tue, 15 Nov 2022 15:36:10 +0000 Subject: [PATCH] languages: update lang-extract.py Fix an issue where it does not run on Windows. When the input argument is regex, we must first search for the files using said regex pattern and then creating a list of files. When opening the files, the encoding needs to be utf-8. --- lang/lang-extract.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lang/lang-extract.py b/lang/lang-extract.py index f0ba84356..73c2e20ce 100755 --- a/lang/lang-extract.py +++ b/lang/lang-extract.py @@ -6,6 +6,10 @@ import polib import regex import sys import lib.charset as cs +from pathlib import Path, PurePosixPath + +BASE_DIR: Path = Path.cwd() +FILE_LIST: list[Path] = [] def line_warning(path, line, msg): print(f'{path}:{line}: {msg}', file=sys.stderr) @@ -43,7 +47,7 @@ def index_to_line(index, lines): def extract_file(path, catalog, warn_skipped=False): - source = open(path).read() + source = open(path, encoding="utf-8").read() newlines = newline_positions(source) # match internationalized quoted strings @@ -144,7 +148,7 @@ def extract_file(path, catalog, warn_skipped=False): def extract_refs(path, catalog): - source = open(path).read() + source = open(path, encoding="utf-8").read() newlines = newline_positions(source) # match message catalog references to add backrefs @@ -261,13 +265,25 @@ def main(): ap.add_argument('file', nargs='+', help='Input files') args = ap.parse_args() + for path in args.file: + if not Path(path).exists(): + # assume its regex + for file in sorted(BASE_DIR.glob(path)): + FILE_LIST.append(file) + else: + FILE_LIST.append(Path(path)) + + # Convert the path to relative and use Posix format + for index, absolute_path in enumerate(FILE_LIST[:]): + FILE_LIST[index] = PurePosixPath(absolute_path).relative_to(BASE_DIR) + # extract strings catalog = {} - for path in args.file: + for path in FILE_LIST: extract_file(path, catalog, warn_skipped=args.warn_skipped) # process backreferences in a 2nd pass - for path in args.file: + for path in FILE_LIST: extract_refs(path, catalog) # check the catalog entries