-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
245 lines (209 loc) · 7.54 KB
/
Copy pathutils.py
File metadata and controls
245 lines (209 loc) · 7.54 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/python
from contextlib import contextmanager
from pathlib import Path
import time
from typing import Generator
import urllib.request
import urllib.parse
import urllib.error
import http.client
import re
@contextmanager
def temp_dir(show: bool = False) -> Generator[Path, None, None]:
from tempfile import TemporaryDirectory
with TemporaryDirectory() as d:
p = Path(d)
yield p.resolve()
if show:
print(p)
for f in p.iterdir():
print(f" {f.name:64} {f.stat().st_size}")
nametrans = bytes.maketrans(rb'\/:*"<>|', b"....'().")
nametrans = (
nametrans[:128]
+ b"CueaaaaceeeiiiAAEaAooouu_OU.$Y_faiounN''"
+ (b"." * 24)
+ b"AAAAAAACEEEEIIIIDNOOOOOx0UUUUYdBaaaaaaaceeeeiiiidnooooo+ouuuuyDy"
)
def fixname(s: str, removeDots: bool = True) -> str:
ss = s.encode("iso8859_1", "ignore")
if removeDots:
x = bytes.translate(ss, nametrans).decode("iso8859_1")
while x and len(x) and (x[-1] == "." or x[-1] == " "):
x = x[:-1]
else:
x = bytes.translate(ss, nametrans).decode("iso8859_1")
return x
def dospath(x: Path) -> Path:
return Path(str(x).replace("/cygdrive/c", "c:").replace("/", "\\"))
def fat32names(dirname: Path):
"""Rename all files in directory to simple ascii names to work on FAT"""
for f in dirname.iterdir():
fixed = fixname(f.name)
if f.name != fixed:
f.rename(dirname / fixed)
f = dirname / fixed
if f.is_dir():
fat32names(f)
def collect(root_dir: Path, min_files: int = 5):
"""
Group files that form logic sets (like disk mags or pack disks) by extracting
a common name, and moving the individual issues into a sub directory of that name.
"""
prefixes: dict[str, list[Path]] = {}
for f in root_dir.iterdir():
base = f.with_suffix("").name
base = base.replace("(es)", "").replace("(AGA)", "")
m = re.match(r"(\D+)(\d+).*\(([^)]+)\)", base)
if m:
pre = m.group(1)
while pre[-1] == " " or pre[-1] == "#" or pre[-1] == "(" or pre[-1] == "-":
pre = pre[:-1]
pre = pre.replace("Vol.", "").replace("Issue", "").replace("Nr.", "")
while pre[-1] == " " or pre[-1] == "#" or pre[-1] == "(" or pre[-1] == "-":
pre = pre[:-1]
pre = f"{pre} ({m.group(3)})"
if pre in prefixes:
prefixes[pre].append(f)
else:
prefixes[pre] = [f]
for pre, l in prefixes.items():
if len(l) >= min_files:
print(pre)
p = Path("target") / pre
p.mkdir(parents=True, exist_ok=True)
for n in l:
print(f" {n}")
n.rename(p / n.name)
def alpha_subdir(root_dir: Path):
"""Iterate over every file or dir in root_dir, take its first letter, and move it
into a subdirectory named that letter"""
for f in root_dir.iterdir():
if len(f.name) == 1:
continue
if f.name[0] == "-":
continue
sub = root_dir / f.name[0]
sub.mkdir(exist_ok=True)
f.rename(sub / f.name)
def reorganize(root_dir: Path, max_files: int, min_files: int = -1):
"""
Reorganize a set of "alphabetic" subdirectories so that no directory
contains more than `max_files` files.
For each subdirectory 'dir' in `root_dir`, if 'dir' contains more than
`max_files` files or directories, 'dir' will be split up into several
numbered directories formed as 'dir' + counter.
Conversely, if at least two directories in order contains less then
`min_files`, they will be joined together.
"""
small_dirs: list[Path] = []
small_count = 0
for f in sorted(root_dir.iterdir()):
if f.is_dir():
files = sorted(f.iterdir())
count = len(files)
i = 0
print(f"Found {count} in {f}")
if small_count > 0 and small_count + count > max_files:
if len(small_dirs) > 1:
t = ""
for s in small_dirs:
t += s.name[0]
target = root_dir / t
print(f"Moving to {target}")
target.mkdir()
for d in small_dirs:
for f2 in d.iterdir():
f2.rename(target / f2.name)
d.rmdir()
small_count = 0
small_dirs = []
count = ""
if len(files) > max_files:
while len(files) > 0:
target = Path(f"{f}{i}")
print(f"Moving to {target}")
target.mkdir()
for f2 in files[0:max_files]:
f2.rename(target / f2.name)
files = files[max_files:]
i += 1
f.rmdir()
elif len(files) < min_files:
small_dirs.append(f)
small_count += len(files)
if len(small_dirs) > 1:
t = ""
for s in small_dirs:
t += s.name[0]
target = root_dir / t
print(f"Moving to {target}")
target.mkdir()
for d in small_dirs:
for f2 in d.iterdir():
f2.rename(target / f2.name)
d.rmdir()
# reorganize(Path("Games"), 250, 50)
def get_cached(url: str) -> Path | None:
t = urllib.parse.unquote_plus(url)
name = urllib.parse.quote_plus(t)
file_name = Path(f"releases/{name}")
if file_name.exists():
return file_name
return None
def download(url: str) -> Path | None:
t = urllib.parse.unquote_plus(url)
name = urllib.parse.quote_plus(t)
file_name = Path(f"releases/{name}")
file_name.parent.mkdir(exist_ok=True)
if not file_name.exists():
try:
print(f"Downloading {url}")
data = urllib.request.urlopen(url.replace(" ", "%20")).read()
file_name.write_bytes(data)
except (OSError, http.client.HTTPException) as e:
# Covers HTTPError/URLError (OSError subclasses) as well as
# abrupt disconnects like RemoteDisconnected/IncompleteRead.
print(f"Download failed: {e}")
return None
return file_name
def remove_in(path: Path):
for r in path.iterdir():
if r.is_dir():
remove_in(r)
r.rmdir()
else:
r.unlink()
def flatten_dir2(path: Path, to: Path):
for r in path.iterdir():
if r.is_dir():
flatten_dir2(r, to)
r.rmdir()
else:
target = to / r.name
if not target.exists():
r.rename(target)
else:
r.unlink()
def flatten_dir(path: Path):
"""Recursively move all files in a tree to the top level"""
for r in path.iterdir():
if r.is_dir():
flatten_dir2(r, path)
r.rmdir()
def apply_template(template: str, d: dict[str, str | int | float]):
# Support nested curlies; if nested variable is empty or negative,
# the outer scope is removed.
r = re.compile(r"{[^{}]*({[^{}]+})[^{}]*}")
while True:
m = r.search(template)
if not m:
break
s0, e0 = m.start(0), m.end(0)
s1, e1 = m.start(1), m.end(1)
x = template[s1:e1].format(**d)
if x == "-1" or x == "":
template = template[:s0] + template[e0:]
else:
template = template[:s0] + template[s0 + 1 : e0 - 1] + template[e0:]
return template.format(**d)