_cli.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import argparse
  2. import pathlib
  3. import sys
  4. import typing
  5. import yaml
  6. import yamily
  7. import yamily.yaml
  8. def _read(path: pathlib.Path) -> typing.Iterator[yamily.Person]:
  9. if path.is_dir():
  10. for file_path in path.glob("**/*.yml"):
  11. for person in _read(file_path):
  12. yield person
  13. else:
  14. with path.open("r") as yaml_file:
  15. yield yaml.load(yaml_file, Loader=yamily.yaml.Loader)
  16. def _list() -> None:
  17. argparser = argparse.ArgumentParser(
  18. description="Recursively find yamily family tree members and print as YAML list."
  19. )
  20. argparser.add_argument(
  21. "paths", nargs="+", metavar="path", help="path to yamily .yml file or folder"
  22. )
  23. args = argparser.parse_args()
  24. collection = yamily.PersonCollection()
  25. for path in args.paths:
  26. for person in _read(pathlib.Path(path)):
  27. collection.add_person(person)
  28. yaml.dump(
  29. sorted(collection, key=lambda p: p.identifier),
  30. sys.stdout,
  31. Dumper=yamily.yaml.Dumper,
  32. default_flow_style=False,
  33. allow_unicode=True,
  34. )