def fnc_build_graphml(df_system, dfConnFinal, global_dict, router_mode, filename): if router_mode in ['1','2','3','4']: G = pyyed.Graph() for router in df_system.itertuples(): router_name = router.name router_function = get_attribute(router_name,"HWfunction",global_dict) router_int = get_attribute(router_name,"Int_Status",global_dict) router_ckt_id = get_attribute(router_name,"ckt_id",global_dict) router_color = get_attribute(router_name,"color",global_dict)['yEd'] router_label = fnc_router_metadata(global_dict, router_name, 'labelHtml', router_function, router_ckt_id, router_mode) router_system = get_attribute(router_name,"system",global_dict) G.add_node(router_name, label=router_name + "\n" + router_system, shape_fill=router_color) for link in dfConnFinal.itertuples(): edgeLabel = link.port1 + "--" + link.port2 if link.cable != "N/A": G.add_edge(link.name1, link.name2, arrowhead='none',arrowfoot='none', label= link.cable + "\n" + edgeLabel) else: G.add_edge(link.name1, link.name2, arrowhead='none',arrowfoot='none', label= edgeLabel) print(filename) G.write_graph(filename+'.graphml') sys.exit(4)
def test_multiple_edges(): g = pyyed.Graph() g.add_node('a', font_family="Zapfino").add_label("a2") g.add_node('b', font_family="Zapfino").add_label("b2") g.add_node('c', font_family="Zapfino").add_label("c2") g.add_edge('a', 'b') g.add_edge('a', 'b') g.add_edge('a', 'c') e1 = g.edges['1'] e2 = g.edges['2'] e3 = g.edges['3'] assert g.nodes[e1.node1].list_of_labels[0]._text == "a" assert g.nodes[e1.node2].list_of_labels[0]._text == "b" assert g.nodes[e2.node1].list_of_labels[0]._text == "a" assert g.nodes[e2.node2].list_of_labels[0]._text == "b" assert g.nodes[e3.node1].list_of_labels[0]._text == "a" assert g.nodes[e3.node2].list_of_labels[0]._text == "c" # Test-cases for the second label assert g.nodes[e1.node1].list_of_labels[1]._text == "a2" assert g.nodes[e1.node2].list_of_labels[1]._text == "b2" assert g.nodes[e2.node1].list_of_labels[1]._text == "a2" assert g.nodes[e2.node2].list_of_labels[1]._text == "b2" assert g.nodes[e3.node1].list_of_labels[1]._text == "a2" assert g.nodes[e3.node2].list_of_labels[1]._text == "c2" assert g.get_graph()
def generateGraphML(databaseFile, outputFile): g = pyyed.Graph() objs = json.load(databaseFile) xcoord = 0 ycoord = 0 currtype = objs[0]['type'] for obj in objs: if obj['type'] != currtype: ycoord += nodeDistanceY xcoord = 0 currtype = obj['type'] g.add_node(obj['id'], label=obj['name'], shape=nodeShape, width=str(nodeWidth), height=str(nodeHeight), x=str(xcoord), y=str(ycoord), shape_fill=colors[obj['type']]) xcoord += nodeDistanceX for parent in objs: for child in parent['prereqs']: parentId = parent['id'] childId = child g.add_edge(childId, parentId) outputFile.write(g.get_graph())
def get_graph(self): g = pyyed.Graph() self.dump_to_graph(g, g) self.dump_edges_to_graph(g) graph = g.get_graph() return minidom.parseString(graph).toprettyxml(indent=" ")
def test_node_already_there_check(): g = pyyed.Graph() g.add_node('a') with pytest.raises(RuntimeWarning): g.add_node('a') with pytest.raises(RuntimeWarning): g.add_group('a') g = pyyed.Graph() g.add_group('a') with pytest.raises(RuntimeWarning): g.add_node('a') with pytest.raises(RuntimeWarning): g.add_group('a') g = pyyed.Graph() g.add_edge('a', 'b') with pytest.raises(RuntimeWarning): g.add_node('a') with pytest.raises(RuntimeWarning): g.add_group('a') g1 = g.add_group('g1') with pytest.raises(RuntimeWarning): g1.add_node('a') with pytest.raises(RuntimeWarning): g1.add_group('a') g = pyyed.Graph() g1 = g.add_group('g1') g1.add_node('a') g2 = g.add_group('g2') with pytest.raises(RuntimeWarning): g.add_node('a') with pytest.raises(RuntimeWarning): g.add_group('a') with pytest.raises(RuntimeWarning): g1.add_node('a') with pytest.raises(RuntimeWarning): g1.add_group('a') with pytest.raises(RuntimeWarning): g2.add_node('a') with pytest.raises(RuntimeWarning): g2.add_group('a')
def test_uml_stereotype_is_optional(): g = pyyed.Graph() expected_attributes = "int foo\nString bar" expected_methods = "foo()\nbar()" g.add_node('Class', node_type="UMLClassNode", UML={"attributes": expected_attributes, "methods": expected_methods}) assert g.nodes["Class"].UML["methods"] == expected_methods assert g.nodes["Class"].UML["attributes"] == expected_attributes graphml = g.get_graph() assertUmlNode(graphml, "", expected_attributes, expected_methods)
def test_numeric_node_ids(): g = pyyed.Graph() g.add_node(1, label="Node1") g.add_node(2, label="Node2") g.add_edge(1, 2) assert g.nodes[1].label == "Node1" assert g.nodes[2].label == "Node2" node1 = g.edges['1_2'].node1 node2 = g.edges['1_2'].node2 assert g.nodes[node1].label == "Node1" assert g.nodes[node2].label == "Node2" assert g.get_graph()
def test_node_properties_after_nodes_and_edges_added(): g = pyyed.Graph() node1 = g.add_node('foo', shape="ellipse") node2 = g.add_node('foo2', shape="roundrectangle", font_style="bolditalic") edge1 = g.add_edge('foo1', 'foo2') node3 = g.add_node('abc', shape="triangle", font_style="bold") assert g.nodes["foo"].shape == "ellipse" assert g.nodes["foo"].list_of_labels[0]._params['fontStyle'] == "plain" assert g.nodes["foo2"].shape == "roundrectangle" assert g.nodes["foo2"].list_of_labels[0]._params['fontStyle']== "bolditalic" assert g.nodes["abc"].shape == "triangle" assert g.nodes["abc"].list_of_labels[0]._params['fontStyle'] == "bold"
def test_node_properties_after_nodes_and_edges_added(): g = pyyed.Graph() g.add_node('foo', shape="ellipse") g.add_node('foo2', shape="roundrectangle", font_style="bolditalic") g.add_edge('foo1', 'foo2') g.add_node('abc', shape="triangle", font_style="bold") assert g.nodes["foo"].shape == "ellipse" assert g.nodes["foo"].font_style == "plain" assert g.nodes["foo2"].shape == "roundrectangle" assert g.nodes["foo2"].font_style == "bolditalic" assert g.nodes["abc"].shape == "triangle" assert g.nodes["abc"].font_style == "bold"
def test_uml_node_properties_are_set(): g = pyyed.Graph() expected_attributes = "int foo\nString bar" expected_methods = "foo()\nbar()" expected_stereotype = "abstract" g.add_node('AbstractClass', node_type="UMLClassNode", UML={"stereotype": expected_stereotype, "attributes": expected_attributes, "methods": expected_methods}) assert g.nodes["AbstractClass"].UML["stereotype"] == expected_stereotype assert g.nodes["AbstractClass"].UML["attributes"] == expected_attributes assert g.nodes["AbstractClass"].UML["methods"] == expected_methods graphml = g.get_graph() assertUmlNode(graphml, expected_stereotype, expected_attributes, expected_methods)
def test_nested_graph_edges(): g = pyyed.Graph() g.add_edge('a', 'b') g1 = g.add_group('g1') g1n1 = g1.add_node('g1n1') g1n1 = g1.add_node('g1n2') g2 = g1.add_group('g2') g2n1 = g2.add_node('g2n1') g2n2 = g2.add_node('g2n2') g3 = g1.add_group('g3') g3n1 = g3.add_node('g3n1') g3n2 = g3.add_node('g3n2') assert g.num_edges == 1 g1.add_edge('g1n1', 'g1n2') assert g.num_edges == 2 g2.add_edge('g2n2', 'g2n2') # No, that's not a typo assert g.num_edges == 3 g3.add_edge('c', 'd') g3.add_edge('c', 'd') assert g.num_edges == 5 g.add_edge('g2n1', 'g2n2') g1.add_edge('g2n1', 'g2n2') g2.add_edge('g2n1', 'g2n2') with pytest.raises(RuntimeWarning): g3.add_edge('g2n1', 'g2n2') assert g.num_edges == 8 with pytest.raises(RuntimeWarning): g2.add_edge('a', 'b') g.add_edge('g1n1', 'g2n2') g1.add_edge('g1n1', 'g2n2') with pytest.raises(RuntimeWarning): g2.add_edge('g1n1', 'g2n2') with pytest.raises(RuntimeWarning): g3.add_edge('g1n1', 'g2n2') assert g.num_edges == 10
def test_multiple_edges(): g = pyyed.Graph() g.add_node('a', font_family="Zapfino") g.add_node('b', font_family="Zapfino") g.add_node('c', font_family="Zapfino") g.add_edge('a', 'b') g.add_edge('a', 'b') g.add_edge('a', 'c') e1 = g.edges['1'] e2 = g.edges['2'] e3 = g.edges['3'] assert g.nodes[e1.node1].label == "a" assert g.nodes[e1.node2].label == "b" assert g.nodes[e2.node1].label == "a" assert g.nodes[e2.node2].label == "b" assert g.nodes[e3.node1].label == "a" assert g.nodes[e3.node2].label == "c" assert g.get_graph()
def __init__(self): self._graph = pyyed.Graph() self._node_id = 0 self._id_for_name = dict() self._name_for_id = dict()
def netgraph(): print('\nCreating network map to pyscan.graphml in current directory...') print('Download yEd to edit pyscan.graphml from https://www.yworks.com/products/yed') G = pyyed.Graph() # f = plt.figure() bn = 'base' G.add_node(bn, label='base') options = { 'with_labels': True, 'node_size': 100, 'font_size': 1, 'node_shape': 's' } tree = ET.parse('pyscan.xml') root = tree.getroot() subnets = root.findall('subnet') for sub in subnets: rd = sub.findtext('pivot') sa = sub.findtext('subnet-address') sn = sub.findtext('subnet-name') subnetText = '{0}\n{1}'.format(sa, sn) G.add_node(sa, label=subnetText, shape="roundrectangle") G.add_edge(bn, sa, label=get_ip_address(sa)) hosts = sub.findall('host') for h in hosts: portnums = 'Ports:' addy = h.find('address').text ports = h.findall('port') hostname = h.find('hostname').text counter = 0 for p in ports: var = p.findtext('number') if counter == 5: portnums = '{0}\n{1}'.format(portnums, var) counter = 1 elif counter == 0: portnums = '{0} {1}'.format(portnums, var) counter += 3 else: portnums = '{0}, {1}'.format(portnums, var) counter += 1 if hostname == None: hostname = 'Hostname Unknown' nodeText = '{0}\n{1}\n{2}'.format(addy, hostname, portnums) G.add_node(addy, label=nodeText, shape="roundrectangle") G.add_edge(sa, addy) # pos = nx.spring_layout(G) # nx.draw(G, pos, **options) # nx.write_graphml(G, "test.graphml") # f.savefig("pyscan.png", dpi=1200) with open('pyscan.graphml', 'w') as fp: fp.write(G.get_graph()) print('Complete!') return
def fnc_build_graphml(routers, edges, global_dict, router_mode, filename): if router_mode in ['1', '2', '3', '4']: G = pyyed.Graph() nodes = pd.DataFrame(routers) nodes.columns = ['router_name', 'port', 'label'] nodes = nodes[['router_name']] nodes = nodes.drop_duplicates() links = pd.DataFrame(edges) links.columns = ['routerA', 'routerB', 'cableID'] for router in nodes.itertuples(): router_name = router.router_name router_function = get_attribute(router_name, "HWfunction", global_dict) router_int = get_attribute(router_name, "Integrado", global_dict) router_ckt_id = get_attribute(router_name, "ckt_id", global_dict) router_color = get_attribute(router_name, "color", global_dict)['yEd'] router_label = fnc_router_metadata(global_dict, router_name, 'labelHtml', router_function, router_ckt_id, router_mode) router_system = get_attribute(router_name, "system", global_dict) G.add_node(router_name, label=router_name + "\n" + router_system, shape_fill=router_color) for link in links.itertuples(): edgeLabel = link.cableID G.add_edge(link.routerA, link.routerB, arrowhead='none', arrowfoot='none', label=edgeLabel) print(filename) G.write_graph(filename + '.graphml') sys.exit(4) elif router_mode in ['0']: G = pyyed.Graph() for router in routers: # Variables router_name = router[0][0] router_function = get_attribute(router_name, "HWfunction", global_dict) router_int = get_attribute(router_name, "Integrado", global_dict) router_ckt_id = get_attribute(router_name, "ckt_id", global_dict) router_color = get_attribute(router_name, "color", global_dict)['yEd'] router_label = fnc_router_metadata(global_dict, router_name, 'labelHtml', router_function, router_ckt_id, router_mode) router_system = get_attribute(router_name, "system", global_dict) grupo = G.add_group(router_name, label=router_name + "\n" + router_system, fill=router_color) for port in router: node_id = port[1] port_id = port[2]['label'] port_id = port_id.replace('<BR />', '\n') port_id = port_id.replace('<', '') port_id = port_id.replace('>', '') grupo.add_node(node_id, label=port_id) for e in edges: edgeA = e[0][0] edgeB = e[0][1] edgeLabel = e[1]['label'] G.add_edge(edgeA, edgeB, arrowhead='none', arrowfoot='none', label=edgeLabel) print(filename) G.write_graph(filename + '.graphml') sys.exit(4)
def test_graph_added_node_has_default_fill(): g = pyyed.Graph() g.add_node('N1') assert "#FF0000" == g.nodes['N1'].shape_fill
def __to_graphml(self): g = pyyed.Graph() colors = {} shapes = {} widths = {} kl = self.schema.levels_keys() self.logger.info(f"level keys {kl}") kr = self.schema.resources_keys() self.logger.info(f"resource keys {kr}") kc = [] for (k1, k2) in self.schema.connections_pairs(): k = self.schema.resource_definition(k1)['connect_id_name'] kc.append(k) self.logger.info(f"connect keys {kc}") for element in self.graph_style: self.logger.info(f"element {element}") if element['selector'] == 'node': for k in kl: widths[k] = '80' # element['css']['width'] for k in kr: widths[k] = '80' # element['css']['width'] for k in kc: widths[k] = '80' # element['css']['width'] elif element['selector'] == 'node.level': for k in kl: shapes[k] = element['css']['shape'] elif element['selector'] == 'node.resource': for k in kr: shapes[k] = element['css']['shape'] for k in kc: shapes[k] = element['css']['shape'] elif element['selector'].startswith('node.'): k = element['selector'].split('.')[1] colors[k] = element['css']['background-color'] self.logger.debug(f"colors {colors}") self.logger.debug(f"shapes {shapes}") self.logger.debug(f"widths {widths}") for node in self.graph_model.G.nodes(data=True): n = node[0] category = n.data['category'] if category not in colors.keys(): category = n.data['category_connect'] self.logger.info(f"node {n}") self.logger.info(f"node category {category}") try: g.add_node(n.data['id'], label=n.data['label'], shape=shapes[category], shape_fill=colors[category], font_size='8', font_style='bold', width=widths[category], height='30') except RuntimeWarning as exc: self.logger.warn(str(exc)) for n1, n2 in self.graph_model.G.edges(): self.logger.info(f"edge {n1}-{n2}") g.add_edge(n1.data["id"], n2.data["id"], width="1.0", color="#888888", arrowhead="standard", arrowfoot="none") return g
def test_graph_added_node_keeps_custom_fill(): g = pyyed.Graph() g.add_node('N1', shape_fill="#99CC00") assert "#99CC00" == g.nodes['N1'].shape_fill
entity_colour[k] = entity_colour[k].replace(",", "") entity_colour[k] = entity_colour[k].replace(" ", "") for k, v in entity_colour.items(): if v == "": entity_colour[k] = 'white' for k, v in entity_colour.items(): try: entity_colour[k] = matplotlib.colors.cnames[entity_colour[k]] except: entity_colour[k] = '#FFFFFF' #default background to white # Creating nodes target_name = os.path.splitext(target)[0] g = pyyed.Graph() for name, colour in entity_colour.items(): if name != 'Placeholder': g.add_node(name, label=name, label_alignment="center", shape="rectangle", shape_fill=colour) else: g.add_node(name, label="", label_alignment="center", shape="rectangle", shape_fill="#FFFFFF", transparent="true",
# License: BSD 3-clause ############################################################################## import csv from os.path import basename, splitext from argparse import ArgumentParser import pyyed parser = ArgumentParser() parser.add_argument('trust_file', help='trust file in .csv format (generate with PowerView)') args = parser.parse_args() if __name__ == '__main__': graph = pyyed.Graph() with open(args.trust_file, 'r', encoding='utf-8') as fd: reader = csv.reader(fd, delimiter=',') next(reader, None) for row in reader: # csv format: # "SourceName","TargetName","TrustType","TrustAttributes","TrustDirection","WhenCreated","WhenChanged" sourceName = row[0].strip().lower() targetName = row[1].strip().lower() trustType = row[2].strip() trustAttributes = row[3].strip() trustDirection = row[4].strip() # if the source and destination domains are the same, skip