(self, graph_xml, graphml_keys, defaults, G=None)
| 864 | yield self.make_graph(g, keys, defaults) |
| 865 | |
| 866 | def make_graph(self, graph_xml, graphml_keys, defaults, G=None): |
| 867 | # set default graph type |
| 868 | edgedefault = graph_xml.get("edgedefault", None) |
| 869 | if G is None: |
| 870 | if edgedefault == "directed": |
| 871 | G = eg.MultiDiGraph() |
| 872 | else: |
| 873 | G = eg.MultiGraph() |
| 874 | # set defaults for graph attributes |
| 875 | G.graph["node_default"] = {} |
| 876 | G.graph["edge_default"] = {} |
| 877 | for key_id, value in defaults.items(): |
| 878 | key_for = graphml_keys[key_id]["for"] |
| 879 | name = graphml_keys[key_id]["name"] |
| 880 | python_type = graphml_keys[key_id]["type"] |
| 881 | if key_for == "node": |
| 882 | G.graph["node_default"].update({name: python_type(value)}) |
| 883 | if key_for == "edge": |
| 884 | G.graph["edge_default"].update({name: python_type(value)}) |
| 885 | # hyperedges are not supported |
| 886 | hyperedge = graph_xml.find(f"{{{self.NS_GRAPHML}}}hyperedge") |
| 887 | if hyperedge is not None: |
| 888 | raise eg.EasyGraphError("GraphML reader doesn't support hyperedges") |
| 889 | # add nodes |
| 890 | for node_xml in graph_xml.findall(f"{{{self.NS_GRAPHML}}}node"): |
| 891 | self.add_node(G, node_xml, graphml_keys, defaults) |
| 892 | # add edges |
| 893 | for edge_xml in graph_xml.findall(f"{{{self.NS_GRAPHML}}}edge"): |
| 894 | self.add_edge(G, edge_xml, graphml_keys) |
| 895 | # add graph data |
| 896 | data = self.decode_data_elements(graphml_keys, graph_xml) |
| 897 | G.graph.update(data) |
| 898 | |
| 899 | # switch to Graph or DiGraph if no parallel edges were found |
| 900 | if self.multigraph: |
| 901 | return G |
| 902 | |
| 903 | G = eg.DiGraph(G) if G.is_directed() else eg.Graph(G) |
| 904 | # add explicit edge "id" from file as attribute in eg graph. |
| 905 | eg.set_edge_attributes(G, values=self.edge_ids, name="id") |
| 906 | return G |
| 907 | |
| 908 | def add_node(self, G, node_xml, graphml_keys, defaults): |
| 909 | """Add a node to the graph.""" |
no test coverage detected