yaml.py 4.0 KB

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