| 230 | |
| 231 | |
| 232 | class GEXFWriter(GEXF): |
| 233 | # class for writing GEXF format files |
| 234 | # use write_gexf() function |
| 235 | def __init__( |
| 236 | self, graph=None, encoding="utf-8", prettyprint=True, version="1.2draft" |
| 237 | ): |
| 238 | self.construct_types() |
| 239 | self.prettyprint = prettyprint |
| 240 | self.encoding = encoding |
| 241 | self.set_version(version) |
| 242 | self.xml = Element( |
| 243 | "gexf", |
| 244 | { |
| 245 | "xmlns": self.NS_GEXF, |
| 246 | "xmlns:xsi": self.NS_XSI, |
| 247 | "xsi:schemaLocation": self.SCHEMALOCATION, |
| 248 | "version": self.VERSION, |
| 249 | }, |
| 250 | ) |
| 251 | |
| 252 | # Make meta element a non-graph element |
| 253 | # Also add lastmodifieddate as attribute, not tag |
| 254 | meta_element = Element("meta") |
| 255 | subelement_text = f"EasyGraph" |
| 256 | SubElement(meta_element, "creator").text = subelement_text |
| 257 | meta_element.set("lastmodifieddate", time.strftime("%Y-%m-%d")) |
| 258 | self.xml.append(meta_element) |
| 259 | |
| 260 | register_namespace("viz", self.NS_VIZ) |
| 261 | |
| 262 | # counters for edge and attribute identifiers |
| 263 | self.edge_id = itertools.count() |
| 264 | self.attr_id = itertools.count() |
| 265 | self.all_edge_ids = set() |
| 266 | # default attributes are stored in dictionaries |
| 267 | self.attr = {} |
| 268 | self.attr["node"] = {} |
| 269 | self.attr["edge"] = {} |
| 270 | self.attr["node"]["dynamic"] = {} |
| 271 | self.attr["node"]["static"] = {} |
| 272 | self.attr["edge"]["dynamic"] = {} |
| 273 | self.attr["edge"]["static"] = {} |
| 274 | |
| 275 | if graph is not None: |
| 276 | self.add_graph(graph) |
| 277 | |
| 278 | def __str__(self): |
| 279 | if self.prettyprint: |
| 280 | self.indent(self.xml) |
| 281 | s = tostring(self.xml).decode(self.encoding) |
| 282 | return s |
| 283 | |
| 284 | def add_graph(self, G): |
| 285 | # first pass through G collecting edge ids |
| 286 | for u, v, dd in G.edges: |
| 287 | eid = dd.get("id") |
| 288 | if eid is not None: |
| 289 | self.all_edge_ids.add(str(eid)) |
no outgoing calls
no test coverage detected