Browse Source

added cli entrypoint `yamily-dot`

Fabian Peter Hammerle 4 years ago
parent
commit
bd920b7751
4 changed files with 69 additions and 1 deletions
  1. 6 1
      setup.py
  2. 12 0
      tests/cli/_dot_test.py
  3. 33 0
      tests/persons/digraph.gz
  4. 18 0
      yamily/_cli.py

+ 6 - 1
setup.py

@@ -24,7 +24,12 @@ setuptools.setup(
         "Topic :: Sociology :: Genealogy",
         "Topic :: Utilities",
     ],
-    entry_points={"console_scripts": ["yamily-list = yamily._cli:_list"]},
+    entry_points={
+        "console_scripts": [
+            "yamily-dot = yamily._cli:_dot",
+            "yamily-list = yamily._cli:_list",
+        ]
+    },
     install_requires=[],
     extras_require={"graphviz": ["graphviz"], "yaml": ["PyYAML"]},
     setup_requires=["setuptools_scm"],

+ 12 - 0
tests/cli/_dot_test.py

@@ -0,0 +1,12 @@
+import pathlib
+from unittest.mock import patch
+
+from yamily._cli import _dot
+
+
+@patch("sys.argv", ["", "persons"])
+def test__dot_recurse_dir(capsys):
+    _dot()
+    out, err = capsys.readouterr()
+    assert not err
+    assert out == pathlib.Path("persons").joinpath("digraph.gz").read_text()

+ 33 - 0
tests/persons/digraph.gz

@@ -0,0 +1,33 @@
+digraph yamily {
+	{
+		rank=same
+		"erika-mustermann" [shape=box]
+		"thomas-mustermann" [shape=box]
+		"relation-erika-mustermann-thomas-mustermann" [shape=point width=0]
+		"erika-mustermann" -> "relation-erika-mustermann-thomas-mustermann" [arrowhead=none constraint=False]
+		"thomas-mustermann" -> "relation-erika-mustermann-thomas-mustermann" [arrowhead=none constraint=False]
+	}
+	{
+		rank=same
+		"alice-mother" [shape=box]
+		"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]
+	}
+	{
+		rank=same
+		"alice-grandmother" [shape=box]
+	}
+	{
+		rank=same
+		alice [shape=box]
+	}
+	{
+		rank=same
+		"max-mustermann" [shape=box]
+	}
+	"alice-grandmother" -> "alice-father"
+	"relation-alice-father-alice-mother" -> alice
+	"relation-erika-mustermann-thomas-mustermann" -> "max-mustermann"
+}

+ 18 - 0
yamily/_cli.py

@@ -6,6 +6,7 @@ import typing
 import yaml
 
 import yamily
+import yamily._graphviz
 import yamily.yaml
 
 
@@ -38,3 +39,20 @@ def _list() -> None:
         default_flow_style=False,
         allow_unicode=True,
     )
+
+
+def _dot() -> None:
+    argparser = argparse.ArgumentParser(
+        description="Create family tree in DOT (graphviz) format. "
+        "Recursively looks for *.yml files containing family members in yamily format."
+    )
+    argparser.add_argument(
+        "paths", nargs="+", metavar="path", help="path to yamily .yml file or folder"
+    )
+    args = argparser.parse_args()
+    collection = yamily.PersonCollection()
+    for path in args.paths:
+        for person in _read(pathlib.Path(path)):
+            collection.add_person(person)
+    graph = yamily._graphviz.digraph(collection)  # pylint: disable=protected-access
+    print(graph.source)