-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiremgari
More file actions
60 lines (50 loc) · 1.43 KB
/
iremgari
File metadata and controls
60 lines (50 loc) · 1.43 KB
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
#!/usr/bin/env python3
import os
from sys import argv
import glob
###### EDIT THESE ######
def eligible(name):
# Determine whether a file should be renamed.
return False
def process(name):
# Return the new name given an old name.
return name
def eligible_edit(name):
# Determine whether a file should be edited, given its old name.
return False
def process_edit(content):
# Return the new file content given the old content.
return content
######
# TODO: guard against overwrites
def rename(path=".", recursive=False, dryrun=False):
print(f"Iremgari search root: {path}")
for oldname in glob.glob("**", root_dir=path, recursive=recursive, include_hidden=True):
if os.path.isfile(os.path.join(path, oldname)):
if eligible_edit(oldname):
print(f"Editing {oldname}")
if not dryrun:
with open(os.path.join(path, oldname)) as f:
content = f.read()
content = process_edit(content)
with open(os.path.join(path, oldname), "w") as f:
f.write(content)
if eligible(oldname):
newname = process(oldname)
print(f"{oldname} -> {newname}")
if not dryrun:
os.rename(os.path.join(path, oldname), os.path.join(path, newname))
argchar_to_param = {
"d": "dryrun",
"r": "recursive"
}
args = dict()
if len(argv) >= 2:
if argv[1][0] == "-":
for argchar in argv[1][1:]:
args[argchar_to_param[argchar]] = True
if len(argv) >= 3:
args["path"] = argv[2]
else:
args["path"] = argv[1]
rename(**args)