-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrenamer.py
79 lines (60 loc) · 2.26 KB
/
renamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
import os
import re
import argparse
import unicodedata
from html.entities import name2codepoint
from pathlib import Path, PurePath
from text_unidecode import unidecode
CHAR_ENTITY_PATTERN = re.compile('&(%s);' % '|'.join(name2codepoint))
DECIMAL_PATTERN = re.compile('&#(\d+);')
HEX_PATTERN = re.compile('&#x([\da-fA-F]+);')
QUOTE_PATTERN = re.compile(r'[\']+')
ALLOWED_CHARS_PATTERN = re.compile(r'[^-a-z0-9]+')
DUPLICATE_DASH_PATTERN = re.compile('-{2,}')
DEFAULT_SEPARATOR = '-'
def slugify_text(text):
if not isinstance(text, str):
text = str(text, 'utf-8', 'ignore')
text = QUOTE_PATTERN.sub(DEFAULT_SEPARATOR, text)
text = unidecode(text)
text = CHAR_ENTITY_PATTERN.sub(
lambda m: unichr(name2codepoint[m.group(1)]), text)
try:
text = DECIMAL_PATTERN.sub(lambda m: unichr(int(m.group(1))), text)
except Exception:
pass
try:
text = HEX_PATTERN.sub(lambda m: unichr(int(m.group(1), 16)), text)
except Exception:
pass
text = unicodedata.normalize('NFKD', text)
text = text.lower()
text = QUOTE_PATTERN.sub('', text)
text = re.sub(ALLOWED_CHARS_PATTERN, DEFAULT_SEPARATOR, text)
text = DUPLICATE_DASH_PATTERN.sub(DEFAULT_SEPARATOR,
text).strip(DEFAULT_SEPARATOR)
return text
def slugify_fpath(fpath):
target_stem = slugify_text(fpath.stem.split('.')[0])
suffixes = map(lambda x: slugify_text(x.lower()[1:]), fpath.suffixes)
target_fname = '.'.join([target_stem] + list(filter(None, suffixes)))
return fpath.with_name(target_fname)
def main():
parser = argparse.ArgumentParser(description='File renamer')
parser.add_argument(
'file', metavar='FILE', type=str, nargs='+', help='Filename(s)')
args = parser.parse_args()
for source in set(args.file):
source = source.rstrip('.')
source_path = PurePath(source)
target_path = slugify_fpath(source_path)
if source_path != target_path:
try:
Path(source_path).rename(Path(target_path))
print("Renamed '{0}' to '{1}'".format(source_path, target_path))
except Exception as e:
print(e)
pass
if __name__ == '__main__':
main()