Skip to content

Commit bc89e06

Browse files
committed
Added a table formatter, applied it to --device-list.
1 parent 8c50e93 commit bc89e06

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ check: check-nonm
66
for T in test/test02*.py; do $$T; done
77

88
check-nonm:
9-
for T in networkmanager/util.py; do python $$T; done
9+
for T in `find . -name \*.py | xargs grep -l '>>>'`; do python $$T; done
1010
for T in test/test01*.py; do $$T; done
1111

1212
install:

cnetworkmanager

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ from networkmanager.monitor import Monitor
99
from networkmanager.applet import NetworkManagerSettings, SYSTEM_SERVICE, USER_SERVICE
1010
from networkmanager.applet.service import NetworkManagerUserSettings
1111
import networkmanager.applet.settings as settings
12+
from networkmanager.util import Table
1213

1314
# must be set before we ask for signals
1415
from dbus.mainloop.glib import DBusGMainLoop
@@ -22,6 +23,10 @@ from optparse import OptionParser
2223

2324
op = OptionParser(version="%prog " + VERSION)
2425

26+
op.add_option("--terse",
27+
action="store_true", default=False,
28+
help="No table headings and padding, suitable for parsing")
29+
2530
# TODO http://docs.python.org/lib/optparse-adding-new-types.html
2631
op.add_option("-w", "--wifi",
2732
choices=["0","1","off","on","no","yes","false","true"],
@@ -94,6 +99,8 @@ op.add_option("--wpa-pass",
9499

95100
(options, args) = op.parse_args()
96101

102+
Table.terse = options.terse
103+
97104
nm = NetworkManager()
98105

99106
true_choices = ["1", "on", "yes", "true"]
@@ -110,8 +117,10 @@ if options.whe:
110117

111118
if options.device_list:
112119
devs = nm.GetDevices()
120+
t = Table("Interface", "Type", "State")
113121
for dev in devs:
114-
print dev["Interface"], dev["DeviceType"], dev["State"]
122+
t.row(dev["Interface"], dev["DeviceType"], dev["State"])
123+
print t
115124

116125
# --device-info, TODO clean up
117126
def get_device(dev_spec, hint):

networkmanager/util.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,78 @@ def __str__(self):
6868
# TODO unknown values?
6969
return ",".join(names)
7070

71+
class Table(object):
72+
"""Formats a table
73+
74+
>>> t = Table("A", "Bee", "C")
75+
76+
>>> t.row("ay", "B", "sea")
77+
78+
>>> t.row("", "b", "c")
79+
80+
>>> print t
81+
A | Bee | C
82+
---+-----+----
83+
ay | B | sea
84+
| b | c
85+
86+
>>> Table.terse = True
87+
88+
>>> print t
89+
ay|B|sea
90+
|b|c
91+
"""
92+
# there are trailing spaces in the above non-terse printout
93+
94+
terse = False
95+
"No headings and no padding, suitable for parsing."
96+
97+
def __init__(self, *args):
98+
"The arguments are column headings."
99+
100+
self.headings = args
101+
self.rows = []
102+
103+
def row(self, *args):
104+
"""The arguments are row items.
105+
106+
str is applied to them"""
107+
self.rows.append(map(str,args))
108+
109+
@staticmethod
110+
def pad_row(row, col_sizes):
111+
return map(str.ljust, row, col_sizes)
112+
113+
def col_widths(self):
114+
"Returns a list of the column widths."
115+
116+
# the table of lengths of original items
117+
lengths = map(lambda cells: map(len, cells), self.rows + [self.headings])
118+
return reduce(lambda cells1, cells2: map(max, cells1, cells2), lengths)
119+
120+
def terse_str(self):
121+
"Formats the table, tersely."
122+
123+
rs = map("|".join, self.rows)
124+
return "\n".join(rs)
125+
126+
def __str__(self):
127+
"Formats the table."
128+
129+
if self.terse:
130+
return self.terse_str()
131+
132+
cw = self.col_widths()
133+
def fmt_row(items):
134+
return " | ".join(self.pad_row(items, cw))
135+
h = fmt_row(self.headings)
136+
s = "-+-".join(map(lambda w: "-" * w, cw))
137+
rs = map(fmt_row, self.rows)
138+
rs.insert(0, s)
139+
rs.insert(0, h)
140+
return "\n".join(rs)
141+
142+
71143
if __name__ == "__main__":
72144
import doctest
73145
doctest.testmod()

0 commit comments

Comments
 (0)