-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
93 lines (68 loc) · 2.32 KB
/
core.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
import os
def prefixfn(string, _, new):
return new + string
def suffixfn(string, _, new):
return string + new
def insertfn(string, substring, new):
if not new or not substring or substring not in string:
return string
before, sep, after = string.partition(substring)
return ''.join([before, new, sep, after])
def appendfn(string, substring, new):
if not new or not substring or substring not in string:
return string
before, sep, after = string.partition(substring)
return ''.join([before, sep, new, after])
def changefn(string, substring, replacement):
if not substring or substring not in string:
return string
before, sep, after = string.partition(substring)
return ''.join([before, replacement, after]) or string
def listdir(path):
os.chdir(path)
return [name for name in os.listdir() if not os.path.isdir(name)]
def rename(path, lst):
os.chdir(path)
count = 0
for (src, dest) in lst:
if src != dest:
os.replace(src, dest)
count += 1
return count
class FilenameCollisionError(Exception):
pass
class Selection:
def __init__(self, filenames):
self.entries = [[name] for name in filenames]
self.stack = [list(range(len(self.entries)))]
def clear(self):
self.stack = [list(range(len(self.entries)))]
def active(self):
return self.stack[-1]
def tighten(self, pattern):
current = self.active()
new = [i for i in current if pattern in self.entries[i][0]]
if current != new:
self.stack.append(new)
def loosen(self):
if len(self.stack) > 1:
self.stack.pop()
def peek(self):
result = []
for i in self.active():
entry = self.entries[i]
result.append((entry[0], entry[-1]))
return result
def transform(self, fn):
for i in self.active():
entry = self.entries[i]
entry.append(fn(entry[-1]))
names = [e[-1] for e in self.entries]
if len(names) != len(set(names)):
self.rollback()
raise FilenameCollisionError('Filename collisions detected.')
def rollback(self):
for i in self.active():
entry = self.entries[i]
if len(entry) > 1:
entry.pop()