From 6e101bc6de59c7064b0a0ffe09d6e7230501e45b Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Sun, 21 Sep 2014 22:18:31 +0200 Subject: [PATCH] simplify mark_gateway --- bat2nodes.py | 13 ++++--------- batman.py | 20 +++++++++----------- nodedb.py | 16 +++++----------- 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/bat2nodes.py b/bat2nodes.py index 3d71d83..cc987ee 100755 --- a/bat2nodes.py +++ b/bat2nodes.py @@ -29,6 +29,7 @@ parser.add_argument('-a', '--aliases', metavar='FILE') parser.add_argument('-m', '--mesh', action='append', + default=["bat0"], help='batman mesh interface') parser.add_argument('-A', '--alfred', action='store_true', @@ -43,17 +44,11 @@ options = vars(args) db = NodeDB(int(time.time())) -if options['mesh']: - for mesh_interface in options['mesh']: - bm = batman(mesh_interface) - db.parse_vis_data(bm.vis_data(options['alfred'])) - for gw in bm.gateway_list(): - db.mark_gateways(gw['mac']) -else: - bm = batman() +for mesh_interface in options['mesh']: + bm = batman(mesh_interface) db.parse_vis_data(bm.vis_data(options['alfred'])) for gw in bm.gateway_list(): - db.mark_gateways([gw['mac']]) + db.mark_gateway(gw) if options['aliases']: for aliases in options['aliases']: diff --git a/batman.py b/batman.py index 583d962..94229ad 100755 --- a/batman.py +++ b/batman.py @@ -45,22 +45,20 @@ class batman: """ output = subprocess.check_output(["batctl","-m",self.mesh_interface,"gwl","-n"]) output_utf8 = output.decode("utf-8") - # TODO Parse information lines = output_utf8.splitlines() - own_mac = re.match(r"^.*MainIF/MAC: [^/]+/([0-9a-f:]+).*$",lines[0]).group(1) - # Remove header line - del lines[0] - # Fill gateway list + + own_mac = re.match(r"^.*MainIF/MAC: [^/]+/([0-9a-f:]+).*$", lines[0]).group(1) + gw = [] gw_mode = self.gateway_mode() if gw_mode['mode'] == 'server': - gw.append({'mac': own_mac, 'bandwidth': gw_mode['bandwidth']}) + gw.append(own_mac) + for line in lines: - gw_line = line.split() - if (gw_line[0] == 'No'): - continue - # When in client gateway mode maybe gw_line[0] is not the right. - gw.append({'mac':gw_line[0], 'bandwidth': gw_line[-1]}) + gw_line = re.match(r"^(?:=>)? +([0-9a-f:]+) ", line) + if gw_line: + gw.append(gw_line.group(1)) + return gw def gateway_mode(self): diff --git a/nodedb.py b/nodedb.py index 5a92231..1a719a3 100644 --- a/nodedb.py +++ b/nodedb.py @@ -224,18 +224,12 @@ class NodeDB: if 'id' in alias: node.id = alias['id'] - # list of macs - # if options['gateway']: - # mark_gateways(options['gateway']) - def mark_gateways(self, gateways): - for gateway in gateways: - try: - node = self.maybe_node_by_mac((gateway, )) - except: - print("WARNING: did not find gateway '",gateway,"' in node list") - continue - + def mark_gateway(self, gateway): + try: + node = self.maybe_node_by_mac((gateway, )) node.flags['gateway'] = True + except KeyError: + print("WARNING: did not find gateway ", gateway, " in node list") def update_vpn_links(self): changes = 1