arpakitlib 1.8.154__py3-none-any.whl → 1.8.156__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.
- arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/util/execute_func_with_story_log.py +61 -44
- arpakitlib/ar_sqlalchemy_util.py +14 -0
- {arpakitlib-1.8.154.dist-info → arpakitlib-1.8.156.dist-info}/METADATA +1 -1
- {arpakitlib-1.8.154.dist-info → arpakitlib-1.8.156.dist-info}/RECORD +7 -7
- {arpakitlib-1.8.154.dist-info → arpakitlib-1.8.156.dist-info}/LICENSE +0 -0
- {arpakitlib-1.8.154.dist-info → arpakitlib-1.8.156.dist-info}/WHEEL +0 -0
- {arpakitlib-1.8.154.dist-info → arpakitlib-1.8.156.dist-info}/entry_points.txt +0 -0
arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/util/execute_func_with_story_log.py
CHANGED
@@ -3,17 +3,29 @@ import inspect
|
|
3
3
|
import logging
|
4
4
|
from typing import Any
|
5
5
|
|
6
|
+
from pydantic import BaseModel, ConfigDict
|
6
7
|
from sqlalchemy.ext.asyncio import AsyncSession
|
7
8
|
from sqlalchemy.orm import Session
|
8
9
|
|
9
10
|
from arpakitlib.ar_exception_util import exception_to_traceback_str
|
10
|
-
from project.core.util import setup_logging
|
11
11
|
from project.sqlalchemy_db_.sqlalchemy_db import get_cached_sqlalchemy_db
|
12
12
|
from project.sqlalchemy_db_.sqlalchemy_model import StoryLogDBM
|
13
13
|
|
14
14
|
_logger = logging.getLogger(__name__)
|
15
15
|
|
16
16
|
|
17
|
+
class ExecuteWithStoryLogRes(BaseModel):
|
18
|
+
func_res: Any = None
|
19
|
+
story_log_dbm: StoryLogDBM | None = None
|
20
|
+
exception: Exception | None = None
|
21
|
+
|
22
|
+
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True, from_attributes=True)
|
23
|
+
|
24
|
+
def raise_if_exception(self):
|
25
|
+
if self.exception is not None:
|
26
|
+
raise self.exception
|
27
|
+
|
28
|
+
|
17
29
|
def sync_execute_with_story_log(
|
18
30
|
func,
|
19
31
|
*,
|
@@ -24,126 +36,131 @@ def sync_execute_with_story_log(
|
|
24
36
|
story_log_type: str = StoryLogDBM.Types.error_in_execute_with_story_log,
|
25
37
|
story_log_title: str | None = None,
|
26
38
|
story_log_extra_data: dict[Any, Any] | None = None,
|
27
|
-
|
39
|
+
raise_if_exception: bool = False
|
40
|
+
) -> ExecuteWithStoryLogRes:
|
28
41
|
if func_args is None:
|
29
42
|
func_args = tuple()
|
30
43
|
if func_kwargs is None:
|
31
44
|
func_kwargs = {}
|
32
45
|
if story_log_extra_data is None:
|
33
46
|
story_log_extra_data = {}
|
47
|
+
story_log_extra_data = story_log_extra_data.copy()
|
48
|
+
|
49
|
+
execute_with_story_log_res = ExecuteWithStoryLogRes()
|
34
50
|
|
35
51
|
try:
|
36
|
-
|
52
|
+
execute_with_story_log_res.func_res = func(*func_args, **func_kwargs)
|
53
|
+
return execute_with_story_log_res
|
37
54
|
except Exception as exception:
|
38
55
|
_logger.error(f"Error in {func.__name__}", exc_info=True)
|
39
56
|
|
57
|
+
execute_with_story_log_res.exception = exception
|
58
|
+
|
40
59
|
if story_log_title is None:
|
41
60
|
story_log_title = f"Error in func {func.__name__}: {type(exception).__name__}: {exception}"
|
42
61
|
story_log_extra_data.update({
|
43
62
|
"exception": str(exception),
|
44
63
|
"exception_traceback": exception_to_traceback_str(exception=exception),
|
45
64
|
"exception_type_name": type(exception).__name__,
|
46
|
-
"
|
65
|
+
"wrapper_func_name": inspect.currentframe().f_code.co_name,
|
66
|
+
"called_func_name": func.__name__
|
47
67
|
})
|
48
68
|
|
49
69
|
if session_ is not None:
|
50
|
-
story_log_dbm = StoryLogDBM(
|
70
|
+
execute_with_story_log_res.story_log_dbm = StoryLogDBM(
|
51
71
|
level=story_log_level,
|
52
72
|
type=story_log_type,
|
53
73
|
title=story_log_title,
|
54
74
|
extra_data=story_log_extra_data
|
55
75
|
)
|
56
|
-
session_.add(story_log_dbm)
|
76
|
+
session_.add(execute_with_story_log_res.story_log_dbm)
|
57
77
|
session_.commit()
|
58
78
|
else:
|
59
79
|
with get_cached_sqlalchemy_db().new_session() as session:
|
60
|
-
story_log_dbm = StoryLogDBM(
|
80
|
+
execute_with_story_log_res.story_log_dbm = StoryLogDBM(
|
61
81
|
level=story_log_level,
|
62
82
|
type=story_log_type,
|
63
83
|
title=story_log_title,
|
64
84
|
extra_data=story_log_extra_data
|
65
85
|
)
|
66
|
-
session.add(story_log_dbm)
|
86
|
+
session.add(execute_with_story_log_res.story_log_dbm)
|
67
87
|
session.commit()
|
68
88
|
|
69
|
-
|
89
|
+
if raise_if_exception:
|
90
|
+
raise
|
91
|
+
|
92
|
+
return execute_with_story_log_res
|
70
93
|
|
71
94
|
|
72
95
|
async def async_execute_with_story_log(
|
73
|
-
|
96
|
+
async_func,
|
74
97
|
*,
|
75
|
-
|
76
|
-
|
98
|
+
async_func_args: tuple[Any] | None = None,
|
99
|
+
async_func_kwargs: dict[Any, Any] | None = None,
|
77
100
|
async_session_: AsyncSession | None = None,
|
78
101
|
story_log_level: str = StoryLogDBM.Levels.error,
|
79
102
|
story_log_type: str = StoryLogDBM.Types.error_in_execute_with_story_log,
|
80
103
|
story_log_title: str | None = None,
|
81
104
|
story_log_extra_data: dict[Any, Any] | None = None,
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
105
|
+
raise_if_exception: bool = False
|
106
|
+
) -> ExecuteWithStoryLogRes:
|
107
|
+
if async_func_args is None:
|
108
|
+
async_func_args = ()
|
109
|
+
if async_func_kwargs is None:
|
110
|
+
async_func_kwargs = {}
|
87
111
|
if story_log_extra_data is None:
|
88
112
|
story_log_extra_data = {}
|
113
|
+
story_log_extra_data = story_log_extra_data.copy()
|
114
|
+
|
115
|
+
execute_with_story_log_res = ExecuteWithStoryLogRes()
|
89
116
|
|
90
117
|
try:
|
91
|
-
|
118
|
+
execute_with_story_log_res.func_res = await async_func(*async_func_args, **async_func_kwargs)
|
119
|
+
return execute_with_story_log_res
|
92
120
|
except Exception as exception:
|
93
|
-
_logger.error(f"Async error in {
|
121
|
+
_logger.error(f"Async error in {async_func.__name__}", exc_info=True)
|
122
|
+
|
123
|
+
execute_with_story_log_res.exception = exception
|
94
124
|
|
95
125
|
if story_log_title is None:
|
96
|
-
story_log_title = f"Async error in func {
|
126
|
+
story_log_title = f"Async error in func {async_func.__name__}: {type(exception).__name__}: {exception}"
|
97
127
|
|
98
128
|
story_log_extra_data.update({
|
99
129
|
"exception": str(exception),
|
100
130
|
"exception_traceback": exception_to_traceback_str(exception=exception),
|
101
131
|
"exception_type_name": type(exception).__name__,
|
102
|
-
"
|
132
|
+
"wrapper_func_name": inspect.currentframe().f_code.co_name,
|
133
|
+
"called_func_name": async_func.__name__
|
103
134
|
})
|
104
135
|
|
105
136
|
if async_session_ is not None:
|
106
|
-
story_log_dbm = StoryLogDBM(
|
137
|
+
execute_with_story_log_res.story_log_dbm = StoryLogDBM(
|
107
138
|
level=story_log_level,
|
108
139
|
type=story_log_type,
|
109
140
|
title=story_log_title,
|
110
141
|
extra_data=story_log_extra_data
|
111
142
|
)
|
112
|
-
async_session_.add(story_log_dbm)
|
143
|
+
async_session_.add(execute_with_story_log_res.story_log_dbm)
|
113
144
|
await async_session_.commit()
|
114
145
|
else:
|
115
146
|
async with get_cached_sqlalchemy_db().new_async_session() as async_session:
|
116
|
-
story_log_dbm = StoryLogDBM(
|
147
|
+
execute_with_story_log_res.story_log_dbm = StoryLogDBM(
|
117
148
|
level=story_log_level,
|
118
149
|
type=story_log_type,
|
119
150
|
title=story_log_title,
|
120
151
|
extra_data=story_log_extra_data
|
121
152
|
)
|
122
|
-
async_session.add(story_log_dbm)
|
153
|
+
async_session.add(execute_with_story_log_res.story_log_dbm)
|
123
154
|
await async_session.commit()
|
124
155
|
|
125
|
-
|
156
|
+
if raise_if_exception:
|
157
|
+
raise
|
126
158
|
|
159
|
+
return execute_with_story_log_res
|
127
160
|
|
128
|
-
async def __async_example():
|
129
|
-
setup_logging()
|
130
|
-
|
131
|
-
def hello_world():
|
132
|
-
print(inspect.currentframe().f_code.co_name)
|
133
|
-
# raise ValueError("1111111")
|
134
|
-
|
135
|
-
print(hello_world())
|
136
161
|
|
137
|
-
|
138
|
-
|
139
|
-
# raise ValueError("1111111")
|
140
|
-
|
141
|
-
# sync_execute_with_story_log(
|
142
|
-
# func=hello_world
|
143
|
-
# )
|
144
|
-
# await async_execute_with_story_log(
|
145
|
-
# func=async_hello_world
|
146
|
-
# )
|
162
|
+
async def __async_example():
|
163
|
+
pass
|
147
164
|
|
148
165
|
|
149
166
|
if __name__ == '__main__':
|
arpakitlib/ar_sqlalchemy_util.py
CHANGED
@@ -220,6 +220,20 @@ class SQLAlchemyDb:
|
|
220
220
|
self.remove_celery_tables_data()
|
221
221
|
self._logger.info("all reinited")
|
222
222
|
|
223
|
+
def remove_rows_from_tables(self):
|
224
|
+
with self.new_session() as session:
|
225
|
+
for table_name, table in BaseDBM.metadata.tables.items():
|
226
|
+
session.execute(sqlalchemy.delete(table))
|
227
|
+
session.commit()
|
228
|
+
self._logger.info(f"rows from tables ({BaseDBM.metadata.tables.keys()}) were removed")
|
229
|
+
|
230
|
+
async def async_remove_rows_from_tables(self):
|
231
|
+
async with self.new_async_session() as async_session:
|
232
|
+
for table_name, table in BaseDBM.metadata.tables.items():
|
233
|
+
await async_session.execute(sqlalchemy.delete(table))
|
234
|
+
await async_session.commit()
|
235
|
+
self._logger.info(f"rows from tables ({BaseDBM.metadata.tables.keys()}) were removed")
|
236
|
+
|
223
237
|
def check_conn(self):
|
224
238
|
self.engine.connect()
|
225
239
|
self._logger.info("db conn is good")
|
@@ -263,7 +263,7 @@ arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/sqlalchemy_model
|
|
263
263
|
arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/sqlalchemy_model/user_token.py,sha256=hX6nHvVvhg5YB3vCjsku4TDoY5Jk1FsuDu5elbcF7VE,1886
|
264
264
|
arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/sqlalchemy_model/verification_code.py,sha256=xrrNPXn5PeCnZ2VWVBdXDUft83EEYtc6b4pvs0oh1uw,3078
|
265
265
|
arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/util/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
266
|
-
arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/util/execute_func_with_story_log.py,sha256=
|
266
|
+
arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/util/execute_func_with_story_log.py,sha256=HrDlG8Y1gbMNVIuDHvCndXbnp8vxc8T8l-_XBVn7zR8,6091
|
267
267
|
arpakitlib/_arpakit_project_template_v_5/project/test_data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
268
268
|
arpakitlib/_arpakit_project_template_v_5/project/test_data/make_test_api_keys.py,sha256=9F2bMfymaqx_Czh_tF945BKpKqNrVNjSIfCQl13Dkxw,911
|
269
269
|
arpakitlib/_arpakit_project_template_v_5/project/test_data/make_test_data_1.py,sha256=F7vCmfWjItw5QzYR7_OAPSHdJt9louuvqXvcfOkU0AU,2246
|
@@ -402,13 +402,13 @@ arpakitlib/ar_schedule_uust_api_client_util.py,sha256=rXI2_3OTaIBgR-GixM1Ti-Ue1f
|
|
402
402
|
arpakitlib/ar_settings_util.py,sha256=Y5wi_cmsjDjfJpM0VJHjbo0NoVPKfypKaD1USowwDtQ,1327
|
403
403
|
arpakitlib/ar_sleep_util.py,sha256=ggaj7ML6QK_ADsHMcyu6GUmUpQ_9B9n-SKYH17h-9lM,1045
|
404
404
|
arpakitlib/ar_sqladmin_util.py,sha256=Jd33EX_fAso4bvrcDhNEYOUlUHkudbadNGOJDg7rqhQ,301
|
405
|
-
arpakitlib/ar_sqlalchemy_util.py,sha256=
|
405
|
+
arpakitlib/ar_sqlalchemy_util.py,sha256=J8AAPT0BgrrRYYkJJn_ezEla6YyF3OL34Q-DNpadziU,11285
|
406
406
|
arpakitlib/ar_ssh_runner_util.py,sha256=yvAwza480MkHKvLkDEsR7JNh2bYNs6P9rCVo4NA85NE,6865
|
407
407
|
arpakitlib/ar_str_util.py,sha256=2lGpnXDf2h1cBZpVf5i1tX_HCv5iBd6IGnrCw4QWWlY,4350
|
408
408
|
arpakitlib/ar_type_util.py,sha256=Cs_tef-Fc5xeyAF54KgISCsP11NHyzIsglm4S3Xx7iM,4049
|
409
409
|
arpakitlib/ar_yookassa_api_client_util.py,sha256=VozuZeCJjmLd1zj2BdC9WfiAQ3XYOrIMsdpNK-AUlm0,5347
|
410
|
-
arpakitlib-1.8.
|
411
|
-
arpakitlib-1.8.
|
412
|
-
arpakitlib-1.8.
|
413
|
-
arpakitlib-1.8.
|
414
|
-
arpakitlib-1.8.
|
410
|
+
arpakitlib-1.8.156.dist-info/LICENSE,sha256=GPEDQMam2r7FSTYqM1mm7aKnxLaWcBotH7UvQtea-ec,11355
|
411
|
+
arpakitlib-1.8.156.dist-info/METADATA,sha256=w7-HsemfipWlnRI-TBdYbQY3yqWzPO7mHUBd3iuBZYI,3706
|
412
|
+
arpakitlib-1.8.156.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
413
|
+
arpakitlib-1.8.156.dist-info/entry_points.txt,sha256=36xqR3PJFT2kuwjkM_EqoIy0qFUDPKSm_mJaI7emewE,87
|
414
|
+
arpakitlib-1.8.156.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|