_graphviz.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. >>> collection = PersonCollection()
  45. >>> collection.add_person(alice)
  46. Person(alice)
  47. >>> graph = digraph(collection)
  48. >>> graph.render("/tmp/tree.dot")
  49. '/tmp/tree.dot.pdf'
  50. """
  51. graph = graphviz.Digraph("yamily")
  52. nodes: typing.Set[Person] = set()
  53. parent_node_names: typing.Dict[typing.Tuple[Person, Person], str] = dict()
  54. for person in collection:
  55. if person in nodes:
  56. continue
  57. # https://graphviz.gitlab.io/_pages/Gallery/directed/cluster.html
  58. with graph.subgraph(name="cluster_" + person.identifier) as subgraph:
  59. subgraph.attr(rank="same", style="invisible")
  60. _add_person_node(subgraph, person)
  61. nodes.add(person)
  62. for coparent in collection.get_coparents(person):
  63. parents = tuple(sorted((person, coparent), key=lambda p: p.identifier))
  64. parent_node_name = _add_parent_node(subgraph, parents)
  65. parent_node_names[parents] = parent_node_name
  66. parent_node_names[tuple(parents[::-1])] = parent_node_name
  67. _add_person_node(subgraph, coparent)
  68. nodes.add(coparent)
  69. for child in collection:
  70. if child.mother is not None and child.father is not None:
  71. parents = sorted(child.parents, key=lambda p: p.identifier)
  72. parent_node_name = parent_node_names[tuple(parents)]
  73. graph.edge(parent_node_name, child.identifier)
  74. elif child.mother is not None:
  75. graph.edge(child.mother.identifier, child.identifier)
  76. elif child.father is not None:
  77. graph.edge(child.father.identifier, child.identifier)
  78. return graph