From 5f9a8bc8a148b762b38336206f6b08067149c2a8 Mon Sep 17 00:00:00 2001 From: Elias Bachaalany Date: Sun, 17 Dec 2023 11:05:45 -0800 Subject: [PATCH] idxtool: '--rename' takes no arguments now it tries to rename files that have no GPTID prefix. --- .scripts/README.md | 4 ++-- .scripts/idxtool.py | 41 +++++++++++++++++++++++++++++++++-------- .vscode/launch.json | 2 +- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/.scripts/README.md b/.scripts/README.md index 1bd7079..1718c63 100644 --- a/.scripts/README.md +++ b/.scripts/README.md @@ -26,7 +26,7 @@ options: style string --parse-gptfile PARSE_GPTFILE Parses a GPT file name - --rename RENAME Rename the file name to include its GPT ID + --rename Rename all the GPT file names to include their GPT ID ``` ## Features @@ -36,7 +36,7 @@ options: - Update Descriptions: Use `--update-description [filename]` to update the descriptions of the GPT file. - Find GPT File: Use `--find-gptfile [gptid or gpt name in quotes]` to find a GPT by its ID or name. - Find GPT in TOC: Use `--find-gpttoc [gptid or string]` to search the TOC.md file for a given gptid or free style string. -- Rename GPT: Use `--rename [filename]` to rename the file name to include its GPT ID. +- Rename GPT: Use `--rename` to rename all the GPTs to include their GPTID as prefix. - Help: Use `--help` to display the help message and usage instructions. ## Usage diff --git a/.scripts/idxtool.py b/.scripts/idxtool.py index dbb9ad8..3a4c554 100644 --- a/.scripts/idxtool.py +++ b/.scripts/idxtool.py @@ -32,12 +32,35 @@ def update_description(filename): print(f"TODO Updating description with file: {filename}") raise NotImplementedError -def rename_gpt(filename): - if filename == '*': - print("TODO: Renaming all GPT files to include their ID") - else: - print(f"TODO: Renaming GPT file to include its ID: {filename}") - raise NotImplementedError +def rename_gpt(): + nb_ok = nb_total = 0 + all_renamed_already = True + + for ok, gpt in enum_gpts(): + nb_total += 1 + if not ok or not (id := gpt.id()): + print(f"[!] {gpt.filename}") + continue + # Skip files with correct prefix + basename = os.path.basename(gpt.filename) + if basename.startswith(f"{id.id}_"): + nb_ok += 1 + continue + all_renamed_already = False + + # New full file name with ID prefix + new_fn = os.path.join(os.path.dirname(gpt.filename), f"{id.id}_{basename}") + print(f"[+] {basename} -> {os.path.basename(new_fn)}") + if os.system(f"git mv \"{gpt.filename}\" \"{new_fn}\"") == 0: + nb_ok += 1 + + msg = f"Renamed {nb_ok} out of {nb_total} GPT files." + ok = nb_ok == nb_total + if all_renamed_already: + msg = f"All {nb_total} GPT files were already renamed. No action taken." + print(msg) + + return (ok, msg) def reformat_gpt_files(src_path: str, dst_path: str) -> Tuple[bool, str]: @@ -177,7 +200,7 @@ def main(): parser.add_argument('--find-gptfile', type=str, help='Find a GPT by its ID or name') parser.add_argument('--find-gpttoc', type=str, help='Searches the TOC.md file for the given gptid or free style string') parser.add_argument('--parse-gptfile', type=str, help='Parses a GPT file name') - parser.add_argument('--rename', type=str, help='Rename the file name to include its GPT ID') + parser.add_argument('--rename', action='store_true', help='Rename the GPT file names to include their GPT ID') # Handle arguments ok = True @@ -200,7 +223,9 @@ def main(): if args.find_gpttoc: find_gpt_in_toc(args.find_gpttoc) if args.rename: - rename_gpt(args.rename) + ok, err = rename_gpt() + if not ok: + print(err) sys.exit(0 if ok else 1) diff --git a/.vscode/launch.json b/.vscode/launch.json index af85e3a..1c2a59e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -30,7 +30,7 @@ "type": "python", "request": "launch", "program": "${workspaceFolder}/.scripts/idxtool.py", - "args": ["--rename", "*"], + "args": ["--rename"], "console": "integratedTerminal" } ]