python3-commons 0.5.19__py2.py3-none-any.whl → 0.5.20__py2.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.

Potentially problematic release.


This version of python3-commons might be problematic. Click here for more details.

python3_commons/audit.py CHANGED
@@ -2,6 +2,7 @@ import asyncio
2
2
  import io
3
3
  import logging
4
4
  import tarfile
5
+ from bz2 import BZ2Compressor
5
6
  from datetime import datetime, timedelta, UTC
6
7
  from typing import Generator, Iterable
7
8
  from uuid import uuid4
@@ -92,6 +93,17 @@ def generate_archive(objects: Iterable[tuple[str, datetime, bytes]],
92
93
  buffer.truncate(0)
93
94
 
94
95
 
96
+ def generate_bzip2(chunks: Generator[bytes, None, None]) -> Generator[bytes, None, None]:
97
+ compressor = BZ2Compressor()
98
+
99
+ for chunk in chunks:
100
+ if compressed_chunk := compressor.compress(chunk):
101
+ yield compressed_chunk
102
+
103
+ if compressed_chunk := compressor.flush():
104
+ yield compressed_chunk
105
+
106
+
95
107
  def write_audit_data_sync(settings: S3Settings, key: str, data: bytes):
96
108
  if settings.s3_secret_access_key:
97
109
  try:
@@ -123,7 +135,8 @@ async def archive_audit_data(root_path: str = 'audit'):
123
135
  logger.info(f'Compacting files in: {date_path}')
124
136
 
125
137
  generator = generate_archive(objects, chunk_size=5*1024*1024)
126
- archive_stream = GeneratedStream(generator)
138
+ bzip2_generator = generate_bzip2(generator)
139
+ archive_stream = GeneratedStream(bzip2_generator)
127
140
 
128
141
  archive_path = object_storage.get_absolute_path(f'audit/.archive/{year}_{month:02}_{day:02}.tar.bz2')
129
142
  object_storage.put_object(bucket_name, archive_path, archive_stream, -1, part_size=5*1024*1024)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python3-commons
3
- Version: 0.5.19
3
+ Version: 0.5.20
4
4
  Summary: Re-usable Python3 code
5
5
  Home-page: https://github.com/kamikaze/python3-commons
6
6
  Author: Oleg Korsak
@@ -14,17 +14,17 @@ Requires-Python: >=3.12
14
14
  Description-Content-Type: text/x-rst; charset=UTF-8
15
15
  License-File: LICENSE
16
16
  License-File: AUTHORS.rst
17
- Requires-Dist: asyncpg ==0.29.0
18
- Requires-Dist: lxml ==5.2.2
19
- Requires-Dist: minio ==7.2.7
20
- Requires-Dist: msgpack ==1.0.8
21
- Requires-Dist: msgspec ==0.18.6
22
- Requires-Dist: pydantic[email] ==2.8.2
23
- Requires-Dist: pydantic-settings ==2.4.0
24
- Requires-Dist: zeep ==4.2.1
17
+ Requires-Dist: asyncpg==0.29.0
18
+ Requires-Dist: lxml==5.2.2
19
+ Requires-Dist: minio==7.2.7
20
+ Requires-Dist: msgpack==1.0.8
21
+ Requires-Dist: msgspec==0.18.6
22
+ Requires-Dist: pydantic[email]==2.8.2
23
+ Requires-Dist: pydantic-settings==2.4.0
24
+ Requires-Dist: zeep==4.2.1
25
25
  Provides-Extra: testing
26
- Requires-Dist: pytest ; extra == 'testing'
27
- Requires-Dist: pytest-cov ; extra == 'testing'
26
+ Requires-Dist: pytest; extra == "testing"
27
+ Requires-Dist: pytest-cov; extra == "testing"
28
28
 
29
29
  Re-usable Python3 code
30
30
  ======================
@@ -1,11 +1,10 @@
1
1
  python3_commons/__init__.py,sha256=h-KTJUaQ50E3RmkTn_GO88IRunmDTEpNc3ylpFvCTOc,339
2
- python3_commons/audit.py,sha256=lQbgThInSSGtEGQDLhrb0XrxDWPteTUqKf8Kx0Oywtk,5453
2
+ python3_commons/audit.py,sha256=fokdHYthtZWc1HQA2lZ74mQVkAh57oFzH-N97RS3tSc,5865
3
3
  python3_commons/conf.py,sha256=vSXyFwXx2wb1uy8IffeeI-RoTqhUZs0RLSSG2OLc2ss,598
4
4
  python3_commons/db.py,sha256=qhaDIdzBWgFyeP_XPKfHZlYVlwS2bpBPYMv84yV6820,738
5
5
  python3_commons/fs.py,sha256=wfLjybXndwLqNlOxTpm_HRJnuTcC4wbrHEOaEeCo9Wc,337
6
6
  python3_commons/helpers.py,sha256=hZG8M-mltBC8I9yx5ZuAM7bABFNuOsqX6FzSaQz4y9U,2480
7
7
  python3_commons/object_storage.py,sha256=pk2J14RL9FLTwaks-IS4EJX9TBMLzid35CroGftLNhU,4067
8
- python3_commons/stream_tar.py,sha256=1aA_IIM4oLdh2EZx_5tSeSm6flgCJ_QWbXLFAjg6hEo,2442
9
8
  python3_commons/logging/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
9
  python3_commons/logging/filters.py,sha256=fuyjXZAUm-i2MNrxvFYag8F8Rr27x8W8MdV3ke6miSs,175
11
10
  python3_commons/logging/formatter.py,sha256=UXmmh1yd5Kc2dpvSHn6uCWLDWE2LMjlYAaH8cg3siV4,720
@@ -13,9 +12,9 @@ python3_commons/serializers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMp
13
12
  python3_commons/serializers/json.py,sha256=P288wWz9ic38QWEMrpp_uwKPYkQiOgvE1cI4WZn6ZCg,808
14
13
  python3_commons/serializers/msgpack.py,sha256=tzIGGyDL3UpZnnouCtnxuYDx6InKM_C3PP1N4PN8wd4,1269
15
14
  python3_commons/serializers/msgspec.py,sha256=EknuMpxi_kU25Iv_m10E9rk8b31AkjVumuzyjp7WgrU,1699
16
- python3_commons-0.5.19.dist-info/AUTHORS.rst,sha256=3R9JnfjfjH5RoPWOeqKFJgxVShSSfzQPIrEr1nxIo9Q,90
17
- python3_commons-0.5.19.dist-info/LICENSE,sha256=xxILuojHm4fKQOrMHPSslbyy6WuKAN2RiG74HbrYfzM,34575
18
- python3_commons-0.5.19.dist-info/METADATA,sha256=uQN3EfXdlIgIHQJMcfpK6a0Arju4Vx4xuGuAe-uCiHk,975
19
- python3_commons-0.5.19.dist-info/WHEEL,sha256=XRxW4r1PNiVhMpP4bT9oWtu3HyndxpJ84SkubFgzp_Y,109
20
- python3_commons-0.5.19.dist-info/top_level.txt,sha256=lJI6sCBf68eUHzupCnn2dzG10lH3jJKTWM_hrN1cQ7M,16
21
- python3_commons-0.5.19.dist-info/RECORD,,
15
+ python3_commons-0.5.20.dist-info/AUTHORS.rst,sha256=3R9JnfjfjH5RoPWOeqKFJgxVShSSfzQPIrEr1nxIo9Q,90
16
+ python3_commons-0.5.20.dist-info/LICENSE,sha256=xxILuojHm4fKQOrMHPSslbyy6WuKAN2RiG74HbrYfzM,34575
17
+ python3_commons-0.5.20.dist-info/METADATA,sha256=q_9B5XQRV-h0giivNG1AhyY09xUW1g2rnTEVLc9rYwc,965
18
+ python3_commons-0.5.20.dist-info/WHEEL,sha256=XRxW4r1PNiVhMpP4bT9oWtu3HyndxpJ84SkubFgzp_Y,109
19
+ python3_commons-0.5.20.dist-info/top_level.txt,sha256=lJI6sCBf68eUHzupCnn2dzG10lH3jJKTWM_hrN1cQ7M,16
20
+ python3_commons-0.5.20.dist-info/RECORD,,
@@ -1,105 +0,0 @@
1
- import logging
2
- import os
3
- import sys
4
- import tarfile
5
- from io import BytesIO
6
-
7
-
8
- logger = logging.getLogger(__name__)
9
- BLOCK_SIZE = 4096
10
-
11
-
12
- class FileStream(object):
13
- def __init__(self):
14
- self.buffer = BytesIO()
15
- self.offset = 0
16
-
17
- def write(self, chunk):
18
- self.buffer.write(chunk)
19
- self.offset += len(chunk)
20
-
21
- def tell(self):
22
- return self.offset
23
-
24
- def close(self):
25
- self.buffer.close()
26
-
27
- def pop(self):
28
- value = self.buffer.getvalue()
29
- self.buffer.seek(0)
30
- self.buffer.truncate()
31
-
32
- return value
33
-
34
-
35
- def stream_build_tar(in_filename, streaming_fp):
36
- tar = tarfile.TarFile.open('test.tar.gz', 'w|gz', streaming_fp)
37
-
38
- stat = os.stat(in_filename)
39
-
40
- tar_info = tarfile.TarInfo(in_filename)
41
-
42
- # Note that you can get this information from the storage backend,
43
- # but it's valid for either to raise a NotImplementedError, so it's
44
- # important to check.
45
- #
46
- # Things like the mode or ownership won't be available.
47
- tar_info.mtime = stat.st_mtime
48
- tar_info.size = stat.st_size
49
-
50
- # Note that we don't pass a fileobj, so we don't write any data
51
- # through addfile. We'll do this ourselves.
52
- tar.addfile(tar_info)
53
-
54
- yield
55
-
56
- with open(in_filename, 'rb') as in_fp:
57
- total_size = 0
58
-
59
- while True:
60
- s = in_fp.read(BLOCK_SIZE)
61
-
62
- if len(s) > 0:
63
- tar.fileobj.write(s)
64
-
65
- yield
66
-
67
- if len(s) < BLOCK_SIZE:
68
- blocks, remainder = divmod(tar_info.size, tarfile.BLOCKSIZE)
69
-
70
- if remainder > 0:
71
- tar.fileobj.write(tarfile.NUL *
72
- (tarfile.BLOCKSIZE - remainder))
73
-
74
- yield
75
-
76
- blocks += 1
77
-
78
- tar.offset += blocks * tarfile.BLOCKSIZE
79
- break
80
-
81
- tar.close()
82
-
83
- yield
84
-
85
-
86
- def main():
87
- if len(sys.argv) != 3:
88
- print('Usage: %s in_filename out_filename' % sys.argv[0])
89
- sys.exit(1)
90
-
91
- in_filename = sys.argv[1]
92
- out_filename = sys.argv[2]
93
-
94
- streaming_fp = FileStream()
95
-
96
- with open(out_filename, 'wb') as out_fp:
97
- for i in stream_build_tar(in_filename, streaming_fp):
98
- s = streaming_fp.pop()
99
-
100
- if len(s) > 0:
101
- print('Writing %d bytes...' % len(s))
102
- out_fp.write(s)
103
- out_fp.flush()
104
-
105
- print('Wrote tar file to %s' % out_filename)