From d1eefeeb20a1c9fc666f10b4a0b13d39fbf0ee39 Mon Sep 17 00:00:00 2001 From: sim Date: Fri, 8 Nov 2024 15:45:14 +0100 Subject: [PATCH] Changes --- tagmerger | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/tagmerger b/tagmerger index c86b7b9..82ec4fe 100755 --- a/tagmerger +++ b/tagmerger @@ -35,6 +35,21 @@ def merge(old_tag, new_tag, old_tags, new_tags): except (KeyError, AttributeError): # new_tag does not already exist new_tags[new_tag] = [old_tag] +def unmerge(old_tag, new_tag, old_tags, new_tags): + """ + Unmerge old_tag from new_tag. + """ + print(f"Unmerging {old_tag} from {new_tag}") + try: + old_tags[old_tag].remove(new_tag) + except ValueError: + pass + try: + new_tags[new_tag].remove(old_tag) + except ValueError: + pass + + def display_options(message, options): """ Print message and a numbered list of options. @@ -94,13 +109,16 @@ def export_tags(old_tags): while True: try: path = input("Export path > ") - with open(path, "w") as fileout: - yaml.dump(old_tags, fileout) - print(f"Tags exported to {path}") + export_tags_path(old_tags, path) return except Exception as e: print(f"An error occured during export: {e}") +def export_tags_path(old_tags, path): + with open(path, "w") as fileout: + yaml.dump(old_tags, fileout) + print(f"Tags exported to {path}") + def display_progress(old_tags): """ Print the fraction of merged tags from the dirty set. @@ -125,10 +143,19 @@ def propose_merge_similar(tag, merge_tag, old_tags, new_tags): break merge(similar_tags[int(choice)], merge_tag, old_tags, new_tags) +def unmerge_all_from(merge_tag, old_tags, new_tags): + """ + Unmerge all tags from the old set from merge_tag. + """ + for tag in new_tags[merge_tag]: + unmerge(tag, merge_tag, old_tags, new_tags) + + parser = argparse.ArgumentParser(description = "A simple tool to semi-automate merging a dirty set of tags into a new set of tags.") parser.add_argument("tags", type=str, help="Path to a yaml file containing the dirty set of tags. The yaml directory must be flat, with one key per tag in the dirty set with value null or set to the merge tag in the new set.") parser.add_argument("--plain-input", action="store_true", help="If set, the input file is assumed to contain one tag of the dirty set per line. No character is escaped, the newline character is used as separator.") +parser.add_argument("--export-to", type=str, help="If set, export the tags after each iteration.") args = parser.parse_args() @@ -143,6 +170,8 @@ for tag in old_tags.keys(): while True: print("") display_progress(old_tags) + if args.export_to != None: + export_tags_path(old_tags, path=args.export_to) print(f"Tag: {tag}") display_options("Merged in:", old_tags[tag]) similar_unmerged_tags = [t for t in find_similar(tag, old_tags.keys()) @@ -178,7 +207,9 @@ for tag in old_tags.keys(): if len(similar_candidates) > 0: display_options("Similar tags already in the new set:", similar_candidates) - choice = pick_option("Continue with chosen tag (default) or index of replacement > ", similar_candidates, default = '') + choice = pick_option("Continue with chosen tag (default) or index of replacement > ", + [str(i) for i in range(len(similar_candidates))], + default = '') if choice != '': merge_tag = similar_candidates[int(choice)] merge(tag, merge_tag, old_tags, new_tags) @@ -187,5 +218,7 @@ for tag in old_tags.keys(): export_tags(old_tags) sys.exit(0) +#unmerge_all_from("__lieux__", old_tags, new_tags) + print("\nAll tags merged !") export_tags(old_tags)