-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
66 lines (57 loc) · 1.74 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
# -----------------------------------------------------------------------------
"""
Utility Functions
"""
# -----------------------------------------------------------------------------
inv_arg = "invalid argument\n"
def int_arg(ui, arg, limits, base):
"""convert a number string to an integer, or None"""
try:
val = int(arg, base)
except ValueError:
ui.put(inv_arg)
return None
if (val < limits[0]) or (val > limits[1]):
ui.put(inv_arg)
return None
return val
# -----------------------------------------------------------------------------
def display_cols(clist, csize=None):
"""
return a string for a list of columns
each element in clist is [col0_str, col1_str, col2_str, ...]
csize is a list of column width minimums
"""
if len(clist) == 0:
return ""
# how many columns?
ncols = len(clist[0])
# make sure we have a well formed csize
if csize is None:
csize = [
0,
] * ncols
else:
assert len(csize) == ncols
# convert any "None" items to ''
for l in clist:
assert len(l) == ncols
for i in range(ncols):
if l[i] is None:
l[i] = ""
# additional column margin
cmargin = 1
# go through the strings and bump up csize widths if required
for l in clist:
for i in range(ncols):
if csize[i] <= len(l[i]):
csize[i] = len(l[i]) + cmargin
# build the format string
fmts = []
for n in csize:
fmts.append("%%-%ds" % n)
fmt = "".join(fmts)
# generate the string
s = [(fmt % tuple(l)) for l in clist]
return "\n".join(s)
# -----------------------------------------------------------------------------