|
@@ -1,13 +1,11 @@
|
|
|
import datetime
|
|
|
-import pathlib
|
|
|
-import typing
|
|
|
|
|
|
import yaml
|
|
|
|
|
|
from yamily import Person
|
|
|
|
|
|
|
|
|
-class _Loader(yaml.SafeLoader):
|
|
|
+class Loader(yaml.SafeLoader):
|
|
|
|
|
|
|
|
|
|
|
@@ -20,7 +18,7 @@ class _Loader(yaml.SafeLoader):
|
|
|
... mother: mum
|
|
|
... father: dad
|
|
|
... '''
|
|
|
- >>> alice = yaml.load(alice_yaml, Loader=_Loader)
|
|
|
+ >>> alice = yaml.load(alice_yaml, Loader=Loader)
|
|
|
>>> alice
|
|
|
Person(alice, Alice Test, *1976-02-01)
|
|
|
>>> alice.mother
|
|
@@ -39,7 +37,7 @@ class _Loader(yaml.SafeLoader):
|
|
|
... identifier: dad
|
|
|
... name: Dad Test
|
|
|
... '''
|
|
|
- >>> alice = yaml.load(alice_yaml, Loader=_Loader)
|
|
|
+ >>> alice = yaml.load(alice_yaml, Loader=Loader)
|
|
|
>>> alice
|
|
|
Person(alice, Alice Test, *1976-02-01)
|
|
|
>>> alice.mother
|
|
@@ -53,7 +51,7 @@ class _Loader(yaml.SafeLoader):
|
|
|
self.add_constructor("!person", self._construct_person)
|
|
|
|
|
|
@staticmethod
|
|
|
- def _construct_person(loader: "_Loader", node: yaml.nodes.MappingNode) -> Person:
|
|
|
+ def _construct_person(loader: "Loader", node: yaml.nodes.MappingNode) -> Person:
|
|
|
(person_attrs,) = loader.construct_yaml_map(node)
|
|
|
person = Person(person_attrs["identifier"])
|
|
|
if "name" in person_attrs:
|
|
@@ -117,21 +115,3 @@ class Dumper(yaml.SafeDumper):
|
|
|
if person.father is not None:
|
|
|
person_attrs["father"] = person.father
|
|
|
return dumper.represent_mapping("!person", person_attrs)
|
|
|
-
|
|
|
-
|
|
|
-def read(yaml_path: typing.Union[str, pathlib.Path]) -> Person:
|
|
|
- """
|
|
|
- >>> read('persons/erika-mustermann.yml')
|
|
|
- Person(erika-mustermann, Erika Mustermann, *1957-08-12)
|
|
|
- >>> maxl = read('persons/max-mustermann.yml')
|
|
|
- >>> maxl
|
|
|
- Person(max-mustermann, Max Mustermann, *1976-02-01)
|
|
|
- >>> maxl.mother
|
|
|
- Person(erika-mustermann)
|
|
|
- >>> maxl.father
|
|
|
- Person(thomas-mustermann)
|
|
|
- """
|
|
|
- if isinstance(yaml_path, str):
|
|
|
- return read(pathlib.Path(yaml_path))
|
|
|
- with yaml_path.open("r") as yaml_file:
|
|
|
- return yaml.load(yaml_file, Loader=_Loader)
|