dictature 0.9.5__py3-none-any.whl → 0.9.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@ from .mock import DictatureTableMock, DictatureBackendMock, Value, ValueMode, Va
6
6
 
7
7
 
8
8
  class DictatureBackendDirectory(DictatureBackendMock):
9
- def __init__(self, directory: Union[Path, str], dir_prefix: str = 'db_') -> None:
9
+ def __init__(self, directory: Union[Path, str], dir_prefix: str = 'db_', item_prefix: str = 'item_') -> None:
10
10
  """
11
11
  Create a new directory backend
12
12
  :param directory: directory to store the data
@@ -16,6 +16,7 @@ class DictatureBackendDirectory(DictatureBackendMock):
16
16
  directory = Path(directory)
17
17
  self.__directory = directory
18
18
  self.__dir_prefix = dir_prefix
19
+ self.__item_prefix = item_prefix
19
20
 
20
21
  def keys(self) -> Iterable[str]:
21
22
  for child in self.__directory.iterdir():
@@ -24,14 +25,15 @@ class DictatureBackendDirectory(DictatureBackendMock):
24
25
  yield DictatureTableDirectory._filename_decode(child.name[len(self.__dir_prefix):], suffix='')
25
26
 
26
27
  def table(self, name: str) -> 'DictatureTableMock':
27
- return DictatureTableDirectory(self.__directory, name, self.__dir_prefix)
28
+ return DictatureTableDirectory(self.__directory, name, self.__dir_prefix, self.__item_prefix)
28
29
 
29
30
 
30
31
  class DictatureTableDirectory(DictatureTableMock):
31
- def __init__(self, path_root: Path, name: str, db_prefix: str, prefix: str = 'item_') -> None:
32
+ def __init__(self, path_root: Path, name: str, db_prefix: str, prefix: str) -> None:
32
33
  self.__path = path_root / (db_prefix + self._filename_encode(name, suffix=''))
33
34
  self.__prefix = prefix
34
- self.__serializer = ValueSerializer(mode=ValueSerializerMode.filename_only)
35
+ self.__name_serializer = ValueSerializer(mode=ValueSerializerMode.filename_only)
36
+ self.__value_serializer = ValueSerializer(mode=ValueSerializerMode.any_string)
35
37
 
36
38
  def keys(self) -> Iterable[str]:
37
39
  for child in self.__path.iterdir():
@@ -48,7 +50,7 @@ class DictatureTableDirectory(DictatureTableMock):
48
50
  file_target = self.__item_path(item)
49
51
  file_target_tmp = file_target.with_suffix('.tmp')
50
52
 
51
- save_data = self.__serializer.serialize(value)
53
+ save_data = self.__value_serializer.serialize(value)
52
54
 
53
55
  file_target_tmp.write_text(save_data)
54
56
  file_target_tmp.rename(file_target)
@@ -56,7 +58,7 @@ class DictatureTableDirectory(DictatureTableMock):
56
58
  def get(self, item: str) -> Value:
57
59
  try:
58
60
  save_data = self.__item_path(item).read_text()
59
- return self.__serializer.deserialize(save_data)
61
+ return self.__value_serializer.deserialize(save_data)
60
62
  except FileNotFoundError:
61
63
  raise KeyError(item)
62
64
 
@@ -0,0 +1,53 @@
1
+ import gzip
2
+ import base64
3
+
4
+ from .mock import MockTransformer
5
+
6
+
7
+ class GzipTransformer(MockTransformer):
8
+ """
9
+ Compresses and decompresses text using Gzip.
10
+
11
+ The compressed data is Base64 encoded to ensure it can be represented as a string.
12
+ """
13
+ def __init__(self) -> None:
14
+ """
15
+ Initializes the GzipTransformer. No parameters needed for basic Gzip.
16
+ """
17
+ # No specific state needed for standard gzip compression/decompression
18
+ pass
19
+
20
+ def forward(self, text: str) -> str:
21
+ """
22
+ Compresses the input text using Gzip and returns a Base64 encoded string.
23
+ :param text: The original text string.
24
+ :return: A Base64 encoded string representing the Gzipped data.
25
+ """
26
+ byte_data = text.encode('utf-8')
27
+ compressed_bytes = gzip.compress(byte_data)
28
+ base64_bytes = base64.b64encode(compressed_bytes)
29
+ base64_string = base64_bytes.decode('ascii')
30
+ return base64_string
31
+
32
+ def backward(self, text: str) -> str:
33
+ """
34
+ Decompresses the Base64 encoded Gzip data back into the original text.
35
+ :param text: A Base64 encoded string representing Gzipped data.
36
+ :return: The original text string.
37
+ :raises ValueError: If the input string is not valid Base64 or not valid Gzip data.
38
+ """
39
+ try:
40
+ base64_bytes = text.encode('ascii')
41
+ compressed_bytes = base64.b64decode(base64_bytes)
42
+ original_bytes = gzip.decompress(compressed_bytes)
43
+ original_text = original_bytes.decode('utf-8')
44
+ return original_text
45
+ except (gzip.BadGzipFile, UnicodeDecodeError) as e:
46
+ # Catch errors related to Base64 decoding, Gzip decompression, or UTF-8 decoding
47
+ raise ValueError(f"Invalid input data for Gzip decompression: {e}") from e
48
+
49
+ @property
50
+ def static(self) -> bool:
51
+ return False
52
+
53
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dictature
3
- Version: 0.9.5
3
+ Version: 0.9.7
4
4
  Summary: dictature -- A generic wrapper around dict-like interface with mulitple backends
5
5
  Author-email: Adam Hlavacek <git@adamhlavacek.com>
6
6
  Project-URL: Homepage, https://github.com/esoadamo/dictature
@@ -78,5 +78,6 @@ dictionary = Dictature(
78
78
  Currently, the following transformers are supported:
79
79
  - `AESTransformer`: encrypts/decrypts the data using AES
80
80
  - `HmacTransformer`: signs the data using HMAC or performs hash integrity checks
81
+ - `GzipTransformer`: compresses given data
81
82
  - `PassthroughTransformer`: does nothing
82
83
  - `PipelineTransformer`: chains multiple transformers
@@ -1,18 +1,19 @@
1
1
  dictature/__init__.py,sha256=UCPJKHeyirRZ0pCYoyeat-rwXa8pDezOJ3UWCipDdyc,33
2
2
  dictature/dictature.py,sha256=SHwG_XvGwm6qpvMt5OjeS8BoU5wLgJi_4jIBKFmYFyI,9330
3
3
  dictature/backend/__init__.py,sha256=d5s6QCJOUzFglVNg8Cqqx_8b61S-AOTGjEUIF6FS69U,149
4
- dictature/backend/directory.py,sha256=pVXzwswxu9I38tiWhpqXmXhFFW57BzW_ScKHanOCPK0,3201
4
+ dictature/backend/directory.py,sha256=nPNJ-wRZOsuzaCOLxgGnNB093qID5LWw7VELAqi7kZc,3384
5
5
  dictature/backend/misp.py,sha256=iPjvgnJg6WveNP2wvgN7OK2vkX-SC9qYPrdoa9ahRT0,4411
6
6
  dictature/backend/mock.py,sha256=BzfLstxkTIjk6mcMTdFKj8rSaFgIqn9-2Cyelslj8bY,5889
7
7
  dictature/backend/sqlite.py,sha256=zyphYEeLY4eGuBCor16i80_-brdipMpXZ3_kONwErsE,5237
8
8
  dictature/transformer/__init__.py,sha256=JIFJpXU6iB9hIUM8L7HL2o9Nqjm_YbMEuQBQC8ZJ6b4,124
9
9
  dictature/transformer/aes.py,sha256=ZhC1dT9QpnziErkDLriWLgXDEFNGQW0KG4aqSN2AZpA,1926
10
+ dictature/transformer/gzip.py,sha256=pngvJQeALa-lv98VBeJ1Pl6_gaAfGcPXD9UR7PexrYA,1921
10
11
  dictature/transformer/hmac.py,sha256=vURsB0HlzRPn_Vkl7lGmZV9OKempQuds8AanmadDxIo,834
11
12
  dictature/transformer/mock.py,sha256=7zu65ZqUV_AVRaPSzNd73cVMXixXt31SeuX9OKZxaJQ,948
12
13
  dictature/transformer/passthrough.py,sha256=Pt3N6G_Qh6HJ_q75ETL5nfAwYHLB-SjkVwUwbbbMik8,344
13
14
  dictature/transformer/pipeline.py,sha256=OaQaJeJ5NpICetJe08r8ontqstsXGuW8jDbKw1zxYs4,842
14
- dictature-0.9.5.dist-info/LICENSE,sha256=n1U9DKr8sM5EY2QHcvxSGiKTDWUT8MyXsOC79w94MT0,1072
15
- dictature-0.9.5.dist-info/METADATA,sha256=1qIyWXXTEogMI-mJ98ra_DCpUhU3soP8UNiV74zA8ek,2826
16
- dictature-0.9.5.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
17
- dictature-0.9.5.dist-info/top_level.txt,sha256=-RO39WWCF44lqiXhSUcACVqbk6SkgReZTz7ZmHKH3-U,10
18
- dictature-0.9.5.dist-info/RECORD,,
15
+ dictature-0.9.7.dist-info/LICENSE,sha256=n1U9DKr8sM5EY2QHcvxSGiKTDWUT8MyXsOC79w94MT0,1072
16
+ dictature-0.9.7.dist-info/METADATA,sha256=oyBWco6Qtzo3C6MsHl_tdco6Oqo0mTyIsyXeui7sfjo,2869
17
+ dictature-0.9.7.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
18
+ dictature-0.9.7.dist-info/top_level.txt,sha256=-RO39WWCF44lqiXhSUcACVqbk6SkgReZTz7ZmHKH3-U,10
19
+ dictature-0.9.7.dist-info/RECORD,,