| 703 | |
| 704 | |
| 705 | class GraphMLWriterLxml(GraphMLWriter): |
| 706 | def __init__( |
| 707 | self, |
| 708 | path, |
| 709 | graph=None, |
| 710 | encoding="utf-8", |
| 711 | prettyprint=True, |
| 712 | infer_numeric_types=False, |
| 713 | named_key_ids=False, |
| 714 | edge_id_from_attribute=None, |
| 715 | ): |
| 716 | self.construct_types() |
| 717 | import lxml.etree as lxmletree |
| 718 | |
| 719 | self.myElement = lxmletree.Element |
| 720 | |
| 721 | self._encoding = encoding |
| 722 | self._prettyprint = prettyprint |
| 723 | self.named_key_ids = named_key_ids |
| 724 | self.edge_id_from_attribute = edge_id_from_attribute |
| 725 | self.infer_numeric_types = infer_numeric_types |
| 726 | |
| 727 | self._xml_base = lxmletree.xmlfile(path, encoding=encoding) |
| 728 | self._xml = self._xml_base.__enter__() |
| 729 | self._xml.write_declaration() |
| 730 | |
| 731 | # We need to have a xml variable that support insertion. This call is |
| 732 | # used for adding the keys to the document. |
| 733 | # We will store those keys in a plain list, and then after the graph |
| 734 | # element is closed we will add them to the main graphml element. |
| 735 | self.xml = [] |
| 736 | self._keys = self.xml |
| 737 | self._graphml = self._xml.element( |
| 738 | "graphml", |
| 739 | { |
| 740 | "xmlns": self.NS_GRAPHML, |
| 741 | "xmlns:xsi": self.NS_XSI, |
| 742 | "xsi:schemaLocation": self.SCHEMALOCATION, |
| 743 | }, |
| 744 | ) |
| 745 | self._graphml.__enter__() |
| 746 | self.keys = {} |
| 747 | self.attribute_types = defaultdict(set) |
| 748 | |
| 749 | if graph is not None: |
| 750 | self.add_graph_element(graph) |
| 751 | |
| 752 | def add_graph_element(self, G): |
| 753 | """ |
| 754 | Serialize graph G in GraphML to the stream. |
| 755 | """ |
| 756 | if G.is_directed(): |
| 757 | default_edge_type = "directed" |
| 758 | else: |
| 759 | default_edge_type = "undirected" |
| 760 | |
| 761 | graphid = G.graph.pop("id", None) |
| 762 | if graphid is None: |
no outgoing calls
no test coverage detected