Browse Source

refactor graph: add _add_parent_node()

Fabian Peter Hammerle 3 weeks ago
parent
commit
23454fa8b2
2 changed files with 15 additions and 18 deletions
  1. 1 1
      tests/persons/digraph.dot
  2. 14 17
      yamily/_graphviz.py

+ 1 - 1
tests/persons/digraph.dot

@@ -12,8 +12,8 @@ digraph yamily {
 		"alice-mother" [label="Mum Test" shape=box]
 		"alice-father" [label="alice-father" shape=box]
 		"relation-alice-father-alice-mother" [shape=point width=0]
-		"alice-mother" -> "relation-alice-father-alice-mother" [arrowhead=none constraint=False]
 		"alice-father" -> "relation-alice-father-alice-mother" [arrowhead=none constraint=False]
+		"alice-mother" -> "relation-alice-father-alice-mother" [arrowhead=none constraint=False]
 	}
 	{
 		rank=same

+ 14 - 17
yamily/_graphviz.py

@@ -29,6 +29,18 @@ def _add_person_node(graph: graphviz.dot.Digraph, person: Person) -> None:
     graph.node(person.identifier, label=label, shape="box")
 
 
+def _add_parent_node(graph: graphviz.dot.Digraph, parents: typing.Set[Person]) -> str:
+    parent_node_name = "relation-{}-{}".format(
+        parents[0].identifier, parents[1].identifier
+    )
+    graph.node(parent_node_name, shape="point", width="0")
+    for parent in parents:
+        graph.edge(
+            parent.identifier, parent_node_name, constraint="False", arrowhead="none",
+        )
+    return parent_node_name
+
+
 def digraph(collection: PersonCollection) -> graphviz.dot.Digraph:
     """
     >>> bob = Person('bob')
@@ -62,8 +74,8 @@ def digraph(collection: PersonCollection) -> graphviz.dot.Digraph:
     		carol [label=carol shape=box]
     		bob [label=bob shape=box]
     		"relation-bob-carol" [shape=point width=0]
-    		carol -> "relation-bob-carol" [arrowhead=none constraint=False]
     		bob -> "relation-bob-carol" [arrowhead=none constraint=False]
+    		carol -> "relation-bob-carol" [arrowhead=none constraint=False]
     	}
     	{
     		rank=same
@@ -99,22 +111,7 @@ def digraph(collection: PersonCollection) -> graphviz.dot.Digraph:
                 _add_person_node(subgraph, coparent)
                 nodes.add(coparent)
                 parents = tuple(sorted((person, coparent), key=lambda p: p.identifier))
-                parent_node_name = "relation-{}-{}".format(
-                    parents[0].identifier, parents[1].identifier
-                )
-                subgraph.node(parent_node_name, shape="point", width="0")
-                subgraph.edge(
-                    person.identifier,
-                    parent_node_name,
-                    constraint="False",
-                    arrowhead="none",
-                )
-                subgraph.edge(
-                    coparent.identifier,
-                    parent_node_name,
-                    constraint="False",
-                    arrowhead="none",
-                )
+                parent_node_name = _add_parent_node(subgraph, parents)
                 parent_node_names[parents] = parent_node_name
                 parent_node_names[tuple(parents[::-1])] = parent_node_name
     for child in collection: