diff --git a/pygraphml/__init__.py b/pygraphml/__init__.py index 95b5cce..ec29d50 100644 --- a/pygraphml/__init__.py +++ b/pygraphml/__init__.py @@ -5,7 +5,7 @@ from __future__ import absolute_import from __future__ import print_function -from .attribute import Attribute +from .attribute import Attribute, EdgeAttribute, NodeAttribute from .item import Item from .point import Point from .node import Node diff --git a/pygraphml/attribute.py b/pygraphml/attribute.py index ae4bec9..d6c6c9b 100644 --- a/pygraphml/attribute.py +++ b/pygraphml/attribute.py @@ -10,7 +10,7 @@ class Attribute: """ """ - def __init__(self, name, value, type = "string"): + def __init__(self, name, value, type="string"): """ """ @@ -26,3 +26,27 @@ def __str__(self): s += "%s : %s" % (self.name, str(self.value)) return s + def for_str(self): + return NotImplemented + + +class EdgeAttribute(Attribute): + """ + """ + + def __init__(self, name, value, type, forStr): + Attribute.__init__(self, name, value, type) + + def for_str(self): + return "edge" + + +class NodeAttribute(Attribute): + """ + """ + + def __init__(self, name, value, type, forStr): + Attribute.__init__(self, name, value, type) + + def for_str(self): + return "node" diff --git a/pygraphml/edge.py b/pygraphml/edge.py index b8f4f8f..0ce714e 100644 --- a/pygraphml/edge.py +++ b/pygraphml/edge.py @@ -7,12 +7,14 @@ from . import Item +from . import EdgeAttribute + class Edge(Item): """ """ - def __init__(self, node1, node2, directed = False): + def __init__(self, node1, node2, directed=False): """ """ @@ -50,7 +52,6 @@ def child(self): return self.node2 - def directed(self): """ """ @@ -63,3 +64,7 @@ def set_directed(self, dir): self._directed = dir + def set_attribute(self, name, value): + """ + """ + self.attr[name] = EdgeAttribute(name, value, type) diff --git a/pygraphml/graph.py b/pygraphml/graph.py index d900be0..f321903 100644 --- a/pygraphml/graph.py +++ b/pygraphml/graph.py @@ -23,8 +23,8 @@ def __init__(self, name=""): self.name = name - self._nodes = [] - self._edges = [] + self._nodes = set() + self._edges = set() self._root = None self.directed = True @@ -124,7 +124,7 @@ def add_node(self, label="", id=None): n = Node(id) n['label'] = label - self._nodes.append(n) + self._nodes.add(n) return n @@ -133,12 +133,14 @@ def add_edge(self, n1, n2, directed=False): """ if n1 not in self._nodes: - raise Test("fff") + print(n1) + raise Test("n1 has not been added") if n2 not in self._nodes: - raise Test("fff") + print(n2) + raise Test("n2 has not been added") e = Edge(n1, n2, directed) - self._edges.append(e) + self._edges.add(e) return e @@ -192,7 +194,7 @@ def set_root_by_attribute(self, value, attribute='label'): self.set_root(n) return n - def get_attributs(self): + def get_attributes(self): """ """ diff --git a/pygraphml/graphml_parser b/pygraphml/graphml_parser deleted file mode 100644 index f30d723..0000000 --- a/pygraphml/graphml_parser +++ /dev/null @@ -1,149 +0,0 @@ -# -*- coding: utf-8 -*- - -from __future__ import unicode_literals -from __future__ import division -from __future__ import absolute_import -from __future__ import print_function - - -from xml.dom import minidom - -from . import Graph -from . import Node -from . import Edge - -class GraphMLParser: - """ - """ - - def __init__(self): - """ - """ - - def write(self, graph, fname): - """ - """ - - doc = minidom.Document() - - root = doc.createElement('graphml') - doc.appendChild(root) - - # Add attributs - for a in graph.get_attributs(): - attr_node = doc.createElement('key') - attr_node.setAttribute('id', a.name) - attr_node.setAttribute('attr.name', a.name) - attr_node.setAttribute('attr.type', a.type) - root.appendChild(attr_node) - - graph_node = doc.createElement('graph') - graph_node.setAttribute('id', graph.name) - if graph.directed: - graph_node.setAttribute('edgedefault', 'directed') - else: - graph_node.setAttribute('edgedefault', 'undirected') - root.appendChild(graph_node) - - # Add nodes - for n in graph.nodes(): - - node = doc.createElement('node') - node.setAttribute('id', n['label']) - for a in n.attributes(): - if a != 'label': - data = doc.createElement('data') - data.setAttribute('key', a) - data.appendChild(doc.createTextNode(str(n[a]))) - node.appendChild(data) - graph_node.appendChild(node) - - # Add edges - for e in graph.edges(): - - edge = doc.createElement('edge') - edge.setAttribute('source', e.node1['label']) - edge.setAttribute('target', e.node2['label']) - if e.directed() != graph.directed: - edge.setAttribute('directed', 'true' if e.directed() else 'false') - for a in e.attributes(): - if e != 'label': - data = doc.createElement('data') - data.setAttribute('key', a) - data.appendChild(doc.createTextNode(e[a])) - edge.appendChild(data) - graph_node.appendChild(edge) - - f = open(fname, 'w') - f.write(doc.toprettyxml(indent = ' ')) - - @staticmethod - def parse_dom(dom): - """Parse dom into a Graph - - :param dom: dom as returned by minidom.parse or minidom.parseString - :return: A Graph representation - """ - root = dom.getElementsByTagName("graphml")[0] - graph = root.getElementsByTagName("graph")[0] - name = graph.getAttribute('id') - - g = Graph(name) - - # # Get attributes - # attributes = [] - # for attr in root.getElementsByTagName("key"): - # attributes.append(attr) - - # Get nodes - for node in graph.getElementsByTagName("node"): - n = g.add_node(id=node.getAttribute('id')) - - for attr in node.getElementsByTagName("data"): - if attr.firstChild: - n[attr.getAttribute("key")] = attr.firstChild.data - else: - n[attr.getAttribute("key")] = "" - - # Get edges - for edge in graph.getElementsByTagName("edge"): - source = edge.getAttribute('source') - dest = edge.getAttribute('target') - - # source/target attributes refer to IDs: http://graphml.graphdrawing.org/xmlns/1.1/graphml-structure.xsd - e = g.add_edge_by_id(source, dest) - - for attr in edge.getElementsByTagName("data"): - if attr.firstChild: - e[attr.getAttribute("key")] = attr.firstChild.data - else: - e[attr.getAttribute("key")] = "" - - return g - - def parse(self, fname): - """Parse a file into a Graph - - :param fname: Filename - :return: Graph - """ - with open(fname, 'r') as f: - dom = minidom.parse(f) - return self.parse_dom(dom) - - def parse_string(self, string): - """Parse a string into a Graph - - :param string: String that is to be passed into Grapg - :return: Graph - """ - dom = minidom.parseString(string) - return self.parse_dom(dom) - - -if __name__ == '__main__': - - parser = GraphMLParser() - g = parser.parse('test.graphml') - - g.show(True) diff --git a/pygraphml/graphml_parser.py b/pygraphml/graphml_parser.py index 91f3d8b..efb8d24 100644 --- a/pygraphml/graphml_parser.py +++ b/pygraphml/graphml_parser.py @@ -12,6 +12,7 @@ from . import Node from . import Edge + class GraphMLParser: """ """ @@ -30,11 +31,12 @@ def write(self, graph, fname=None): doc.appendChild(root) # Add attributs - for a in graph.get_attributs(): + for a in graph.get_attributes(): attr_node = doc.createElement('key') attr_node.setAttribute('id', a.name) attr_node.setAttribute('attr.name', a.name) attr_node.setAttribute('attr.type', a.type) + attr_node.setAttribute('for', a.for_str()) root.appendChild(attr_node) graph_node = doc.createElement('graph') @@ -65,7 +67,8 @@ def write(self, graph, fname=None): edge.setAttribute('source', e.node1['label']) edge.setAttribute('target', e.node2['label']) if e.directed() != graph.directed: - edge.setAttribute('directed', 'true' if e.directed() else 'false') + edge.setAttribute( + 'directed', 'true' if e.directed() else 'false') for a in e.attributes(): if e != 'label': data = doc.createElement('data') @@ -85,7 +88,7 @@ def parse(self, fname): """ g = None - with open( fname, 'r' ) as f: + with open(fname, 'r') as f: dom = minidom.parse(f) root = dom.getElementsByTagName("graphml")[0] graph = root.getElementsByTagName("graph")[0] @@ -131,5 +134,3 @@ def parse(self, fname): g = parser.parse('test.graphml') g.show(True) - - diff --git a/pygraphml/item.py b/pygraphml/item.py index 387fcd1..7046031 100644 --- a/pygraphml/item.py +++ b/pygraphml/item.py @@ -8,6 +8,7 @@ from . import Attribute + class Item(object): """ """ @@ -37,17 +38,16 @@ def __str__(self): return s - def __setitem__(self, name, value): + def set_attribute(self, name, value): """ """ + return NotImplemented - self.attr[name] = Attribute(name, value) - - def __getitem__(self, name): + def set_attribute(self, name, value, type): """ """ - return self.attr[name].value + self.attr[name] = Attribute(name, value, type) def attributes(self): """ diff --git a/pygraphml/node.py b/pygraphml/node.py index 8f5ce02..776d2ab 100644 --- a/pygraphml/node.py +++ b/pygraphml/node.py @@ -7,6 +7,8 @@ from . import Item +from . import NodeAttribute + class Node(Item): """ @@ -26,7 +28,6 @@ def edges(self, ): return self._edges - def children(self): """ """ @@ -49,3 +50,7 @@ def parent(self): return parent + def set_attribute(self, name, value, type): + """ + """ + self.attr[name] = NodeAttribute(name, value, type)