u-toolkit 0.1.10__py3-none-any.whl → 0.1.11__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.
u_toolkit/fastapi/cbv.py CHANGED
@@ -39,7 +39,7 @@ EndpointsClassInterfaceT = TypeVar(
39
39
  )
40
40
 
41
41
 
42
- LiteralUpperMethods = Literal[
42
+ LiteralUpperMethod = Literal[
43
43
  "GET",
44
44
  "POST",
45
45
  "PATCH",
@@ -49,7 +49,7 @@ LiteralUpperMethods = Literal[
49
49
  "HEAD",
50
50
  "TRACE",
51
51
  ]
52
- LiteralLowerMethods = Literal[
52
+ LiteralLowerMethod = Literal[
53
53
  "get",
54
54
  "post",
55
55
  "patch",
@@ -72,23 +72,31 @@ class Methods(StrEnum):
72
72
  TRACE = auto()
73
73
 
74
74
 
75
+ RequestMethod = Methods | LiteralLowerMethod | LiteralUpperMethod
76
+
77
+
75
78
  METHOD_PATTERNS = {
76
79
  method: re.compile(f"^({method}_|{method})", re.IGNORECASE)
77
80
  for method in Methods
78
81
  }
79
82
 
83
+
80
84
  _FnName = str
81
85
 
82
86
 
83
87
  class EndpointInfo(NamedTuple):
84
- fn: Callable
85
- original_name: str
86
- method: Methods
88
+ handle: Callable
89
+ original_handle_name: str
90
+ handle_name: str
91
+ method: RequestMethod
87
92
  method_pattern: re.Pattern
88
93
  path: str
89
94
 
90
95
 
91
- def get_method(name: str):
96
+ _MethodInfo = tuple[RequestMethod, re.Pattern[str]]
97
+
98
+
99
+ def get_method(name: str) -> _MethodInfo | None:
92
100
  for method, method_pattern in METHOD_PATTERNS.items():
93
101
  if method_pattern.search(name):
94
102
  return method, method_pattern
@@ -100,29 +108,41 @@ def valid_endpoint(name: str):
100
108
  raise ValueError("Invalid endpoint function.")
101
109
 
102
110
 
103
- def iter_endpoints(cls: type[_T]):
104
- prefix = "/"
111
+ def iter_endpoints(
112
+ cls: type[_T],
113
+ valid_method: Callable[[str, Callable], list[_MethodInfo] | None]
114
+ | None = None,
115
+ ):
116
+ prefix = ""
105
117
 
106
118
  if not cls.__name__.startswith("_"):
107
- prefix += f"{to_snake(cls.__name__)}"
119
+ prefix += f"/{to_snake(cls.__name__)}"
108
120
 
109
- for name, fn in inspect.getmembers(
121
+ for name, handle in inspect.getmembers(
110
122
  cls,
111
123
  lambda arg: inspect.ismethoddescriptor(arg) or inspect.isfunction(arg),
112
124
  ):
113
125
  paths = [prefix]
114
126
 
127
+ methods: list[_MethodInfo] = []
115
128
  if method := get_method(name):
116
- path = method[1].sub("", name).replace("__", "/")
129
+ methods.append(method)
130
+ elif valid_method:
131
+ methods.extend(valid_method(name, handle) or [])
132
+
133
+ for method, pattern in methods:
134
+ handle_name = pattern.sub("", name)
135
+ path = handle_name.replace("__", "/")
117
136
  if path:
118
137
  paths.append(path)
119
138
 
120
139
  yield EndpointInfo(
121
- fn=fn,
122
- original_name=name,
140
+ handle=handle,
141
+ original_handle_name=name,
142
+ handle_name=handle_name,
123
143
  path="/".join(paths),
124
- method=method[0],
125
- method_pattern=method[1],
144
+ method=method,
145
+ method_pattern=pattern,
126
146
  )
127
147
 
128
148
 
@@ -171,7 +191,7 @@ class CBV:
171
191
  *,
172
192
  cls: type[EndpointsClassInterfaceT],
173
193
  path: str,
174
- method: Methods | LiteralUpperMethods | LiteralLowerMethods,
194
+ method: RequestMethod,
175
195
  method_name: str,
176
196
  ):
177
197
  class_tags = list(cls.tags) if cls.tags else []
@@ -221,8 +241,7 @@ class CBV:
221
241
  self,
222
242
  *,
223
243
  path: str | None = None,
224
- methods: list[Methods | LiteralUpperMethods | LiteralLowerMethods]
225
- | None = None,
244
+ methods: list[RequestMethod] | None = None,
226
245
  tags: list[str | Enum] | None = None,
227
246
  dependencies: list | None = None,
228
247
  responses: list[Response] | None = None,
@@ -261,7 +280,7 @@ class CBV:
261
280
 
262
281
  default_data = {}
263
282
  for endpoint in iter_endpoints(n_cls):
264
- default_data[endpoint.original_name] = {}
283
+ default_data[endpoint.original_handle_name] = {}
265
284
 
266
285
  self._state.setdefault(n_cls, default_data)
267
286
  result = self._build_cls(n_cls)
@@ -361,14 +380,31 @@ class CBV:
361
380
 
362
381
  decorator = self.__create_class_dependencies_injector(cls_)
363
382
 
364
- for endpoint_info in iter_endpoints(cls):
383
+ def valid_method(
384
+ name: str, _handle: Callable
385
+ ) -> None | list[_MethodInfo]:
386
+ if (cls_state := self._state.get(cls_)) and (
387
+ method_state := cls_state.get(name)
388
+ ):
389
+ methods: list[RequestMethod] = (
390
+ method_state.get("methods") or []
391
+ )
392
+ result: list[_MethodInfo] = []
393
+ for i in methods:
394
+ method = Methods(i.lower())
395
+ result.append((method, METHOD_PATTERNS[method]))
396
+ return result
397
+
398
+ return None
399
+
400
+ for endpoint_info in iter_endpoints(cls, valid_method):
365
401
  route = self.create_route(
366
402
  cls=cast(type[EndpointsClassInterface], cls),
367
403
  path=endpoint_info.path,
368
404
  method=endpoint_info.method,
369
- method_name=endpoint_info.original_name,
405
+ method_name=endpoint_info.original_handle_name,
370
406
  )
371
- method = getattr(instance, endpoint_info.original_name)
407
+ method = getattr(instance, endpoint_info.original_handle_name)
372
408
  endpoint = decorator(method)
373
409
  route(endpoint)
374
410
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: u-toolkit
3
- Version: 0.1.10
3
+ Version: 0.1.11
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.11
6
6
  Requires-Dist: pydantic>=2.11.3
@@ -11,7 +11,7 @@ u_toolkit/object.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
11
  u_toolkit/path.py,sha256=IkyIHcU9hKBCOZfF30FrKf4CfL-MH91fjeYF9EY7eos,128
12
12
  u_toolkit/signature.py,sha256=-Q6n28PYBYYdd2OXBKESeVkL2rYpV6EaY3IVwQmzezQ,2161
13
13
  u_toolkit/fastapi/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
14
- u_toolkit/fastapi/cbv.py,sha256=gtNhV8mQAEQNf9uIwyjlrA-ZbVwLeJenJ-f2G2Co1SE,10808
14
+ u_toolkit/fastapi/cbv.py,sha256=oxQFHWa48kZxxhL7UFeq9mUVqo9Q0QgQK_IllkyOFUk,11937
15
15
  u_toolkit/fastapi/config.py,sha256=kGpokR9XXr1KxMA1GVKYkCdKwqIQAIwOJ-v6sGbqzAQ,267
16
16
  u_toolkit/fastapi/exception.py,sha256=P9apEEQvbBMmffiHX57768gmemMeAy69-nb-1JQZAZE,4624
17
17
  u_toolkit/fastapi/helpers.py,sha256=BCMMLxa1c6BMA_rKq-hCi0iyEjrR3Z5rPMeTvgaVJB0,447
@@ -30,7 +30,7 @@ u_toolkit/sqlalchemy/type_vars.py,sha256=m2VeV41CBIK_1QX3w2kgz-n556sILAGZ-Kaz3TD
30
30
  u_toolkit/sqlalchemy/orm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
31
31
  u_toolkit/sqlalchemy/orm/fields.py,sha256=3zoYil23I6YLtc_59aHDt9w5l1NBTkePT9AfXI3DMiY,593
32
32
  u_toolkit/sqlalchemy/orm/models.py,sha256=V8vf4ps3phAmwxyaFYK7pw8Igz7h097o4QBjKB0gwC8,705
33
- u_toolkit-0.1.10.dist-info/METADATA,sha256=NWFuzJQXuyvpF3ef2r8mZsW8HBPi5CkPv2Y09UWv0bc,366
34
- u_toolkit-0.1.10.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
35
- u_toolkit-0.1.10.dist-info/entry_points.txt,sha256=hTfAYCd5vvRiqgnJk2eBsoRIiIVB9pK8WZm3Q3jjKFU,45
36
- u_toolkit-0.1.10.dist-info/RECORD,,
33
+ u_toolkit-0.1.11.dist-info/METADATA,sha256=XqvuAjDSFm1JLYjz-ag0CVE3gmJ8EKs8BbGRk92yb6o,366
34
+ u_toolkit-0.1.11.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
35
+ u_toolkit-0.1.11.dist-info/entry_points.txt,sha256=hTfAYCd5vvRiqgnJk2eBsoRIiIVB9pK8WZm3Q3jjKFU,45
36
+ u_toolkit-0.1.11.dist-info/RECORD,,