Gramps: move assets to local storage
This commit is contained in:
65
files/gramps/gramps_rename.py
Executable file
65
files/gramps/gramps_rename.py
Executable file
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3.12
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Rename Gramps document files by appending extensions from a list."
|
||||
)
|
||||
parser.add_argument("directory", type=Path, help="Directory containing hashed files")
|
||||
parser.add_argument("names_file", type=Path, help="Text file with target names")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def read_names(path: Path) -> list[str]:
|
||||
if not path.is_file():
|
||||
raise FileNotFoundError(f"Names file not found: {path}")
|
||||
|
||||
names = []
|
||||
for line in path.read_text(encoding="utf-8").splitlines():
|
||||
name = line.strip()
|
||||
if name:
|
||||
names.append(name)
|
||||
return names
|
||||
|
||||
|
||||
def rename_files(directory: Path, names: list[str]) -> None:
|
||||
if not directory.is_dir():
|
||||
raise NotADirectoryError(f"Directory not found: {directory}")
|
||||
|
||||
for name in names:
|
||||
hash_part, dot, _ = name.partition(".")
|
||||
if not dot:
|
||||
print(f"Skipping invalid entry (missing extension): {name}", file=sys.stderr)
|
||||
continue
|
||||
|
||||
source = directory / hash_part
|
||||
target = directory / name
|
||||
|
||||
if target.exists():
|
||||
print(f"Target already exists, skipping: {target}", file=sys.stderr)
|
||||
continue
|
||||
|
||||
if not source.exists():
|
||||
print(f"Source not found, skipping: {source}", file=sys.stderr)
|
||||
continue
|
||||
|
||||
source.rename(target)
|
||||
print(f"Renamed {source.name} -> {target.name}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
try:
|
||||
names = read_names(args.names_file)
|
||||
rename_files(args.directory, names)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
print(str(exc), file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user