Coverage for src/blob_dict/blob/__init__.py: 0%

30 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-03-27 22:31 -0700

1from __future__ import annotations 

2 

3from base64 import b64decode, b64encode 

4from typing import override 

5 

6 

7class BytesBlob: 

8 def __init__(self, blob: bytes) -> None: 

9 super().__init__() 

10 

11 self._blob_bytes: bytes = blob 

12 

13 def as_blob(self, blob_class: type[BytesBlob]) -> BytesBlob: 

14 return blob_class(self._blob_bytes) 

15 

16 def as_bytes(self) -> bytes: 

17 return self._blob_bytes 

18 

19 @staticmethod 

20 def from_b64_str(blob: str) -> BytesBlob: 

21 return BytesBlob(b64decode(blob)) 

22 

23 def as_b64_str(self) -> str: 

24 return b64encode(self._blob_bytes).decode("ascii") 

25 

26 def __repr__(self) -> str: 

27 return f"{self.__class__.__name__}({self.as_bytes().__repr__()})" 

28 

29 

30class StrBlob(BytesBlob): 

31 def __init__(self, blob: bytes | str) -> None: 

32 if isinstance(blob, str): 

33 blob = blob.encode() 

34 

35 super().__init__(blob) 

36 

37 def as_str(self) -> str: 

38 return self._blob_bytes.decode() 

39 

40 def __str__(self) -> str: 

41 return self.as_str() 

42 

43 @override 

44 def __repr__(self) -> str: 

45 return f"{self.__class__.__name__}({self.as_str().__repr__()})"