python3-commons 0.5.17__py2.py3-none-any.whl → 0.5.19__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
@@ -24,16 +24,25 @@ class GeneratedStream(io.BytesIO):
24
24
  self.generator = generator
25
25
 
26
26
  def read(self, size: int = -1):
27
- if size == -1:
28
- size = 4096
29
-
30
- while self.tell() < size:
31
- try:
32
- chunk = next(self.generator)
33
- except StopIteration:
34
- break
27
+ if size < 0:
28
+ while True:
29
+ try:
30
+ chunk = next(self.generator)
31
+ except StopIteration:
32
+ break
33
+ else:
34
+ self.write(chunk)
35
+ else:
36
+ total_written_size = 0
35
37
 
36
- self.write(chunk)
38
+ while total_written_size < size:
39
+ try:
40
+ chunk = next(self.generator)
41
+ except StopIteration:
42
+ break
43
+ else:
44
+ self.write(chunk)
45
+ total_written_size += len(chunk)
37
46
 
38
47
  self.seek(0)
39
48
 
@@ -46,6 +55,8 @@ class GeneratedStream(io.BytesIO):
46
55
  if unread_data_size > 0:
47
56
  buf[:unread_data_size] = buf[pos:pos+unread_data_size]
48
57
 
58
+ del buf
59
+
49
60
  self.seek(0)
50
61
  self.truncate(unread_data_size)
51
62
 
@@ -66,7 +77,6 @@ def generate_archive(objects: Iterable[tuple[str, datetime, bytes]],
66
77
  info.size = len(content)
67
78
  info.mtime = last_modified.timestamp()
68
79
  archive.addfile(info, io.BytesIO(content))
69
- archive.fileobj.flush()
70
80
 
71
81
  buffer.seek(0)
72
82
 
@@ -1,5 +1,6 @@
1
1
  import io
2
2
  import logging
3
+ from contextlib import contextmanager
3
4
  from datetime import datetime
4
5
  from typing import Generator, Iterable
5
6
 
@@ -42,9 +43,7 @@ def get_absolute_path(path: str) -> str:
42
43
 
43
44
 
44
45
  def put_object(bucket_name: str, path: str, data: io.BytesIO, length: int, part_size: int = 0) -> str:
45
- s3_client = ObjectStorage(s3_settings).get_client()
46
-
47
- if s3_client:
46
+ if s3_client := ObjectStorage(s3_settings).get_client():
48
47
  result = s3_client.put_object(bucket_name, path, data, length, part_size=part_size)
49
48
 
50
49
  logger.debug(f'Stored object into object storage: {bucket_name}:{path}')
@@ -54,10 +53,9 @@ def put_object(bucket_name: str, path: str, data: io.BytesIO, length: int, part_
54
53
  logger.warning(f'No S3 client available, skipping object put')
55
54
 
56
55
 
56
+ @contextmanager
57
57
  def get_object_stream(bucket_name: str, path: str):
58
- s3_client = ObjectStorage(s3_settings).get_client()
59
-
60
- if s3_client:
58
+ if s3_client := ObjectStorage(s3_settings).get_client():
61
59
  logger.debug(f'Getting object from object storage: {bucket_name}:{path}')
62
60
 
63
61
  try:
@@ -67,19 +65,17 @@ def get_object_stream(bucket_name: str, path: str):
67
65
 
68
66
  raise
69
67
 
70
- return response
68
+ yield response
69
+
70
+ response.close()
71
+ response.release_conn()
71
72
  else:
72
73
  logger.warning(f'No S3 client available, skipping object put')
73
74
 
74
75
 
75
76
  def get_object(bucket_name: str, path: str) -> bytes:
76
- response = get_object_stream(bucket_name, path)
77
-
78
- try:
79
- body = response.read()
80
- finally:
81
- response.close()
82
- response.release_conn()
77
+ with get_object_stream(bucket_name, path) as stream:
78
+ body = stream.read()
83
79
 
84
80
  logger.debug(f'Loaded object from object storage: {bucket_name}:{path}')
85
81
 
@@ -1,9 +1,14 @@
1
+ import logging
1
2
  import os
2
3
  import sys
3
4
  import tarfile
4
5
  from io import BytesIO
5
6
 
6
7
 
8
+ logger = logging.getLogger(__name__)
9
+ BLOCK_SIZE = 4096
10
+
11
+
7
12
  class FileStream(object):
8
13
  def __init__(self):
9
14
  self.buffer = BytesIO()
@@ -28,7 +33,7 @@ class FileStream(object):
28
33
 
29
34
 
30
35
  def stream_build_tar(in_filename, streaming_fp):
31
- tar = tarfile.TarFile.open(out_filename, 'w|gz', streaming_fp)
36
+ tar = tarfile.TarFile.open('test.tar.gz', 'w|gz', streaming_fp)
32
37
 
33
38
  stat = os.stat(in_filename)
34
39
 
@@ -78,25 +83,23 @@ def stream_build_tar(in_filename, streaming_fp):
78
83
  yield
79
84
 
80
85
 
81
- BLOCK_SIZE = 4096
86
+ def main():
87
+ if len(sys.argv) != 3:
88
+ print('Usage: %s in_filename out_filename' % sys.argv[0])
89
+ sys.exit(1)
82
90
 
91
+ in_filename = sys.argv[1]
92
+ out_filename = sys.argv[2]
83
93
 
84
- if len(sys.argv) != 3:
85
- print('Usage: %s in_filename out_filename' % sys.argv[0])
86
- sys.exit(1)
94
+ streaming_fp = FileStream()
87
95
 
88
- in_filename = sys.argv[1]
89
- out_filename = sys.argv[2]
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()
90
99
 
91
- streaming_fp = FileStream()
92
-
93
- with open(out_filename, 'wb') as out_fp:
94
- for i in stream_build_tar(in_filename, streaming_fp):
95
- s = streaming_fp.pop()
96
-
97
- if len(s) > 0:
98
- print('Writing %d bytes...' % len(s))
99
- out_fp.write(s)
100
- out_fp.flush()
100
+ if len(s) > 0:
101
+ print('Writing %d bytes...' % len(s))
102
+ out_fp.write(s)
103
+ out_fp.flush()
101
104
 
102
- print('Wrote tar file to %s' % out_filename)
105
+ print('Wrote tar file to %s' % out_filename)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python3-commons
3
- Version: 0.5.17
3
+ Version: 0.5.19
4
4
  Summary: Re-usable Python3 code
5
5
  Home-page: https://github.com/kamikaze/python3-commons
6
6
  Author: Oleg Korsak
@@ -20,7 +20,7 @@ Requires-Dist: minio ==7.2.7
20
20
  Requires-Dist: msgpack ==1.0.8
21
21
  Requires-Dist: msgspec ==0.18.6
22
22
  Requires-Dist: pydantic[email] ==2.8.2
23
- Requires-Dist: pydantic-settings ==2.3.4
23
+ Requires-Dist: pydantic-settings ==2.4.0
24
24
  Requires-Dist: zeep ==4.2.1
25
25
  Provides-Extra: testing
26
26
  Requires-Dist: pytest ; extra == 'testing'
@@ -1,11 +1,11 @@
1
1
  python3_commons/__init__.py,sha256=h-KTJUaQ50E3RmkTn_GO88IRunmDTEpNc3ylpFvCTOc,339
2
- python3_commons/audit.py,sha256=1CHNFcz87F_QvaiI0CUCc2IYpIGsxT718oHjIl4Wqs4,5118
2
+ python3_commons/audit.py,sha256=lQbgThInSSGtEGQDLhrb0XrxDWPteTUqKf8Kx0Oywtk,5453
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
- python3_commons/object_storage.py,sha256=GEy3G-Y6QNY3NTp5N7QfaKYIYqBbUAtDDo1JWvUgNtw,4061
8
- python3_commons/stream_tar.py,sha256=JPmiHzPokow8CsEG7bel8egn7tMuAsAKFnYfeDkmz34,2321
7
+ python3_commons/object_storage.py,sha256=pk2J14RL9FLTwaks-IS4EJX9TBMLzid35CroGftLNhU,4067
8
+ python3_commons/stream_tar.py,sha256=1aA_IIM4oLdh2EZx_5tSeSm6flgCJ_QWbXLFAjg6hEo,2442
9
9
  python3_commons/logging/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  python3_commons/logging/filters.py,sha256=fuyjXZAUm-i2MNrxvFYag8F8Rr27x8W8MdV3ke6miSs,175
11
11
  python3_commons/logging/formatter.py,sha256=UXmmh1yd5Kc2dpvSHn6uCWLDWE2LMjlYAaH8cg3siV4,720
@@ -13,9 +13,9 @@ python3_commons/serializers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMp
13
13
  python3_commons/serializers/json.py,sha256=P288wWz9ic38QWEMrpp_uwKPYkQiOgvE1cI4WZn6ZCg,808
14
14
  python3_commons/serializers/msgpack.py,sha256=tzIGGyDL3UpZnnouCtnxuYDx6InKM_C3PP1N4PN8wd4,1269
15
15
  python3_commons/serializers/msgspec.py,sha256=EknuMpxi_kU25Iv_m10E9rk8b31AkjVumuzyjp7WgrU,1699
16
- python3_commons-0.5.17.dist-info/AUTHORS.rst,sha256=3R9JnfjfjH5RoPWOeqKFJgxVShSSfzQPIrEr1nxIo9Q,90
17
- python3_commons-0.5.17.dist-info/LICENSE,sha256=xxILuojHm4fKQOrMHPSslbyy6WuKAN2RiG74HbrYfzM,34575
18
- python3_commons-0.5.17.dist-info/METADATA,sha256=TY7PX7pUGLrFc0Iua413-DfQhYgm0y4Ur7SnSa8H3uQ,975
19
- python3_commons-0.5.17.dist-info/WHEEL,sha256=pWvVuNuBTVmNV7Lp2jMAgt1NplTICeFdl1SW8U3MWN4,109
20
- python3_commons-0.5.17.dist-info/top_level.txt,sha256=lJI6sCBf68eUHzupCnn2dzG10lH3jJKTWM_hrN1cQ7M,16
21
- python3_commons-0.5.17.dist-info/RECORD,,
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,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (70.3.0)
2
+ Generator: setuptools (72.1.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py2-none-any
5
5
  Tag: py3-none-any