/
rename.py
executable file
·59 lines (50 loc) · 1.53 KB
/
rename.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
#!/usr/bin/env python3
import argparse
import yaml
import itertools as it
import os
from pathlib import Path
import re
import sys
def reverse_split(s: str, reverse: bool = False):
if reverse:
l = s.split()
l.reverse()
return l
return s.split()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("pattern_file", type=Path)
parser.add_argument("directory", type=Path)
parser.add_argument("--dry", "-n", action="store_true")
parser.add_argument("--reverse", "-r", action="store_true")
args = parser.parse_args(sys.argv[1:])
with args.pattern_file.open() as f:
scheme = yaml.load(f)
templates = [reverse_split(template, args.reverse) for template in scheme["files"]]
wildcards = {
name: f"({match})" for name, match in scheme.get("wildcards", {}).items()
}
sub_tags = {
name: f"\\1{i+1}" for name, i in enumerate()
}
print([
( regex.format(**wildcards), sub.format(**wildcards) )
for regex, sub in templates
])
exit()
patterns = [
( re.compile(regex.format(**wildcards)), sub.format(**wildcards) )
for regex, sub in templates
]
os.chdir(args.directory)
for path, (regex, replace) in it.product(Path().rglob("*"), patterns):
if regex.match(str(path)):
new = regex.sub(replace, str(path))
if args.dry:
print(str(path) + " -> " + new)
else:
path.rename(new)
if __name__ == "__main__":
main()
exit(0)