plexus-python-pulse-common 1.1.88__py3-none-any.whl → 1.1.89__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.
@@ -1,3 +1,4 @@
1
+ from collections.abc import Callable, Mapping
1
2
  from contextlib import AbstractContextManager
2
3
 
3
4
  import sqlalchemy.orm as sa_orm
@@ -5,14 +6,21 @@ from fastapi import HTTPException
5
6
  from plexus.common.utils import logger
6
7
 
7
8
  __all__ = [
8
- "managed_db_session",
9
+ "database_transaction",
9
10
  ]
10
11
 
11
12
 
12
- class DBSessionExceptionManager(AbstractContextManager):
13
- def __init__(self, db: sa_orm.Session, commit_on_exit: bool):
13
+ class DBTransactionManager(AbstractContextManager):
14
+ def __init__(
15
+ self,
16
+ db: sa_orm.Session,
17
+ *,
18
+ commit_on_exit: bool,
19
+ redirect: Mapping[type[Exception], Callable[[Exception], HTTPException]] | None = None,
20
+ ):
14
21
  self.db = db
15
22
  self.commit_on_exit = commit_on_exit
23
+ self.redirect = redirect
16
24
 
17
25
  def __enter__(self):
18
26
  return self
@@ -39,9 +47,18 @@ class DBSessionExceptionManager(AbstractContextManager):
39
47
  if isinstance(exc_value, HTTPException) or not isinstance(exc_value, Exception):
40
48
  return
41
49
 
50
+ for exception_type, make_response in (self.redirect or {}).items():
51
+ if isinstance(exc_value, exception_type):
52
+ raise make_response(exc_value) from exc_value
53
+
42
54
  logger.error("Database session failed", exc_info=(type(exc_value), exc_value, exc_value.__traceback__))
43
55
  raise HTTPException(status_code=500, detail="Internal server error") from exc_value
44
56
 
45
57
 
46
- def managed_db_session(db: sa_orm.Session, commit_on_exit: bool = True) -> DBSessionExceptionManager:
47
- return DBSessionExceptionManager(db, commit_on_exit)
58
+ def database_transaction(
59
+ db: sa_orm.Session,
60
+ *,
61
+ commit_on_exit: bool = True,
62
+ redirect: Mapping[type[Exception], Callable[[Exception], HTTPException]] | None = None,
63
+ ) -> DBTransactionManager:
64
+ return DBTransactionManager(db, commit_on_exit=commit_on_exit, redirect=redirect)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: plexus-python-pulse-common
3
- Version: 1.1.88
3
+ Version: 1.1.89
4
4
  Classifier: Programming Language :: Python :: 3
5
5
  Classifier: Programming Language :: Python :: 3.12
6
6
  Classifier: Programming Language :: Python :: 3.13
@@ -9,7 +9,7 @@ plexus/pulse/common/resources/tags/__init__.py,sha256=1BmOq9aIT2Fr4YTzDwTrYySDhi
9
9
  plexus/pulse/common/resources/tags/unittest-1.0.0.tagset.yaml,sha256=8zWjsYp3OxGfBJSiS6-r08q61Jhulol65iV5-pvF1-M,5338
10
10
  plexus/pulse/common/resources/tags/universal-1.0.0.tagset.yaml,sha256=PRxyS5xwWcNwUjAbYqN-SafLZVviE-QSzU-DXyXNffw,80736
11
11
  plexus/pulse/common/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
- plexus/pulse/common/utils/apiutils.py,sha256=AEGyzteSJrCq13EvQO07xDs3rROMSvG6OaFdj7swFdA,1530
12
+ plexus/pulse/common/utils/apiutils.py,sha256=dBK4QZLfBPDofaSfjmCUB-1WInL5jGyh2bf0ij8pV5c,2073
13
13
  plexus/pulse/common/utils/bagutils.py,sha256=JcTD-9xJAhV_BYXw-Zfa_85hkyO5wUVqTqw2eMJFgVI,13116
14
14
  plexus/pulse/common/utils/config.py,sha256=YPtFK5QHTVsXwcFBGV0rlZad0GFtsgwysQNyKYjdzvk,2409
15
15
  plexus/pulse/common/utils/datautils.py,sha256=OWlnyFSa6UaPbSSG3HzCJHxdiHyfHAE-FUVHaLfNZoM,9335
@@ -24,7 +24,7 @@ plexus/pulse/common/utils/sqlutils.py,sha256=D6kTBjhO5YlNRt3uFlPt6z3uH61m9ajEzPY
24
24
  plexus/pulse/common/utils/strutils.py,sha256=8kl88-u89FApoaq-D2ulk5DNl9kenP6vqksvVJ0iH4s,17325
25
25
  plexus/pulse/common/utils/tagutils.py,sha256=V8eUxtWSijX6nHSCuRX2dTJz3why3TCOzPVK5YXt28s,66944
26
26
  plexus/pulse/common/utils/testutils.py,sha256=hKPRvoN4mg8QGQJH121l59a0uo8jN5rntGQAVYplGBw,6166
27
- plexus_python_pulse_common-1.1.88.dist-info/METADATA,sha256=bNGz7B5jj23hayiYBRvtfRpfnsD21flHjbg-sgdiKMY,1555
28
- plexus_python_pulse_common-1.1.88.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
29
- plexus_python_pulse_common-1.1.88.dist-info/top_level.txt,sha256=ug_g7CVwaMQuas5UzAXbHUrQvKGCn8ezc6ZNvvRlJOE,7
30
- plexus_python_pulse_common-1.1.88.dist-info/RECORD,,
27
+ plexus_python_pulse_common-1.1.89.dist-info/METADATA,sha256=yJzCWy2CxaYx3yvYUMoozkglqdmkoWukEWDRyWsaUUA,1555
28
+ plexus_python_pulse_common-1.1.89.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
29
+ plexus_python_pulse_common-1.1.89.dist-info/top_level.txt,sha256=ug_g7CVwaMQuas5UzAXbHUrQvKGCn8ezc6ZNvvRlJOE,7
30
+ plexus_python_pulse_common-1.1.89.dist-info/RECORD,,