users_loader.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import json
  2. import pathlib
  3. from .user import user
  4. from .user import user_factory
  5. from .user import user_builder
  6. from .users_collection import users_collection
  7. from .exception import config_exception
  8. class users_loader:
  9. """
  10. This is responsible for loading users from the config file. It create
  11. collection with all of the users from the file.
  12. """
  13. def __init__(self, target: pathlib.Path) -> None:
  14. """
  15. This create new loader. It get file as pathlib location, to open it
  16. and load. File must be a JSON, with array of the users object.
  17. Parameters:
  18. target (Path): Path to JSON file with users
  19. """
  20. self.__collection = users_collection()
  21. if not target.is_file():
  22. error = "Users config file \"" + str(target) + "\" not exists."
  23. raise config_exception(error)
  24. with target.open() as handler:
  25. self.__parse(json.loads(handler.read()))
  26. def __parse(self, target: list) -> None:
  27. """
  28. This parse array from JSON, as users.
  29. Parameters:
  30. target (list): List of users dicts from JSON
  31. """
  32. for count in target:
  33. self.collection.add(user_builder(count).result)
  34. @property
  35. def collection(self) -> users_collection:
  36. """ Collections with the users. """
  37. return self.__collection