tnfr 4.5.2__py3-none-any.whl → 7.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.

Potentially problematic release.


This version of tnfr might be problematic. Click here for more details.

Files changed (195) hide show
  1. tnfr/__init__.py +275 -51
  2. tnfr/__init__.pyi +33 -0
  3. tnfr/_compat.py +10 -0
  4. tnfr/_generated_version.py +34 -0
  5. tnfr/_version.py +49 -0
  6. tnfr/_version.pyi +7 -0
  7. tnfr/alias.py +117 -31
  8. tnfr/alias.pyi +108 -0
  9. tnfr/cache.py +6 -572
  10. tnfr/cache.pyi +16 -0
  11. tnfr/callback_utils.py +16 -38
  12. tnfr/callback_utils.pyi +79 -0
  13. tnfr/cli/__init__.py +34 -14
  14. tnfr/cli/__init__.pyi +26 -0
  15. tnfr/cli/arguments.py +211 -28
  16. tnfr/cli/arguments.pyi +27 -0
  17. tnfr/cli/execution.py +470 -50
  18. tnfr/cli/execution.pyi +70 -0
  19. tnfr/cli/utils.py +18 -3
  20. tnfr/cli/utils.pyi +8 -0
  21. tnfr/config/__init__.py +13 -0
  22. tnfr/config/__init__.pyi +10 -0
  23. tnfr/{constants_glyphs.py → config/constants.py} +26 -20
  24. tnfr/config/constants.pyi +12 -0
  25. tnfr/config/feature_flags.py +83 -0
  26. tnfr/{config.py → config/init.py} +11 -7
  27. tnfr/config/init.pyi +8 -0
  28. tnfr/config/operator_names.py +93 -0
  29. tnfr/config/operator_names.pyi +28 -0
  30. tnfr/config/presets.py +84 -0
  31. tnfr/config/presets.pyi +7 -0
  32. tnfr/constants/__init__.py +80 -29
  33. tnfr/constants/__init__.pyi +92 -0
  34. tnfr/constants/aliases.py +31 -0
  35. tnfr/constants/core.py +4 -4
  36. tnfr/constants/core.pyi +17 -0
  37. tnfr/constants/init.py +1 -1
  38. tnfr/constants/init.pyi +12 -0
  39. tnfr/constants/metric.py +7 -15
  40. tnfr/constants/metric.pyi +19 -0
  41. tnfr/dynamics/__init__.py +165 -633
  42. tnfr/dynamics/__init__.pyi +82 -0
  43. tnfr/dynamics/adaptation.py +267 -0
  44. tnfr/dynamics/aliases.py +23 -0
  45. tnfr/dynamics/coordination.py +385 -0
  46. tnfr/dynamics/dnfr.py +2283 -400
  47. tnfr/dynamics/dnfr.pyi +24 -0
  48. tnfr/dynamics/integrators.py +406 -98
  49. tnfr/dynamics/integrators.pyi +34 -0
  50. tnfr/dynamics/runtime.py +881 -0
  51. tnfr/dynamics/sampling.py +10 -5
  52. tnfr/dynamics/sampling.pyi +7 -0
  53. tnfr/dynamics/selectors.py +719 -0
  54. tnfr/execution.py +70 -48
  55. tnfr/execution.pyi +45 -0
  56. tnfr/flatten.py +13 -9
  57. tnfr/flatten.pyi +21 -0
  58. tnfr/gamma.py +66 -53
  59. tnfr/gamma.pyi +34 -0
  60. tnfr/glyph_history.py +110 -52
  61. tnfr/glyph_history.pyi +35 -0
  62. tnfr/glyph_runtime.py +16 -0
  63. tnfr/glyph_runtime.pyi +9 -0
  64. tnfr/immutable.py +69 -28
  65. tnfr/immutable.pyi +34 -0
  66. tnfr/initialization.py +16 -16
  67. tnfr/initialization.pyi +65 -0
  68. tnfr/io.py +6 -240
  69. tnfr/io.pyi +16 -0
  70. tnfr/locking.pyi +7 -0
  71. tnfr/mathematics/__init__.py +81 -0
  72. tnfr/mathematics/backend.py +426 -0
  73. tnfr/mathematics/dynamics.py +398 -0
  74. tnfr/mathematics/epi.py +254 -0
  75. tnfr/mathematics/generators.py +222 -0
  76. tnfr/mathematics/metrics.py +119 -0
  77. tnfr/mathematics/operators.py +233 -0
  78. tnfr/mathematics/operators_factory.py +71 -0
  79. tnfr/mathematics/projection.py +78 -0
  80. tnfr/mathematics/runtime.py +173 -0
  81. tnfr/mathematics/spaces.py +247 -0
  82. tnfr/mathematics/transforms.py +292 -0
  83. tnfr/metrics/__init__.py +10 -10
  84. tnfr/metrics/__init__.pyi +20 -0
  85. tnfr/metrics/coherence.py +993 -324
  86. tnfr/metrics/common.py +23 -16
  87. tnfr/metrics/common.pyi +46 -0
  88. tnfr/metrics/core.py +251 -35
  89. tnfr/metrics/core.pyi +13 -0
  90. tnfr/metrics/diagnosis.py +708 -111
  91. tnfr/metrics/diagnosis.pyi +85 -0
  92. tnfr/metrics/export.py +27 -15
  93. tnfr/metrics/glyph_timing.py +232 -42
  94. tnfr/metrics/reporting.py +33 -22
  95. tnfr/metrics/reporting.pyi +12 -0
  96. tnfr/metrics/sense_index.py +987 -43
  97. tnfr/metrics/sense_index.pyi +9 -0
  98. tnfr/metrics/trig.py +214 -23
  99. tnfr/metrics/trig.pyi +13 -0
  100. tnfr/metrics/trig_cache.py +115 -22
  101. tnfr/metrics/trig_cache.pyi +10 -0
  102. tnfr/node.py +542 -136
  103. tnfr/node.pyi +178 -0
  104. tnfr/observers.py +152 -35
  105. tnfr/observers.pyi +31 -0
  106. tnfr/ontosim.py +23 -19
  107. tnfr/ontosim.pyi +28 -0
  108. tnfr/operators/__init__.py +601 -82
  109. tnfr/operators/__init__.pyi +45 -0
  110. tnfr/operators/definitions.py +513 -0
  111. tnfr/operators/definitions.pyi +78 -0
  112. tnfr/operators/grammar.py +760 -0
  113. tnfr/operators/jitter.py +107 -38
  114. tnfr/operators/jitter.pyi +11 -0
  115. tnfr/operators/registry.py +75 -0
  116. tnfr/operators/registry.pyi +13 -0
  117. tnfr/operators/remesh.py +149 -88
  118. tnfr/py.typed +0 -0
  119. tnfr/rng.py +46 -143
  120. tnfr/rng.pyi +14 -0
  121. tnfr/schemas/__init__.py +8 -0
  122. tnfr/schemas/grammar.json +94 -0
  123. tnfr/selector.py +25 -19
  124. tnfr/selector.pyi +19 -0
  125. tnfr/sense.py +72 -62
  126. tnfr/sense.pyi +23 -0
  127. tnfr/structural.py +522 -262
  128. tnfr/structural.pyi +69 -0
  129. tnfr/telemetry/__init__.py +35 -0
  130. tnfr/telemetry/cache_metrics.py +226 -0
  131. tnfr/telemetry/nu_f.py +423 -0
  132. tnfr/telemetry/nu_f.pyi +123 -0
  133. tnfr/telemetry/verbosity.py +37 -0
  134. tnfr/tokens.py +1 -3
  135. tnfr/tokens.pyi +36 -0
  136. tnfr/trace.py +270 -113
  137. tnfr/trace.pyi +40 -0
  138. tnfr/types.py +574 -6
  139. tnfr/types.pyi +331 -0
  140. tnfr/units.py +69 -0
  141. tnfr/units.pyi +16 -0
  142. tnfr/utils/__init__.py +217 -0
  143. tnfr/utils/__init__.pyi +202 -0
  144. tnfr/utils/cache.py +2395 -0
  145. tnfr/utils/cache.pyi +468 -0
  146. tnfr/utils/chunks.py +104 -0
  147. tnfr/utils/chunks.pyi +21 -0
  148. tnfr/{collections_utils.py → utils/data.py} +147 -90
  149. tnfr/utils/data.pyi +64 -0
  150. tnfr/utils/graph.py +85 -0
  151. tnfr/utils/graph.pyi +10 -0
  152. tnfr/utils/init.py +770 -0
  153. tnfr/utils/init.pyi +78 -0
  154. tnfr/utils/io.py +456 -0
  155. tnfr/{helpers → utils}/numeric.py +51 -24
  156. tnfr/utils/numeric.pyi +21 -0
  157. tnfr/validation/__init__.py +113 -0
  158. tnfr/validation/__init__.pyi +77 -0
  159. tnfr/validation/compatibility.py +95 -0
  160. tnfr/validation/compatibility.pyi +6 -0
  161. tnfr/validation/grammar.py +71 -0
  162. tnfr/validation/grammar.pyi +40 -0
  163. tnfr/validation/graph.py +138 -0
  164. tnfr/validation/graph.pyi +17 -0
  165. tnfr/validation/rules.py +281 -0
  166. tnfr/validation/rules.pyi +55 -0
  167. tnfr/validation/runtime.py +263 -0
  168. tnfr/validation/runtime.pyi +31 -0
  169. tnfr/validation/soft_filters.py +170 -0
  170. tnfr/validation/soft_filters.pyi +37 -0
  171. tnfr/validation/spectral.py +159 -0
  172. tnfr/validation/spectral.pyi +46 -0
  173. tnfr/validation/syntax.py +40 -0
  174. tnfr/validation/syntax.pyi +10 -0
  175. tnfr/validation/window.py +39 -0
  176. tnfr/validation/window.pyi +1 -0
  177. tnfr/viz/__init__.py +9 -0
  178. tnfr/viz/matplotlib.py +246 -0
  179. tnfr-7.0.0.dist-info/METADATA +179 -0
  180. tnfr-7.0.0.dist-info/RECORD +185 -0
  181. {tnfr-4.5.2.dist-info → tnfr-7.0.0.dist-info}/licenses/LICENSE.md +1 -1
  182. tnfr/grammar.py +0 -344
  183. tnfr/graph_utils.py +0 -84
  184. tnfr/helpers/__init__.py +0 -71
  185. tnfr/import_utils.py +0 -228
  186. tnfr/json_utils.py +0 -162
  187. tnfr/logging_utils.py +0 -116
  188. tnfr/presets.py +0 -60
  189. tnfr/validators.py +0 -84
  190. tnfr/value_utils.py +0 -59
  191. tnfr-4.5.2.dist-info/METADATA +0 -379
  192. tnfr-4.5.2.dist-info/RECORD +0 -67
  193. {tnfr-4.5.2.dist-info → tnfr-7.0.0.dist-info}/WHEEL +0 -0
  194. {tnfr-4.5.2.dist-info → tnfr-7.0.0.dist-info}/entry_points.txt +0 -0
  195. {tnfr-4.5.2.dist-info → tnfr-7.0.0.dist-info}/top_level.txt +0 -0
tnfr/utils/init.py ADDED
@@ -0,0 +1,770 @@
1
+ """Core logging and import helpers for :mod:`tnfr`.
2
+
3
+ This module merges the functionality that historically lived in
4
+ ``tnfr.logging_utils`` and ``tnfr.import_utils``. The behaviour is kept
5
+ identical so downstream consumers can keep relying on the same APIs while
6
+ benefiting from a consolidated entry point under :mod:`tnfr.utils`.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import importlib
12
+ import logging
13
+ import threading
14
+ import warnings
15
+ import weakref
16
+ from collections import OrderedDict
17
+ from dataclasses import dataclass, field
18
+ from typing import Any, Callable, Hashable, Iterable, Iterator, Literal, Mapping
19
+
20
+ from .cache import CacheManager
21
+
22
+ __all__ = (
23
+ "_configure_root",
24
+ "cached_import",
25
+ "warm_cached_import",
26
+ "LazyImportProxy",
27
+ "get_logger",
28
+ "get_numpy",
29
+ "get_nodenx",
30
+ "prune_failed_imports",
31
+ "WarnOnce",
32
+ "warn_once",
33
+ "IMPORT_LOG",
34
+ "EMIT_MAP",
35
+ "_warn_failure",
36
+ "_IMPORT_STATE",
37
+ "_reset_logging_state",
38
+ "_reset_import_state",
39
+ "_FAILED_IMPORT_LIMIT",
40
+ "_DEFAULT_CACHE_SIZE",
41
+ )
42
+
43
+
44
+ _LOGGING_CONFIGURED = False
45
+
46
+
47
+ def _reset_logging_state() -> None:
48
+ """Reset cached logging configuration state."""
49
+
50
+ global _LOGGING_CONFIGURED
51
+ _LOGGING_CONFIGURED = False
52
+
53
+
54
+ def _configure_root() -> None:
55
+ """Ensure the root logger has handlers and a default format."""
56
+
57
+ global _LOGGING_CONFIGURED
58
+ if _LOGGING_CONFIGURED:
59
+ return
60
+
61
+ root = logging.getLogger()
62
+ if not root.handlers:
63
+ kwargs = {"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"}
64
+ if root.level == logging.NOTSET:
65
+ kwargs["level"] = logging.INFO
66
+ logging.basicConfig(**kwargs)
67
+
68
+ _LOGGING_CONFIGURED = True
69
+
70
+
71
+ def get_logger(name: str) -> logging.Logger:
72
+ """Return a module-specific logger."""
73
+
74
+ _configure_root()
75
+ return logging.getLogger(name)
76
+
77
+
78
+ class WarnOnce:
79
+ """Log a warning only once for each unique key.
80
+
81
+ ``WarnOnce`` tracks seen keys in a bounded :class:`set`. When ``maxsize`` is
82
+ reached an arbitrary key is evicted to keep memory usage stable; ordered
83
+ eviction is intentionally avoided to keep the implementation lightweight.
84
+ Instances are callable and accept either a mapping of keys to values or a
85
+ single key/value pair. Passing ``maxsize <= 0`` disables caching and logs on
86
+ every invocation.
87
+ """
88
+
89
+ def __init__(
90
+ self, logger: logging.Logger, msg: str, *, maxsize: int = 1024
91
+ ) -> None:
92
+ self._logger = logger
93
+ self._msg = msg
94
+ self._maxsize = maxsize
95
+ self._seen: set[Hashable] = set()
96
+ self._lock = threading.Lock()
97
+
98
+ def _mark_seen(self, key: Hashable) -> bool:
99
+ """Return ``True`` when ``key`` has not been seen before."""
100
+
101
+ if self._maxsize <= 0:
102
+ # Caching disabled – always log.
103
+ return True
104
+ if key in self._seen:
105
+ return False
106
+ if len(self._seen) >= self._maxsize:
107
+ # ``set.pop()`` removes an arbitrary element which is acceptable for
108
+ # this lightweight cache.
109
+ self._seen.pop()
110
+ self._seen.add(key)
111
+ return True
112
+
113
+ def __call__(
114
+ self,
115
+ data: Mapping[Hashable, Any] | Hashable,
116
+ value: Any | None = None,
117
+ ) -> None:
118
+ """Log new keys found in ``data``.
119
+
120
+ ``data`` may be a mapping of keys to payloads or a single key. When
121
+ called with a single key ``value`` customises the payload passed to the
122
+ logging message; the key itself is used when ``value`` is omitted.
123
+ """
124
+
125
+ if isinstance(data, Mapping):
126
+ new_items: dict[Hashable, Any] = {}
127
+ with self._lock:
128
+ for key, item_value in data.items():
129
+ if self._mark_seen(key):
130
+ new_items[key] = item_value
131
+ if new_items:
132
+ self._logger.warning(self._msg, new_items)
133
+ return
134
+
135
+ key = data
136
+ payload = value if value is not None else data
137
+ with self._lock:
138
+ should_log = self._mark_seen(key)
139
+ if should_log:
140
+ self._logger.warning(self._msg, payload)
141
+
142
+ def clear(self) -> None:
143
+ """Reset tracked keys."""
144
+
145
+ with self._lock:
146
+ self._seen.clear()
147
+
148
+
149
+ def warn_once(
150
+ logger: logging.Logger,
151
+ msg: str,
152
+ *,
153
+ maxsize: int = 1024,
154
+ ) -> WarnOnce:
155
+ """Return a :class:`WarnOnce` logger."""
156
+
157
+ return WarnOnce(logger, msg, maxsize=maxsize)
158
+
159
+
160
+ _FAILED_IMPORT_LIMIT = 128
161
+ _DEFAULT_CACHE_SIZE = 128
162
+
163
+ _SUCCESS_CACHE_NAME = "import.success"
164
+ _FAILURE_CACHE_NAME = "import.failure"
165
+
166
+
167
+ def _import_key(module_name: str, attr: str | None) -> str:
168
+ return module_name if attr is None else f"{module_name}.{attr}"
169
+
170
+
171
+ @dataclass(slots=True)
172
+ class ImportRegistry:
173
+ """Process-wide registry tracking failed imports and emitted warnings."""
174
+
175
+ limit: int = 128
176
+ failed: OrderedDict[str, None] = field(default_factory=OrderedDict)
177
+ warned: set[str] = field(default_factory=set)
178
+ lock: threading.Lock = field(default_factory=threading.Lock)
179
+
180
+ def _insert(self, key: str) -> None:
181
+ self.failed[key] = None
182
+ self.failed.move_to_end(key)
183
+ while len(self.failed) > self.limit:
184
+ self.failed.popitem(last=False)
185
+
186
+ def record_failure(self, key: str, *, module: str | None = None) -> None:
187
+ """Record ``key`` and, optionally, ``module`` as failed imports."""
188
+
189
+ with self.lock:
190
+ self._insert(key)
191
+ if module and module != key:
192
+ self._insert(module)
193
+
194
+ def discard(self, key: str) -> None:
195
+ """Remove ``key`` from the registry and clear its warning state."""
196
+
197
+ with self.lock:
198
+ self.failed.pop(key, None)
199
+ self.warned.discard(key)
200
+
201
+ def mark_warning(self, module: str) -> bool:
202
+ """Mark ``module`` as warned and return ``True`` if it was new."""
203
+
204
+ with self.lock:
205
+ if module in self.warned:
206
+ return False
207
+ self.warned.add(module)
208
+ return True
209
+
210
+ def clear(self) -> None:
211
+ """Remove all failure records and warning markers."""
212
+
213
+ with self.lock:
214
+ self.failed.clear()
215
+ self.warned.clear()
216
+
217
+ def __contains__(self, key: str) -> bool: # pragma: no cover - trivial
218
+ with self.lock:
219
+ return key in self.failed
220
+
221
+
222
+ # Successful imports are cached so lazy proxies can resolve once and later
223
+ # requests return the concrete object without recreating the proxy. The cache
224
+ # stores weak references whenever possible so unused imports can be collected
225
+ # after external references disappear.
226
+
227
+
228
+ class _CacheEntry:
229
+ """Container storing either a weak or strong reference to a value."""
230
+
231
+ __slots__ = ("_kind", "_value")
232
+
233
+ def __init__(
234
+ self,
235
+ value: Any,
236
+ *,
237
+ key: str,
238
+ remover: Callable[[str, weakref.ReferenceType[Any]], None],
239
+ ) -> None:
240
+ try:
241
+ reference = weakref.ref(value, lambda ref, key=key: remover(key, ref))
242
+ except TypeError:
243
+ self._kind = "strong"
244
+ self._value = value
245
+ else:
246
+ self._kind = "weak"
247
+ self._value = reference
248
+
249
+ def get(self) -> Any | None:
250
+ if self._kind == "weak":
251
+ return self._value()
252
+ return self._value
253
+
254
+ def matches(self, ref: weakref.ReferenceType[Any]) -> bool:
255
+ return self._kind == "weak" and self._value is ref
256
+
257
+
258
+ _IMPORT_CACHE_MANAGER = CacheManager(default_capacity=_DEFAULT_CACHE_SIZE)
259
+
260
+
261
+ def _success_cache_factory() -> OrderedDict[str, _CacheEntry]:
262
+ return OrderedDict()
263
+
264
+
265
+ def _failure_cache_factory() -> OrderedDict[str, Exception]:
266
+ return OrderedDict()
267
+
268
+
269
+ _IMPORT_CACHE_MANAGER.register(_SUCCESS_CACHE_NAME, _success_cache_factory)
270
+ _IMPORT_CACHE_MANAGER.register(_FAILURE_CACHE_NAME, _failure_cache_factory)
271
+
272
+
273
+ def _remove_success_entry(key: str, ref: weakref.ReferenceType[Any]) -> None:
274
+
275
+ def _cleanup(cache: OrderedDict[str, _CacheEntry]) -> OrderedDict[str, _CacheEntry]:
276
+ entry = cache.get(key)
277
+ if entry is not None and entry.matches(ref):
278
+ cache.pop(key, None)
279
+ _IMPORT_CACHE_MANAGER.increment_eviction(_SUCCESS_CACHE_NAME)
280
+ return cache
281
+
282
+ _IMPORT_CACHE_MANAGER.update(_SUCCESS_CACHE_NAME, _cleanup)
283
+
284
+
285
+ def _trim_cache(name: str, cache: OrderedDict[str, Any]) -> None:
286
+ capacity = _IMPORT_CACHE_MANAGER.get_capacity(name)
287
+ if capacity is None:
288
+ return
289
+ while len(cache) > capacity:
290
+ cache.popitem(last=False)
291
+ _IMPORT_CACHE_MANAGER.increment_eviction(name)
292
+
293
+
294
+ def _get_success(key: str) -> Any | None:
295
+ result: Any | None = None
296
+ hit = False
297
+
298
+ with _IMPORT_CACHE_MANAGER.timer(_SUCCESS_CACHE_NAME):
299
+
300
+ def _lookup(
301
+ cache: OrderedDict[str, _CacheEntry],
302
+ ) -> OrderedDict[str, _CacheEntry]:
303
+ nonlocal result, hit
304
+ entry = cache.get(key)
305
+ if entry is None:
306
+ return cache
307
+ value = entry.get()
308
+ if value is None:
309
+ cache.pop(key, None)
310
+ _IMPORT_CACHE_MANAGER.increment_eviction(_SUCCESS_CACHE_NAME)
311
+ return cache
312
+ cache.move_to_end(key)
313
+ result = value
314
+ hit = True
315
+ return cache
316
+
317
+ _IMPORT_CACHE_MANAGER.update(_SUCCESS_CACHE_NAME, _lookup)
318
+ if hit:
319
+ _IMPORT_CACHE_MANAGER.increment_hit(_SUCCESS_CACHE_NAME)
320
+ return result
321
+ _IMPORT_CACHE_MANAGER.increment_miss(_SUCCESS_CACHE_NAME)
322
+ return None
323
+
324
+
325
+ def _store_success(key: str, value: Any) -> None:
326
+ entry = _CacheEntry(value, key=key, remover=_remove_success_entry)
327
+
328
+ def _store(cache: OrderedDict[str, _CacheEntry]) -> OrderedDict[str, _CacheEntry]:
329
+ cache[key] = entry
330
+ cache.move_to_end(key)
331
+ _trim_cache(_SUCCESS_CACHE_NAME, cache)
332
+ return cache
333
+
334
+ def _purge_failure(
335
+ cache: OrderedDict[str, Exception],
336
+ ) -> OrderedDict[str, Exception]:
337
+ if cache.pop(key, None) is not None:
338
+ _IMPORT_CACHE_MANAGER.increment_eviction(_FAILURE_CACHE_NAME)
339
+ return cache
340
+
341
+ _IMPORT_CACHE_MANAGER.update(_SUCCESS_CACHE_NAME, _store)
342
+ _IMPORT_CACHE_MANAGER.update(_FAILURE_CACHE_NAME, _purge_failure)
343
+
344
+
345
+ def _get_failure(key: str) -> Exception | None:
346
+ result: Exception | None = None
347
+ hit = False
348
+
349
+ with _IMPORT_CACHE_MANAGER.timer(_FAILURE_CACHE_NAME):
350
+
351
+ def _lookup(cache: OrderedDict[str, Exception]) -> OrderedDict[str, Exception]:
352
+ nonlocal result, hit
353
+ exc = cache.get(key)
354
+ if exc is None:
355
+ return cache
356
+ cache.move_to_end(key)
357
+ result = exc
358
+ hit = True
359
+ return cache
360
+
361
+ _IMPORT_CACHE_MANAGER.update(_FAILURE_CACHE_NAME, _lookup)
362
+ if hit:
363
+ _IMPORT_CACHE_MANAGER.increment_hit(_FAILURE_CACHE_NAME)
364
+ return result
365
+ _IMPORT_CACHE_MANAGER.increment_miss(_FAILURE_CACHE_NAME)
366
+ return None
367
+
368
+
369
+ def _store_failure(key: str, exc: Exception) -> None:
370
+
371
+ def _store(cache: OrderedDict[str, Exception]) -> OrderedDict[str, Exception]:
372
+ cache[key] = exc
373
+ cache.move_to_end(key)
374
+ _trim_cache(_FAILURE_CACHE_NAME, cache)
375
+ return cache
376
+
377
+ def _purge_success(
378
+ cache: OrderedDict[str, _CacheEntry],
379
+ ) -> OrderedDict[str, _CacheEntry]:
380
+ if cache.pop(key, None) is not None:
381
+ _IMPORT_CACHE_MANAGER.increment_eviction(_SUCCESS_CACHE_NAME)
382
+ return cache
383
+
384
+ _IMPORT_CACHE_MANAGER.update(_FAILURE_CACHE_NAME, _store)
385
+ _IMPORT_CACHE_MANAGER.update(_SUCCESS_CACHE_NAME, _purge_success)
386
+
387
+
388
+ def _clear_import_cache() -> None:
389
+ _IMPORT_CACHE_MANAGER.clear()
390
+
391
+
392
+ _IMPORT_STATE = ImportRegistry()
393
+ # Public alias to ease direct introspection in tests and diagnostics.
394
+ IMPORT_LOG = _IMPORT_STATE
395
+
396
+
397
+ def _reset_import_state() -> None:
398
+ """Reset cached import tracking structures."""
399
+
400
+ global _IMPORT_STATE, IMPORT_LOG
401
+ _IMPORT_STATE = ImportRegistry()
402
+ IMPORT_LOG = _IMPORT_STATE
403
+ _clear_import_cache()
404
+
405
+
406
+ def _import_cached(module_name: str, attr: str | None) -> tuple[bool, Any]:
407
+ """Import ``module_name`` (and optional ``attr``) capturing failures."""
408
+
409
+ key = _import_key(module_name, attr)
410
+ cached_value = _get_success(key)
411
+ if cached_value is not None:
412
+ return True, cached_value
413
+
414
+ cached_failure = _get_failure(key)
415
+ if cached_failure is not None:
416
+ return False, cached_failure
417
+
418
+ try:
419
+ module = importlib.import_module(module_name)
420
+ obj = getattr(module, attr) if attr else module
421
+ except (ImportError, AttributeError) as exc:
422
+ _store_failure(key, exc)
423
+ return False, exc
424
+
425
+ _store_success(key, obj)
426
+ return True, obj
427
+
428
+
429
+ logger = get_logger(__name__)
430
+
431
+
432
+ def _format_failure_message(module: str, attr: str | None, err: Exception) -> str:
433
+ """Return a standardised failure message."""
434
+
435
+ return (
436
+ f"Failed to import module '{module}': {err}"
437
+ if isinstance(err, ImportError)
438
+ else f"Module '{module}' has no attribute '{attr}': {err}"
439
+ )
440
+
441
+
442
+ EMIT_MAP: dict[str, Callable[[str], None]] = {
443
+ "warn": lambda msg: _emit(msg, "warn"),
444
+ "log": lambda msg: _emit(msg, "log"),
445
+ "both": lambda msg: _emit(msg, "both"),
446
+ }
447
+
448
+
449
+ def _emit(message: str, mode: Literal["warn", "log", "both"]) -> None:
450
+ """Emit ``message`` via :mod:`warnings`, logger or both."""
451
+
452
+ if mode in ("warn", "both"):
453
+ warnings.warn(message, RuntimeWarning, stacklevel=2)
454
+ if mode in ("log", "both"):
455
+ logger.warning(message)
456
+
457
+
458
+ def _warn_failure(
459
+ module: str,
460
+ attr: str | None,
461
+ err: Exception,
462
+ *,
463
+ emit: Literal["warn", "log", "both"] = "warn",
464
+ ) -> None:
465
+ """Emit a warning about a failed import."""
466
+
467
+ msg = _format_failure_message(module, attr, err)
468
+ if _IMPORT_STATE.mark_warning(module):
469
+ EMIT_MAP[emit](msg)
470
+ else:
471
+ logger.debug(msg)
472
+
473
+
474
+ class LazyImportProxy:
475
+ """Descriptor that defers imports until first use."""
476
+
477
+ __slots__ = (
478
+ "_module",
479
+ "_attr",
480
+ "_emit",
481
+ "_fallback",
482
+ "_target_ref",
483
+ "_strong_target",
484
+ "_lock",
485
+ "_key",
486
+ "__weakref__",
487
+ )
488
+
489
+ _UNRESOLVED = object()
490
+
491
+ def __init__(
492
+ self,
493
+ module_name: str,
494
+ attr: str | None,
495
+ emit: Literal["warn", "log", "both"],
496
+ fallback: Any | None,
497
+ ) -> None:
498
+ self._module = module_name
499
+ self._attr = attr
500
+ self._emit = emit
501
+ self._fallback = fallback
502
+ self._target_ref: weakref.ReferenceType[Any] | None = None
503
+ self._strong_target: Any = self._UNRESOLVED
504
+ self._lock = threading.Lock()
505
+ self._key = _import_key(module_name, attr)
506
+
507
+ def _store_target(self, target: Any) -> None:
508
+ try:
509
+ self_ref = weakref.ref(self)
510
+
511
+ def _cleanup(ref: weakref.ReferenceType[Any]) -> None:
512
+ proxy = self_ref()
513
+ if proxy is None:
514
+ return
515
+ with proxy._lock:
516
+ if proxy._target_ref is ref:
517
+ proxy._target_ref = None
518
+
519
+ self._target_ref = weakref.ref(target, _cleanup)
520
+ except TypeError:
521
+ self._strong_target = target
522
+ self._target_ref = None
523
+ else:
524
+ self._strong_target = self._UNRESOLVED
525
+
526
+ def _resolved_target(self) -> Any:
527
+ if self._strong_target is not self._UNRESOLVED:
528
+ return self._strong_target
529
+ if self._target_ref is None:
530
+ return self._UNRESOLVED
531
+ target = self._target_ref()
532
+ if target is None:
533
+ self._target_ref = None
534
+ return self._UNRESOLVED
535
+ return target
536
+
537
+ def _resolve(self) -> Any:
538
+ target = self._resolved_target()
539
+ if target is not self._UNRESOLVED:
540
+ return target
541
+
542
+ with self._lock:
543
+ target = self._resolved_target()
544
+ if target is self._UNRESOLVED:
545
+ target = _resolve_import(
546
+ self._module,
547
+ self._attr,
548
+ self._emit,
549
+ self._fallback,
550
+ )
551
+ self._store_target(target)
552
+ return target
553
+
554
+ def resolve(self) -> Any:
555
+ """Eagerly resolve and return the proxied object."""
556
+
557
+ return self._resolve()
558
+
559
+ def __getattr__(self, item: str) -> Any:
560
+ """Proxy attribute access to the resolved target."""
561
+
562
+ return getattr(self._resolve(), item)
563
+
564
+ def __call__(self, *args: Any, **kwargs: Any) -> Any:
565
+ """Invoke the resolved target with ``args``/``kwargs``."""
566
+
567
+ return self._resolve()(*args, **kwargs)
568
+
569
+ def __bool__(self) -> bool:
570
+ """Return truthiness of the resolved target."""
571
+
572
+ return bool(self._resolve())
573
+
574
+ def __repr__(self) -> str: # pragma: no cover - representation helper
575
+ """Return representation showing resolution status."""
576
+
577
+ target = self._resolved_target()
578
+ if target is self._UNRESOLVED:
579
+ return f"<LazyImportProxy pending={self._key!r}>"
580
+ return repr(target)
581
+
582
+ def __str__(self) -> str: # pragma: no cover - representation helper
583
+ """Return string representation of the resolved target."""
584
+
585
+ return str(self._resolve())
586
+
587
+ def __iter__(self) -> Iterator[Any]: # pragma: no cover - passthrough helper
588
+ """Yield iteration from the resolved target."""
589
+
590
+ return iter(self._resolve())
591
+
592
+
593
+ def _resolve_import(
594
+ module_name: str,
595
+ attr: str | None,
596
+ emit: Literal["warn", "log", "both"],
597
+ fallback: Any | None,
598
+ ) -> Any | None:
599
+ key = _import_key(module_name, attr)
600
+ success, result = _import_cached(module_name, attr)
601
+ if success:
602
+ _IMPORT_STATE.discard(key)
603
+ if attr is not None:
604
+ _IMPORT_STATE.discard(module_name)
605
+ return result
606
+
607
+ exc: Exception = result
608
+ include_module = isinstance(exc, ImportError)
609
+ _warn_failure(module_name, attr, exc, emit=emit)
610
+ _IMPORT_STATE.record_failure(key, module=module_name if include_module else None)
611
+ return fallback
612
+
613
+
614
+ def cached_import(
615
+ module_name: str,
616
+ attr: str | None = None,
617
+ *,
618
+ fallback: Any | None = None,
619
+ emit: Literal["warn", "log", "both"] = "warn",
620
+ lazy: bool = False,
621
+ ) -> Any | None:
622
+ """Import ``module_name`` (and optional ``attr``) with caching and fallback.
623
+
624
+ When ``lazy`` is ``True`` the import is deferred until the returned proxy is
625
+ first used. The proxy integrates with the shared cache so subsequent calls
626
+ return the resolved object directly.
627
+ """
628
+
629
+ key = _import_key(module_name, attr)
630
+
631
+ if lazy:
632
+ cached_obj = _get_success(key)
633
+ if cached_obj is not None:
634
+ return cached_obj
635
+ return LazyImportProxy(module_name, attr, emit, fallback)
636
+
637
+ return _resolve_import(module_name, attr, emit, fallback)
638
+
639
+
640
+ _ModuleSpec = str | tuple[str, str | None]
641
+
642
+
643
+ def _normalise_warm_specs(
644
+ module: _ModuleSpec | Iterable[_ModuleSpec],
645
+ extra: tuple[_ModuleSpec, ...],
646
+ attr: str | None,
647
+ ) -> list[tuple[str, str | None]]:
648
+ if attr is not None:
649
+ if extra:
650
+ raise ValueError("'attr' can only be combined with a single module name")
651
+ if not isinstance(module, str):
652
+ raise TypeError(
653
+ "'attr' requires the first argument to be a module name string"
654
+ )
655
+ return [(module, attr)]
656
+
657
+ specs: list[_ModuleSpec]
658
+ if extra:
659
+ specs = [module, *extra]
660
+ elif isinstance(module, tuple) and len(module) == 2:
661
+ specs = [module]
662
+ elif isinstance(module, str):
663
+ specs = [module]
664
+ else:
665
+ if isinstance(module, Iterable):
666
+ specs = list(module)
667
+ if not specs:
668
+ raise ValueError("At least one module specification is required")
669
+ else:
670
+ raise TypeError("Unsupported module specification for warm_cached_import")
671
+
672
+ normalised: list[tuple[str, str | None]] = []
673
+ for spec in specs:
674
+ if isinstance(spec, str):
675
+ normalised.append((spec, None))
676
+ continue
677
+ if isinstance(spec, tuple) and len(spec) == 2:
678
+ module_name, module_attr = spec
679
+ if not isinstance(module_name, str) or (
680
+ module_attr is not None and not isinstance(module_attr, str)
681
+ ):
682
+ raise TypeError("Invalid module specification for warm_cached_import")
683
+ normalised.append((module_name, module_attr))
684
+ continue
685
+ raise TypeError(
686
+ "Module specifications must be strings or (module, attr) tuples"
687
+ )
688
+
689
+ return normalised
690
+
691
+
692
+ def warm_cached_import(
693
+ module: _ModuleSpec | Iterable[_ModuleSpec],
694
+ *extra: _ModuleSpec,
695
+ attr: str | None = None,
696
+ fallback: Any | None = None,
697
+ emit: Literal["warn", "log", "both"] = "warn",
698
+ lazy: bool = False,
699
+ resolve: bool = False,
700
+ ) -> Any | dict[str, Any | None]:
701
+ """Pre-populate the import cache for the provided module specifications.
702
+
703
+ When ``lazy`` is ``True`` the cached objects are returned as proxies by
704
+ default. Setting ``resolve`` forces those proxies to resolve immediately
705
+ during the warm-up phase while still sharing the same cache entries.
706
+ """
707
+
708
+ if resolve and not lazy:
709
+ raise ValueError("'resolve' can only be used when 'lazy' is True")
710
+
711
+ specs = _normalise_warm_specs(module, extra, attr)
712
+ results: dict[str, Any | None] = {}
713
+ for module_name, module_attr in specs:
714
+ key = _import_key(module_name, module_attr)
715
+ results[key] = cached_import(
716
+ module_name,
717
+ module_attr,
718
+ fallback=fallback,
719
+ emit=emit,
720
+ lazy=lazy,
721
+ )
722
+ if resolve and isinstance(results[key], LazyImportProxy):
723
+ results[key] = results[key].resolve()
724
+
725
+ if len(results) == 1:
726
+ return next(iter(results.values()))
727
+ return results
728
+
729
+
730
+ def _clear_default_cache() -> None:
731
+ global _NP_MISSING_LOGGED
732
+
733
+ _clear_import_cache()
734
+ _NP_MISSING_LOGGED = False
735
+
736
+
737
+ cached_import.cache_clear = _clear_default_cache # type: ignore[attr-defined]
738
+
739
+
740
+ _NP_MISSING_LOGGED = False
741
+
742
+
743
+ def get_numpy() -> Any | None:
744
+ """Return the cached :mod:`numpy` module when available."""
745
+
746
+ global _NP_MISSING_LOGGED
747
+
748
+ np = cached_import("numpy")
749
+ if np is None:
750
+ if not _NP_MISSING_LOGGED:
751
+ logger.debug("Failed to import numpy; continuing in non-vectorised mode")
752
+ _NP_MISSING_LOGGED = True
753
+ return None
754
+
755
+ if _NP_MISSING_LOGGED:
756
+ _NP_MISSING_LOGGED = False
757
+ return np
758
+
759
+
760
+ def get_nodenx() -> type | None:
761
+ """Return :class:`tnfr.node.NodeNX` using import caching."""
762
+
763
+ return cached_import("tnfr.node", "NodeNX")
764
+
765
+
766
+ def prune_failed_imports() -> None:
767
+ """Clear the registry of recorded import failures and warnings."""
768
+
769
+ _IMPORT_STATE.clear()
770
+ _IMPORT_CACHE_MANAGER.clear(_FAILURE_CACHE_NAME)