-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
214 lines (153 loc) · 3.4 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
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
# # utils.py
import sys
import re
def isint(s):
try:
int(s)
except ValueError:
return False
return True
def isfloat(s):
try:
float(s)
except ValueError:
return False
return True
def ishex(s):
s = s.lower()
if not s.startswith('0x'):
return False
s = s[2:]
try:
int(s, 16)
except ValueError:
return False
return True
def allints(l):
return all(isint(s) for s in l)
def allfloats(l):
return all(isfloat(s) for s in l)
def allhexs(l):
return all(ishex(s) for s in l)
def tofloat(s):
try:
return float(s)
except ValueError:
return s
def toint(s):
try:
return int(s)
except ValueError:
return s
def tohexint(s):
try:
return int(s, 16)
except ValueError:
return s
def settype(untypedlist):
typedlist = []
numparts = len(untypedlist)
if allints(untypedlist):
typedlist.extend( [toint(s) for s in untypedlist] )
elif allfloats(untypedlist):
typedlist.extend( [tofloat(s) for s in untypedlist] )
elif allhexs(untypedlist):
typedlist.extend( [tohexint(s) for s in untypedlist] )
else:
typedlist.append(untypedlist[0])
return typedlist
def istrue(s):
return s.lower() in ['true', 'on', 'yes', 'y']
def isfalse(s):
return s.lower() in ['false', 'off', 'no', 'n']
def isbool(s):
return istrue(s) or isfalse(s)
def tobool(s):
return istrue(s)
def slurplines(filename):
lines = []
with open(filename, 'r') as f:
lines = f.read().splitlines()
return lines
def removecomments(lines, commentstart='#'):
return [re.sub(commentstart + '.*', '', s) for s in lines]
def removeblanks(lines):
return list(filter(None, lines))
def getscale(v):
s = str(v)
parts = s.split('.', 1)
if len(parts) > 1:
return(len(parts[1]))
return 0
def getscales(l):
scales = []
for v in l:
scales.append(getscale(v))
return scales
def getmaxscale(l):
return max( getscales(l) )
def combinelines(lines, marker="'''"):
olines = []
combine = False
oline = ''
for line in lines:
markerfound = marker in line
if markerfound:
line = line.replace(marker, '')
if combine:
oline = oline + line + '\n'
else:
oline = line
if markerfound:
combine = not combine
if not combine:
olines.append(oline)
oline = ''
return olines
def longestkey(d):
klen = 0
for k,v in d.items():
if len(k) > klen:
klen = len(k)
return klen
def strkv(d, name = '', sep='\n', skip={}, firstcol=0):
klen = longestkey(d) + len(name) + 2
printlist = []
for k,v in d.items():
if type(v) == list and firstcol>0:
v = v[firstcol:]
if k not in skip:
if name != '':
k = f'{name}[{k}]'
if sep == '\n':
k = k.ljust(klen)
s = f'{k} = {v}'
else:
s = f'{k}={v}'
printlist.append(s)
return sep.join(printlist)
def replacewithkv(s, d, pre='', post=''):
for k,v in d.items():
fromstr = pre + str(k) + post
tostr = str(v)
s = s.replace(fromstr, tostr)
return s
def stripall(lines):
return [s.strip() for s in lines]
def dbg(s):
print(s, file=sys.stderr)
def abort(s='', err=1):
dbg(f'Error {err}: {s}', file=sys.stderr)
sys.exit(err)
def getorquit(d,k):
if k not in d:
abort(f"'{k}' not defined")
return d[k]
def first(od):
return next(iter(od.items()))
def get_nth(od, n):
if n > len(od)-1:
return
k = list(od.keys())[n]
v = od[k]
return k,v