Browse Source

yaml loader: support inline !person

Fabian Peter Hammerle 4 years ago
parent
commit
e5623417a6
3 changed files with 74 additions and 6 deletions
  1. 18 4
      tests/cli/_list_test.py
  2. 11 0
      tests/persons/inline-parents.yml
  3. 45 2
      yamily/yaml.py

+ 18 - 4
tests/cli/_list_test.py

@@ -71,16 +71,30 @@ def test__list_recurse_dir(capsys):
     out, err = capsys.readouterr()
     assert not err
     assert out == (
-        "- &id001 !person\n"
+        "- !person\n"
+        "  father: &id001 !person\n"
+        "    identifier: alice-father\n"
+        "    mother: &id002 !person\n"
+        "      identifier: alice-grandmother\n"
+        "      name: Grandma Test\n"
+        "  identifier: alice\n"
+        "  mother: &id003 !person\n"
+        "    identifier: alice-mother\n"
+        "    name: Mum Test\n"
+        "  name: Alice Test\n"
+        "- *id001\n"
+        "- *id002\n"
+        "- *id003\n"
+        "- &id004 !person\n"
         "  birth_date: 1957-08-12\n"
         "  identifier: erika-mustermann\n"
         "  name: Erika Mustermann\n"
         "- !person\n"
         "  birth_date: 1976-02-01\n"
-        "  father: &id002 !person\n"
+        "  father: &id005 !person\n"
         "    identifier: thomas-mustermann\n"
         "  identifier: max-mustermann\n"
-        "  mother: *id001\n"
+        "  mother: *id004\n"
         "  name: Max Mustermann\n"
-        "- *id002\n"
+        "- *id005\n"
     )

+ 11 - 0
tests/persons/inline-parents.yml

@@ -0,0 +1,11 @@
+!person
+identifier: alice
+name: Alice Test
+mother: !person
+  identifier: alice-mother
+  name: Mum Test
+father: !person
+  identifier: alice-father
+  mother: !person
+    identifier: alice-grandmother
+    name: Grandma Test

+ 45 - 2
yamily/yaml.py

@@ -11,6 +11,43 @@ class _Loader(yaml.SafeLoader):
 
     # pylint: disable=too-many-ancestors
 
+    """
+    >>> alice_yaml = '''
+    ... !person
+    ... identifier: alice
+    ... name: Alice Test
+    ... birth_date: 1976-02-01
+    ... mother: mum
+    ... father: dad
+    ... '''
+    >>> alice = yaml.load(alice_yaml, Loader=_Loader)
+    >>> alice
+    Person(alice, Alice Test, *1976-02-01)
+    >>> alice.mother
+    Person(mum)
+    >>> alice.father
+    Person(dad)
+
+    >>> alice_yaml = '''
+    ... !person
+    ... identifier: alice
+    ... name: Alice Test
+    ... birth_date: 1976-02-01
+    ... mother: !person
+    ...   identifier: mum
+    ... father: !person
+    ...   identifier: dad
+    ...   name: Dad Test
+    ... '''
+    >>> alice = yaml.load(alice_yaml, Loader=_Loader)
+    >>> alice
+    Person(alice, Alice Test, *1976-02-01)
+    >>> alice.mother
+    Person(mum)
+    >>> alice.father
+    Person(dad, Dad Test)
+    """
+
     def __init__(self, stream):
         super().__init__(stream)
         self.add_constructor("!person", self._construct_person)
@@ -24,9 +61,15 @@ class _Loader(yaml.SafeLoader):
         if "birth_date" in person_attrs:
             person.birth_date = person_attrs["birth_date"]
         if "mother" in person_attrs:
-            person.mother = Person(person_attrs["mother"])
+            if isinstance(person_attrs["mother"], Person):
+                person.mother = person_attrs["mother"]
+            else:
+                person.mother = Person(person_attrs["mother"])
         if "father" in person_attrs:
-            person.father = Person(person_attrs["father"])
+            if isinstance(person_attrs["father"], Person):
+                person.father = person_attrs["father"]
+            else:
+                person.father = Person(person_attrs["father"])
         return person