tracktolib 0.45.0__py3-none-any.whl → 0.46.1__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.
tracktolib/api.py CHANGED
@@ -24,6 +24,7 @@ from .utils import json_serial
24
24
 
25
25
  try:
26
26
  from fastapi import params, APIRouter
27
+ from fastapi.routing import APIRoute
27
28
  from fastapi.responses import JSONResponse
28
29
  from pydantic.alias_generators import to_camel
29
30
  from pydantic import BaseModel, ConfigDict
@@ -205,14 +206,56 @@ _NoneType = type(None)
205
206
 
206
207
  class IgnoreConfig(BaseModel):
207
208
  endpoints: dict[str, dict[Method, bool]]
208
- ignore_default: bool = True
209
+ ignore_missing: bool = True
209
210
 
210
211
 
211
- def _get_ignore_config() -> IgnoreConfig | None:
212
+ def get_ignore_config() -> IgnoreConfig | None:
212
213
  _config = os.getenv("IGNORE_CONFIG")
213
214
  return IgnoreConfig.model_validate_json(_config) if _config else None
214
215
 
215
216
 
217
+ def set_ignore_config(config: str | IgnoreConfig):
218
+ if isinstance(config, str):
219
+ config = IgnoreConfig.model_validate_json(config)
220
+ os.environ["IGNORE_CONFIG"] = config.model_dump_json()
221
+
222
+
223
+ def _filter_route(route: APIRoute, ignored_route: dict[Method, bool], ignore_missing: bool) -> APIRoute | None:
224
+ # If no config is provided and default is to ignore missing, return the route
225
+ if ignored_route is None and not ignore_missing:
226
+ return route
227
+
228
+ has_methods = False
229
+ enabled_methods = {method for method, has_access in ignored_route.items() if has_access}
230
+ for method in list(route.methods):
231
+ # If the config is not specified, we remove the method if ignore_missing is True
232
+ if method not in ignored_route:
233
+ if ignore_missing:
234
+ route.methods -= {method}
235
+ continue
236
+ else:
237
+ has_methods = True
238
+ continue
239
+ elif method not in enabled_methods:
240
+ route.methods -= {method}
241
+ else:
242
+ has_methods = True
243
+
244
+ if not has_methods:
245
+ return None
246
+ return route
247
+
248
+
249
+ def filter_routes(routes: list[APIRoute], ignore_config: IgnoreConfig) -> list[APIRoute]:
250
+ _routes = []
251
+ for route in routes:
252
+ _ignored_route = ignore_config.endpoints.get(route.path)
253
+ _route = _filter_route(route, _ignored_route or {}, ignore_missing=ignore_config.ignore_missing)
254
+ if _route is not None:
255
+ _routes.append(_route)
256
+ return _routes
257
+
258
+
216
259
  def _get_return_type(fn):
217
260
  return_type = get_type_hints(fn)["return"]
218
261
  _args = get_args(return_type)
@@ -228,14 +271,8 @@ def add_endpoint(
228
271
  *,
229
272
  dependencies: Dependencies = None,
230
273
  ):
231
- _ignore_config = _get_ignore_config()
274
+ _ignore_config = get_ignore_config()
232
275
  for _method, _meta in endpoint.methods.items():
233
- # Do not add endpoint if it is not in the ignore config
234
- if _ignore_config is not None:
235
- _has_access = _ignore_config.endpoints.get(path, {}).get(_method, not _ignore_config.ignore_default)
236
- if not _has_access:
237
- continue
238
- #
239
276
  _fn = _meta["fn"]
240
277
  _status_code = _meta["status_code"]
241
278
  _dependencies = _meta["dependencies"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tracktolib
3
- Version: 0.45.0
3
+ Version: 0.46.1
4
4
  Summary: Utility library for python
5
5
  Home-page: https://github.com/tracktor/tracktolib
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  LICENSE,sha256=uUanH0X7SeZEPdsRTHegMSMTiIHMurt9H0jSwEwKE1Y,1081
2
2
  tracktolib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- tracktolib/api.py,sha256=uJYVqRnHE_QTi6g2eD-zDpP-GsVPmq40h3hy1v3Agms,8558
3
+ tracktolib/api.py,sha256=DILKtPQn6HRXmQk5FgYHA9o5ToSNGudHsMEF_ZXEuI8,9826
4
4
  tracktolib/http_utils.py,sha256=dqjRJTNCo1qDnvQBTcImw1ibi1LnqETrpWDAU5txBHs,2633
5
5
  tracktolib/logs.py,sha256=enezPjBU4v2s8knG_V6kIbQsoyxfNIKEpKVSSkr_2U8,2212
6
6
  tracktolib/pg/__init__.py,sha256=32kKBJzIDyppszZMAOSMgM3ggz7Ow0TaNKiMs740gyw,333
@@ -13,7 +13,7 @@ tracktolib/s3/minio.py,sha256=JaRpHOM6NqpMieplp58Fpc7uTh7PItyP8On7jMXhB-Q,1234
13
13
  tracktolib/s3/s3.py,sha256=0aPUo3Vnfy5Rk_Qp_y0KkCZa2kuMWw2JU2xrodn7oAE,3486
14
14
  tracktolib/tests.py,sha256=FS0uae3ShiiJOD7WE2WEv9h3QwRJCbLDHpe_8Ikc0j0,554
15
15
  tracktolib/utils.py,sha256=YZZROGgOioblJkqgneHGZRbof45nmZ-VLSk9LqcVkkE,4847
16
- tracktolib-0.45.0.dist-info/LICENSE,sha256=uUanH0X7SeZEPdsRTHegMSMTiIHMurt9H0jSwEwKE1Y,1081
17
- tracktolib-0.45.0.dist-info/WHEEL,sha256=vxFmldFsRN_Hx10GDvsdv1wroKq8r5Lzvjp6GZ4OO8c,88
18
- tracktolib-0.45.0.dist-info/METADATA,sha256=j7mvkYhNZZN0JdGdpoU73sfWg3nyBj2xgmTnZND5gZI,3610
19
- tracktolib-0.45.0.dist-info/RECORD,,
16
+ tracktolib-0.46.1.dist-info/LICENSE,sha256=uUanH0X7SeZEPdsRTHegMSMTiIHMurt9H0jSwEwKE1Y,1081
17
+ tracktolib-0.46.1.dist-info/WHEEL,sha256=vxFmldFsRN_Hx10GDvsdv1wroKq8r5Lzvjp6GZ4OO8c,88
18
+ tracktolib-0.46.1.dist-info/METADATA,sha256=wi_XoRBTxEhgO2DvDPf1h_cf4XL8mcd0JP4i76_lHu0,3610
19
+ tracktolib-0.46.1.dist-info/RECORD,,