From 419ff64f782da7b79ce8284110676e8235ba02c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Mon, 13 Jan 2025 15:53:32 +0100 Subject: [PATCH] Restore file from accidental revertion Previous commits reverted the keyword_uri_map_generator.py file by mistake to a previous version. --- .../src/fodt/keyword_uri_map_generator.py | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/scripts/python/src/fodt/keyword_uri_map_generator.py b/scripts/python/src/fodt/keyword_uri_map_generator.py index 215b6107..0d7070d2 100644 --- a/scripts/python/src/fodt/keyword_uri_map_generator.py +++ b/scripts/python/src/fodt/keyword_uri_map_generator.py @@ -161,17 +161,34 @@ def add_alias(kw_uri_map: dict[str, str], keyword: str, alias: str) -> None: uri = kw_uri_map[keyword] kw_uri_map[alias] = uri +def display_map_diff(orig_kw_uri_map: dict[str, str], kw_uri_map: dict[str, str]) -> None: + # First check keys + for kw in sorted(orig_kw_uri_map.keys()): + if kw not in kw_uri_map: + logging.error(f"Keyword {kw} not found in new map.") + else: + if orig_kw_uri_map[kw] != kw_uri_map[kw]: + logging.error(f"URI for keyword {kw} has changed. Old: {orig_kw_uri_map[kw]}, New: {kw_uri_map[kw]}") + for kw in sorted(kw_uri_map.keys()): + if kw not in orig_kw_uri_map: + logging.error(f"Keyword {kw} not found in old map.") + return + # fodt-gen-kw-uri-map # ------------------- # # SHELL USAGE: # -# fodt-gen-kw-uri-map --maindir= --keyword_dir= +# fodt-gen-kw-uri-map --maindir= \ +# --keyword_dir= \ +# --check-changed # # DESCRIPTION: # # Generates a map: KW_NAME -> URI for all keywords. The map is saved to the file -# "meta/kw_uri_map.txt" in the main directory. +# "meta/kw_uri_map.txt" in the main directory. If the --check-changed option is +# given, the script will only check if the files have changed and not write the +# output file. It will return a non-zero exit code if any files have changed. # # EXAMPLE: # @@ -182,11 +199,21 @@ def add_alias(kw_uri_map: dict[str, str], keyword: str, alias: str) -> None: @click.command() @ClickOptions.maindir() @ClickOptions.keyword_dir -def gen_kw_uri_map_cli(maindir: str|None, keyword_dir: str|None) -> None: +@click.option('--check-changed', is_flag=True, help='Check if files have changed') +def gen_kw_uri_map_cli(maindir: str|None, keyword_dir: str|None, check_changed: bool) -> None: logging.basicConfig(level=logging.INFO) keyword_dir = helpers.get_keyword_dir(keyword_dir) maindir = helpers.get_maindir(maindir) kw_uri_map = get_kw_uri_map(maindir, keyword_dir) + if check_changed: + orig_kw_uri_map = helpers.load_kw_uri_map(maindir) + if orig_kw_uri_map != kw_uri_map: + logging.error("Files have changed.") + display_map_diff(orig_kw_uri_map, kw_uri_map) + exit(1) + else: + logging.info("Files have not changed.") + exit(0) with open(maindir / Directories.meta / FileNames.kw_uri_map, "w", encoding='utf8') as f: for kw in sorted(kw_uri_map.keys()): f.write(f"{kw} {kw_uri_map[kw]}\n")