container_blob.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import base64
  2. import asyncio
  3. class container_blob:
  4. def __init__(self, content: bytes | str) -> None:
  5. self.__content = content
  6. @property
  7. def content(self) -> bytes | str:
  8. return self.__content
  9. def result(self) -> bytes | str:
  10. return self.__content
  11. class decoded(container_blob):
  12. def __init__(self, content: bytes | str) -> None:
  13. if type(content) is str:
  14. content = content.encode("UTF-8")
  15. super().__init__(content)
  16. @staticmethod
  17. def __encode(content: bytes) -> container_blob:
  18. coded = base64.b64encode(content)
  19. coded = coded.decode("ascii")
  20. return encoded(coded)
  21. async def encode(self) -> container_blob:
  22. return await asyncio.to_thread(
  23. self.__class__.__encode,
  24. self.content
  25. )
  26. def __str__(self) -> str:
  27. return self.content.decode("UTF-8")
  28. def __bytes__(self) -> bytes:
  29. return self.content
  30. class encoded(container_blob):
  31. def __init__(self, content: str) -> None:
  32. super().__init__(content)
  33. @staticmethod
  34. def __decode(content: str) -> container_blob:
  35. content = content.encode("ascii")
  36. coded = base64.b64decode(content)
  37. return decoded(coded)
  38. async def decode(self) -> container_blob:
  39. return await asyncio.to_thread(
  40. self.__class__.__decode,
  41. self.content
  42. )
  43. def __str__(self) -> str:
  44. return self.content