-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
173 lines (153 loc) · 4.19 KB
/
utils.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
import json
import os
import sys
import time
from collections import defaultdict
def sanity_check_fail(msg):
print(msg)
sys.exit(1)
class NonUniqException(Exception):
def __init__(self, message, iterable):
self.message = message
self.iterable = iterable
class Logger:
def __init__(self):
self.debug = False # just wrap logging
def __call__(self, m):
sys.stderr.write(u'{0}'.format(m))
def entry(self, m):
self.__call__(m)
self.__call__('\n')
def warn(self, m):
self.__call__(m)
def debugonly(self, *args):
if self.debug: print(args)
def trystr(self, x):
try:
# does nothing for python 3 and we no longer support 2...
return x #unicode(x).encode('utf-8', errors='ignore').strip().decode('ascii', 'ignore')
except Exception as e:
self.__call__('#warn... unicode issue... ' + str(e))
return x
logger = Logger()
class Utils:
def __init__(self):
self.config = dict()
self.encoding = 'utf-8'
def fread(self, f, encoding=''):
if encoding:
with open(f, encoding=encoding) as infile:
return infile.read()
else:
with open(f, encoding=encoding) as infile:
return infile.read()
def fentries(self, f):
with open(f) as infile:
return [e.strip() for e in infile]
def writel(self, f, entries, sep=b'\n', encoding=None):
if not encoding: encoding = self.encoding
assert isinstance(entries, list), ['expected list, found:' , type(entries)]
with open(f, "wb") as outfile:
# handle mixed string, unicode lists
for e in entries:
if isinstance(e, str):
outfile.write(e.encode(encoding))
else:
outfile.write(e) #sep.join(entries))
outfile.write(sep)
return f
def dumpf(self, f, data):
with open(f, "w") as out:
out.write(data)
return f
def fexists(self, filename):
if os.path.exists(filename): return True
try:
with open(filename) as test:
return True
except IOError: return False
def demanduniq(self, iterable):
if len(iterable) == 1:
return iterable[0]
uniq = set()
for e in iterable:
uniq.add(e)
if len(uniq) > 1:
raise NonUniqException('#__nonUniqInDemandUniq:', uniq)
if len(uniq) != 1:
raise NonUniqException('#__nonUniqInDemandUniq:' , uniq)
return iterable[0]
def tryuniq(self, iterable):
if len(iterable) == 1:
return iterable[0]
uniq = set()
for e in iterable:
uniq.add(e)
uniqlist = list(uniq)
if len(uniqlist) == 1:
return uniqlist[0]
else:
logger('#__warn__ ... repeated values found where unique expeced... {0}\n'.format(str(uniqlist)))
return uniqlist
def safedict(self, tuples):
hashed = dict()
for k, v in tuples:
assert not k in hashed, '#__repeated__:{0} old:{1} new:{2}'.format(k, hashed[k], v)
hashed[k] = v
return hashed
def now(self):
current = time.ctime().split()
return ('-'.join(current[1:3] + [current[-1], current[-2]])).replace(':', '.')
def basename(self, f):
return os.path.basename(f)
def getpath(data, ifmissing, *args):
d = data
path = list()
try:
for e in args:
path.append(e)
d = d[e]
return d
except Exception as err:
return ifmissing
class Json:
def __init__(self):
self.multidict = True
self.sort_keys = True
self.indent = 4
self.separators = (',', ': ')
def loadf(self, f):
try:
if self.multidict:
return self.multidictLoadf(f)
else:
with open(f) as target:
return json.load(target)
except Exception as e:
print('__failed_at:' , f)
raise e
def dumpf(self, f, data):
stringed = self.pretty(data)
with open(f, 'w') as outfile:
outfile.write(stringed)
return f
def multidictLoadf(self, f):
with open(f) as target:
return self.multidictLoadStr(target.read())
def loads(self, data):
return self.multidictLoadStr(data)
def multidictLoadStr(self, data):
def multidictParser(pairs):
d = defaultdict(list)
for k, v in pairs:
d[k].append(v)
return { k: v[0] if len(v) == 1 else v for k, v in d.items()}
return json.JSONDecoder(object_pairs_hook=multidictParser).decode(data)
def pretty(self, arg):
return json.dumps(arg, sort_keys=self.sort_keys, indent=self.indent, separators=self.separators)
def pp(self, arg):
print(self.pretty(arg))
def copyobj(self, x):
return self.multidictLoadStr(self.pretty(x))
json2 = Json()
cmn = Utils()