yaml.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 datetime # pylint: disable=unused-import; doctest
  18. import yaml
  19. from yamily import Person
  20. class Loader(yaml.SafeLoader):
  21. # pylint: disable=too-many-ancestors
  22. """
  23. >>> alice_yaml = '''
  24. ... !person
  25. ... identifier: alice
  26. ... name: Alice Test
  27. ... birth_date: 1876-02-01
  28. ... death_date: 1976-01-02
  29. ... mother: mum
  30. ... father: dad
  31. ... '''
  32. >>> alice = yaml.load(alice_yaml, Loader=Loader)
  33. >>> alice
  34. Person(alice, Alice Test, *1876-02-01, †1976-01-02)
  35. >>> alice.mother
  36. Person(mum)
  37. >>> alice.father
  38. Person(dad)
  39. >>> alice_yaml = '''
  40. ... !person
  41. ... identifier: alice
  42. ... name: Alice Test
  43. ... birth_date: 1976-02-01
  44. ... mother: !person
  45. ... identifier: mum
  46. ... father: !person
  47. ... identifier: dad
  48. ... name: Dad Test
  49. ... '''
  50. >>> alice = yaml.load(alice_yaml, Loader=Loader)
  51. >>> alice
  52. Person(alice, Alice Test, *1976-02-01)
  53. >>> alice.mother
  54. Person(mum)
  55. >>> alice.father
  56. Person(dad, Dad Test)
  57. """
  58. def __init__(self, stream):
  59. super().__init__(stream)
  60. self.add_constructor("!person", self._construct_person)
  61. @staticmethod
  62. def _construct_person(loader: "Loader", node: yaml.nodes.MappingNode) -> Person:
  63. (person_attrs,) = loader.construct_yaml_map(node)
  64. person = Person(person_attrs["identifier"])
  65. if "name" in person_attrs:
  66. person.name = person_attrs["name"]
  67. if "birth_date" in person_attrs:
  68. person.birth_date = person_attrs["birth_date"]
  69. if "death_date" in person_attrs:
  70. person.death_date = person_attrs["death_date"]
  71. if "mother" in person_attrs:
  72. if isinstance(person_attrs["mother"], Person):
  73. person.mother = person_attrs["mother"]
  74. else:
  75. person.mother = Person(person_attrs["mother"])
  76. if "father" in person_attrs:
  77. if isinstance(person_attrs["father"], Person):
  78. person.father = person_attrs["father"]
  79. else:
  80. person.father = Person(person_attrs["father"])
  81. return person
  82. class Dumper(yaml.SafeDumper):
  83. """
  84. >>> p = Person('alice')
  85. >>> p.name = 'Alice'
  86. >>> p.birth_date = datetime.date(1976, 2, 1)
  87. >>> p.death_date = datetime.date(2043, 1, 17)
  88. >>> print(yaml.dump(p, Dumper=Dumper))
  89. !person
  90. birth_date: 1976-02-01
  91. death_date: 2043-01-17
  92. identifier: alice
  93. name: Alice
  94. <BLANKLINE>
  95. >>> p = Person('bob')
  96. >>> p.mother = Person('bob-mum')
  97. >>> p.father = Person('bob-father')
  98. >>> print(yaml.dump(p, Dumper=Dumper))
  99. !person
  100. father: !person
  101. identifier: bob-father
  102. identifier: bob
  103. mother: !person
  104. identifier: bob-mum
  105. <BLANKLINE>
  106. """
  107. # pylint: disable=too-many-ancestors
  108. def __init__(self, stream, **kwargs):
  109. super().__init__(stream, **kwargs)
  110. self.add_representer(Person, self._represent_person)
  111. @staticmethod
  112. def _represent_person(dumper: "_Dumper", person: Person) -> yaml.nodes.MappingNode:
  113. person_attrs = {"identifier": person.identifier}
  114. if person.name is not None:
  115. person_attrs["name"] = person.name
  116. if person.birth_date is not None:
  117. person_attrs["birth_date"] = person.birth_date
  118. if person.death_date is not None:
  119. person_attrs["death_date"] = person.death_date
  120. if person.mother is not None:
  121. person_attrs["mother"] = person.mother
  122. if person.father is not None:
  123. person_attrs["father"] = person.father
  124. return dumper.represent_mapping("!person", person_attrs)