Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iwconfig/7c3b5e55b532b3c15ead6c6ab725121c to your computer and use it in GitHub Desktop.
Save iwconfig/7c3b5e55b532b3c15ead6c6ab725121c to your computer and use it in GitHub Desktop.
(WIP - currently not working correctly!) reference: https://stackoverflow.com/a/9758019
# Normalizes filenames and folders !!
# because fuck you HFS+
# I hate you so
# so much
# Very WIP and probably abandoned because
# I dont feel like fixing the bugs and i'll just
# consider all this as wasted time and redo my
# rsync command with `--iconv=utf-8-mac,utf-8`,
# bite the fucking bullet and simply re-wait all
# those lost precious hours of slow transfer rate
# god damnit
from rich import print
from pathlib import Path
import unicodedata
base_path = Path(".")
dest_path = base_path / "normalized"
def normalize_name(p: Path) -> Path:
return Path(unicodedata.ucd_3_2_0.normalize("NFC", str(p)))
def get_dirs(base_path: Path = base_path) -> list[Path]:
return [p for p in base_path.rglob("*") if p.is_dir()]
def get_files(base_path: Path = base_path) -> list[Path]:
return [p for p in base_path.rglob("*") if p.is_file()]
def rename_dirs(
dry_run: bool = True, dest_path: Path = dest_path, base_path: Path = base_path
) -> list[Path]:
dest_paths = [
dest_path.absolute() / normalize_name(p)
for p in get_dirs(base_path)
if p != dest_path
]
if dry_run:
return dest_paths
return [p.parent.mkdir(parents=True, exist_ok=True) for p in dest_paths]
def rename_files(
dry_run: bool = True, dest_path: Path = dest_path, base_path: Path = base_path
) -> list[Path]:
dest_paths = [
(p, dest_path.absolute() / normalize_name(p))
for p in get_files(base_path)
if p.parent != dest_path
]
if dry_run:
return list(map(lambda p: p[1], dest_paths))
return [source_path.rename(dest_path) for source_path, dest_path in dest_paths]
def normalize(
dry_run: bool = True, base_path: Path = base_path, dest_path: Path = dest_path
) -> list[Path]:
return {
"dirs": rename_dirs(dry_run, dest_path, base_path),
"files": rename_files(dry_run, dest_path, base_path),
}
print(normalize())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment