-
Notifications
You must be signed in to change notification settings - Fork 0
/
rerename.py
executable file
·340 lines (274 loc) · 9.24 KB
/
rerename.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python3
"""
RErename
Convenient and safe bulk renamer.
* Performs dry-run by default
* Pre-run check to avoid data loss caused by non-unique output names.
TODO:
* Better error message with bad regex
* Add \index and \index0 special substitition (ie. 001, 002, ...)
"""
import argparse
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
import re
import sys
import colorama
def parse_arguments(args):
"""
Create and run `argparse`-based command parser.
"""
parser = argparse.ArgumentParser(
description="Bulk rename of files and folders in current directory.")
# All
parser.add_argument(
'-a', '--all', action='store_true', dest='show_all',
help="show hidden files and folders")
# Extension
parser.add_argument(
'-e', '--extension', action='store_false', dest='preserve_suffix',
help="file extensions should also be renamed")
# Force
parser.add_argument(
'-f', '--force', action='store_false', dest='dry_run',
help="actually perform rename operation")
# Ignore case
parser.add_argument(
'-i', '--ignore-case', action='store_true', dest='ignore_case',
help="Match case insensitively")
# ~ parser.add_argument(
# ~ '-s', '--spaces', action='store_true', dest='spaces',
# ~ help="replace non-alphanumeric characters with spaces")
# Search
parser.add_argument(
'search_regex', metavar='SEARCH', type=str,
help='use regex with (capturing) patterns')
# Replace
parser.add_argument(
'replace_template', default='',
metavar='REPLACE', nargs='?', type=str,
help='literal with (\\1) replacements, leave blank for deletion')
options = parser.parse_args(args)
return options
def path_type(path):
"""
Give the english name for the type of file-system entry given.
"""
if not path.exists():
raise FileNotFoundError()
elif path.is_file():
return 'file'
elif path.is_dir():
return 'folder'
else:
return 'entry'
class Colour(Enum):
BLUE = colorama.Fore.BLUE
GREEN = colorama.Fore.GREEN
RED = colorama.Fore.RED + colorama.Style.BRIGHT
WHITE = colorama.Fore.WHITE
YELLOW = colorama.Fore.YELLOW + colorama.Style.BRIGHT
class Terminal:
@staticmethod
def stderr(string, colour=Colour.WHITE):
Terminal.print(string, colour, file=sys.stderr)
@staticmethod
def print(string, colour=Colour.WHITE, *, file=sys.stdout):
parts = [colour.value, string, colorama.Style.RESET_ALL]
print(''.join(parts), file=file)
@dataclass
class RenamerConfiguration:
"""
Configuration options for the `Renamer` class.
"""
dry_run: bool
ignore_case: bool
preserve_suffix: bool
replace_template: str
search_regex: str
show_all: bool
@dataclass
class Rename:
"""
A single rename operation.
"""
old: str # Original file name
new: str # New file name
match_start: int # Regex match start
match_end: int # Regex match end
replaced: str # Replaced literal
class RenameError(RuntimeError):
pass
class Renamer:
def __init__(self, folder, configuration):
"""
Initialiser.
Args:
folder: `Path` object of folder to examine.
configuration: `RenamerConfiguration` options.
"""
self.folder = folder
self.num_entries = 0
self.num_matches = 0
self.config = configuration
def check(self, renames):
"""
Ensure that no files will be lost before renaming begins.
Files can be lost in two main ways:
1) Destination name overwrites existing file
2) Destination name is not unique. New file is
immediately overwritten.
If either problem is detected, details will be printed and we will
abort directly.
"""
seen = {}
for rename in renames:
path_new = self.folder / rename.new
# Existing folder entry
if path_new.exists():
message = "Existing {} would be overwritten: {!r}"
raise RenameError(message.format(path_type(path_new), rename.new))
# Collisions?
if rename.new in seen:
first = seen[rename.new]
second = rename.old
message = "Both {!r} and {!r} rename to {!r}"
raise RenameError(message.format(first, second, rename.new))
seen[rename.new] = rename.old
def rename(self):
"""
Make it happen.
"""
# Read folder
paths = self.list()
# Pre-Calculate renames
renames = self.calculate(
paths,
self.config.search_regex,
self.config.replace_template,
self.config.preserve_suffix,
)
# Check renames for safety
try:
self.check(renames)
except RenameError as e:
Terminal.stderr("Aborting: Data-loss detected!", Colour.RED)
Terminal.stderr(str(e))
raise SystemExit(2)
# Finish him!
self.print_renames(renames)
if not self.config.dry_run:
self.execute(renames)
self.print_summary(renames)
def execute(self, renames):
"""
Actually perform the rename operations.
"""
for rename in renames:
# Double check that we don't over-write existing file.
# This should never happen, but... you know.
new = self.folder / rename.new
if new.exists():
message = "Rename target {!r} already exists. Aborting."
raise RenameError(message.format(new))
path = self.folder / rename.old
path.rename(new)
def print_entry(self, path):
"""
Print plain directory entry.
"""
if path.is_dir():
print(Terminal.blue(path.name) + Terminal.reset() + '/')
else:
print(path.name)
def print_renames(self, renames):
"""
Highlight the matched part of the orignal string.
"""
for rename in renames:
old, start, end = rename.old, rename.match_start, rename.match_end
parts = []
add = parts.append
# Prefix
add(old[:start])
# Matched
add(Colour.RED.value)
add(old[start:end])
add(colorama.Style.RESET_ALL)
# Replaced
add(Colour.GREEN.value)
add(rename.replaced)
add(colorama.Style.RESET_ALL)
# Suffix
add(old[end:])
print(''.join(parts))
def print_summary(self, renames):
status = f"{len(renames)} renames from {self.num_entries} entries"
if self.config.dry_run:
status += " (DRY RUN)"
Terminal.stderr(status, Colour.YELLOW)
def list(self, show_all=False):
"""
Reads files and folders found in the given folder.
Args:
root: `Path` to root folder.
use_hidden: list entries that start with a period.
Returns: List of `Path` objects.
"""
allow_hidden = not self.config.show_all
entries = []
for entry in self.folder.iterdir():
if allow_hidden and entry.name.startswith('.'):
continue
entries.append(entry)
entries.sort()
return entries
def calculate(self, paths, search, replace, preserve_suffix):
"""
Build a list of renames to perform.
Args:
entries: Iterable of folder entries
search: Search regex.
replace: Replacement template.
preserve_suffix: Do not do any renames on suffix
Returns:
List of 2-tuples. The first element is a `Path` object for a folder
entry, the second is string with the new name.
"""
# Flags
flags = 0
if self.config.ignore_case:
flags |= re.IGNORECASE
# Time to match
search = re.compile(search, flags=flags)
self.num_entries = 0
renames = []
for path in paths:
self.num_entries += 1
# Keep suffix as is?
if preserve_suffix:
haystack = path.stem
suffix = path.suffix
else:
haystack = path.name
suffix = ''
# Find match
match = search.search(haystack)
if match:
self.num_matches += 1
start, end = match.span()
replaced = match.expand(replace)
new = haystack[:start] + replaced + haystack[end:] + suffix
# Nothing to do?
if path.name == new:
continue
renames.append(Rename(path.name, new, *match.span(), replaced))
return renames
if __name__ == '__main__':
colorama.init()
options = parse_arguments(sys.argv[1:])
config = RenamerConfiguration(**vars(options))
renamer = Renamer(Path.cwd(), config)
status = renamer.rename()
sys.exit(status)