package_import.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import json
  2. from .decode import decode
  3. from .package import package
  4. from .command import command
  5. from .command import encoded_command
  6. from .file import file
  7. from .file import encoded_file
  8. from .package import package
  9. from .package import package_builder
  10. from .exceptions import broken_package
  11. class package_import(decode):
  12. def __init__(self, content: str) -> None:
  13. self.__content = content
  14. @property
  15. def __json_string(self) -> str:
  16. return self._decode_str(self.__content)
  17. @property
  18. def __flat_content(self) -> dict:
  19. return json.loads(self.__json_string)
  20. def __process_commands(self, commands: list) -> list:
  21. result = list()
  22. for count in commands:
  23. result.push(encoded_command(count).decode())
  24. return result
  25. def __process_files(self, files: list) -> list:
  26. result = list()
  27. for count in files:
  28. encoded_path = count["path"]
  29. encoded_content = count["content"]
  30. encoded_count = encoded_file(encoded_path, encoded_content)
  31. result.push(encoded_count.decode())
  32. return result
  33. def unpack(self) -> package:
  34. try:
  35. content = self.__flat_content
  36. commands = self.__process_commands(content["commands"])
  37. files = self.__process_files(content["files"])
  38. builder = package_builder()
  39. for count in commands:
  40. builder.add_command(count)
  41. for count in files:
  42. builder.add_file(count)
  43. return builder.pack()
  44. except Exception as error:
  45. raise broken_package(error)