fastapi-rtk 0.2.27__py3-none-any.whl → 1.0.13__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.
- fastapi_rtk/__init__.py +39 -35
- fastapi_rtk/_version.py +1 -0
- fastapi_rtk/api/model_rest_api.py +476 -221
- fastapi_rtk/auth/auth.py +0 -9
- fastapi_rtk/backends/generic/__init__.py +6 -0
- fastapi_rtk/backends/generic/column.py +21 -12
- fastapi_rtk/backends/generic/db.py +42 -7
- fastapi_rtk/backends/generic/filters.py +21 -16
- fastapi_rtk/backends/generic/interface.py +14 -8
- fastapi_rtk/backends/generic/model.py +19 -11
- fastapi_rtk/backends/sqla/__init__.py +1 -0
- fastapi_rtk/backends/sqla/db.py +77 -17
- fastapi_rtk/backends/sqla/extensions/audit/audit.py +401 -189
- fastapi_rtk/backends/sqla/extensions/geoalchemy2/filters.py +15 -12
- fastapi_rtk/backends/sqla/filters.py +50 -21
- fastapi_rtk/backends/sqla/interface.py +96 -34
- fastapi_rtk/backends/sqla/model.py +56 -39
- fastapi_rtk/bases/__init__.py +20 -0
- fastapi_rtk/bases/db.py +94 -7
- fastapi_rtk/bases/file_manager.py +47 -3
- fastapi_rtk/bases/filter.py +22 -0
- fastapi_rtk/bases/interface.py +49 -5
- fastapi_rtk/bases/model.py +3 -0
- fastapi_rtk/bases/session.py +2 -0
- fastapi_rtk/cli/cli.py +62 -9
- fastapi_rtk/cli/commands/__init__.py +23 -0
- fastapi_rtk/cli/{db.py → commands/db/__init__.py} +107 -50
- fastapi_rtk/cli/{templates → commands/db/templates}/fastapi/env.py +2 -3
- fastapi_rtk/cli/{templates → commands/db/templates}/fastapi-multidb/env.py +10 -9
- fastapi_rtk/cli/{templates → commands/db/templates}/fastapi-multidb/script.py.mako +3 -1
- fastapi_rtk/cli/{export.py → commands/export.py} +12 -10
- fastapi_rtk/cli/{security.py → commands/security.py} +73 -7
- fastapi_rtk/cli/commands/translate.py +299 -0
- fastapi_rtk/cli/decorators.py +9 -4
- fastapi_rtk/cli/utils.py +46 -0
- fastapi_rtk/config.py +41 -1
- fastapi_rtk/const.py +29 -1
- fastapi_rtk/db.py +76 -40
- fastapi_rtk/decorators.py +1 -1
- fastapi_rtk/dependencies.py +134 -62
- fastapi_rtk/exceptions.py +51 -1
- fastapi_rtk/fastapi_react_toolkit.py +186 -171
- fastapi_rtk/file_managers/file_manager.py +8 -6
- fastapi_rtk/file_managers/s3_file_manager.py +69 -33
- fastapi_rtk/globals.py +22 -12
- fastapi_rtk/lang/__init__.py +3 -0
- fastapi_rtk/lang/babel/__init__.py +4 -0
- fastapi_rtk/lang/babel/cli.py +40 -0
- fastapi_rtk/lang/babel/config.py +17 -0
- fastapi_rtk/lang/babel.cfg +1 -0
- fastapi_rtk/lang/lazy_text.py +120 -0
- fastapi_rtk/lang/messages.pot +238 -0
- fastapi_rtk/lang/translations/de/LC_MESSAGES/messages.mo +0 -0
- fastapi_rtk/lang/translations/de/LC_MESSAGES/messages.po +248 -0
- fastapi_rtk/lang/translations/en/LC_MESSAGES/messages.mo +0 -0
- fastapi_rtk/lang/translations/en/LC_MESSAGES/messages.po +244 -0
- fastapi_rtk/manager.py +355 -37
- fastapi_rtk/mixins.py +12 -0
- fastapi_rtk/routers.py +208 -72
- fastapi_rtk/schemas.py +142 -39
- fastapi_rtk/security/sqla/apis.py +39 -13
- fastapi_rtk/security/sqla/models.py +8 -23
- fastapi_rtk/security/sqla/security_manager.py +369 -11
- fastapi_rtk/setting.py +446 -88
- fastapi_rtk/types.py +94 -27
- fastapi_rtk/utils/__init__.py +8 -0
- fastapi_rtk/utils/async_task_runner.py +286 -61
- fastapi_rtk/utils/csv_json_converter.py +243 -40
- fastapi_rtk/utils/hooks.py +34 -0
- fastapi_rtk/utils/merge_schema.py +3 -3
- fastapi_rtk/utils/multiple_async_contexts.py +21 -0
- fastapi_rtk/utils/pydantic.py +46 -1
- fastapi_rtk/utils/run_utils.py +31 -1
- fastapi_rtk/utils/self_dependencies.py +1 -1
- fastapi_rtk/utils/use_default_when_none.py +1 -1
- fastapi_rtk/version.py +6 -1
- fastapi_rtk-1.0.13.dist-info/METADATA +28 -0
- fastapi_rtk-1.0.13.dist-info/RECORD +133 -0
- {fastapi_rtk-0.2.27.dist-info → fastapi_rtk-1.0.13.dist-info}/WHEEL +1 -2
- fastapi_rtk/backends/gremlinpython/__init__.py +0 -108
- fastapi_rtk/backends/gremlinpython/column.py +0 -208
- fastapi_rtk/backends/gremlinpython/db.py +0 -228
- fastapi_rtk/backends/gremlinpython/exceptions.py +0 -34
- fastapi_rtk/backends/gremlinpython/filters.py +0 -461
- fastapi_rtk/backends/gremlinpython/interface.py +0 -734
- fastapi_rtk/backends/gremlinpython/model.py +0 -364
- fastapi_rtk/backends/gremlinpython/session.py +0 -23
- fastapi_rtk/cli/commands.py +0 -295
- fastapi_rtk-0.2.27.dist-info/METADATA +0 -23
- fastapi_rtk-0.2.27.dist-info/RECORD +0 -126
- fastapi_rtk-0.2.27.dist-info/top_level.txt +0 -1
- /fastapi_rtk/cli/{templates → commands/db/templates}/fastapi/README +0 -0
- /fastapi_rtk/cli/{templates → commands/db/templates}/fastapi/alembic.ini.mako +0 -0
- /fastapi_rtk/cli/{templates → commands/db/templates}/fastapi/script.py.mako +0 -0
- /fastapi_rtk/cli/{templates → commands/db/templates}/fastapi-multidb/README +0 -0
- /fastapi_rtk/cli/{templates → commands/db/templates}/fastapi-multidb/alembic.ini.mako +0 -0
- {fastapi_rtk-0.2.27.dist-info → fastapi_rtk-1.0.13.dist-info}/entry_points.txt +0 -0
- {fastapi_rtk-0.2.27.dist-info → fastapi_rtk-1.0.13.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import abc
|
|
1
2
|
import csv
|
|
2
3
|
import enum
|
|
4
|
+
import inspect
|
|
3
5
|
import json
|
|
4
6
|
import typing
|
|
5
7
|
|
|
6
|
-
from
|
|
8
|
+
from .run_utils import safe_call
|
|
9
|
+
|
|
10
|
+
if typing.TYPE_CHECKING:
|
|
11
|
+
from ..bases import BasicModel
|
|
7
12
|
|
|
8
13
|
__all__ = ["Line", "CSVJSONConverter"]
|
|
9
14
|
|
|
@@ -23,6 +28,8 @@ class CSVJSONConverter:
|
|
|
23
28
|
A utility class for converting CSV data to JSON format and vice versa.
|
|
24
29
|
"""
|
|
25
30
|
|
|
31
|
+
ExportMode = typing.Literal["simplified", "detailed"]
|
|
32
|
+
|
|
26
33
|
@classmethod
|
|
27
34
|
def csv_to_json(
|
|
28
35
|
cls,
|
|
@@ -57,7 +64,7 @@ class CSVJSONConverter:
|
|
|
57
64
|
@classmethod
|
|
58
65
|
def json_to_csv(
|
|
59
66
|
cls,
|
|
60
|
-
data: dict[str, typing.Any] | list[dict[str, typing.Any]],
|
|
67
|
+
data: "dict[str, typing.Any] | list[dict[str, typing.Any]] | BasicModel | list[BasicModel]",
|
|
61
68
|
/,
|
|
62
69
|
*,
|
|
63
70
|
list_columns: list[str],
|
|
@@ -71,8 +78,10 @@ class CSVJSONConverter:
|
|
|
71
78
|
"""
|
|
72
79
|
Converts JSON data to CSV format.
|
|
73
80
|
|
|
81
|
+
- Data can also be a subclass of `BasicModel` or a list of subclasses of `BasicModel`.
|
|
82
|
+
|
|
74
83
|
Args:
|
|
75
|
-
data (dict[str, Any] | list[dict[str, Any]]): The JSON data to be converted.
|
|
84
|
+
data (dict[str, typing.Any] | list[dict[str, typing.Any]] | BasicModel | list[BasicModel]): The JSON data to be converted. Can also be a subclass of `BasicModel` or a list of subclasses of `BasicModel`.
|
|
76
85
|
list_columns (list[str]): The list of columns to be included in the CSV.
|
|
77
86
|
label_columns (dict[str, str]): The mapping of column names to labels.
|
|
78
87
|
with_header (bool, optional): Whether to include the header in the CSV. Defaults to True.
|
|
@@ -93,11 +102,11 @@ class CSVJSONConverter:
|
|
|
93
102
|
writer.writerow(header)
|
|
94
103
|
csv_data = line.read()
|
|
95
104
|
|
|
96
|
-
if isinstance(data,
|
|
105
|
+
if not isinstance(data, list):
|
|
97
106
|
data = [data]
|
|
98
107
|
|
|
99
108
|
for item in data:
|
|
100
|
-
row = cls.
|
|
109
|
+
row = cls.json_to_csv_single(
|
|
101
110
|
item,
|
|
102
111
|
list_columns=list_columns,
|
|
103
112
|
delimiter=delimiter,
|
|
@@ -110,9 +119,9 @@ class CSVJSONConverter:
|
|
|
110
119
|
return csv_data.strip()
|
|
111
120
|
|
|
112
121
|
@classmethod
|
|
113
|
-
def
|
|
122
|
+
def json_to_csv_single(
|
|
114
123
|
self,
|
|
115
|
-
data: dict[str, typing.Any],
|
|
124
|
+
data: "dict[str, typing.Any] | BasicModel",
|
|
116
125
|
/,
|
|
117
126
|
*,
|
|
118
127
|
list_columns: list[str],
|
|
@@ -123,8 +132,10 @@ class CSVJSONConverter:
|
|
|
123
132
|
"""
|
|
124
133
|
Converts single JSON object to CSV format.
|
|
125
134
|
|
|
135
|
+
- Data can also be a subclass of `BasicModel`.
|
|
136
|
+
|
|
126
137
|
Args:
|
|
127
|
-
data (dict[str, Any]): The JSON data to be converted.
|
|
138
|
+
data (dict[str, typing.Any] | BasicModel): The JSON data to be converted. Can also be a subclass of `BasicModel`.
|
|
128
139
|
list_columns (list[str]): The list of columns to be included in the CSV.
|
|
129
140
|
delimiter (str, optional): The delimiter to use in the CSV. Defaults to ",".
|
|
130
141
|
relation_separator (str, optional): The separator to use for nested keys. Defaults to ".".
|
|
@@ -134,40 +145,60 @@ class CSVJSONConverter:
|
|
|
134
145
|
str: The CSV data as a string.
|
|
135
146
|
"""
|
|
136
147
|
csv_data: list[str] = []
|
|
148
|
+
data_pipeline = DataPipeline()
|
|
149
|
+
data_pipeline.add_processor(ColumnProcessor(relation_separator))
|
|
150
|
+
data_pipeline.add_processor(ModelProcessor())
|
|
151
|
+
data_pipeline.add_processor(
|
|
152
|
+
ListProcessor(delimiter=delimiter, export_mode=export_mode)
|
|
153
|
+
)
|
|
154
|
+
data_pipeline.add_processor(EnumProcessor())
|
|
155
|
+
data_pipeline.add_processor(FallbackProcessor())
|
|
137
156
|
|
|
138
157
|
for col in list_columns:
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
158
|
+
value = data_pipeline.process(data, col)
|
|
159
|
+
csv_data.append(value)
|
|
160
|
+
|
|
161
|
+
return csv_data
|
|
162
|
+
|
|
163
|
+
@classmethod
|
|
164
|
+
async def ajson_to_csv_single(
|
|
165
|
+
cls,
|
|
166
|
+
data: "dict[str, typing.Any] | BasicModel",
|
|
167
|
+
/,
|
|
168
|
+
*,
|
|
169
|
+
list_columns: list[str],
|
|
170
|
+
delimiter=",",
|
|
171
|
+
relation_separator=".",
|
|
172
|
+
export_mode: ExportMode = "simplified",
|
|
173
|
+
):
|
|
174
|
+
"""
|
|
175
|
+
Asynchronously converts single JSON object to CSV format.
|
|
176
|
+
|
|
177
|
+
- Data can also be a `BasicModel`.
|
|
178
|
+
|
|
179
|
+
Args:
|
|
180
|
+
data (dict[str, typing.Any] | BasicModel): The JSON data to be converted. Can also be a `BasicModel`.
|
|
181
|
+
list_columns (list[str]): The list of columns to be included in the CSV.
|
|
182
|
+
delimiter (str, optional): The delimiter to use in the CSV. Defaults to ",".
|
|
183
|
+
relation_separator (str, optional): The separator to use for nested keys. Defaults to ".".
|
|
184
|
+
export_mode (ExportMode, optional): Export mode (simplified or detailed). Defaults to "simplified".
|
|
185
|
+
|
|
186
|
+
Returns:
|
|
187
|
+
str: The CSV data as a string.
|
|
188
|
+
"""
|
|
189
|
+
csv_data: list[str] = []
|
|
190
|
+
data_pipeline = DataPipeline()
|
|
191
|
+
data_pipeline.add_processor(AsyncColumnProcessor(relation_separator))
|
|
192
|
+
data_pipeline.add_processor(ModelProcessor())
|
|
193
|
+
data_pipeline.add_processor(
|
|
194
|
+
ListProcessor(delimiter=delimiter, export_mode=export_mode)
|
|
195
|
+
)
|
|
196
|
+
data_pipeline.add_processor(EnumProcessor())
|
|
197
|
+
data_pipeline.add_processor(FallbackProcessor())
|
|
198
|
+
|
|
199
|
+
for col in list_columns:
|
|
200
|
+
value = await data_pipeline.aprocess(data, col)
|
|
201
|
+
csv_data.append(value)
|
|
171
202
|
|
|
172
203
|
return csv_data
|
|
173
204
|
|
|
@@ -224,3 +255,175 @@ class CSVJSONConverter:
|
|
|
224
255
|
value = value.split(list_delimiter)
|
|
225
256
|
current[parts[-1]] = [item.strip() for item in value if item.strip()]
|
|
226
257
|
return result
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
class DataPipeline:
|
|
261
|
+
def __init__(self):
|
|
262
|
+
self.processors = list[DataProcessor]()
|
|
263
|
+
|
|
264
|
+
def add_processor(self, processor: "DataProcessor"):
|
|
265
|
+
"""
|
|
266
|
+
Adds a data processor to the pipeline.
|
|
267
|
+
|
|
268
|
+
Args:
|
|
269
|
+
processor (DataProcessor): The data processor to add.
|
|
270
|
+
"""
|
|
271
|
+
self.processors.append(processor)
|
|
272
|
+
|
|
273
|
+
def process(self, data: typing.Any, col: str):
|
|
274
|
+
"""
|
|
275
|
+
Processes the data through the pipeline.
|
|
276
|
+
|
|
277
|
+
Args:
|
|
278
|
+
data (typing.Any): The data to process.
|
|
279
|
+
col (str): The column to process.
|
|
280
|
+
|
|
281
|
+
Returns:
|
|
282
|
+
typing.Any: The processed data.
|
|
283
|
+
"""
|
|
284
|
+
for processor in self.processors:
|
|
285
|
+
data, col, should_continue = processor.process(data, col)
|
|
286
|
+
if not should_continue:
|
|
287
|
+
break
|
|
288
|
+
return data
|
|
289
|
+
|
|
290
|
+
async def aprocess(self, data: typing.Any, col: str):
|
|
291
|
+
"""
|
|
292
|
+
Asynchronously processes the data through the pipeline.
|
|
293
|
+
|
|
294
|
+
Args:
|
|
295
|
+
data (typing.Any): The data to process.
|
|
296
|
+
col (str): The column to process.
|
|
297
|
+
|
|
298
|
+
Returns:
|
|
299
|
+
typing.Any: The processed data.
|
|
300
|
+
"""
|
|
301
|
+
for processor in self.processors:
|
|
302
|
+
data, col, should_continue = await safe_call(processor.process(data, col))
|
|
303
|
+
if not should_continue:
|
|
304
|
+
break
|
|
305
|
+
return data
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
class DataProcessor(abc.ABC):
|
|
309
|
+
@abc.abstractmethod
|
|
310
|
+
def process(self, data: typing.Any, col: str) -> tuple[typing.Any, str, bool]:
|
|
311
|
+
"""
|
|
312
|
+
Processes the data for a specific column.
|
|
313
|
+
|
|
314
|
+
Args:
|
|
315
|
+
data (typing.Any): The data to process.
|
|
316
|
+
col (str): The column to process.
|
|
317
|
+
|
|
318
|
+
Returns:
|
|
319
|
+
tuple[typing.Any, str, bool]: The processed data, the column name, and a boolean indicating whether to continue processing.
|
|
320
|
+
"""
|
|
321
|
+
raise NotImplementedError()
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
class ColumnProcessor(DataProcessor):
|
|
325
|
+
def __init__(self, relation_separator: str = "."):
|
|
326
|
+
super().__init__()
|
|
327
|
+
self.relation_separator = relation_separator
|
|
328
|
+
|
|
329
|
+
def process(self, data, col):
|
|
330
|
+
sub_col = []
|
|
331
|
+
if self.relation_separator in col:
|
|
332
|
+
col, *sub_col = col.split(self.relation_separator)
|
|
333
|
+
data = data.get(col, "") if isinstance(data, dict) else getattr(data, col, "")
|
|
334
|
+
for sub in sub_col:
|
|
335
|
+
data = (
|
|
336
|
+
data.get(sub, "") if isinstance(data, dict) else getattr(data, sub, "")
|
|
337
|
+
)
|
|
338
|
+
return data, col, True
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
class AsyncColumnProcessor(ColumnProcessor):
|
|
342
|
+
async def process(self, data, col):
|
|
343
|
+
data, col, continue_processing = super().process(data, col)
|
|
344
|
+
if inspect.iscoroutine(data):
|
|
345
|
+
data = await data
|
|
346
|
+
return data, col, continue_processing
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
class ModelProcessor(DataProcessor):
|
|
350
|
+
def __init__(self, attr="name_"):
|
|
351
|
+
super().__init__()
|
|
352
|
+
self.attr = attr
|
|
353
|
+
|
|
354
|
+
def process(self, data, col):
|
|
355
|
+
from ..bases import BasicModel
|
|
356
|
+
|
|
357
|
+
continue_processing = True
|
|
358
|
+
|
|
359
|
+
if isinstance(data, BasicModel):
|
|
360
|
+
data = getattr(data, self.attr, "")
|
|
361
|
+
continue_processing = False
|
|
362
|
+
return data, col, continue_processing
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
class DictProcessor(ModelProcessor):
|
|
366
|
+
def process(self, data, col):
|
|
367
|
+
continue_processing = True
|
|
368
|
+
|
|
369
|
+
if isinstance(data, dict):
|
|
370
|
+
data = data.get(self.attr, json.dumps(data))
|
|
371
|
+
continue_processing = False
|
|
372
|
+
return data, col, continue_processing
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
class ListProcessor(DataProcessor):
|
|
376
|
+
def __init__(
|
|
377
|
+
self,
|
|
378
|
+
delimiter=",",
|
|
379
|
+
export_mode: CSVJSONConverter.ExportMode = "simplified",
|
|
380
|
+
attr_detailed="id_",
|
|
381
|
+
attr_simplified="name_",
|
|
382
|
+
):
|
|
383
|
+
super().__init__()
|
|
384
|
+
self.separator = "," if delimiter == ";" else ";"
|
|
385
|
+
self.export_mode = export_mode
|
|
386
|
+
self.model_processor = ModelProcessor(
|
|
387
|
+
attr_detailed if export_mode == "detailed" else attr_simplified
|
|
388
|
+
)
|
|
389
|
+
self.dict_processor = DictProcessor(
|
|
390
|
+
attr_detailed if export_mode == "detailed" else attr_simplified
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
def process(self, data, col):
|
|
394
|
+
from ..bases import BasicModel
|
|
395
|
+
|
|
396
|
+
continue_processing = True
|
|
397
|
+
|
|
398
|
+
if isinstance(data, list):
|
|
399
|
+
processed_list = []
|
|
400
|
+
for item in data:
|
|
401
|
+
if isinstance(item, dict):
|
|
402
|
+
item_processed, _, _ = self.dict_processor.process(item, col)
|
|
403
|
+
elif isinstance(item, BasicModel):
|
|
404
|
+
item_processed, _, _ = self.model_processor.process(item, col)
|
|
405
|
+
else:
|
|
406
|
+
item_processed = str(item)
|
|
407
|
+
processed_list.append(item_processed)
|
|
408
|
+
data = self.separator.join(processed_list)
|
|
409
|
+
continue_processing = False
|
|
410
|
+
return data, col, continue_processing
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
class EnumProcessor(DataProcessor):
|
|
414
|
+
def process(self, data, col):
|
|
415
|
+
continue_processing = True
|
|
416
|
+
|
|
417
|
+
if isinstance(data, enum.Enum):
|
|
418
|
+
data = data.value
|
|
419
|
+
continue_processing = False
|
|
420
|
+
return data, col, continue_processing
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
class FallbackProcessor(DataProcessor):
|
|
424
|
+
def process(self, data, col):
|
|
425
|
+
if data is None:
|
|
426
|
+
data = ""
|
|
427
|
+
else:
|
|
428
|
+
data = str(data)
|
|
429
|
+
return data, col, False
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
__all__ = ["hooks"]
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def hooks(
|
|
8
|
+
pre: typing.Callable[..., None] | None = None,
|
|
9
|
+
post: typing.Callable[..., None] | None = None,
|
|
10
|
+
):
|
|
11
|
+
"""
|
|
12
|
+
Decorator to add optional pre and post hook methods to a function.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
pre (typing.Callable[..., None] | None): Function to run before the main function. Defaults to None.
|
|
16
|
+
post (typing.Callable[..., None] | None): Function to run after the main function. Defaults to None.
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
typing.Callable[..., typing.Any]: The decorated function.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def decorator(func):
|
|
23
|
+
@functools.wraps(func)
|
|
24
|
+
def wrapper(*args, **kwargs):
|
|
25
|
+
if pre:
|
|
26
|
+
pre(*args, **kwargs)
|
|
27
|
+
result = func(*args, **kwargs)
|
|
28
|
+
if post:
|
|
29
|
+
post(result, *args, **kwargs)
|
|
30
|
+
return result
|
|
31
|
+
|
|
32
|
+
return wrapper
|
|
33
|
+
|
|
34
|
+
return decorator
|
|
@@ -7,7 +7,7 @@ __all__ = ["merge_schema"]
|
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
def merge_schema(
|
|
10
|
-
schema: pydantic.BaseModel,
|
|
10
|
+
schema: typing.Type[pydantic.BaseModel],
|
|
11
11
|
fields: typing.Dict[str, tuple[type, pydantic.fields.FieldInfo]],
|
|
12
12
|
only_update=False,
|
|
13
13
|
name: str | None = None,
|
|
@@ -16,13 +16,13 @@ def merge_schema(
|
|
|
16
16
|
Replace or add fields to the given schema.
|
|
17
17
|
|
|
18
18
|
Args:
|
|
19
|
-
schema (BaseModel): The schema to be updated.
|
|
19
|
+
schema (typing.Type[pydantic.BaseModel]): The schema to be updated.
|
|
20
20
|
fields (Dict[str, tuple[type, Field]]): The fields to be added or updated.
|
|
21
21
|
only_update (bool): If True, only update the fields with the same name. Otherwise, add new fields.
|
|
22
22
|
name (str, optional): The name of the new schema. If not given, the schema name will be suffixed with a random hex string. Defaults to None.
|
|
23
23
|
|
|
24
24
|
Returns:
|
|
25
|
-
BaseModel: The updated schema.
|
|
25
|
+
typing.Type[pydantic.BaseModel]: The updated schema.
|
|
26
26
|
"""
|
|
27
27
|
name = name or f"{schema.__name__}-{secrets.token_hex(3)}"
|
|
28
28
|
new_fields = dict()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
|
|
3
|
+
__all__ = ["multiple_async_contexts"]
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@contextlib.asynccontextmanager
|
|
7
|
+
async def multiple_async_contexts(
|
|
8
|
+
contexts: list[contextlib.AbstractAsyncContextManager],
|
|
9
|
+
):
|
|
10
|
+
"""
|
|
11
|
+
Async context manager to enter multiple async contexts.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
contexts (list): A list of async context managers to enter.
|
|
15
|
+
|
|
16
|
+
Yields:
|
|
17
|
+
list: A list of entered context values.
|
|
18
|
+
"""
|
|
19
|
+
async with contextlib.AsyncExitStack() as stack:
|
|
20
|
+
entered = [await stack.enter_async_context(ctx) for ctx in contexts]
|
|
21
|
+
yield entered
|
fastapi_rtk/utils/pydantic.py
CHANGED
|
@@ -2,7 +2,7 @@ import typing
|
|
|
2
2
|
|
|
3
3
|
import pydantic
|
|
4
4
|
|
|
5
|
-
__all__ = ["generate_schema_from_typed_dict"]
|
|
5
|
+
__all__ = ["generate_schema_from_typed_dict", "get_pydantic_model_field"]
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
def generate_schema_from_typed_dict(typed_dict: type):
|
|
@@ -25,3 +25,48 @@ def generate_schema_from_typed_dict(typed_dict: type):
|
|
|
25
25
|
else:
|
|
26
26
|
fields[key] = (val, pydantic.Field(...))
|
|
27
27
|
return pydantic.create_model(typed_dict.__name__, **fields)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def get_pydantic_model_field(model_cls: typing.Type[pydantic.BaseModel], field: str):
|
|
31
|
+
"""
|
|
32
|
+
Get the Pydantic model class for a specific field in a Pydantic model.
|
|
33
|
+
|
|
34
|
+
Also works if the field is a list of Pydantic models.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
model_cls (typing.Type[pydantic.BaseModel]): The Pydantic model class.
|
|
38
|
+
field (str): The name of the field.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
typing.Type[pydantic.BaseModel] | None: The Pydantic model class for the field, or None if not found.
|
|
42
|
+
"""
|
|
43
|
+
annotation = model_cls.model_fields.get(field).annotation
|
|
44
|
+
field_types = _get_list_of_singular_type(annotation)
|
|
45
|
+
return next(
|
|
46
|
+
(
|
|
47
|
+
t
|
|
48
|
+
for t in field_types
|
|
49
|
+
if isinstance(t, type) and issubclass(t, pydantic.BaseModel)
|
|
50
|
+
),
|
|
51
|
+
None,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _get_list_of_singular_type(type_or_union: type) -> list[type]:
|
|
56
|
+
"""
|
|
57
|
+
Recursively extract all singular (leaf) types from a typing.Union, typing.List, etc.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
type_or_union (type): The type to analyze, which may be a generic type like Union or List.
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
list[type]: A list of singular types extracted from the input type.
|
|
64
|
+
"""
|
|
65
|
+
args = typing.get_args(type_or_union)
|
|
66
|
+
if not args:
|
|
67
|
+
# Base case: not a generic type
|
|
68
|
+
return [type_or_union]
|
|
69
|
+
result = []
|
|
70
|
+
for arg in args:
|
|
71
|
+
result.extend(_get_list_of_singular_type(arg))
|
|
72
|
+
return result
|
fastapi_rtk/utils/run_utils.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import asyncio
|
|
2
|
+
import contextvars
|
|
2
3
|
import inspect
|
|
3
4
|
import typing
|
|
4
5
|
from concurrent.futures import ThreadPoolExecutor
|
|
@@ -16,6 +17,7 @@ __all__ = [
|
|
|
16
17
|
"safe_call",
|
|
17
18
|
"safe_call_sync",
|
|
18
19
|
"run_coroutine_in_threadpool",
|
|
20
|
+
"run_function_in_threadpool",
|
|
19
21
|
"call_with_valid_kwargs",
|
|
20
22
|
]
|
|
21
23
|
|
|
@@ -119,7 +121,35 @@ def run_coroutine_in_threadpool(
|
|
|
119
121
|
T: The result of the coroutine.
|
|
120
122
|
"""
|
|
121
123
|
with ThreadPoolExecutor(max_workers=max_workers, **kwargs) as executor:
|
|
122
|
-
|
|
124
|
+
ctx = contextvars.copy_context()
|
|
125
|
+
future = executor.submit(ctx.run, asyncio.run, coro)
|
|
126
|
+
return future.result()
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def run_function_in_threadpool(
|
|
130
|
+
func: typing.Callable[P, T],
|
|
131
|
+
*args: P.args,
|
|
132
|
+
max_workers=1,
|
|
133
|
+
thread_pool_kwargs: dict[str, typing.Any] | None = None,
|
|
134
|
+
**kwargs: P.kwargs,
|
|
135
|
+
) -> T:
|
|
136
|
+
"""
|
|
137
|
+
Run a synchronous function in a thread pool executor.
|
|
138
|
+
|
|
139
|
+
Args:
|
|
140
|
+
func (typing.Callable[P, T]): The function to run.
|
|
141
|
+
*args (P.args): Positional arguments to be passed to the function.
|
|
142
|
+
max_workers (int, optional): The maximum number of workers in the thread pool. Defaults to 1.
|
|
143
|
+
thread_pool_kwargs (dict[str, typing.Any] | None, optional): Additional keyword arguments to pass to the `ThreadPoolExecutor`. Defaults to None.
|
|
144
|
+
**kwargs (P.kwargs): Keyword arguments to be passed to the function.
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
T: The result of the function.
|
|
148
|
+
"""
|
|
149
|
+
with ThreadPoolExecutor(
|
|
150
|
+
max_workers=max_workers, **(thread_pool_kwargs or {})
|
|
151
|
+
) as executor:
|
|
152
|
+
future = executor.submit(func, *args, **kwargs)
|
|
123
153
|
return future.result()
|
|
124
154
|
|
|
125
155
|
|
|
@@ -5,7 +5,7 @@ T = typing.TypeVar("T")
|
|
|
5
5
|
__all__ = ["use_default_when_none"]
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
def use_default_when_none(value: T, default: T) -> T:
|
|
8
|
+
def use_default_when_none(value: T | None, default: T) -> T:
|
|
9
9
|
"""
|
|
10
10
|
Returns the value if it is not None, otherwise returns the default value.
|
|
11
11
|
|
fastapi_rtk/version.py
CHANGED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-rtk
|
|
3
|
+
Version: 1.0.13
|
|
4
|
+
Summary: A package that provides a set of tools to build a FastAPI application with a Class-Based CRUD API.
|
|
5
|
+
Project-URL: Homepage, https://codeberg.org/datatactics/fastapi-rtk
|
|
6
|
+
Project-URL: Issues, https://codeberg.org/datatactics/fastapi-rtk/issues
|
|
7
|
+
Author-email: Matthias Leinweber <m.leinweber@datatactics.de>, Agustinus Nicander Hery <a.hery@datatactics.de>
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Requires-Dist: alembic>=1.17.0
|
|
18
|
+
Requires-Dist: beautifulsoup4>=4.14.2
|
|
19
|
+
Requires-Dist: fastapi-babel>=1.0.0
|
|
20
|
+
Requires-Dist: fastapi-users[oauth,sqlalchemy]>=14.0.1
|
|
21
|
+
Requires-Dist: fastapi[standard]>=0.119.1
|
|
22
|
+
Requires-Dist: jsonschema2md>=1.7.0
|
|
23
|
+
Requires-Dist: marshmallow-sqlalchemy>=1.4.2
|
|
24
|
+
Requires-Dist: polib>=1.2.0
|
|
25
|
+
Requires-Dist: prometheus-fastapi-instrumentator>=7.1.0
|
|
26
|
+
Requires-Dist: secweb>=1.25.2
|
|
27
|
+
Requires-Dist: sqlalchemy-utils>=0.42.0
|
|
28
|
+
Requires-Dist: uvicorn==0.38.0
|