class GeoMapBuilder: kml_template = """ %s """ def __init__(self, db): self._db = db def build(self): text = [] nodes = self._db.get_nodes() for node in nodes: try: text.append(GeoNode(node).render()) except: continue for link in self._db.get_links(): try: text.append(GeoEdge(nodes, link).render()) except: continue return self.kml_template % "".join(text) def gps_format(s): return ",".join(s.split(" ")[::-1]) class GeoNode: kml_template = """ %s #router-%s %s,0 %s """ def __init__(self, node): self._node = node def render(self): if not self._node.gps: raise name = self._node.name status = "up" if self._node.online else "down" gps = gps_format(self._node.gps) text = " ".join(self._node.macs) return self.kml_template % (name, status, gps, text) class GeoEdge: kml_template = """ %s,0. %s,0. #%s """ def __init__(self, nodes, link): self._link = link self.pair = [nodes[k] for k in link.pair] def render(self): if not (self.pair[0].gps and self.pair[1].gps): raise a = gps_format(self.pair[0].gps) b = gps_format(self.pair[1].gps) return self.kml_template % (a, b, "wifi-link")