def parse_active_nets(lines, units): """Return an orderd dict with nets, each containing a list of links.""" nets_lines = list(lib.split_iter_of_consecutive(lines[1:], lambda s: not s.strip(), 2)) nets = odict() for net_lines in lib.strip_iter_items(nets_lines): info = lib.strip_list(net_lines) name = info[0].strip() block = list(lib.iter_block(lib.strip_list(info[1:]), r"Net members:", r"\s.*Quality =")) table, quality_line = block[:-2], block[-1] max_quality = int(re.search("Quality = (\d+)", quality_line).group(1)) grid_field = re.match("Net members:\s*(.*?)\s*Role:", table[0]).group(1) grid_fields = ["Net members:", grid_field, "Role:", "System:", "Antenna:"] rows = list(lib.parse_table(table, grid_fields, lambda s: not s.startswith('#'))) net_members = create_odict_from_items("net_member", "net_members", rows) links = [] for link in get_net_links(rows, grid_field, units): peers = (link["node1"].net_members, link["node2"].net_members) link = lib.Struct("Link", peers=peers, quality=link["quality"], distance=link["distance"]) links.append(link) nets[name] = lib.Struct("Network", name=name, net_members=net_members, links=links, max_quality=max_quality) return nets
def parse_active_nets(lines, units): """Return an orderd dict with nets, each containing a list of links.""" nets_lines = list( lib.split_iter_of_consecutive(lines[1:], lambda s: not s.strip(), 2)) nets = odict() for net_lines in lib.strip_iter_items(nets_lines): info = lib.strip_list(net_lines) name = info[0].strip() block = list( lib.iter_block(lib.strip_list(info[1:]), r"Net members:", r"\s.*Quality =")) table, quality_line = block[:-2], block[-1] max_quality = int(re.search("Quality = (\d+)", quality_line).group(1)) grid_field = re.match("Net members:\s*(.*?)\s*Role:", table[0]).group(1) grid_fields = [ "Net members:", grid_field, "Role:", "System:", "Antenna:" ] rows = list( lib.parse_table(table, grid_fields, lambda s: not s.startswith('#'))) net_members = create_odict_from_items("net_member", "net_members", rows) links = [] for link in get_net_links(rows, grid_field, units): peers = (link["node1"].net_members, link["node2"].net_members) link = lib.Struct("Link", peers=peers, quality=link["quality"], distance=link["distance"]) links.append(link) nets[name] = lib.Struct("Network", name=name, net_members=net_members, links=links, max_quality=max_quality) return nets
def test_strip_iter_items(self): lst = [1, 0, 0, 2, 3, 4, 0, 5, 0, 0, 6, 7] self.assertEqual(list(lib.strip_iter_items(lst, lambda x: x != 0)), [1, 2, 3, 4, 5, 6, 7])