Skip to content

Commit

Permalink
Restore file from accidental revertion
Browse files Browse the repository at this point in the history
Previous commits reverted the keyword_uri_map_generator.py file
by mistake to a previous version.
  • Loading branch information
hakonhagland committed Jan 17, 2025
1 parent fb260fe commit 419ff64
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions scripts/python/src/fodt/keyword_uri_map_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=<main_dir> --keyword_dir=<keyword_dir>
# fodt-gen-kw-uri-map --maindir=<main_dir> \
# --keyword_dir=<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:
#
Expand All @@ -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")
Expand Down

0 comments on commit 419ff64

Please sign in to comment.