Skip to content

Commit

Permalink
idxtool: '--rename' takes no arguments now
Browse files Browse the repository at this point in the history
it tries to rename files that have no GPTID prefix.
  • Loading branch information
0xeb committed Dec 17, 2023
1 parent a2b5eeb commit 5f9a8bc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
41 changes: 33 additions & 8 deletions .scripts/idxtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/.scripts/idxtool.py",
"args": ["--rename", "*"],
"args": ["--rename"],
"console": "integratedTerminal"
}
]
Expand Down

0 comments on commit 5f9a8bc

Please sign in to comment.