-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommonlib.py
334 lines (306 loc) · 7.7 KB
/
commonlib.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
import os
import re
import sys
import glob
import time
import random
import unicodedata
try:
import apDisplay
except:
pass
"""
File for common functions to musiclib, movielib, and photolib
"""
class CommonLib(object):
#=======================
def humantime(self, secs):
mins, secs = divmod(secs, 60)
hours, mins = divmod(mins, 60)
days, hours = divmod(hours, 24)
if days > 0:
return '%02dd:%02dh' % (days, hours)
elif hours > 0:
return '%02dh:%02dm' % (hours, mins)
elif mins > 0:
return '%02dm:%02ds' % (mins, secs)
elif secs > 0.2:
return '%.1fs' % (secs)
else:
return '%.1es' % (secs)
#=======================
def isint(self, n):
if re.search("^\d+$", n):
return True
return False
#=======================
def extraCleanName(self, f):
g = self.cleanName(f)
if g is None:
return 'None'
g = self.cleanName(g.lower())
if g is None:
return 'None'
g = re.sub("_", "", g)
g = re.sub("-", "", g)
g = g.lower()
return g
#=======================
def compareStrings(self, s1, s2):
size = max(len(s1), len(s2))
return self.levenshtein(s1, s2)/float(size)
#=======================
def levenshtein(self, s1, s2):
if len(s1) < len(s2):
return self.levenshtein(s2, s1)
if not s1:
return len(s2)
previous_row = range(len(s2) + 1)
for i, c1 in enumerate(s1):
current_row = [i + 1]
for j, c2 in enumerate(s2):
insertions = previous_row[j + 1] + 1 # j+1 instead of j since previous_row and current_row are one character longer
deletions = current_row[j] + 1 # than s2
substitutions = previous_row[j] + (c1 != c2)
current_row.append(min(insertions, deletions, substitutions))
previous_row = current_row
return previous_row[-1]
#=======================
def cleanName(self, f, cut=True):
specials = { "!!!": "ChkChkChk", "!": "Exclaim",
"*": "Asterix", "( )": "Parens", "+/-": "PlusMinus", }
words = ['of', 'the', 'a', 'in', 'for', 'am', 'is', 'on',
'to', 'than', 'with', 'by', 'from', 'or', 'and',
]
if f is None:
return None
g = f.strip()
if g in specials:
return specials[g]
if len(g) < 1:
return None
g = self.unicodeToString(g)
g = re.sub(" ", "_", g)
g = re.sub("\'", "", g)
g = re.sub("\"", "", g)
g = re.sub("\]", "_", g)
g = re.sub("\[", "_", g)
g = re.sub("^the_", "", g, re.IGNORECASE)
g = re.sub("^an_", "", g, re.IGNORECASE)
g = re.sub("^a_", "", g, re.IGNORECASE)
g = re.sub("^The_", "", g, re.IGNORECASE)
g = re.sub("^An_", "", g, re.IGNORECASE)
g = re.sub("^A_", "", g, re.IGNORECASE)
g = re.sub("&", "and", g)
g = re.sub("[^a-zA-z0-9_-]", "_", g)
for word in words:
a = re.search("_("+word+")_", g, re.IGNORECASE)
if a:
for inword in a.groups():
g = re.sub("_"+inword+"_", "_"+word+"_", g)
g = re.sub("[_ ]feat[_ ][a-zA-Z0-9 _-]*$", "and", g)
g = re.sub("[_ ]featuring[_ ][a-zA-Z0-9 _-]*$", "and", g)
g = re.sub("[_ ]ft[_ ][a-zA-Z0-9 _-]*$", "and", g)
if re.search('[^a-z]feat[^a-z]', g):
print(g)
g = re.sub("\^", "_", g)
g = re.sub(",", "_", g)
g = re.sub("^-", "", g)
g = re.sub("_-", "-", g)
g = re.sub("-_", "-", g)
g = re.sub("__", "_", g)
g = re.sub("__", "_", g)
g = re.sub("_*$", "", g)
g = re.sub("^_*", "", g)
g = re.sub("^-*", "", g)
if g == "unknown":
return None
if re.search('[^a-zA-z0-9_-]', g):
print("\033[1;32mWeird character: "+g+"\033[0m")
time.sleep(2)
if len(g) == 0:
print("\033[31mERROR: "+f+"\033[0m")
return None
sys.exit(1)
if cut is True and len(g) > 40:
if re.search("_[0-9]*$", g):
g = re.sub("_[0-9]*$", "", g)
g = g[:40]
g = re.sub("_*$", "", g)
if len(g) < 1:
return None
elif len(g) < 2:
return g
g = g[0].upper()+g[1:]
return g
#===============
def getMountPoint(self, filename):
"""
returns file or directory mount point
"""
path = os.path.abspath(filename)
while not os.path.ismount(path):
path = os.path.dirname(path)
return path
#===============
def fileSize(self, filename):
"""
return file size in bytes
"""
if not os.path.isfile(filename):
return 0
stats = os.stat(filename)
size = stats[6]
return size
#=======================
def unicodeToString(self, uni):
if not isinstance(uni, str) and not isinstance(uni, str):
try:
uni = uni.decode("utf8")
except AttributeError:
uni = uni.text[0]
try:
uni = str(uni)
string = unicodedata.normalize('NFKD', uni)
except UnicodeDecodeError:
print(uni)
string = uni
string = str(string.encode('ascii', 'ignore'))
return string
#===============
def md5sumfile(self, filename):
"""
Returns an md5 hash for file filename
"""
if not os.path.isfile(filename):
apDisplay.printError("MD5SUM, file not found: "+filename)
f = file(filename, 'rb')
#this next library is deprecated in python 2.6+, need to use hashlib
import hashlib
m = hashlib.md5()
while True:
d = f.read(8096)
if not d:
break
m.update(d)
f.close()
return m.hexdigest()
#===============
def quickmd5(self, filename):
"""
Returns a quick md5 hash for file filename
"""
if not os.path.isfile(filename):
print(self.colorString("MD5SUM, file not found: "+filename, "red"))
sys.exit(1)
f = file(filename, 'rb')
#this next library is deprecated in python 2.6+, need to use hashlib
import hashlib
m = hashlib.md5()
for i in range(9):
d = f.read(8096)
if not d:
break
m.update(d)
f.seek(8096)
f.close()
return m.hexdigest()
#=======================
def getNumFilesInDir(self, dirname):
absdirname = os.path.abspath(dirname)
if not os.path.isdir(absdirname):
return 0
files = glob.glob(os.path.join(absdirname, "*.*"))
return len(files)
#=======================
def getFiles(self, depth=6, extlist=[], folder=None, shuffle=False):
files = []
for ext in extlist:
sstr = "*."+ext
for i in range(depth+1):
if folder is not None:
sstr2 = os.path.join(folder, sstr)
else:
sstr2 = sstr
files.extend(glob.glob(sstr2))
sstr = "*/"+sstr
files.sort()
if shuffle is True:
random.shuffle(files)
print("Found %d files"%(len(files)))
return files
#=======================
def rightPadString(self, s,n=10,fill=" "):
n = int(n)
s = str(s)
if(len(s) > n):
return s[:n]
while(len(s) < n):
s += fill
return s
#=======================
def leftPadString(self, s,n=10,fill=" "):
n = int(n)
s = str(s)
if(len(s) > n):
return s[:n]
while(len(s) < n):
s = fill+s
return s
#=======================
def colorString(self, text, fg=None, bg=None):
"""Return colored text.
Uses terminal color codes; set avk_util.enable_color to 0 to
return plain un-colored text. If fg is a tuple, it's assumed to
be (fg, bg). Both colors may be 'None'.
"""
colors = {
"black" :"30",
"red" :"31",
"green" :"32",
"brown" :"33",
"orange":"33",
"blue" :"34",
"violet":"35",
"purple":"35",
"magenta":"35",
"maroon":"35",
"cyan" :"36",
"lgray" :"37",
"gray" :"1;30",
"lred" :"1;31",
"lgreen":"1;32",
"yellow":"1;33",
"lblue" :"1;34",
"pink" :"1;35",
"lcyan" :"1;36",
"white" :"1;37"
}
if fg is None:
return text
if type(fg) in (tuple, list):
fg, bg = fg
if not fg:
return text
opencol = "\033["
closecol = "m"
clear = opencol + "0" + closecol
xterm = 0
if os.environ.get("TERM") is not None and os.environ.get("TERM").startswith("xterm"):
xterm = True
else:
xterm = False
b = ''
# In xterm, brown comes out as yellow..
if xterm and fg == "yellow":
fg = "brown"
f = opencol + colors[fg] + closecol
if bg:
if bg == "yellow" and xterm:
bg = "brown"
try:
b = colors[bg].replace('3', '4', 1)
b = opencol + b + closecol
except KeyError:
pass
return "%s%s%s%s" % (b, f, text, clear)