python3-commons 0.4.0__py2.py3-none-any.whl → 0.5.1__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 +63 -5
- python3_commons/object_storage.py +0 -14
- {python3_commons-0.4.0.dist-info → python3_commons-0.5.1.dist-info}/METADATA +3 -1
- {python3_commons-0.4.0.dist-info → python3_commons-0.5.1.dist-info}/RECORD +8 -8
- {python3_commons-0.4.0.dist-info → python3_commons-0.5.1.dist-info}/AUTHORS.rst +0 -0
- {python3_commons-0.4.0.dist-info → python3_commons-0.5.1.dist-info}/LICENSE +0 -0
- {python3_commons-0.4.0.dist-info → python3_commons-0.5.1.dist-info}/WHEEL +0 -0
- {python3_commons-0.4.0.dist-info → python3_commons-0.5.1.dist-info}/top_level.txt +0 -0
python3_commons/audit.py
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
+
import asyncio
|
|
1
2
|
import logging
|
|
2
3
|
import tarfile
|
|
3
|
-
from datetime import date, timedelta
|
|
4
|
+
from datetime import date, datetime, timedelta, UTC
|
|
4
5
|
from io import BytesIO
|
|
6
|
+
from uuid import uuid4
|
|
7
|
+
from zoneinfo import ZoneInfo
|
|
8
|
+
|
|
9
|
+
from lxml import etree
|
|
10
|
+
from zeep.plugins import Plugin
|
|
11
|
+
from zeep.wsdl.definitions import AbstractOperation
|
|
5
12
|
|
|
6
13
|
from python3_commons import object_storage
|
|
7
14
|
from python3_commons.conf import s3_settings
|
|
@@ -9,11 +16,62 @@ from python3_commons.conf import s3_settings
|
|
|
9
16
|
logger = logging.getLogger(__name__)
|
|
10
17
|
|
|
11
18
|
|
|
19
|
+
class ZeepAuditPlugin(Plugin):
|
|
20
|
+
def __init__(self, audit_name: str = 'zeep'):
|
|
21
|
+
super().__init__()
|
|
22
|
+
self.audit_name = audit_name
|
|
23
|
+
|
|
24
|
+
@staticmethod
|
|
25
|
+
def store_audit_in_s3(envelope, operation: AbstractOperation, direction: str):
|
|
26
|
+
xml = etree.tostring(envelope, encoding='UTF-8', pretty_print=True)
|
|
27
|
+
now = datetime.now(tz=UTC)
|
|
28
|
+
date_path = now.strftime('%Y/%m/%d')
|
|
29
|
+
timestamp = now.strftime('%H%M%S')
|
|
30
|
+
coro = object_storage.store_bytes_in_s3(
|
|
31
|
+
settings, xml,
|
|
32
|
+
f'audit/{date_path}/{self.audit_name}/{operation.name}/{timestamp}_{str(uuid4())[-12:]}_{direction}.xml'
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
loop = asyncio.get_running_loop()
|
|
37
|
+
except RuntimeError:
|
|
38
|
+
loop = None
|
|
39
|
+
|
|
40
|
+
if loop and loop.is_running():
|
|
41
|
+
loop.create_task(coro)
|
|
42
|
+
else:
|
|
43
|
+
asyncio.run(coro)
|
|
44
|
+
|
|
45
|
+
def ingress(self, envelope, http_headers, operation: AbstractOperation):
|
|
46
|
+
self.store_audit_in_s3(envelope, operation, 'ingress')
|
|
47
|
+
|
|
48
|
+
return envelope, http_headers
|
|
49
|
+
|
|
50
|
+
def egress(self, envelope, http_headers, operation: AbstractOperation, binding_options):
|
|
51
|
+
self.store_audit_in_s3(envelope, operation, 'egress')
|
|
52
|
+
|
|
53
|
+
return envelope, http_headers
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
async def write_audit_data(settings: S3Settings, key: str, data: bytes):
|
|
57
|
+
if settings.s3_secret_access_key:
|
|
58
|
+
try:
|
|
59
|
+
client = get_s3_client(settings)
|
|
60
|
+
|
|
61
|
+
client.put_object(settings.s3_bucket, key, io.BytesIO(data), len(data))
|
|
62
|
+
except S3Error as e:
|
|
63
|
+
logger.error(f'Failed storing object in storage: {e}')
|
|
64
|
+
else:
|
|
65
|
+
logger.debug(f'Stored object in storage: {key}')
|
|
66
|
+
else:
|
|
67
|
+
logger.debug(f'S3 is not configured, not storing object in storage: {key}')
|
|
68
|
+
|
|
69
|
+
|
|
12
70
|
async def archive_audit_data(root_path: str = 'audit'):
|
|
13
|
-
|
|
14
|
-
year =
|
|
15
|
-
month =
|
|
16
|
-
day =
|
|
71
|
+
now = datetime.now(tz=UTC) - timedelta(days=1)
|
|
72
|
+
year = now.year
|
|
73
|
+
month = now.month
|
|
74
|
+
day = now.day
|
|
17
75
|
bucket_name = s3_settings.s3_bucket
|
|
18
76
|
fo = BytesIO()
|
|
19
77
|
object_names = []
|
|
@@ -125,17 +125,3 @@ def remove_objects(bucket_name: str, prefix: str = None,
|
|
|
125
125
|
errors = s3_client.remove_objects(bucket_name, delete_object_list)
|
|
126
126
|
|
|
127
127
|
return errors
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
async def store_bytes_in_s3(settings: S3Settings, data: bytes, key: str):
|
|
131
|
-
if settings.s3_secret_access_key:
|
|
132
|
-
try:
|
|
133
|
-
client = get_s3_client(settings)
|
|
134
|
-
|
|
135
|
-
client.put_object(settings.s3_bucket, key, io.BytesIO(data), len(data))
|
|
136
|
-
except S3Error as e:
|
|
137
|
-
logger.error(f'Failed storing object in storage: {e}')
|
|
138
|
-
else:
|
|
139
|
-
logger.debug(f'Stored object in storage: {key}')
|
|
140
|
-
else:
|
|
141
|
-
logger.debug(f'S3 is not configured, not storing object in storage: {key}')
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: python3-commons
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.1
|
|
4
4
|
Summary: Re-usable Python3 code
|
|
5
5
|
Home-page: https://github.com/kamikaze/python3-commons
|
|
6
6
|
Author: Oleg Korsak
|
|
@@ -15,11 +15,13 @@ Description-Content-Type: text/x-rst; charset=UTF-8
|
|
|
15
15
|
License-File: LICENSE
|
|
16
16
|
License-File: AUTHORS.rst
|
|
17
17
|
Requires-Dist: asyncpg ==0.29.0
|
|
18
|
+
Requires-Dist: lxml ==5.2.2
|
|
18
19
|
Requires-Dist: minio ==7.2.7
|
|
19
20
|
Requires-Dist: msgpack ==1.0.8
|
|
20
21
|
Requires-Dist: msgspec ==0.18.6
|
|
21
22
|
Requires-Dist: pydantic[email] ==2.7.2
|
|
22
23
|
Requires-Dist: pydantic-settings ==2.2.1
|
|
24
|
+
Requires-Dist: zeep ==4.2.1
|
|
23
25
|
Provides-Extra: testing
|
|
24
26
|
Requires-Dist: pytest ; extra == 'testing'
|
|
25
27
|
Requires-Dist: pytest-cov ; extra == 'testing'
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
python3_commons/__init__.py,sha256=h-KTJUaQ50E3RmkTn_GO88IRunmDTEpNc3ylpFvCTOc,339
|
|
2
|
-
python3_commons/audit.py,sha256=
|
|
2
|
+
python3_commons/audit.py,sha256=n63-x3Segw7PjYvDak5RefMxQ8uC_IxtzmQr1L-VTaw,3571
|
|
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=wI7afc-8o-SBpLBHQnWXiudw456EAVKguJaeZiSNXwE,1377
|
|
7
|
-
python3_commons/object_storage.py,sha256=
|
|
7
|
+
python3_commons/object_storage.py,sha256=xQy8Jzb0_nS6vxQ6RyNA5FV_ql3k88iZQ5RPWJJiBUo,3798
|
|
8
8
|
python3_commons/logging/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
9
|
python3_commons/logging/filters.py,sha256=fuyjXZAUm-i2MNrxvFYag8F8Rr27x8W8MdV3ke6miSs,175
|
|
10
10
|
python3_commons/logging/formatter.py,sha256=UXmmh1yd5Kc2dpvSHn6uCWLDWE2LMjlYAaH8cg3siV4,720
|
|
@@ -12,9 +12,9 @@ python3_commons/serializers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMp
|
|
|
12
12
|
python3_commons/serializers/json.py,sha256=P288wWz9ic38QWEMrpp_uwKPYkQiOgvE1cI4WZn6ZCg,808
|
|
13
13
|
python3_commons/serializers/msgpack.py,sha256=P7CZoRTBeDtgALT5GTOZVCNM_3snOfCnfh5S3zcCIEY,1679
|
|
14
14
|
python3_commons/serializers/msgspec.py,sha256=ZrfQWBz_67t3yjU_S6avnRJZzYN-HFzef2qGB5W21KI,2023
|
|
15
|
-
python3_commons-0.
|
|
16
|
-
python3_commons-0.
|
|
17
|
-
python3_commons-0.
|
|
18
|
-
python3_commons-0.
|
|
19
|
-
python3_commons-0.
|
|
20
|
-
python3_commons-0.
|
|
15
|
+
python3_commons-0.5.1.dist-info/AUTHORS.rst,sha256=3R9JnfjfjH5RoPWOeqKFJgxVShSSfzQPIrEr1nxIo9Q,90
|
|
16
|
+
python3_commons-0.5.1.dist-info/LICENSE,sha256=xxILuojHm4fKQOrMHPSslbyy6WuKAN2RiG74HbrYfzM,34575
|
|
17
|
+
python3_commons-0.5.1.dist-info/METADATA,sha256=GZJx_6XasOPWyBd2YyafDhSB20ISvnkFwjXI8sakZJo,974
|
|
18
|
+
python3_commons-0.5.1.dist-info/WHEEL,sha256=DZajD4pwLWue70CAfc7YaxT1wLUciNBvN_TTcvXpltE,110
|
|
19
|
+
python3_commons-0.5.1.dist-info/top_level.txt,sha256=lJI6sCBf68eUHzupCnn2dzG10lH3jJKTWM_hrN1cQ7M,16
|
|
20
|
+
python3_commons-0.5.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|