_cli.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import argparse
  2. import pathlib
  3. import sys
  4. import typing
  5. import yaml
  6. import yamily
  7. import yamily._graphviz
  8. import yamily.yaml
  9. def _read(path: pathlib.Path) -> typing.Iterator[yamily.Person]:
  10. if path.is_dir():
  11. for file_path in path.glob("**/*.yml"):
  12. for person in _read(file_path):
  13. yield person
  14. else:
  15. with path.open("r") as yaml_file:
  16. yield yaml.load(yaml_file, Loader=yamily.yaml.Loader)
  17. def _list() -> None:
  18. argparser = argparse.ArgumentParser(
  19. description="Recursively find yamily family tree members and print as YAML list."
  20. )
  21. argparser.add_argument(
  22. "paths", nargs="+", metavar="path", help="path to yamily .yml file or folder"
  23. )
  24. args = argparser.parse_args()
  25. collection = yamily.PersonCollection()
  26. for path in args.paths:
  27. for person in _read(pathlib.Path(path)):
  28. collection.add_person(person)
  29. yaml.dump(
  30. sorted(collection, key=lambda p: p.identifier),
  31. sys.stdout,
  32. Dumper=yamily.yaml.Dumper,
  33. default_flow_style=False,
  34. allow_unicode=True,
  35. )
  36. def _dot() -> None:
  37. argparser = argparse.ArgumentParser(
  38. description="Create family tree in DOT (graphviz) format. "
  39. "Recursively looks for *.yml files containing family members in yamily format."
  40. )
  41. argparser.add_argument(
  42. "paths", nargs="+", metavar="path", help="path to yamily .yml file or folder"
  43. )
  44. args = argparser.parse_args()
  45. collection = yamily.PersonCollection()
  46. for path in args.paths:
  47. for person in _read(pathlib.Path(path)):
  48. collection.add_person(person)
  49. graph = yamily._graphviz.digraph(collection) # pylint: disable=protected-access
  50. print(graph.source)