pico-ioc 1.4.0__py3-none-any.whl → 2.0.0__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.
- pico_ioc/__init__.py +91 -45
- pico_ioc/_version.py +1 -1
- pico_ioc/aop.py +247 -0
- pico_ioc/api.py +791 -207
- pico_ioc/config_runtime.py +289 -0
- pico_ioc/constants.py +10 -0
- pico_ioc/container.py +289 -189
- pico_ioc/event_bus.py +224 -0
- pico_ioc/exceptions.py +66 -0
- pico_ioc/factory.py +48 -0
- pico_ioc/locator.py +53 -0
- pico_ioc/scope.py +106 -40
- pico_ioc-2.0.0.dist-info/METADATA +230 -0
- pico_ioc-2.0.0.dist-info/RECORD +17 -0
- pico_ioc/_state.py +0 -75
- pico_ioc/builder.py +0 -294
- pico_ioc/config.py +0 -332
- pico_ioc/decorators.py +0 -158
- pico_ioc/interceptors.py +0 -56
- pico_ioc/plugins.py +0 -28
- pico_ioc/policy.py +0 -245
- pico_ioc/proxy.py +0 -129
- pico_ioc/public_api.py +0 -76
- pico_ioc/resolver.py +0 -132
- pico_ioc/scanner.py +0 -203
- pico_ioc/utils.py +0 -25
- pico_ioc-1.4.0.dist-info/METADATA +0 -241
- pico_ioc-1.4.0.dist-info/RECORD +0 -22
- {pico_ioc-1.4.0.dist-info → pico_ioc-2.0.0.dist-info}/WHEEL +0 -0
- {pico_ioc-1.4.0.dist-info → pico_ioc-2.0.0.dist-info}/licenses/LICENSE +0 -0
- {pico_ioc-1.4.0.dist-info → pico_ioc-2.0.0.dist-info}/top_level.txt +0 -0
pico_ioc/policy.py
DELETED
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
# src/pico_ioc/policy.py
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
import inspect
|
|
5
|
-
import os
|
|
6
|
-
from collections import defaultdict
|
|
7
|
-
from typing import Any, Dict, List, Optional, Tuple
|
|
8
|
-
|
|
9
|
-
from .utils import create_alias_provider
|
|
10
|
-
from .decorators import CONDITIONAL_META, PRIMARY_FLAG, ON_MISSING_META
|
|
11
|
-
from . import _state
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
# ------------------- helpers -------------------
|
|
15
|
-
|
|
16
|
-
def _target_from_provider(provider):
|
|
17
|
-
"""Try to resolve the 'real' target behind a provider closure (class, function or bound method)."""
|
|
18
|
-
fn = provider
|
|
19
|
-
try:
|
|
20
|
-
cells = getattr(fn, "__closure__", None) or ()
|
|
21
|
-
first_func, first_cls = None, None
|
|
22
|
-
for cell in cells:
|
|
23
|
-
cc = getattr(cell, "cell_contents", None)
|
|
24
|
-
if inspect.ismethod(cc):
|
|
25
|
-
return cc
|
|
26
|
-
if first_func is None and inspect.isfunction(cc):
|
|
27
|
-
first_func = cc
|
|
28
|
-
elif first_cls is None and inspect.isclass(cc):
|
|
29
|
-
first_cls = cc
|
|
30
|
-
return first_func or first_cls or fn
|
|
31
|
-
except Exception:
|
|
32
|
-
return fn
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def _owner_func(obj):
|
|
36
|
-
"""If obj is a bound method, return the unbound function on its owner class."""
|
|
37
|
-
try:
|
|
38
|
-
if inspect.ismethod(obj) and getattr(obj, "__self__", None) is not None:
|
|
39
|
-
owner = obj.__self__.__class__
|
|
40
|
-
name = getattr(obj, "__name__", None)
|
|
41
|
-
if name and hasattr(owner, name):
|
|
42
|
-
cand = getattr(owner, name)
|
|
43
|
-
if inspect.isfunction(cand):
|
|
44
|
-
return cand
|
|
45
|
-
except Exception:
|
|
46
|
-
pass
|
|
47
|
-
return None
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
def _find_attribute_on_target(target: Any, attr_name: str) -> Any:
|
|
51
|
-
"""Look for metadata on object, its underlying function, or owner class method."""
|
|
52
|
-
val = getattr(target, attr_name, None)
|
|
53
|
-
if val is not None:
|
|
54
|
-
return val
|
|
55
|
-
base_func = getattr(target, "__func__", None)
|
|
56
|
-
if base_func:
|
|
57
|
-
val = getattr(base_func, attr_name, None)
|
|
58
|
-
if val is not None:
|
|
59
|
-
return val
|
|
60
|
-
of = _owner_func(target)
|
|
61
|
-
if of:
|
|
62
|
-
val = getattr(of, attr_name, None)
|
|
63
|
-
if val is not None:
|
|
64
|
-
return val
|
|
65
|
-
return None
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
def _has_flag(obj, flag_name: str) -> bool:
|
|
69
|
-
return bool(_find_attribute_on_target(obj, flag_name))
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def _get_meta(obj, meta_name: str) -> Any:
|
|
73
|
-
return _find_attribute_on_target(obj, meta_name)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
def _on_missing_meta(target):
|
|
77
|
-
"""Normalize @on_missing metadata."""
|
|
78
|
-
meta = _get_meta(target, ON_MISSING_META)
|
|
79
|
-
if not meta:
|
|
80
|
-
return None
|
|
81
|
-
return (meta.get("selector"), int(meta.get("priority", 0)))
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
def _conditional_active(target, *, profiles: List[str]) -> bool:
|
|
85
|
-
"""Check if target is active given profiles/env/predicate."""
|
|
86
|
-
meta = _get_meta(target, CONDITIONAL_META)
|
|
87
|
-
if not meta:
|
|
88
|
-
return True
|
|
89
|
-
|
|
90
|
-
profs = tuple(meta.get("profiles", ()))
|
|
91
|
-
req_env = tuple(meta.get("require_env", ()))
|
|
92
|
-
pred = meta.get("predicate")
|
|
93
|
-
|
|
94
|
-
if profs and (not profiles or not any(p in profs for p in profiles)):
|
|
95
|
-
return False
|
|
96
|
-
if req_env and not all(os.getenv(k) not in (None, "") for k in req_env):
|
|
97
|
-
return False
|
|
98
|
-
if callable(pred):
|
|
99
|
-
try:
|
|
100
|
-
if not bool(pred()):
|
|
101
|
-
return False
|
|
102
|
-
except Exception:
|
|
103
|
-
return False
|
|
104
|
-
return True
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
# ------------------- public API -------------------
|
|
108
|
-
|
|
109
|
-
def apply_policy(container, *, profiles: Optional[List[str]] = None) -> None:
|
|
110
|
-
"""Run all policy stages on the given container."""
|
|
111
|
-
profiles = list(profiles or [])
|
|
112
|
-
|
|
113
|
-
_filter_inactive_factory_candidates(container, profiles=profiles)
|
|
114
|
-
_collapse_identical_keys_preferring_primary(container)
|
|
115
|
-
_create_active_component_base_aliases(container, profiles=profiles)
|
|
116
|
-
apply_defaults(container)
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
def apply_defaults(container) -> None:
|
|
120
|
-
"""Bind defaults declared with @on_missing if no binding exists for selector."""
|
|
121
|
-
defaults: dict[Any, list[tuple[int, Any]]] = {}
|
|
122
|
-
|
|
123
|
-
# class components
|
|
124
|
-
for prov_key, meta in list(container._providers.items()): # type: ignore
|
|
125
|
-
if not isinstance(prov_key, type):
|
|
126
|
-
continue
|
|
127
|
-
target = _target_from_provider(meta.get("factory"))
|
|
128
|
-
om = _on_missing_meta(target)
|
|
129
|
-
if om:
|
|
130
|
-
selector, prio = om
|
|
131
|
-
defaults.setdefault(selector, []).append((prio, prov_key))
|
|
132
|
-
|
|
133
|
-
# factory provides
|
|
134
|
-
for prov_key, meta in list(container._providers.items()): # type: ignore
|
|
135
|
-
prov = meta.get("factory")
|
|
136
|
-
base = getattr(prov, "_pico_alias_for", None)
|
|
137
|
-
if base is None:
|
|
138
|
-
continue
|
|
139
|
-
target = _target_from_provider(prov)
|
|
140
|
-
om = _on_missing_meta(target)
|
|
141
|
-
if om:
|
|
142
|
-
_sel, prio = om
|
|
143
|
-
defaults.setdefault(base, []).append((prio, prov_key))
|
|
144
|
-
|
|
145
|
-
# bind highest priority candidate
|
|
146
|
-
for base, cands in defaults.items():
|
|
147
|
-
if container.has(base):
|
|
148
|
-
continue
|
|
149
|
-
cands.sort(key=lambda t: t[0], reverse=True)
|
|
150
|
-
chosen_key = cands[0][1]
|
|
151
|
-
|
|
152
|
-
def _delegate(_k=chosen_key):
|
|
153
|
-
def _f():
|
|
154
|
-
return container.get(_k)
|
|
155
|
-
return _f
|
|
156
|
-
|
|
157
|
-
container.bind(base, _delegate(), lazy=True)
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
# ------------------- stages -------------------
|
|
161
|
-
|
|
162
|
-
def _filter_inactive_factory_candidates(container, *, profiles: List[str]) -> None:
|
|
163
|
-
"""Remove factories inactive under profiles/env/predicate."""
|
|
164
|
-
to_delete = []
|
|
165
|
-
for prov_key, meta in list(container._providers.items()): # type: ignore
|
|
166
|
-
prov = meta.get("factory")
|
|
167
|
-
base = getattr(prov, "_pico_alias_for", None)
|
|
168
|
-
if base is None:
|
|
169
|
-
continue
|
|
170
|
-
target = _target_from_provider(prov)
|
|
171
|
-
if not _conditional_active(target, profiles=profiles):
|
|
172
|
-
to_delete.append(prov_key)
|
|
173
|
-
for k in to_delete:
|
|
174
|
-
container._providers.pop(k, None) # type: ignore
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
def _collapse_identical_keys_preferring_primary(container) -> None:
|
|
178
|
-
"""For multiple factory candidates of same base, keep one (prefer @primary)."""
|
|
179
|
-
groups: dict[Any, list[tuple[Any, dict]]] = defaultdict(list)
|
|
180
|
-
for k, m in list(container._providers.items()): # type: ignore
|
|
181
|
-
prov = m.get("factory")
|
|
182
|
-
base = getattr(prov, "_pico_alias_for", None)
|
|
183
|
-
if base is not None:
|
|
184
|
-
groups[base].append((k, m))
|
|
185
|
-
|
|
186
|
-
for base, entries in groups.items():
|
|
187
|
-
if not entries:
|
|
188
|
-
continue
|
|
189
|
-
if len(entries) == 1:
|
|
190
|
-
keep, _ = entries[0]
|
|
191
|
-
if (not container.has(base)) or (base != keep):
|
|
192
|
-
factory = create_alias_provider(container, keep)
|
|
193
|
-
container.bind(base, factory, lazy=True)
|
|
194
|
-
continue
|
|
195
|
-
|
|
196
|
-
prims = [(kk, mm) for (kk, mm) in entries if _has_flag(_target_from_provider(mm["factory"]), PRIMARY_FLAG)]
|
|
197
|
-
if prims:
|
|
198
|
-
keep, _ = prims[0]
|
|
199
|
-
if (not container.has(base)) or (base != keep):
|
|
200
|
-
factory = create_alias_provider(container, keep)
|
|
201
|
-
container.bind(base, factory, lazy=True)
|
|
202
|
-
for kk, _mm in entries:
|
|
203
|
-
if kk != keep and kk != base:
|
|
204
|
-
container._providers.pop(kk, None) # type: ignore
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
def _create_active_component_base_aliases(container, *, profiles: List[str]) -> None:
|
|
208
|
-
"""For active class components, create base->impl aliases (prefer @primary)."""
|
|
209
|
-
impls: List[Tuple[type, dict]] = []
|
|
210
|
-
for key, meta in list(container._providers.items()): # type: ignore
|
|
211
|
-
if not isinstance(key, type):
|
|
212
|
-
continue
|
|
213
|
-
tgt = _target_from_provider(meta.get("factory"))
|
|
214
|
-
if _conditional_active(tgt, profiles=profiles):
|
|
215
|
-
impls.append((key, meta))
|
|
216
|
-
|
|
217
|
-
base_to_impls: Dict[Any, List[Tuple[Any, dict]]] = defaultdict(list)
|
|
218
|
-
for impl_key, impl_meta in impls:
|
|
219
|
-
for base in getattr(impl_key, "__mro__", ())[1:]:
|
|
220
|
-
if base is object:
|
|
221
|
-
break
|
|
222
|
-
base_to_impls[base].append((impl_key, impl_meta))
|
|
223
|
-
|
|
224
|
-
for base, impl_list in base_to_impls.items():
|
|
225
|
-
if container.has(base) or not impl_list:
|
|
226
|
-
continue
|
|
227
|
-
|
|
228
|
-
regular, fallbacks = [], []
|
|
229
|
-
for ik, im in impl_list:
|
|
230
|
-
tgt = _target_from_provider(im["factory"])
|
|
231
|
-
(fallbacks if _on_missing_meta(tgt) else regular).append((ik, im))
|
|
232
|
-
|
|
233
|
-
def pick(cands: List[Tuple[Any, dict]]) -> Optional[Any]:
|
|
234
|
-
if not cands:
|
|
235
|
-
return None
|
|
236
|
-
prims = [(ik, im) for ik, im in cands if _has_flag(_target_from_provider(im["factory"]), PRIMARY_FLAG)]
|
|
237
|
-
return prims[0][0] if prims else cands[0][0]
|
|
238
|
-
|
|
239
|
-
chosen = pick(regular) or pick(fallbacks)
|
|
240
|
-
if not chosen:
|
|
241
|
-
continue
|
|
242
|
-
|
|
243
|
-
factory = create_alias_provider(container, chosen)
|
|
244
|
-
container.bind(base, factory, lazy=True)
|
|
245
|
-
|
pico_ioc/proxy.py
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
# src/pico_ioc/proxy.py
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
import inspect
|
|
5
|
-
from functools import lru_cache
|
|
6
|
-
from typing import Any, Callable, Sequence
|
|
7
|
-
|
|
8
|
-
from .interceptors import Invocation, dispatch, MethodInterceptor
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
class ComponentProxy:
|
|
12
|
-
"""Proxy for lazy components. Creates the real object only when accessed."""
|
|
13
|
-
def __init__(self, object_creator: Callable[[], Any]):
|
|
14
|
-
object.__setattr__(self, "_object_creator", object_creator)
|
|
15
|
-
object.__setattr__(self, "__real_object", None)
|
|
16
|
-
|
|
17
|
-
def _get_real_object(self) -> Any:
|
|
18
|
-
real = object.__getattribute__(self, "__real_object")
|
|
19
|
-
if real is None:
|
|
20
|
-
real = object.__getattribute__(self, "_object_creator")()
|
|
21
|
-
object.__setattr__(self, "__real_object", real)
|
|
22
|
-
return real
|
|
23
|
-
|
|
24
|
-
@property
|
|
25
|
-
def __class__(self):
|
|
26
|
-
return self._get_real_object().__class__
|
|
27
|
-
|
|
28
|
-
def __getattr__(self, name): return getattr(self._get_real_object(), name)
|
|
29
|
-
def __setattr__(self, name, value): setattr(self._get_real_object(), name, value)
|
|
30
|
-
def __delattr__(self, name): delattr(self._get_real_object(), name)
|
|
31
|
-
def __str__(self): return str(self._get_real_object())
|
|
32
|
-
def __repr__(self): return repr(self._get_real_object())
|
|
33
|
-
def __dir__(self): return dir(self._get_real_object())
|
|
34
|
-
|
|
35
|
-
# container-like behavior
|
|
36
|
-
def __len__(self): return len(self._get_real_object())
|
|
37
|
-
def __getitem__(self, key): return self._get_real_object()[key]
|
|
38
|
-
def __setitem__(self, key, value): self._get_real_object()[key] = value
|
|
39
|
-
def __delitem__(self, key): del self._get_real_object()[key]
|
|
40
|
-
def __iter__(self): return iter(self._get_real_object())
|
|
41
|
-
def __reversed__(self): return reversed(self._get_real_object())
|
|
42
|
-
def __contains__(self, item): return item in self._get_real_object()
|
|
43
|
-
|
|
44
|
-
# operators
|
|
45
|
-
def __add__(self, other): return self._get_real_object() + other
|
|
46
|
-
def __sub__(self, other): return self._get_real_object() - other
|
|
47
|
-
def __mul__(self, other): return self._get_real_object() * other
|
|
48
|
-
def __matmul__(self, other): return self._get_real_object() @ other
|
|
49
|
-
def __truediv__(self, other): return self._get_real_object() / other
|
|
50
|
-
def __floordiv__(self, other): return self._get_real_object() // other
|
|
51
|
-
def __mod__(self, other): return self._get_real_object() % other
|
|
52
|
-
def __divmod__(self, other): return divmod(self._get_real_object(), other)
|
|
53
|
-
def __pow__(self, other, modulo=None): return pow(self._get_real_object(), other, modulo)
|
|
54
|
-
def __lshift__(self, other): return self._get_real_object() << other
|
|
55
|
-
def __rshift__(self, other): return self._get_real_object() >> other
|
|
56
|
-
def __and__(self, other): return self._get_real_object() & other
|
|
57
|
-
def __xor__(self, other): return self._get_real_object() ^ other
|
|
58
|
-
def __or__(self, other): return self._get_real_object() | other
|
|
59
|
-
|
|
60
|
-
# reflected operators
|
|
61
|
-
def __radd__(self, other): return other + self._get_real_object()
|
|
62
|
-
def __rsub__(self, other): return other - self._get_real_object()
|
|
63
|
-
def __rmul__(self, other): return other * self._get_real_object()
|
|
64
|
-
def __rmatmul__(self, other): return other @ self._get_real_object()
|
|
65
|
-
def __rtruediv__(self, other): return other / self._get_real_object()
|
|
66
|
-
def __rfloordiv__(self, other): return other // self._get_real_object()
|
|
67
|
-
def __rmod__(self, other): return other % self._get_real_object()
|
|
68
|
-
def __rdivmod__(self, other): return divmod(other, self._get_real_object())
|
|
69
|
-
def __rpow__(self, other): return pow(other, self._get_real_object())
|
|
70
|
-
def __rlshift__(self, other): return other << self._get_real_object()
|
|
71
|
-
def __rrshift__(self, other): return other >> self._get_real_object()
|
|
72
|
-
def __rand__(self, other): return other & self._get_real_object()
|
|
73
|
-
def __rxor__(self, other): return other ^ self._get_real_object()
|
|
74
|
-
def __ror__(self, other): return other | self._get_real_object()
|
|
75
|
-
|
|
76
|
-
# misc
|
|
77
|
-
def __neg__(self): return -self._get_real_object()
|
|
78
|
-
def __pos__(self): return +self._get_real_object()
|
|
79
|
-
def __abs__(self): return abs(self._get_real_object())
|
|
80
|
-
def __invert__(self): return ~self._get_real_object()
|
|
81
|
-
def __eq__(self, other): return self._get_real_object() == other
|
|
82
|
-
def __ne__(self, other): return self._get_real_object() != other
|
|
83
|
-
def __lt__(self, other): return self._get_real_object() < other
|
|
84
|
-
def __le__(self, other): return self._get_real_object() <= other
|
|
85
|
-
def __gt__(self, other): return self._get_real_object() > other
|
|
86
|
-
def __ge__(self, other): return self._get_real_object() >= other
|
|
87
|
-
def __hash__(self): return hash(self._get_real_object())
|
|
88
|
-
def __bool__(self): return bool(self._get_real_object())
|
|
89
|
-
|
|
90
|
-
# callables & context
|
|
91
|
-
def __call__(self, *args, **kwargs): return self._get_real_object()(*args, **kwargs)
|
|
92
|
-
def __enter__(self): return self._get_real_object().__enter__()
|
|
93
|
-
def __exit__(self, exc_type, exc_val, exc_tb): return self._get_real_object().__exit__(exc_type, exc_val, exc_tb)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
class IoCProxy:
|
|
97
|
-
"""Proxy that wraps an object and applies MethodInterceptors on method calls."""
|
|
98
|
-
__slots__ = ("_target", "_interceptors")
|
|
99
|
-
|
|
100
|
-
def __init__(self, target: object, interceptors: Sequence[MethodInterceptor]):
|
|
101
|
-
self._target = target
|
|
102
|
-
self._interceptors = tuple(interceptors)
|
|
103
|
-
|
|
104
|
-
def __getattr__(self, name: str) -> Any:
|
|
105
|
-
attr = getattr(self._target, name)
|
|
106
|
-
if not callable(attr):
|
|
107
|
-
return attr
|
|
108
|
-
if hasattr(attr, "__get__"):
|
|
109
|
-
bound_fn = attr.__get__(self._target, type(self._target))
|
|
110
|
-
else:
|
|
111
|
-
bound_fn = attr
|
|
112
|
-
|
|
113
|
-
@lru_cache(maxsize=None)
|
|
114
|
-
def _wrap(fn: Callable[..., Any]):
|
|
115
|
-
if inspect.iscoroutinefunction(fn):
|
|
116
|
-
async def aw(*args, **kwargs):
|
|
117
|
-
inv = Invocation(self._target, name, fn, args, kwargs)
|
|
118
|
-
return await dispatch(self._interceptors, inv)
|
|
119
|
-
return aw
|
|
120
|
-
else:
|
|
121
|
-
def sw(*args, **kwargs):
|
|
122
|
-
inv = Invocation(self._target, name, fn, args, kwargs)
|
|
123
|
-
res = dispatch(self._interceptors, inv)
|
|
124
|
-
if inspect.isawaitable(res):
|
|
125
|
-
raise RuntimeError(f"Async interceptor on sync method: {name}")
|
|
126
|
-
return res
|
|
127
|
-
return sw
|
|
128
|
-
return _wrap(bound_fn)
|
|
129
|
-
|
pico_ioc/public_api.py
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
# pico_ioc/public_api.py
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
import importlib
|
|
5
|
-
import inspect
|
|
6
|
-
import pkgutil
|
|
7
|
-
import sys
|
|
8
|
-
from types import ModuleType
|
|
9
|
-
from typing import Dict, Iterable, Optional, Tuple
|
|
10
|
-
|
|
11
|
-
from .decorators import COMPONENT_FLAG, FACTORY_FLAG, PLUGIN_FLAG
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
def export_public_symbols_decorated(
|
|
15
|
-
*packages: str,
|
|
16
|
-
include_also: Optional[Iterable[str]] = None,
|
|
17
|
-
include_plugins: bool = True,
|
|
18
|
-
):
|
|
19
|
-
index: Dict[str, Tuple[str, str]] = {}
|
|
20
|
-
|
|
21
|
-
def _collect(m: ModuleType):
|
|
22
|
-
names = getattr(m, "__all__", None)
|
|
23
|
-
if isinstance(names, (list, tuple, set)):
|
|
24
|
-
for n in names:
|
|
25
|
-
if hasattr(m, n):
|
|
26
|
-
index.setdefault(n, (m.__name__, n))
|
|
27
|
-
return
|
|
28
|
-
|
|
29
|
-
for n, obj in m.__dict__.items():
|
|
30
|
-
if not inspect.isclass(obj):
|
|
31
|
-
continue
|
|
32
|
-
is_component = getattr(obj, COMPONENT_FLAG, False)
|
|
33
|
-
is_factory = getattr(obj, FACTORY_FLAG, False)
|
|
34
|
-
is_plugin = include_plugins and getattr(obj, PLUGIN_FLAG, False)
|
|
35
|
-
if is_component or is_factory or is_plugin:
|
|
36
|
-
index.setdefault(n, (m.__name__, n))
|
|
37
|
-
|
|
38
|
-
for pkg_name in packages:
|
|
39
|
-
try:
|
|
40
|
-
base = importlib.import_module(pkg_name)
|
|
41
|
-
except Exception:
|
|
42
|
-
continue
|
|
43
|
-
if hasattr(base, "__path__"):
|
|
44
|
-
prefix = base.__name__ + "."
|
|
45
|
-
for _, modname, _ in pkgutil.walk_packages(base.__path__, prefix):
|
|
46
|
-
try:
|
|
47
|
-
m = importlib.import_module(modname)
|
|
48
|
-
except Exception:
|
|
49
|
-
continue
|
|
50
|
-
_collect(m)
|
|
51
|
-
else:
|
|
52
|
-
_collect(base)
|
|
53
|
-
|
|
54
|
-
for qual in tuple(include_also or ()):
|
|
55
|
-
modname, _, attr = qual.partition(":")
|
|
56
|
-
if modname and attr:
|
|
57
|
-
try:
|
|
58
|
-
m = importlib.import_module(modname)
|
|
59
|
-
if hasattr(m, attr):
|
|
60
|
-
index.setdefault(attr, (m.__name__, attr))
|
|
61
|
-
except Exception:
|
|
62
|
-
pass
|
|
63
|
-
|
|
64
|
-
def __getattr__(name: str):
|
|
65
|
-
try:
|
|
66
|
-
modname, attr = index[name]
|
|
67
|
-
except KeyError as e:
|
|
68
|
-
raise AttributeError(f"module has no attribute {name!r}") from e
|
|
69
|
-
mod = sys.modules.get(modname) or importlib.import_module(modname)
|
|
70
|
-
return getattr(mod, attr)
|
|
71
|
-
|
|
72
|
-
def __dir__():
|
|
73
|
-
return sorted(index.keys())
|
|
74
|
-
|
|
75
|
-
return __getattr__, __dir__
|
|
76
|
-
|
pico_ioc/resolver.py
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
# src/pico_ioc/resolver.py
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
import inspect
|
|
5
|
-
from typing import Any, Annotated, Callable, get_args, get_origin, get_type_hints
|
|
6
|
-
from contextvars import ContextVar
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
_path: ContextVar[list[tuple[str, str]]] = ContextVar("pico_resolve_path", default=[])
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def _get_hints(obj, owner_cls=None) -> dict:
|
|
13
|
-
"""Return type hints with include_extras=True, using correct globals/locals."""
|
|
14
|
-
mod = inspect.getmodule(obj)
|
|
15
|
-
g = getattr(mod, "__dict__", {})
|
|
16
|
-
l = vars(owner_cls) if owner_cls is not None else None
|
|
17
|
-
return get_type_hints(obj, globalns=g, localns=l, include_extras=True)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def _is_collection_hint(tp) -> bool:
|
|
21
|
-
origin = get_origin(tp) or tp
|
|
22
|
-
return origin in (list, tuple)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def _base_and_qualifiers_from_hint(tp):
|
|
26
|
-
"""
|
|
27
|
-
Extract (base, qualifiers, container_kind) from a type hint.
|
|
28
|
-
Supports list[T], tuple[T], Annotated[T, "qual1", ...].
|
|
29
|
-
"""
|
|
30
|
-
origin = get_origin(tp) or tp
|
|
31
|
-
args = get_args(tp) or ()
|
|
32
|
-
container_kind = list if origin is list else tuple
|
|
33
|
-
|
|
34
|
-
if not args:
|
|
35
|
-
return (object, (), container_kind)
|
|
36
|
-
|
|
37
|
-
inner = args[0]
|
|
38
|
-
if get_origin(inner) is Annotated:
|
|
39
|
-
base, *extras = get_args(inner)
|
|
40
|
-
quals = tuple(a for a in extras if isinstance(a, str))
|
|
41
|
-
return (base, quals, container_kind)
|
|
42
|
-
|
|
43
|
-
return (inner, (), container_kind)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
class Resolver:
|
|
47
|
-
def __init__(self, container, *, prefer_name_first: bool = True):
|
|
48
|
-
self.c = container
|
|
49
|
-
self._prefer_name_first = bool(prefer_name_first)
|
|
50
|
-
|
|
51
|
-
# --- core resolution ---
|
|
52
|
-
|
|
53
|
-
def _resolve_dependencies_for_callable(self, fn: Callable, owner_cls: Any = None) -> dict:
|
|
54
|
-
sig = inspect.signature(fn)
|
|
55
|
-
hints = _get_hints(fn, owner_cls=owner_cls)
|
|
56
|
-
kwargs = {}
|
|
57
|
-
|
|
58
|
-
path_owner = getattr(owner_cls, "__name__", getattr(fn, "__qualname__", "callable"))
|
|
59
|
-
if fn.__name__ == "__init__" and owner_cls:
|
|
60
|
-
path_owner = f"{path_owner}.__init__"
|
|
61
|
-
|
|
62
|
-
for name, param in sig.parameters.items():
|
|
63
|
-
if param.kind in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD) or name == "self":
|
|
64
|
-
continue
|
|
65
|
-
|
|
66
|
-
ann = hints.get(name, param.annotation)
|
|
67
|
-
st = _path.get()
|
|
68
|
-
_path.set(st + [(path_owner, name)])
|
|
69
|
-
try:
|
|
70
|
-
kwargs[name] = self._resolve_param(name, ann)
|
|
71
|
-
except NameError as e:
|
|
72
|
-
if param.default is not inspect.Parameter.empty:
|
|
73
|
-
_path.set(st)
|
|
74
|
-
continue
|
|
75
|
-
if "(required by" in str(e):
|
|
76
|
-
raise
|
|
77
|
-
chain = " -> ".join(f"{owner}.{param}" for owner, param in _path.get())
|
|
78
|
-
raise NameError(f"{e} (required by {chain})") from e
|
|
79
|
-
finally:
|
|
80
|
-
cur = _path.get()
|
|
81
|
-
if cur:
|
|
82
|
-
_path.set(cur[:-1])
|
|
83
|
-
return kwargs
|
|
84
|
-
|
|
85
|
-
def create_instance(self, cls: type) -> Any:
|
|
86
|
-
"""Instantiate a class by resolving its __init__ dependencies."""
|
|
87
|
-
ctor_kwargs = self._resolve_dependencies_for_callable(cls.__init__, owner_cls=cls)
|
|
88
|
-
return cls(**ctor_kwargs)
|
|
89
|
-
|
|
90
|
-
def kwargs_for_callable(self, fn: Callable, *, owner_cls: Any = None) -> dict:
|
|
91
|
-
"""Resolve all keyword arguments for any callable."""
|
|
92
|
-
return self._resolve_dependencies_for_callable(fn, owner_cls=owner_cls)
|
|
93
|
-
|
|
94
|
-
# --- param resolution ---
|
|
95
|
-
|
|
96
|
-
def _notify_resolve(self, key, ann, quals=()):
|
|
97
|
-
for ci in getattr(self.c, "_container_interceptors", ()):
|
|
98
|
-
try:
|
|
99
|
-
ci.on_resolve(key, ann, tuple(quals) if quals else ())
|
|
100
|
-
except Exception:
|
|
101
|
-
pass
|
|
102
|
-
|
|
103
|
-
def _resolve_param(self, name: str, ann: Any):
|
|
104
|
-
# collections
|
|
105
|
-
if _is_collection_hint(ann):
|
|
106
|
-
base, quals, kind = _base_and_qualifiers_from_hint(ann)
|
|
107
|
-
self._notify_resolve(base, ann, quals)
|
|
108
|
-
items = self.c._resolve_all_for_base(base, qualifiers=quals)
|
|
109
|
-
return list(items) if kind is list else tuple(items)
|
|
110
|
-
|
|
111
|
-
# precedence
|
|
112
|
-
if self._prefer_name_first and self.c.has(name):
|
|
113
|
-
self._notify_resolve(name, ann, ())
|
|
114
|
-
return self.c.get(name)
|
|
115
|
-
|
|
116
|
-
if ann is not inspect._empty and self.c.has(ann):
|
|
117
|
-
self._notify_resolve(ann, ann, ())
|
|
118
|
-
return self.c.get(ann)
|
|
119
|
-
|
|
120
|
-
if ann is not inspect._empty and isinstance(ann, type):
|
|
121
|
-
for base in ann.__mro__[1:]:
|
|
122
|
-
if self.c.has(base):
|
|
123
|
-
self._notify_resolve(base, ann, ())
|
|
124
|
-
return self.c.get(base)
|
|
125
|
-
|
|
126
|
-
if self.c.has(name):
|
|
127
|
-
self._notify_resolve(name, ann, ())
|
|
128
|
-
return self.c.get(name)
|
|
129
|
-
|
|
130
|
-
missing = ann if ann is not inspect._empty else name
|
|
131
|
-
raise NameError(f"No provider found for key {missing!r}")
|
|
132
|
-
|