Browse Source

simple yamily.graph.digraph prototype

Fabian Peter Hammerle 4 years ago
parent
commit
203b3dfad3
6 changed files with 47 additions and 4 deletions
  1. 1 1
      Pipfile
  2. 9 1
      Pipfile.lock
  3. 1 1
      README.md
  4. 1 0
      pytest.ini
  5. 1 1
      setup.py
  6. 34 0
      yamily/_graphviz.py

+ 1 - 1
Pipfile

@@ -10,7 +10,7 @@ black = "==19.10b0"
 pylint = "*"
 pytest = "*"
 pytest-cov = "*"
-yamily = {editable = true,extras = ["yaml"],path = "."}
+yamily = {editable = true,extras = ["graphviz", "yaml"],path = "."}
 
 [requires]
 python_version = "3"

+ 9 - 1
Pipfile.lock

@@ -1,7 +1,7 @@
 {
     "_meta": {
         "hash": {
-            "sha256": "99d13a62fe09ae53c64b23fa80e842de2a905fa22db42580154679ef9896173d"
+            "sha256": "6b1d5eb54a3568ee1b680def5ee94c6ff3b1e498da867a0c349fe5742b518dd8"
         },
         "pipfile-spec": 6,
         "requires": {
@@ -89,6 +89,13 @@
             ],
             "version": "==5.0.1"
         },
+        "graphviz": {
+            "hashes": [
+                "sha256:241fb099e32b8e8c2acca747211c8237e40c0b89f24b1622860075d59f4c4b25",
+                "sha256:60acbeee346e8c14555821eab57dbf68a169e6c10bce40e83c1bf44f63a62a01"
+            ],
+            "version": "==0.13.2"
+        },
         "importlib-metadata": {
             "hashes": [
                 "sha256:073a852570f92da5f744a3472af1b61e28e9f78ccf0c9117658dc32b15de7b45",
@@ -302,6 +309,7 @@
         "yamily": {
             "editable": true,
             "extras": [
+                "graphviz",
                 "yaml"
             ],
             "path": "."

+ 1 - 1
README.md

@@ -6,7 +6,7 @@ Define family trees in YAML
 
 ```sh
 $ sudo apt-get install python3-yaml # optional on debian
-$ pip3 install --user --upgrade yamily
+$ pip3 install --user --upgrade yamily[yaml,graphviz]
 ```
 
 ## Usage

+ 1 - 0
pytest.ini

@@ -1,2 +1,3 @@
 [pytest]
 addopts = --doctest-modules
+doctest_optionflags = NORMALIZE_WHITESPACE

+ 1 - 1
setup.py

@@ -26,7 +26,7 @@ setuptools.setup(
     ],
     entry_points={"console_scripts": ["yamily-list = yamily._cli:_list"]},
     install_requires=[],
-    extras_require={"yaml": ["PyYAML"],},
+    extras_require={"graphviz": ["graphviz"], "yaml": ["PyYAML"]},
     setup_requires=["setuptools_scm"],
     tests_require=["pytest"],
 )

+ 34 - 0
yamily/_graphviz.py

@@ -0,0 +1,34 @@
+import graphviz
+
+from yamily import Person, PersonCollection  # pylint: disable=unused-import; doctest
+
+
+def digraph(collection: PersonCollection) -> graphviz.dot.Digraph:
+    """
+    >>> alice = Person('alice')
+    >>> alice.name = 'Alice'
+    >>> alice.mother = Person('carol')
+    >>> alice.father = Person('bob')
+    >>> collection = PersonCollection()
+    >>> collection.add_person(alice)
+    Person(alice, Alice)
+    >>> graph = digraph(collection)
+    >>> print(graph.source)
+    digraph yamily {
+    	carol [label=unnamed]
+    	bob [label=unnamed]
+    	alice [label=Alice]
+    	carol -> alice
+    	bob -> alice
+    }
+    >>> graph.render('/tmp/yamily.gv')
+    '/tmp/yamily.gv.pdf'
+    """
+    graph = graphviz.Digraph("yamily")
+    for person in collection:
+        graph.node(person.identifier, str(person))
+    for child in collection:
+        for parent in [child.mother, child.father]:
+            if parent is not None:
+                graph.edge(parent.identifier, child.identifier)
+    return graph