- Timestamp:
- 10/09/13 22:22:47 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
etherws/trunk/etherws.py
r254 r255 982 982 return json.loads(urllib2.urlopen(req, json.dumps(data)).read()) 983 983 984 def maxlen(dict_, key, min_): 985 if not dict_: 986 return min_ 987 max_ = max(len(str(r[key])) for r in dict_) 988 return min_ if max_ < min_ else max_ 984 def print_table(rows): 985 cols = zip(*rows) 986 maxlen = [0] * len(cols) 987 988 for i in xrange(len(cols)): 989 maxlen[i] = max(len(str(c)) for c in cols[i]) 990 991 fmt = ' '.join(['%%-%ds' % maxlen[i] for i in xrange(len(cols))]) 992 fmt = ' ' + fmt 993 994 for row in rows: 995 print(fmt % tuple(row)) 989 996 990 997 def print_portlist(result): 991 pmax = maxlen(result, 'port', 4) 992 ymax = maxlen(result, 'type', 4) 993 smax = maxlen(result, 'shut', 5) 994 rmax = maxlen(result, 'rx', 2) 995 tmax = maxlen(result, 'tx', 2) 996 fmt = ' %%%ds %%%ds %%%ds %%%ds %%%ds %%s' % \ 997 (pmax, ymax, smax, rmax, tmax) 998 print(fmt % ('Port', 'Type', 'State', 'RX', 'TX', 'Target')) 998 rows = [['Port', 'Type', 'State', 'RX', 'TX', 'Target']] 999 999 for r in result: 1000 shut = 'shut' if r['shut'] else 'up'1001 print(fmt %1002 (r['port'], r['type'], shut, r['rx'], r['tx'], r['target']))1000 rows.append([r['port'], r['type'], 'shut' if r['shut'] else 'up', 1001 r['rx'], r['tx'], r['target']]) 1002 print_table(rows) 1003 1003 1004 1004 def print_iflist(result): 1005 pmax = maxlen(result, 'port', 4) 1006 tmax = maxlen(result, 'type', 4) 1007 amax = maxlen(result, 'address', 7) 1008 nmax = maxlen(result, 'netmask', 7) 1009 mmax = maxlen(result, 'mtu', 3) 1010 fmt = ' %%%ds %%%ds %%%ds %%%ds %%%ds %%s' % \ 1011 (pmax, tmax, amax, nmax, mmax) 1012 print(fmt % ('Port', 'Type', 'Address', 'Netmask', 'MTU', 'Target')) 1005 rows = [['Port', 'Type', 'Address', 'Netmask', 'MTU', 'Target']] 1013 1006 for r in result: 1014 print(fmt % (r['port'], r['type'], 1015 r['address'], r['netmask'], r['mtu'], r['target'])) 1007 rows.append([r['port'], r['type'], r['address'], 1008 r['netmask'], r['mtu'], r['target']]) 1009 print_table(rows) 1016 1010 1017 1011 def handle_ctl_addport(args): … … 1105 1099 if 'error' in result: 1106 1100 return _print_error(result['error']) 1107 result = result['result']['entries'] 1108 pmax = maxlen(result, 'port', 4) 1109 vmax = maxlen(result, 'vid', 4) 1110 mmax = maxlen(result, 'mac', 3) 1111 amax = maxlen(result, 'age', 3) 1112 fmt = ' %%%ds %%%ds %%-%ds %%%ds' % (pmax, vmax, mmax, amax) 1113 print(fmt % ('Port', 'VLAN', 'MAC', 'Age')) 1114 for r in result: 1115 print(fmt % (r['port'], r['vid'], r['mac'], r['age'])) 1101 1102 rows = [['Port', 'VLAN', 'MAC', 'Age']] 1103 for r in result['result']['entries']: 1104 rows.append([r['port'], r['vid'], r['mac'], r['age']]) 1105 print_table(rows) 1116 1106 1117 1107 locals()['handle_ctl_' + args.control_method](args)
Note: See TracChangeset
for help on using the changeset viewer.