python3-commons 0.22.4__py3-none-any.whl → 0.22.6__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.
@@ -185,7 +185,7 @@ class AsyncSessionManager:
185
185
  try:
186
186
  yield session
187
187
  except Exception:
188
- logger.exception('Database communication error for %r; rolling back', name)
188
+ logger.exception('Error occured while db session %r was open; rolling back', name)
189
189
  await session.rollback()
190
190
 
191
191
  raise
@@ -0,0 +1,310 @@
1
+ Metadata-Version: 2.4
2
+ Name: python3-commons
3
+ Version: 0.22.6
4
+ Summary: Re-usable Python3 code
5
+ Author-email: Oleg Korsak <kamikaze.is.waiting.you@gmail.com>
6
+ License-Expression: GPL-3.0
7
+ Project-URL: Homepage, https://github.com/kamikaze/python3-commons
8
+ Project-URL: Documentation, https://github.com/kamikaze/python3-commons/wiki
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Programming Language :: Python
11
+ Requires-Python: <3.15.0,>=3.14.0
12
+ Description-Content-Type: text/x-rst
13
+ License-File: LICENSE
14
+ License-File: AUTHORS.rst
15
+ Requires-Dist: msgpack~=1.2.1
16
+ Requires-Dist: msgspec~=0.21.1
17
+ Requires-Dist: pydantic-settings~=2.14.2
18
+ Provides-Extra: all
19
+ Requires-Dist: python3_commons[api-client,audit,authn,authz,cache,database,object-storage,soap-client]; extra == "all"
20
+ Provides-Extra: api-client
21
+ Requires-Dist: aiohttp[speedups]<3.15.0,>=3.14.1; extra == "api-client"
22
+ Requires-Dist: certifi~=2026.6.17; extra == "api-client"
23
+ Requires-Dist: python3_commons[audit,object-storage]; extra == "api-client"
24
+ Provides-Extra: audit
25
+ Requires-Dist: lxml~=6.1.1; extra == "audit"
26
+ Requires-Dist: zeep[async]~=4.3.3; extra == "audit"
27
+ Requires-Dist: python3_commons[object-storage]; extra == "audit"
28
+ Provides-Extra: authn
29
+ Requires-Dist: aiohttp[speedups]<3.15.0,>=3.14.1; extra == "authn"
30
+ Requires-Dist: certifi~=2026.6.17; extra == "authn"
31
+ Requires-Dist: python3_commons[api-client]; extra == "authn"
32
+ Provides-Extra: authz
33
+ Requires-Dist: python3_commons[database]; extra == "authz"
34
+ Requires-Dist: python3_commons[api-client]; extra == "authz"
35
+ Provides-Extra: cache
36
+ Requires-Dist: valkey[libvalkey]~=6.1.1; extra == "cache"
37
+ Provides-Extra: database
38
+ Requires-Dist: asyncpg~=0.31.0; extra == "database"
39
+ Requires-Dist: SQLAlchemy[asyncio]~=2.0.51; extra == "database"
40
+ Provides-Extra: object-storage
41
+ Requires-Dist: aiobotocore~=3.7.0; extra == "object-storage"
42
+ Requires-Dist: object-storage-client~=0.0.38; extra == "object-storage"
43
+ Provides-Extra: soap-client
44
+ Requires-Dist: aiohttp[speedups]<3.15.0,>=3.14.1; extra == "soap-client"
45
+ Requires-Dist: certifi~=2026.6.17; extra == "soap-client"
46
+ Requires-Dist: lxml~=6.1.1; extra == "soap-client"
47
+ Requires-Dist: requests~=2.34.2; extra == "soap-client"
48
+ Requires-Dist: zeep[async]~=4.3.3; extra == "soap-client"
49
+ Dynamic: license-file
50
+
51
+ Re-usable Python3 code
52
+ ======================
53
+
54
+ Re-usable code for Python 3 projects.
55
+
56
+ Installation
57
+ ------------
58
+
59
+ .. code-block:: bash
60
+
61
+ uv add python3-commons
62
+
63
+ Optional Dependencies
64
+ ~~~~~~~~~~~~~~~~~~~~~
65
+
66
+ Some features require extra dependencies. You can install them individually or all at once:
67
+
68
+ - ``api-client``: For ``python3_commons.api_client``
69
+ - ``audit``: For ``python3_commons.audit``
70
+ - ``authn``: For ``python3_commons.auth``
71
+ - ``authz``: For ``python3_commons.permissions``
72
+ - ``cache``: For ``python3_commons.cache``
73
+ - ``database``: For ``python3_commons.db``
74
+ - ``object-storage``: For ``python3_commons.object_storage``
75
+ - ``soap-client``: For ``python3_commons.soap_client``
76
+ - ``all``: Install all optional dependencies
77
+
78
+ .. code-block:: bash
79
+
80
+ uv add "python3-commons[all]"
81
+
82
+ Features
83
+ --------
84
+
85
+ Async LRU Cache
86
+ ~~~~~~~~~~~~~~~
87
+
88
+ An LRU cache decorator for asynchronous functions that handles ``await`` and prevents the dogpile effect (mitigating multiple concurrent calls for the same key by waiting for the first one to finish).
89
+
90
+ .. code-block:: python
91
+
92
+ from python3_commons.async_functools import async_lru_cache
93
+
94
+ @async_lru_cache(maxsize=128)
95
+ async def fetch_expensive_data(item_id: int):
96
+ # This function will only be called once for a given item_id
97
+ # even if multiple tasks await it simultaneously.
98
+ return await db.get(item_id)
99
+
100
+ # Usage
101
+ result = await fetch_expensive_data(42)
102
+
103
+ API Client
104
+ ~~~~~~~~~~
105
+
106
+ A context manager for ``aiohttp`` requests with built-in audit logging to S3 and standardized error mapping to Python exceptions.
107
+
108
+ .. code-block:: python
109
+
110
+ from python3_commons.api_client import request
111
+ from aiohttp import ClientSession
112
+
113
+ async with ClientSession() as session:
114
+ async with request(
115
+ session,
116
+ base_url="https://api.example.com",
117
+ uri="/data",
118
+ method="get",
119
+ audit_name="my_service_audit"
120
+ ) as response:
121
+ data = await response.json()
122
+
123
+ SOAP Client
124
+ ~~~~~~~~~~~
125
+
126
+ Async SOAP client support for ``zeep`` with S3 auditing capabilities.
127
+
128
+ .. code-block:: python
129
+
130
+ from python3_commons.soap_client import soap_client
131
+
132
+ async with soap_client("https://example.com/service?wsdl") as client:
133
+ result = await client.service.GetData(id=42)
134
+
135
+ Database Management
136
+ ~~~~~~~~~~~~~~~~~~~
137
+
138
+ SQLAlchemy async engine and session management with pool tuning, health checks, and dynamic query builders.
139
+
140
+ .. code-block:: python
141
+
142
+ from python3_commons.db import AsyncSessionManager
143
+ from python3_commons.conf import DBSettings
144
+
145
+ # Configuration
146
+ configs = {"default": DBSettings(dsn="postgresql+asyncpg://user:pass@localhost/db")}
147
+ manager = AsyncSessionManager(configs)
148
+
149
+ # Usage
150
+ async with manager.get_session_context("default") as session:
151
+ result = await session.execute(...)
152
+
153
+ # Health check
154
+ from python3_commons.db import is_healthy
155
+ await is_healthy(manager.get_engine("default"))
156
+
157
+ Object Storage
158
+ ~~~~~~~~~~~~~~
159
+
160
+ Utilities for async S3 operations using ``aiobotocore``.
161
+
162
+ .. code-block:: python
163
+
164
+ from python3_commons import object_storage
165
+ import io
166
+
167
+ # Upload an object
168
+ await object_storage.put_object(
169
+ bucket_name="my-bucket",
170
+ path="uploads/file.txt",
171
+ data=io.BytesIO(b"Hello World"),
172
+ length=11
173
+ )
174
+
175
+ # Download an object
176
+ content = await object_storage.get_object("my-bucket", "uploads/file.txt")
177
+
178
+ # List objects
179
+ async for obj in object_storage.list_objects("my-bucket", "uploads/"):
180
+ print(obj['Key'])
181
+
182
+ OIDC Authentication
183
+ ~~~~~~~~~~~~~~~~~~~
184
+
185
+ Client for OpenID Connect authentication, supporting configuration fetching, JWKS, and token acquisition.
186
+
187
+ .. code-block:: python
188
+
189
+ from python3_commons.auth import OIDCClient
190
+ from pydantic import HttpUrl
191
+
192
+ client = OIDCClient(
193
+ authority_url=HttpUrl("https://auth.example.com/realms/myrealm"),
194
+ client_id="my-app-client",
195
+ client_secret="secret"
196
+ )
197
+
198
+ async with client:
199
+ token_response = await client.fetch_token(username="user", password="password")
200
+ print(token_response.access_token)
201
+
202
+ Valkey Cache
203
+ ~~~~~~~~~~~~
204
+
205
+ Async caching using Valkey (Redis-compatible) with automatic Msgpack serialization for complex types.
206
+
207
+ .. code-block:: python
208
+
209
+ from python3_commons import cache
210
+
211
+ # Store a dictionary
212
+ await cache.store("user:123", {"name": "Alice", "role": "admin"}, ttl=3600)
213
+
214
+ # Retrieve it
215
+ user_data = await cache.get("user:123")
216
+
217
+ # Set operations
218
+ await cache.add_set_item("active_users", "user:123")
219
+ is_active = await cache.has_set_item("active_users", "user:123")
220
+
221
+ Structured Logging
222
+ ~~~~~~~~~~~~~~~~~~
223
+
224
+ A ``JSONFormatter`` for structured logging, compatible with standard Python ``logging``.
225
+
226
+ .. code-block:: python
227
+
228
+ import logging
229
+ from python3_commons.log.formatters import JSONFormatter
230
+
231
+ handler = logging.StreamHandler()
232
+ handler.setFormatter(JSONFormatter())
233
+ logging.getLogger().addHandler(handler)
234
+
235
+ logger = logging.getLogger("app")
236
+ logger.info("User logged in", extra={"user_id": "abc-123", "ip": "1.2.3.4"})
237
+
238
+ Serialization
239
+ ~~~~~~~~~~~~~
240
+
241
+ Enhanced JSON and Msgpack serialization for types not handled by default (Decimal, datetime, date, dataclasses, Pydantic models).
242
+
243
+ .. code-block:: python
244
+
245
+ from python3_commons.serializers.msgspec import serialize_msgpack, deserialize_msgpack
246
+ from decimal import Decimal
247
+ from datetime import datetime
248
+
249
+ data = {
250
+ "amount": Decimal("150.75"),
251
+ "timestamp": datetime.now(),
252
+ "tags": {"finance", "internal"}
253
+ }
254
+
255
+ # Serialize to Msgpack
256
+ binary = serialize_msgpack(data)
257
+
258
+ # Deserialize back
259
+ restored = deserialize_msgpack(binary)
260
+
261
+ RBAC Permissions
262
+ ~~~~~~~~~~~~~~~~
263
+
264
+ Database-backed Role-Based Access Control (RBAC) permission checking.
265
+
266
+ .. code-block:: python
267
+
268
+ from python3_commons.permissions import has_user_permission
269
+ from uuid import UUID
270
+
271
+ user_uuid = UUID("...")
272
+ allowed = await has_user_permission(session, user_uuid, "reports.view")
273
+
274
+ General Helpers
275
+ ~~~~~~~~~~~~~~~
276
+
277
+ A collection of useful utility functions:
278
+
279
+ - ``to_snake_case(text)``: Converts strings to snake_case.
280
+ - ``round_decimal(value, places)``: Rounds ``Decimal`` values.
281
+ - ``tries(n)``: An async retry decorator.
282
+ - ``log_execution_time``: An async decorator to log how long a function takes.
283
+ - ``date_from_string`` / ``datetime_from_string``: Flexible date/time parsing.
284
+ - ``request_to_curl``: Converts request parameters to a ``curl`` command string.
285
+
286
+ .. code-block:: python
287
+
288
+ from python3_commons.helpers import tries, log_execution_time
289
+
290
+ @tries(3)
291
+ @log_execution_time
292
+ async def flaky_network_call():
293
+ ...
294
+
295
+ Async CSV Stream
296
+ ~~~~~~~~~~~~~~~~
297
+
298
+ Efficiently generate CSV data as a byte stream from an async generator of tuples.
299
+
300
+ .. code-block:: python
301
+
302
+ from python3_commons.generators import tuple_csv_stream
303
+
304
+ async def generate_rows():
305
+ for i in range(1000):
306
+ yield (i, f"Name {i}", 10.5 * i)
307
+
308
+ async for chunk in tuple_csv_stream(generate_rows(), header=("ID", "Name", "Value")):
309
+ # Send chunk to HTTP response or write to file
310
+ pass
@@ -12,7 +12,7 @@ python3_commons/helpers.py,sha256=WTPu_jIOGYtxt1GYnH6sBMNDHry99AKv6ZaggGW2l1A,49
12
12
  python3_commons/object_storage.py,sha256=2l8v1mDB5WWN6jhxH2t5xmHBXaVPRJl0z44wExmlO80,7175
13
13
  python3_commons/permissions.py,sha256=gaMKSWg0MgPQTdP1voll4ItXcblXku9BlD0Lq3Xv64U,1724
14
14
  python3_commons/soap_client.py,sha256=w2lOyhhtkhwiNnHjvir8DfjGBO51XVg5rlDHyuudU2A,7169
15
- python3_commons/db/__init__.py,sha256=umxg70BJ9Vf7Joj9w7sr5i2EKXeO9TEEp2RaAJ4JXx0,10180
15
+ python3_commons/db/__init__.py,sha256=OvRLAKPvOzEId_M6ZAd1-KqGJJXxsDKjD6xUiRG6ilU,10187
16
16
  python3_commons/db/helpers.py,sha256=xRpWs4aVkBge6HCLjb6OLSnWc1nlV6rKGyaVhZ_x12w,2001
17
17
  python3_commons/db/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
18
  python3_commons/db/models/auth.py,sha256=t6z_0c7l_1eB-CCdnPSV9OzJ_ymgkwD1cLpecNhQgoA,773
@@ -27,9 +27,11 @@ python3_commons/serializers/common.py,sha256=VkA7C6wODvHk0QBXVX_x2JieDstihx3U__U
27
27
  python3_commons/serializers/json.py,sha256=UPkC3ps13x2C_NxwVV-K7Ewp4VjkVHSSUkJVw5k7Wiw,712
28
28
  python3_commons/serializers/msgpack.py,sha256=zESFBX34GsZ8rDu6Zk5V6CLT6P0mPilU0r04Ka6TblI,1474
29
29
  python3_commons/serializers/msgspec.py,sha256=upy5CBmK66-8hYnK5bAM_sZvZY5CAqZmzCw9GIF346I,2988
30
- python3_commons-0.22.4.dist-info/licenses/AUTHORS.rst,sha256=3R9JnfjfjH5RoPWOeqKFJgxVShSSfzQPIrEr1nxIo9Q,90
31
- python3_commons-0.22.4.dist-info/licenses/LICENSE,sha256=xxILuojHm4fKQOrMHPSslbyy6WuKAN2RiG74HbrYfzM,34575
32
- python3_commons-0.22.4.dist-info/METADATA,sha256=fKsEnrtBHhgaKa1X58WPpgQSJSWVsqJsDRDQSdoylwk,2333
33
- python3_commons-0.22.4.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
34
- python3_commons-0.22.4.dist-info/top_level.txt,sha256=lJI6sCBf68eUHzupCnn2dzG10lH3jJKTWM_hrN1cQ7M,16
35
- python3_commons-0.22.4.dist-info/RECORD,,
30
+ python3_commons-0.22.6.dist-info/licenses/AUTHORS.rst,sha256=3R9JnfjfjH5RoPWOeqKFJgxVShSSfzQPIrEr1nxIo9Q,90
31
+ python3_commons-0.22.6.dist-info/licenses/LICENSE,sha256=xxILuojHm4fKQOrMHPSslbyy6WuKAN2RiG74HbrYfzM,34575
32
+ python3_commons-0.22.6.dist-info/METADATA,sha256=bkTFbeGhsCPx_RGutoUS8bG-bQIn7Vx2BEUO2lIom3A,9472
33
+ python3_commons-0.22.6.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
34
+ python3_commons-0.22.6.dist-info/scm_file_list.json,sha256=Hjo_NQrKMIL3RWUjLVGGDP57Rok5PvQpVvbIVYHC2Nw,2518
35
+ python3_commons-0.22.6.dist-info/scm_version.json,sha256=bcBml9-5V4SzNVw8Ro8CgsqejWvP-uNAqCzpJKsPFKQ,161
36
+ python3_commons-0.22.6.dist-info/top_level.txt,sha256=lJI6sCBf68eUHzupCnn2dzG10lH3jJKTWM_hrN1cQ7M,16
37
+ python3_commons-0.22.6.dist-info/RECORD,,
@@ -0,0 +1,75 @@
1
+ {
2
+ "files": [
3
+ ".coveragerc",
4
+ ".pre-commit-config.yaml",
5
+ "README.md",
6
+ "uv.lock",
7
+ ".python-version",
8
+ "LICENSE",
9
+ "pyproject.toml",
10
+ "CHANGELOG.rst",
11
+ ".env_template",
12
+ "README.rst",
13
+ ".gitignore",
14
+ "AUTHORS.rst",
15
+ ".devcontainer/Dockerfile",
16
+ ".devcontainer/docker-compose.yml",
17
+ ".devcontainer/devcontainer.json",
18
+ "docs/Makefile",
19
+ "docs/license.rst",
20
+ "docs/authors.rst",
21
+ "docs/index.rst",
22
+ "docs/changelog.rst",
23
+ "docs/conf.py",
24
+ "docs/_static/.gitignore",
25
+ "src/python3_commons/helpers.py",
26
+ "src/python3_commons/soap_client.py",
27
+ "src/python3_commons/api_client.py",
28
+ "src/python3_commons/__init__.py",
29
+ "src/python3_commons/exceptions.py",
30
+ "src/python3_commons/object_storage.py",
31
+ "src/python3_commons/permissions.py",
32
+ "src/python3_commons/auth.py",
33
+ "src/python3_commons/audit.py",
34
+ "src/python3_commons/cache.py",
35
+ "src/python3_commons/fs.py",
36
+ "src/python3_commons/conf.py",
37
+ "src/python3_commons/async_functools.py",
38
+ "src/python3_commons/generators.py",
39
+ "src/python3_commons/db/helpers.py",
40
+ "src/python3_commons/db/__init__.py",
41
+ "src/python3_commons/db/models/rbac.py",
42
+ "src/python3_commons/db/models/__init__.py",
43
+ "src/python3_commons/db/models/users.py",
44
+ "src/python3_commons/db/models/auth.py",
45
+ "src/python3_commons/db/models/common.py",
46
+ "src/python3_commons/log/__init__.py",
47
+ "src/python3_commons/log/filters.py",
48
+ "src/python3_commons/log/formatters.py",
49
+ "src/python3_commons/serializers/__init__.py",
50
+ "src/python3_commons/serializers/msgpack.py",
51
+ "src/python3_commons/serializers/json.py",
52
+ "src/python3_commons/serializers/msgspec.py",
53
+ "src/python3_commons/serializers/common.py",
54
+ "tests/__init__.py",
55
+ "tests/integration/__init__.py",
56
+ "tests/integration/test_auth.py",
57
+ "tests/integration/conftest.py",
58
+ "tests/integration/test_osc.py",
59
+ "tests/integration/test_cache.py",
60
+ "tests/unit/__init__.py",
61
+ "tests/unit/test_helpers.py",
62
+ "tests/unit/test_async_functools.py",
63
+ "tests/unit/test_msgspec.py",
64
+ "tests/unit/conftest.py",
65
+ "tests/unit/test_audit.py",
66
+ "tests/unit/test_msgpack.py",
67
+ "tests/unit/db/__init__.py",
68
+ "tests/unit/db/test_async_session_manager.py",
69
+ "tests/unit/log/__init__.py",
70
+ "tests/unit/log/test_formatters.py",
71
+ ".github/workflows/release-on-tag-push.yml",
72
+ ".github/workflows/python-publish.yaml",
73
+ ".github/workflows/checks.yml"
74
+ ]
75
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "tag": "0.22.6",
3
+ "distance": 0,
4
+ "node": "gd66dd259ee583bc3a586ae4be41f9f159169f450",
5
+ "dirty": false,
6
+ "branch": "HEAD",
7
+ "node_date": "2026-06-26"
8
+ }
@@ -1,54 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: python3-commons
3
- Version: 0.22.4
4
- Summary: Re-usable Python3 code
5
- Author-email: Oleg Korsak <kamikaze.is.waiting.you@gmail.com>
6
- License-Expression: GPL-3.0
7
- Project-URL: Homepage, https://github.com/kamikaze/python3-commons
8
- Project-URL: Documentation, https://github.com/kamikaze/python3-commons/wiki
9
- Classifier: Development Status :: 4 - Beta
10
- Classifier: Programming Language :: Python
11
- Requires-Python: <3.15.0,>=3.14.0
12
- Description-Content-Type: text/x-rst
13
- License-File: LICENSE
14
- License-File: AUTHORS.rst
15
- Requires-Dist: msgpack~=1.2.0
16
- Requires-Dist: msgspec~=0.21.1
17
- Requires-Dist: pydantic-settings~=2.14.1
18
- Provides-Extra: all
19
- Requires-Dist: python3_commons[api-client,audit,authn,authz,cache,database,object-storage,soap-client]; extra == "all"
20
- Provides-Extra: api-client
21
- Requires-Dist: aiohttp[speedups]<3.15.0,>=3.14.1; extra == "api-client"
22
- Requires-Dist: certifi~=2026.5.20; extra == "api-client"
23
- Requires-Dist: python3_commons[audit,object-storage]; extra == "api-client"
24
- Provides-Extra: audit
25
- Requires-Dist: lxml~=6.1.1; extra == "audit"
26
- Requires-Dist: zeep[async]~=4.3.2; extra == "audit"
27
- Requires-Dist: python3_commons[object-storage]; extra == "audit"
28
- Provides-Extra: authn
29
- Requires-Dist: aiohttp[speedups]<3.15.0,>=3.14.1; extra == "authn"
30
- Requires-Dist: certifi~=2026.5.20; extra == "authn"
31
- Requires-Dist: python3_commons[api-client]; extra == "authn"
32
- Provides-Extra: authz
33
- Requires-Dist: python3_commons[database]; extra == "authz"
34
- Requires-Dist: python3_commons[api-client]; extra == "authz"
35
- Provides-Extra: cache
36
- Requires-Dist: valkey[libvalkey]~=6.1.1; extra == "cache"
37
- Provides-Extra: database
38
- Requires-Dist: asyncpg~=0.31.0; extra == "database"
39
- Requires-Dist: SQLAlchemy[asyncio]~=2.0.50; extra == "database"
40
- Provides-Extra: object-storage
41
- Requires-Dist: aiobotocore~=3.7.0; extra == "object-storage"
42
- Requires-Dist: object-storage-client~=0.0.37; extra == "object-storage"
43
- Provides-Extra: soap-client
44
- Requires-Dist: aiohttp[speedups]<3.15.0,>=3.14.1; extra == "soap-client"
45
- Requires-Dist: certifi~=2026.5.20; extra == "soap-client"
46
- Requires-Dist: lxml~=6.1.1; extra == "soap-client"
47
- Requires-Dist: requests~=2.34.2; extra == "soap-client"
48
- Requires-Dist: zeep[async]~=4.3.2; extra == "soap-client"
49
- Dynamic: license-file
50
-
51
- Re-usable Python3 code
52
- ======================
53
-
54
- Some description here