From b40370113f4fae0f98fe365a73dc15f902bb2114 Mon Sep 17 00:00:00 2001 From: Martin Matusiak Date: Thu, 20 May 2010 18:47:11 +0200 Subject: [PATCH] update --- network.py | 5 ++++- ui.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/network.py b/network.py index 90af27a..3fc073b 100644 --- a/network.py +++ b/network.py @@ -136,7 +136,7 @@ def read_scan(scan_data): return config.live_networks blocks = scan_data.split("Cell")[1:] - + fields = sdict() fields.bssid = "Address: ([^\s]*)" fields.essid = "ESSID:\"(.*?)\"" @@ -192,6 +192,9 @@ def read_scan(scan_data): elif net.encrypted and net.encrypted == "on": net.sec = 'wep' + # record time + net.seen = time.time() + config.live_networks = config.live_networks.merge1(net, 'bssid') config.live_networks = opts.post_negate_nets(config.live_networks, 'essid') return config.live_networks diff --git a/ui.py b/ui.py index a8b8114..2bf635f 100644 --- a/ui.py +++ b/ui.py @@ -132,23 +132,58 @@ def display_live_networks(nets, field=None, column=None): if not nets: return "No networks detected" - keys = ['bssid', 'essid', 'channel', 'bitrate', 'sec', 'signal', 'quality'] + keys = ['bssid', 'essid', 'channel', 'bitrate', 'sec', 'signal', 'seen'] width = 19; mw = 6; sp = 2 # width: max width mw : min width sp : separate if field: nets = nets.sort(sort_key=field) elif column and 0 < column <= len(keys): nets = nets.sort(sort_key=keys[column-1]) - else: nets = nets.sort(sort_key='signal') + else: + nets = nets.sort(sort_key='signal') - # figure out column length to assign to fields based on content length - def col_len(dicts, key): + def col_len(dcts, key): + "figure out column length to assign to fields based on content length" l = [] - for dict in dicts: - if key in dict and dict[key]: l.append(len(dict[key])+sp) - else: l.append(sp) + for dct in dcts: + # ugly special case for seen field + if key == 'seen': + l.append(len('seen')) + continue + + val = dct.get(key) + if val: + l.append(len(val)+sp) + else: + l.append(sp) return l ws = [max(min(max(col_len(nets, k)), width), mw) for k in keys] + + def format_timespan(span): + prefix = '' + units = ['s', 'm', 'h', 'd', 'w'] + unit_id = 0 + if span < 1: + prefix = '<' + span = 1 + # minutes + if span > 59: + span /= 60 + unit_id += 1 + # hours + if span > 59: + span /= 60 + unit_id += 1 + # days + if span > 23: + span /= 24 + unit_id += 1 + # weeks + if span > 6: + span /= 7 + unit_id += 1 + span = int(round(span)) + return "%s%s%s" % (prefix, span, units[unit_id]) s = "" for (i, key) in enumerate(keys): @@ -156,8 +191,16 @@ def display_live_networks(nets, field=None, column=None): s = s[:-sp] + "\n" for net in nets: for (i, key) in enumerate(keys): - if key in net: s += (net[key][:ws[i]-sp]).ljust(ws[i]) - else: s += "".ljust(ws[i]) + val = net.get(key) + + # ugly special case for seen field + if key == 'seen': + val = "%s" % format_timespan(time.time()-val) + + if val: + s += (val[:ws[i]-sp]).ljust(ws[i]) + else: + s += "".ljust(ws[i]) s = s[:-sp] + "\n" return s + "%d network(s) found" % len(nets) -- 2.11.4.GIT