_graphviz.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # yamify - define family trees in YAML
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import typing
  18. import graphviz
  19. from yamily import Person, PersonCollection
  20. def _add_person_node(graph: graphviz.dot.Digraph, person: Person) -> None:
  21. label = person.name or person.identifier
  22. if person.birth_date is not None:
  23. label += r"\n*{}".format(person.birth_date.isoformat())
  24. graph.node(person.identifier, label=label, shape="box")
  25. def _add_parent_node(graph: graphviz.dot.Digraph, parents: typing.Set[Person]) -> str:
  26. parent_node_name = "relation-{}-{}".format(
  27. parents[0].identifier, parents[1].identifier
  28. )
  29. graph.node(parent_node_name, shape="point", width="0")
  30. for parent in parents:
  31. graph.edge(
  32. parent.identifier, parent_node_name, constraint="False", arrowhead="none",
  33. )
  34. return parent_node_name
  35. def digraph(collection: PersonCollection) -> graphviz.dot.Digraph:
  36. """
  37. >>> bob = Person('bob')
  38. >>> bob.father = Person('frank')
  39. >>> carol = Person('carol')
  40. >>> carol.mother = Person('grace')
  41. >>> alice = Person('alice')
  42. >>> alice.mother = carol
  43. >>> alice.father = bob
  44. >>> david = Person('david')
  45. >>> david.mother = carol
  46. >>> david.father = bob
  47. >>> collection = PersonCollection()
  48. >>> collection.add_person(alice)
  49. Person(alice)
  50. >>> collection.add_person(david)
  51. Person(david)
  52. >>> graph = digraph(collection)
  53. >>> print(graph.source)
  54. digraph yamily {
  55. subgraph cluster_grace {
  56. rank=same style=invisible
  57. grace [label=grace shape=box]
  58. }
  59. subgraph cluster_carol {
  60. rank=same style=invisible
  61. carol [label=carol shape=box]
  62. "relation-bob-carol" [shape=point width=0]
  63. bob -> "relation-bob-carol" [arrowhead=none constraint=False]
  64. carol -> "relation-bob-carol" [arrowhead=none constraint=False]
  65. bob [label=bob shape=box]
  66. }
  67. subgraph cluster_frank {
  68. rank=same style=invisible
  69. frank [label=frank shape=box]
  70. }
  71. subgraph cluster_alice {
  72. rank=same style=invisible
  73. alice [label=alice shape=box]
  74. }
  75. subgraph cluster_david {
  76. rank=same style=invisible
  77. david [label=david shape=box]
  78. }
  79. grace -> carol
  80. frank -> bob
  81. "relation-bob-carol" -> alice
  82. "relation-bob-carol" -> david
  83. }
  84. >>> graph.render('/tmp/yamily.gv')
  85. '/tmp/yamily.gv.pdf'
  86. """
  87. graph = graphviz.Digraph("yamily")
  88. nodes: typing.Set[Person] = set()
  89. parent_node_names: typing.Dict[typing.Tuple[Person, Person], str] = dict()
  90. for person in collection:
  91. if person in nodes:
  92. continue
  93. # https://graphviz.gitlab.io/_pages/Gallery/directed/cluster.html
  94. with graph.subgraph(name="cluster_" + person.identifier) as subgraph:
  95. subgraph.attr(rank="same", style="invisible")
  96. _add_person_node(subgraph, person)
  97. nodes.add(person)
  98. for coparent in collection.get_coparents(person):
  99. parents = tuple(sorted((person, coparent), key=lambda p: p.identifier))
  100. parent_node_name = _add_parent_node(subgraph, parents)
  101. parent_node_names[parents] = parent_node_name
  102. parent_node_names[tuple(parents[::-1])] = parent_node_name
  103. _add_person_node(subgraph, coparent)
  104. nodes.add(coparent)
  105. for child in collection:
  106. if child.mother is not None and child.father is not None:
  107. parents = sorted(child.parents, key=lambda p: p.identifier)
  108. parent_node_name = parent_node_names[tuple(parents)]
  109. graph.edge(parent_node_name, child.identifier)
  110. elif child.mother is not None:
  111. graph.edge(child.mother.identifier, child.identifier)
  112. elif child.father is not None:
  113. graph.edge(child.father.identifier, child.identifier)
  114. return graph