arpakitlib 1.8.155__py3-none-any.whl → 1.8.157__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.
@@ -0,0 +1,11 @@
1
+ from project.core.util import setup_logging
2
+ from project.sqlalchemy_db_.sqlalchemy_db import get_cached_sqlalchemy_db
3
+
4
+
5
+ def __command():
6
+ setup_logging()
7
+ get_cached_sqlalchemy_db().remove_rows_from_tables()
8
+
9
+
10
+ if __name__ == '__main__':
11
+ __command()
@@ -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,49 +36,60 @@ 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
- ) -> Any:
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
- return func(*func_args, **func_kwargs)
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
- "func_name": inspect.currentframe().f_code.co_name
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
- raise exception
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(
@@ -79,19 +102,26 @@ async def async_execute_with_story_log(
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
- ) -> Any:
105
+ raise_if_exception: bool = False
106
+ ) -> ExecuteWithStoryLogRes:
83
107
  if async_func_args is None:
84
108
  async_func_args = ()
85
109
  if async_func_kwargs is None:
86
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
- return await async_func(*async_func_args, **async_func_kwargs)
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
121
  _logger.error(f"Async error in {async_func.__name__}", exc_info=True)
94
122
 
123
+ execute_with_story_log_res.exception = exception
124
+
95
125
  if story_log_title is None:
96
126
  story_log_title = f"Async error in func {async_func.__name__}: {type(exception).__name__}: {exception}"
97
127
 
@@ -99,51 +129,38 @@ async def async_execute_with_story_log(
99
129
  "exception": str(exception),
100
130
  "exception_traceback": exception_to_traceback_str(exception=exception),
101
131
  "exception_type_name": type(exception).__name__,
102
- "func_name": inspect.currentframe().f_code.co_name
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
- raise exception
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
161
 
135
- print(hello_world())
136
-
137
- # async def async_hello_world():
138
- # print(1)
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__':
@@ -220,6 +220,28 @@ 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, exclude_tables: list[str] | None = None):
224
+ if exclude_tables is None:
225
+ exclude_tables = []
226
+ with self.new_session() as session:
227
+ for table_name, table in BaseDBM.metadata.tables.items():
228
+ if table_name not in exclude_tables:
229
+ session.execute(sqlalchemy.delete(table))
230
+ session.commit()
231
+ removed_tables = [t for t in BaseDBM.metadata.tables.keys() if t not in exclude_tables]
232
+ self._logger.info(f"rows from tables ({removed_tables}) were removed")
233
+
234
+ async def async_remove_rows_from_tables(self, exclude_tables: list[str] | None = None):
235
+ if exclude_tables is None:
236
+ exclude_tables = []
237
+ async with self.new_async_session() as async_session:
238
+ for table_name, table in BaseDBM.metadata.tables.items():
239
+ if table_name not in exclude_tables:
240
+ await async_session.execute(sqlalchemy.delete(table))
241
+ await async_session.commit()
242
+ removed_tables = [t for t in BaseDBM.metadata.tables.keys() if t not in exclude_tables]
243
+ self._logger.info(f"rows from tables ({removed_tables}) were removed")
244
+
223
245
  def check_conn(self):
224
246
  self.engine.connect()
225
247
  self._logger.info("db conn is good")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: arpakitlib
3
- Version: 1.8.155
3
+ Version: 1.8.157
4
4
  Summary: arpakitlib
5
5
  License: Apache-2.0
6
6
  Keywords: arpakitlib,arpakit,arpakit-company,arpakitcompany,arpakit_company
@@ -53,6 +53,7 @@ arpakitlib/_arpakit_project_template_v_5/command/reinit_all_sqlalchemy_db.py,sha
53
53
  arpakitlib/_arpakit_project_template_v_5/command/reinit_json_db.py,sha256=NYY1dPVbJ_mO9aCu8VTiDT7kfloiGvG624hJ_5g2tBM,318
54
54
  arpakitlib/_arpakit_project_template_v_5/command/reinit_sqlalchemy_db.py,sha256=aCoLtUvB_dubMHMGlOFgVzxkhhPAL4lHeMU0LM4CBCs,343
55
55
  arpakitlib/_arpakit_project_template_v_5/command/remove_operations.py,sha256=l1BMAT9kQMy_aZ2ZNc20vTi1xXktmDrJvKDvo85b8xA,326
56
+ arpakitlib/_arpakit_project_template_v_5/command/remove_rows_from_tables_sqlalchemy_db.py,sha256=9XDeYhjdELLq1T7ArskaMjzqpGerYuP38PjOwUFDnqQ,259
56
57
  arpakitlib/_arpakit_project_template_v_5/command/remove_story_logs.py,sha256=l-whsYahKY6ssk-c24ufiybF-U24iFqMdkNlnlASm2U,498
57
58
  arpakitlib/_arpakit_project_template_v_5/command/rm_all_records_in_json_db.py,sha256=qF5c_Aso7XpNcBZOInz7MWXX9Wm8L73rcOZBKGXlFfg,326
58
59
  arpakitlib/_arpakit_project_template_v_5/command/set_tg_bot_commands.py,sha256=6HbuL6acw-U8vkUdqLVIEffe_FcBsDWRKZ90AakZbv8,456
@@ -263,7 +264,7 @@ arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/sqlalchemy_model
263
264
  arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/sqlalchemy_model/user_token.py,sha256=hX6nHvVvhg5YB3vCjsku4TDoY5Jk1FsuDu5elbcF7VE,1886
264
265
  arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/sqlalchemy_model/verification_code.py,sha256=xrrNPXn5PeCnZ2VWVBdXDUft83EEYtc6b4pvs0oh1uw,3078
265
266
  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=TVdhSL7g_L6tW5-DSZTFUJbCQtXlv2Fs_A6PaLZawdQ,5057
267
+ arpakitlib/_arpakit_project_template_v_5/project/sqlalchemy_db_/util/execute_func_with_story_log.py,sha256=HrDlG8Y1gbMNVIuDHvCndXbnp8vxc8T8l-_XBVn7zR8,6091
267
268
  arpakitlib/_arpakit_project_template_v_5/project/test_data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
268
269
  arpakitlib/_arpakit_project_template_v_5/project/test_data/make_test_api_keys.py,sha256=9F2bMfymaqx_Czh_tF945BKpKqNrVNjSIfCQl13Dkxw,911
269
270
  arpakitlib/_arpakit_project_template_v_5/project/test_data/make_test_data_1.py,sha256=F7vCmfWjItw5QzYR7_OAPSHdJt9louuvqXvcfOkU0AU,2246
@@ -402,13 +403,13 @@ arpakitlib/ar_schedule_uust_api_client_util.py,sha256=rXI2_3OTaIBgR-GixM1Ti-Ue1f
402
403
  arpakitlib/ar_settings_util.py,sha256=Y5wi_cmsjDjfJpM0VJHjbo0NoVPKfypKaD1USowwDtQ,1327
403
404
  arpakitlib/ar_sleep_util.py,sha256=ggaj7ML6QK_ADsHMcyu6GUmUpQ_9B9n-SKYH17h-9lM,1045
404
405
  arpakitlib/ar_sqladmin_util.py,sha256=Jd33EX_fAso4bvrcDhNEYOUlUHkudbadNGOJDg7rqhQ,301
405
- arpakitlib/ar_sqlalchemy_util.py,sha256=Fo7xdHu1rjyHNj2XzN7r_yXbaajGK8dxBPsOL6-DzVU,10559
406
+ arpakitlib/ar_sqlalchemy_util.py,sha256=w_tGPTWIMVjHkTEYo9tVe1sfK_4vvfd3zGATLiYC2J4,11775
406
407
  arpakitlib/ar_ssh_runner_util.py,sha256=yvAwza480MkHKvLkDEsR7JNh2bYNs6P9rCVo4NA85NE,6865
407
408
  arpakitlib/ar_str_util.py,sha256=2lGpnXDf2h1cBZpVf5i1tX_HCv5iBd6IGnrCw4QWWlY,4350
408
409
  arpakitlib/ar_type_util.py,sha256=Cs_tef-Fc5xeyAF54KgISCsP11NHyzIsglm4S3Xx7iM,4049
409
410
  arpakitlib/ar_yookassa_api_client_util.py,sha256=VozuZeCJjmLd1zj2BdC9WfiAQ3XYOrIMsdpNK-AUlm0,5347
410
- arpakitlib-1.8.155.dist-info/LICENSE,sha256=GPEDQMam2r7FSTYqM1mm7aKnxLaWcBotH7UvQtea-ec,11355
411
- arpakitlib-1.8.155.dist-info/METADATA,sha256=QKV0wMCBp4PPJHanz2EEe30ZmJyGGfrt_Tt3wdP7Gq4,3706
412
- arpakitlib-1.8.155.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
413
- arpakitlib-1.8.155.dist-info/entry_points.txt,sha256=36xqR3PJFT2kuwjkM_EqoIy0qFUDPKSm_mJaI7emewE,87
414
- arpakitlib-1.8.155.dist-info/RECORD,,
411
+ arpakitlib-1.8.157.dist-info/LICENSE,sha256=GPEDQMam2r7FSTYqM1mm7aKnxLaWcBotH7UvQtea-ec,11355
412
+ arpakitlib-1.8.157.dist-info/METADATA,sha256=JrajnfsEw_XyxLDxB-5qvd1cjf6LQSaVCmjGtjBahyQ,3706
413
+ arpakitlib-1.8.157.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
414
+ arpakitlib-1.8.157.dist-info/entry_points.txt,sha256=36xqR3PJFT2kuwjkM_EqoIy0qFUDPKSm_mJaI7emewE,87
415
+ arpakitlib-1.8.157.dist-info/RECORD,,