forked from albumidentify/albumidentify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
112 lines (99 loc) · 2.6 KB
/
util.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
import re
import sys
import time
import datetime
VERBOSE=0
def clean_name(name):
"Clean up an mp3 name for comparison"
if name is None:
return None
name = re.sub(r"\(.*\)","",name)
name = re.sub(r"\[.*\]","",name)
name = re.sub(r"\{.*\}","",name)
name = re.sub(r"[^A-Za-z0-9]","",name)
return name.lower()
def comp_name(n1,n2):
"Copmare two names for equality, after applying cleanups"
return clean_name(n1) == clean_name(n2)
def _combinations(func,doneargs,todoargs):
if todoargs==():
yield func(*doneargs)
elif type(todoargs[0]) not in [type(()),type([])]:
for ret in _combinations(func,doneargs+(todoargs[0],),todoargs[1:]):
yield ret
else:
for arg in todoargs[0]:
for ret in _combinations(func,doneargs+(arg,),todoargs[1:]):
yield ret
return
def combinations(func,*args):
"""This function takes a function, and some arguments, some of which may be
collections. For each sequence, the function is call combinatorially"""
for ret in _combinations(func,(),args):
yield ret
def update_progress(msg):
"Display some progress"
if type(msg) == type(''):
msg = msg.decode('utf8','ignore')
if VERBOSE:
print time.strftime("%H:%M:%S"),msg.encode("ascii","ignore")
else:
sys.stdout.write(time.strftime("%H:%M:%S ")+msg.encode("ascii","ignore")+"\x1b[K\r")
sys.stdout.flush()
def update_verbose_progress(msg):
"Display some progress"
if type(msg) == type(''):
msg = msg.decode('utf8','ignore')
if VERBOSE:
print msg.encode("ascii","ignore")
else:
sys.stdout.write(msg.encode("ascii","ignore")+"\x1b[K\n")
sys.stdout.flush()
def output_list(l):
"Give na list of integers, return a string with ranges collapsed"
if not l:
return "[]"
l.sort()
ret=[]
start=l[0]
end=l[0]
for i in l[1:]:
if end+1==i:
end=i
continue
if start!=end:
ret.append("%d-%d" % (start,end))
else:
ret.append("%d" % start)
start=i
end=i
if start!=end:
ret.append("%d-%d" % (start,end))
else:
ret.append("%d" % start)
return "[%s]" % (",".join(ret))
def list_difference(src,remove):
res=src[:]
for i in remove:
if i in res:
res.remove(i)
return res
report_entries = []
def report(message):
ts = datetime.datetime.now().ctime()
if (type(message) == type([])):
for m in message:
report(m)
return
report_entries.append("%s: %s" % (ts, message))
print message
def write_report(reportpath):
global report_entries
f = open(reportpath, "w")
try:
for r in report_entries:
f.write(r.encode("utf8"))
f.write("\n")
finally:
f.close()
report_entries = []