-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappend.py
154 lines (132 loc) · 5.33 KB
/
append.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import click
from multiprocessing import Pool, cpu_count
import os
from lib.utils import get_files_to_process, get_kv_pairs
def assemble_new_filepath(old_filepath, kv_pairs):
split_path = old_filepath.split("/")
kv_pairs = ["{%s}" % pair for pair in kv_pairs]
new_filename = ".".join(["".join(kv_pairs), old_filepath.split(".")[-1]])
new_filepath = "/".join(split_path[:-1] + [new_filename])
return new_filepath
def append_or_change_filenames(dir, key, newkey, value):
for file in dir["files"]:
append_or_change_filename(file, key, newkey, value)
def append_or_change_filename(file, key, newkey, value):
appendix = "%s_%s" % (key, value)
override = False
kv_pairs = get_kv_pairs(file)
keys = [pair.split("_")[0] for pair in kv_pairs]
values = [pair.split("_")[1] for pair in kv_pairs]
for i, existing_key in enumerate(keys):
if existing_key == key and newkey is not None:
override = True
existing_value = values[i]
kv_pairs[i] = "%s_%s" % (newkey, existing_value)
break
elif existing_key == key and newkey is None:
override = True
kv_pairs[i] = "%s_%s" % (key, value)
break
if not override:
kv_pairs.append(appendix)
new_filepath = assemble_new_filepath(file, kv_pairs)
os.rename(file, new_filepath)
return new_filepath
def delete_key(dir, key):
for file in dir["files"]:
split_path = file.split("/")
filename = split_path[-1]
kv_string = filename.split(".")[0]
kv_pairs = get_kv_pairs(kv_string)
for i, pair in enumerate(kv_pairs):
if pair.split("_")[0] == key:
kv_pairs.pop(i)
break
new_filepath = assemble_new_filepath(file, kv_pairs)
os.rename(file, new_filepath)
def do_uid_insert(dir, uid_insert):
for file in dir["files"]:
split_path = file.split("/")
filename = split_path[-1]
kv_string = filename.split(".")[0]
kv_pairs = get_kv_pairs(kv_string)
for i, pair in enumerate(kv_pairs):
if pair.split("_")[0].lower() == "uid":
old_uid = pair.split("_")[1]
new_uid = "-".join(
old_uid.split("-")[:-1] + [uid_insert] + [old_uid.split("-")[-1]]
)
kv_pairs[i] = "_".join(["UID", new_uid])
new_filepath = assemble_new_filepath(file, kv_pairs)
os.rename(file, new_filepath)
def replace_uid_year(dir, uid_year):
for file in dir["files"]:
split_path = file.split("/")
filename = split_path[-1]
kv_string = filename.split(".")[0]
kv_pairs = get_kv_pairs(kv_string)
for i, pair in enumerate(kv_pairs):
if pair.split("_")[0].lower() == "uid":
old_uid = pair.split("_")[1]
new_uid = "-".join(old_uid.split("-")[:-1] + [str(uid_year)])
kv_pairs[i] = "_".join(["UID", new_uid])
new_filepath = assemble_new_filepath(file, kv_pairs)
os.rename(file, new_filepath)
@click.command()
@click.option(
"--src",
"-s",
type=click.Path(exists=True),
help="source directory of images to process",
)
@click.option("--deletekey", "-dk", help="The key to delete.")
@click.option("--key", "-k", help="The key.")
@click.option("--newkey", "-nk", help="New key to overwrite 'key'.")
@click.option("--uid-insert", help="Value to insert into UID before the year.")
@click.option(
"--uid-year", help="Value to replace the year in the UID with.", type=click.INT
)
@click.option("--value", "-v", help="The value. Leave blank to remove the key.")
def run(src, deletekey, key, newkey, uid_insert, uid_year, value):
if src is None:
click.secho("No source specified...", fg="red")
click.echo("run 'python append.py --help to see options'")
return
if deletekey is not None:
if click.confirm("Are you sure you want to delete key '%s'" % deletekey):
subdirs = get_files_to_process(src)
with Pool(processes=cpu_count()) as pool:
pool.starmap(delete_key, [(dir, deletekey) for dir in subdirs])
return
if key is not None and newkey is not None:
if click.confirm(
"Are you sure you want to replace the key '%s' with '%s'" % (key, newkey)
):
pass
else:
return
if (key is not None and value is not None) or (
key is not None and newkey is not None
):
subdirs = get_files_to_process(src)
with Pool(processes=cpu_count()) as pool:
pool.starmap(
append_or_change_filenames,
[(dir, key, newkey, value) for dir in subdirs],
)
return
if uid_insert is not None:
subdirs = get_files_to_process(src)
with Pool(processes=cpu_count()) as pool:
pool.starmap(do_uid_insert, [(dir, uid_insert) for dir in subdirs])
return
if uid_year is not None:
subdirs = get_files_to_process(src)
with Pool(processes=cpu_count()) as pool:
pool.starmap(replace_uid_year, [(dir, uid_year) for dir in subdirs])
return
else:
click.secho("I'm afraid I don't quite know what to do.", fg="red")
click.echo("run 'python append.py --help to see options'")
if __name__ == "__main__":
run()