arrayprobe 0.1.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.
- arrayprobe/__init__.py +219 -0
- arrayprobe/__main__.py +19 -0
- arrayprobe/_v314_lazy.py +48 -0
- arrayprobe/ai_ready.py +212 -0
- arrayprobe/attributed_export.py +1333 -0
- arrayprobe/authordefaults.py +267 -0
- arrayprobe/cli.py +3510 -0
- arrayprobe/combine.py +12 -0
- arrayprobe/compare.py +950 -0
- arrayprobe/config.py +1239 -0
- arrayprobe/constants.py +12 -0
- arrayprobe/count_closure.py +204 -0
- arrayprobe/diagnostic_views.py +769 -0
- arrayprobe/diode_report.py +265 -0
- arrayprobe/driver/__init__.py +5 -0
- arrayprobe/driver/_load_record.py +331 -0
- arrayprobe/driver/preflight_inputs.py +250 -0
- arrayprobe/driver/reprocess.py +553 -0
- arrayprobe/driver/run_flight.py +8166 -0
- arrayprobe/driver/tas_from_probe_housekeeping.py +124 -0
- arrayprobe/dropsize.py +519 -0
- arrayprobe/friendly.py +206 -0
- arrayprobe/geometry/__init__.py +77 -0
- arrayprobe/geometry/min_rectangle.py +92 -0
- arrayprobe/intarr.py +12 -0
- arrayprobe/intarr_analysis.py +16 -0
- arrayprobe/intarr_plots.py +12 -0
- arrayprobe/inventory.py +129 -0
- arrayprobe/jit_kernels.py +344 -0
- arrayprobe/lwc.py +44 -0
- arrayprobe/masks.py +349 -0
- arrayprobe/matio.py +410 -0
- arrayprobe/matlab_compat.py +13 -0
- arrayprobe/ncio.py +26 -0
- arrayprobe/particle.py +55 -0
- arrayprobe/particle_perimeter.py +12 -0
- arrayprobe/products.py +78 -0
- arrayprobe/profiles.py +179 -0
- arrayprobe/provenance.py +21 -0
- arrayprobe/py.typed +11 -0
- arrayprobe/readers/__init__.py +9 -0
- arrayprobe/readers/dmt.py +603 -0
- arrayprobe/readers/mask_csv.py +12 -0
- arrayprobe/readers/pms.py +12 -0
- arrayprobe/readers/sea.py +433 -0
- arrayprobe/readers/tasinfo.py +12 -0
- arrayprobe/regimes.py +507 -0
- arrayprobe/render.py +728 -0
- arrayprobe/reproduce_report.py +149 -0
- arrayprobe/review_agreement.py +135 -0
- arrayprobe/review_sampling.py +327 -0
- arrayprobe/run_page.py +624 -0
- arrayprobe/sample_area.py +12 -0
- arrayprobe/selector.py +206 -0
- arrayprobe/sizing.py +151 -0
- arrayprobe/time_convert.py +12 -0
- arrayprobe/timing_closure.py +141 -0
- arrayprobe/tools/__init__.py +27 -0
- arrayprobe/tools/make_demo_flight.py +1089 -0
- arrayprobe/tools/make_tas_file.py +760 -0
- arrayprobe/tools/probe_comparison.py +569 -0
- arrayprobe/tools/synthetic_truth.py +698 -0
- arrayprobe/tools/tas_sources.py +705 -0
- arrayprobe/triage_context.py +488 -0
- arrayprobe/triage_server.py +5215 -0
- arrayprobe/tune/__init__.py +15 -0
- arrayprobe/tune/diodes.py +400 -0
- arrayprobe/tune/distributions.py +300 -0
- arrayprobe/tune/sample.py +243 -0
- arrayprobe/tune/score.py +230 -0
- arrayprobe/tune/settings.py +354 -0
- arrayprobe/validate_by_condition.py +208 -0
- arrayprobe/validation_report.py +166 -0
- arrayprobe-0.1.0.dist-info/METADATA +260 -0
- arrayprobe-0.1.0.dist-info/RECORD +189 -0
- arrayprobe-0.1.0.dist-info/WHEEL +5 -0
- arrayprobe-0.1.0.dist-info/entry_points.txt +2 -0
- arrayprobe-0.1.0.dist-info/licenses/LICENSE +31 -0
- arrayprobe-0.1.0.dist-info/licenses/LICENSES/BSD-3-Clause.txt +47 -0
- arrayprobe-0.1.0.dist-info/licenses/NOTICE.md +369 -0
- arrayprobe-0.1.0.dist-info/top_level.txt +4 -0
- arrayprobe_soda2/__init__.py +131 -0
- arrayprobe_soda2/angles.py +143 -0
- arrayprobe_soda2/aspect_ratio.py +541 -0
- arrayprobe_soda2/backfill.py +100 -0
- arrayprobe_soda2/binary_dof.py +181 -0
- arrayprobe_soda2/buildindex.py +113 -0
- arrayprobe_soda2/decompress_dmt.py +285 -0
- arrayprobe_soda2/enhancement_hooks.py +275 -0
- arrayprobe_soda2/export_ascii.py +732 -0
- arrayprobe_soda2/export_ncdf.py +683 -0
- arrayprobe_soda2/export_ncdf_raf.py +396 -0
- arrayprobe_soda2/fillholes.py +161 -0
- arrayprobe_soda2/find_radius.py +105 -0
- arrayprobe_soda2/findsize.py +407 -0
- arrayprobe_soda2/fit_poisson.py +170 -0
- arrayprobe_soda2/fix2dimage.py +63 -0
- arrayprobe_soda2/fixraking.py +162 -0
- arrayprobe_soda2/fixstuckbits.py +146 -0
- arrayprobe_soda2/hvps4_read_frame.py +121 -0
- arrayprobe_soda2/jit_kernels.py +152 -0
- arrayprobe_soda2/label_blobs.py +177 -0
- arrayprobe_soda2/min_circle_fast.py +320 -0
- arrayprobe_soda2/particlesort.py +703 -0
- arrayprobe_soda2/pbp_writer.py +558 -0
- arrayprobe_soda2/poisson_spot_correct.py +183 -0
- arrayprobe_soda2/princomp.py +107 -0
- arrayprobe_soda2/probespecs.py +361 -0
- arrayprobe_soda2/process_2d.py +1364 -0
- arrayprobe_soda2/process_2d_parallel.py +626 -0
- arrayprobe_soda2/process_hk.py +512 -0
- arrayprobe_soda2/processbuffer_2dc.py +155 -0
- arrayprobe_soda2/processbuffer_cip.py +213 -0
- arrayprobe_soda2/processbuffer_f2dc.py +139 -0
- arrayprobe_soda2/processbuffer_spec.py +441 -0
- arrayprobe_soda2/processbuffer_tail.py +350 -0
- arrayprobe_soda2/pth_readers.py +245 -0
- arrayprobe_soda2/py.typed +11 -0
- arrayprobe_soda2/read2dbuffer.py +371 -0
- arrayprobe_soda2/reject.py +246 -0
- arrayprobe_soda2/reprocess_pbp.py +156 -0
- arrayprobe_soda2/sample_area.py +264 -0
- arrayprobe_soda2/save_writer.py +480 -0
- arrayprobe_soda2/sea.py +324 -0
- arrayprobe_soda2/spec_absolutepointers.py +109 -0
- arrayprobe_soda2/spec_decompress.py +203 -0
- arrayprobe_soda2/spec_index.py +476 -0
- arrayprobe_soda2/spec_read_frame.py +286 -0
- arrayprobe_soda2/spec_read_hk.py +301 -0
- arrayprobe_soda2/spec_readint.py +127 -0
- arrayprobe_soda2/timeconv.py +77 -0
- arrayprobe_soda2/update_op.py +101 -0
- arrayprobe_uiops/__init__.py +45 -0
- arrayprobe_uiops/combine.py +192 -0
- arrayprobe_uiops/combine_fork.py +359 -0
- arrayprobe_uiops/constants.py +830 -0
- arrayprobe_uiops/decode_naming.py +113 -0
- arrayprobe_uiops/diode_mask.py +250 -0
- arrayprobe_uiops/engine_absence.py +41 -0
- arrayprobe_uiops/export_oap.py +325 -0
- arrayprobe_uiops/geometry_core.py +366 -0
- arrayprobe_uiops/habit.py +749 -0
- arrayprobe_uiops/habit_core.py +248 -0
- arrayprobe_uiops/hollow_size_rule.py +376 -0
- arrayprobe_uiops/imgproc.py +2081 -0
- arrayprobe_uiops/imgproc_core.py +275 -0
- arrayprobe_uiops/intarr.py +167 -0
- arrayprobe_uiops/intarr_analysis.py +569 -0
- arrayprobe_uiops/intarr_plots.py +119 -0
- arrayprobe_uiops/jit_kernels.py +430 -0
- arrayprobe_uiops/lwc.py +70 -0
- arrayprobe_uiops/matlab_compat.py +860 -0
- arrayprobe_uiops/measure.py +346 -0
- arrayprobe_uiops/min_circle.py +204 -0
- arrayprobe_uiops/min_ellipse.py +161 -0
- arrayprobe_uiops/min_rectangle.py +200 -0
- arrayprobe_uiops/ncar_2dc_timing.py +100 -0
- arrayprobe_uiops/ncio.py +810 -0
- arrayprobe_uiops/oap_read.py +228 -0
- arrayprobe_uiops/particle.py +51 -0
- arrayprobe_uiops/particle_perimeter.py +94 -0
- arrayprobe_uiops/particle_physics.py +199 -0
- arrayprobe_uiops/pecan_params.py +134 -0
- arrayprobe_uiops/probe_cases.py +298 -0
- arrayprobe_uiops/product_sanity.py +337 -0
- arrayprobe_uiops/profile.py +71 -0
- arrayprobe_uiops/profiles.py +756 -0
- arrayprobe_uiops/provenance.py +804 -0
- arrayprobe_uiops/py.typed +11 -0
- arrayprobe_uiops/qc.py +169 -0
- arrayprobe_uiops/readers/__init__.py +8 -0
- arrayprobe_uiops/readers/mask_csv.py +74 -0
- arrayprobe_uiops/readers/pms.py +239 -0
- arrayprobe_uiops/readers/tasinfo.py +57 -0
- arrayprobe_uiops/rect_ellipse.py +129 -0
- arrayprobe_uiops/register_image_assisted.py +25 -0
- arrayprobe_uiops/reject.py +501 -0
- arrayprobe_uiops/reject_core.py +220 -0
- arrayprobe_uiops/sample_area.py +93 -0
- arrayprobe_uiops/sizedist.py +1923 -0
- arrayprobe_uiops/sizedist_core.py +814 -0
- arrayprobe_uiops/sizedist_tables.py +642 -0
- arrayprobe_uiops/spec.py +1094 -0
- arrayprobe_uiops/spec_core.py +703 -0
- arrayprobe_uiops/stray_pixel.py +103 -0
- arrayprobe_uiops/tas.py +191 -0
- arrayprobe_uiops/time_convert.py +41 -0
- uiops/__init__.py +402 -0
- uiops/py.typed +11 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
# Reproduces SODA-2, Copyright (c) 2016 UCAR; see LICENSES/BSD-3-Clause.txt and NOTICE.md.
|
|
3
|
+
"""Named hook points through which an optional correction package can substitute one port call.
|
|
4
|
+
|
|
5
|
+
Port tooling, not IDL: no ``.pro`` counterpart, and it computes nothing. The
|
|
6
|
+
"recorded, not applied" gap: ``arrayprobe soda2 run`` could only *record* which SODA-2
|
|
7
|
+
corrections a config turned on, because nothing let a correction reach the driver. This module
|
|
8
|
+
is that surface, kept on the port's side of the one-way boundary so the port never imports the
|
|
9
|
+
corrections -- ``arrayprobe_soda2_enhancements`` imports *this*, never the reverse
|
|
10
|
+
(``tests/test_no_enhancement_coupling.py``).
|
|
11
|
+
|
|
12
|
+
**What a hook point is.** Every port module binds its callees at import time
|
|
13
|
+
(``from .label_blobs import label_blobs``), and the driver chain looks each callee up in the
|
|
14
|
+
*consumer's* module globals at call time. A :class:`HookPoint` names one such binding --
|
|
15
|
+
consumer module, attribute, the call site that reads it -- so substituting that one global
|
|
16
|
+
changes exactly that one call and nothing else. No driver line had to change for this: the
|
|
17
|
+
bindings already exist, and the table below names them. (The alternative -- an explicit
|
|
18
|
+
``hooks.call("label_blobs", ...)`` at each call site -- would edit ``process_2d.py``,
|
|
19
|
+
``processbuffer_tail.py`` and four other port modules for no gain in what can be substituted.)
|
|
20
|
+
|
|
21
|
+
**No default behaviour change, measured rather than asserted.** At rest every consumer binding
|
|
22
|
+
*is* (identity, not equality) the port's own function captured when this module was imported;
|
|
23
|
+
:func:`pristine` reports it per point and ``tests/test_soda2_enhancement_hooks.py`` asserts it,
|
|
24
|
+
and also runs the driver with every point replaced by a counting pass-through to prove each named
|
|
25
|
+
binding is really called by the driver and that routing a call through a hook moves no written
|
|
26
|
+
value. :func:`overridden` restores the captured originals in ``finally``, so an exception inside
|
|
27
|
+
the ``with`` block cannot leave a substitution behind.
|
|
28
|
+
|
|
29
|
+
**Refusals.** An unknown point name, a point already overridden (no silent stacking: two
|
|
30
|
+
corrections on one point must be composed into one callable by whoever owns them), and a point
|
|
31
|
+
found not pristine on entry (someone else patched the binding) each raise :class:`HookError`.
|
|
32
|
+
|
|
33
|
+
Not thread-safe: the bindings are module globals, so a substitution is process-wide for the
|
|
34
|
+
duration of the ``with`` block. The SODA-2 driver is single-threaded.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
from __future__ import annotations
|
|
38
|
+
|
|
39
|
+
import importlib
|
|
40
|
+
from collections.abc import Callable, Iterator, Mapping
|
|
41
|
+
from contextlib import contextmanager
|
|
42
|
+
from dataclasses import dataclass
|
|
43
|
+
from types import ModuleType
|
|
44
|
+
from typing import Any
|
|
45
|
+
|
|
46
|
+
__all__ = [
|
|
47
|
+
"HOOK_POINTS",
|
|
48
|
+
"HookError",
|
|
49
|
+
"HookPoint",
|
|
50
|
+
"Reconstruction",
|
|
51
|
+
"active",
|
|
52
|
+
"current",
|
|
53
|
+
"original",
|
|
54
|
+
"overridden",
|
|
55
|
+
"pristine",
|
|
56
|
+
"reconstruction_plan",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class HookError(RuntimeError):
|
|
61
|
+
"""A substitution was refused: unknown point, already overridden, or not pristine."""
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True)
|
|
65
|
+
class HookPoint:
|
|
66
|
+
"""One call the driver chain makes through a module-global binding.
|
|
67
|
+
|
|
68
|
+
``consumer`` is the module whose global the call site reads; ``provider`` the module that
|
|
69
|
+
defines the port's own function; ``attribute`` the name in both. ``call_site`` cites the
|
|
70
|
+
port line (and the IDL line it ports) where the binding is read.
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
name: str
|
|
74
|
+
consumer: str
|
|
75
|
+
provider: str
|
|
76
|
+
attribute: str
|
|
77
|
+
call_site: str
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@dataclass(frozen=True)
|
|
81
|
+
class Reconstruction:
|
|
82
|
+
"""How a *fresh interpreter* rebuilds one :func:`overridden` substitution from plain data.
|
|
83
|
+
|
|
84
|
+
A substitution is a mapping of point name -> **function object**, and a function
|
|
85
|
+
object neither pickles in general nor survives a ``spawn``-started worker's re-import of
|
|
86
|
+
the package -- which is why ``process_2d_parallel.refuse_unsplittable`` refused ``jobs >
|
|
87
|
+
1`` outright whenever anything was substituted. This dataclass is the narrow escape: the
|
|
88
|
+
installer states *how to build the same mapping again from data*, and both halves of it
|
|
89
|
+
are data.
|
|
90
|
+
|
|
91
|
+
``factory`` is ``"module.path:callable"``, importable by name in a fresh interpreter;
|
|
92
|
+
``payload`` is the one picklable argument it takes. :meth:`build` imports and calls it and
|
|
93
|
+
returns the mapping. The installer is responsible for the factory being a pure function of
|
|
94
|
+
its payload -- ``overridden`` checks that it rebuilds *the same point names*, which catches
|
|
95
|
+
a stale or mismatched plan, and the parallel path's byte-identity test is what proves the
|
|
96
|
+
rebuilt callables are the same corrections.
|
|
97
|
+
|
|
98
|
+
A caller that passes an ad-hoc lambda through :func:`overridden` supplies no
|
|
99
|
+
reconstruction, and stays refused. That is the honest residual: the refusal narrows from
|
|
100
|
+
*"any live substitution"* to *"a substitution this process cannot rebuild by name"*.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
factory: str
|
|
104
|
+
payload: Any
|
|
105
|
+
|
|
106
|
+
def build(self) -> dict[str, Callable[..., Any]]:
|
|
107
|
+
"""Import ``factory`` and call it on ``payload``; the substitution mapping."""
|
|
108
|
+
mod_name, sep, attr = self.factory.partition(":")
|
|
109
|
+
if not sep or not mod_name or not attr:
|
|
110
|
+
raise HookError(f"reconstruction factory {self.factory!r} is not 'module:callable'")
|
|
111
|
+
fn = getattr(_module(mod_name), attr)
|
|
112
|
+
built = fn(self.payload)
|
|
113
|
+
if not isinstance(built, Mapping):
|
|
114
|
+
raise HookError(f"reconstruction factory {self.factory!r} returned "
|
|
115
|
+
f"{type(built).__name__}, not a mapping of point name -> callable")
|
|
116
|
+
return dict(built)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
_PKG = "arrayprobe_soda2"
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _point(name: str, consumer: str, provider: str, call_site: str,
|
|
123
|
+
attribute: str | None = None) -> HookPoint:
|
|
124
|
+
return HookPoint(name=name, consumer=f"{_PKG}.{consumer}", provider=f"{_PKG}.{provider}",
|
|
125
|
+
attribute=attribute or name, call_site=call_site)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
#: Every substitutable binding, keyed by point name. Ordered driver-outermost first.
|
|
129
|
+
HOOK_POINTS: dict[str, HookPoint] = {p.name: p for p in (
|
|
130
|
+
_point("spec_index", "buildindex", "spec_index",
|
|
131
|
+
"buildindex.py:28 (soda2_buildindex.pro -> spec_index)"),
|
|
132
|
+
_point("open_pbp_ncdf", "process_2d", "pbp_writer",
|
|
133
|
+
"process_2d.py:520 (soda2_process_2d.pro, ncdfparticlefile)"),
|
|
134
|
+
_point("process_hk", "process_2d", "process_hk",
|
|
135
|
+
"process_2d.py:534 (soda2_process_2d.pro -> process_hk)"),
|
|
136
|
+
_point("process_spec_buffer", "processbuffer_spec", "processbuffer_spec",
|
|
137
|
+
"processbuffer_spec.py:390 (soda2_processbuffer.pro -> spec_processbuffer)"),
|
|
138
|
+
_point("binary_dof", "processbuffer_spec", "binary_dof",
|
|
139
|
+
"processbuffer_spec.py:276 (spec_processbuffer.pro:303, dofreject == 3)"),
|
|
140
|
+
_point("fixstuckbits", "processbuffer_tail", "fixstuckbits",
|
|
141
|
+
"processbuffer_tail.py:196 (soda2_processbuffer.pro, stuckbits)"),
|
|
142
|
+
_point("label_blobs", "processbuffer_tail", "label_blobs",
|
|
143
|
+
"processbuffer_tail.py:253-257 (soda2_processbuffer.pro, the three label_blobs calls)"),
|
|
144
|
+
_point("fillholes", "processbuffer_tail", "fillholes",
|
|
145
|
+
"processbuffer_tail.py:287 (soda2_processbuffer.pro, the line before :716)"),
|
|
146
|
+
_point("poisson_spot_correct", "processbuffer_tail", "poisson_spot_correct",
|
|
147
|
+
"processbuffer_tail.py:289 (soda2_processbuffer.pro:716)"),
|
|
148
|
+
_point("soda2_findsize", "processbuffer_tail", "findsize",
|
|
149
|
+
"processbuffer_tail.py:295 (soda2_processbuffer.pro:719-737)"),
|
|
150
|
+
_point("aspect_ratio", "findsize", "aspect_ratio",
|
|
151
|
+
"findsize.py:247 (soda2_findsize.pro -> aspect_ratio)"),
|
|
152
|
+
_point("axis_and_ratio", "aspect_ratio", "aspect_ratio",
|
|
153
|
+
"aspect_ratio.py:528 (aspect_ratio.pro's axis/ratio block, :66 and :105)"),
|
|
154
|
+
)}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _module(name: str) -> ModuleType:
|
|
158
|
+
return importlib.import_module(name)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
#: The port's own function behind every point, captured once, at import of this module --
|
|
162
|
+
#: the reference :func:`pristine` compares against by identity. Captured from the provider,
|
|
163
|
+
#: so a point whose consumer and provider are the same module still has an independent side.
|
|
164
|
+
_ORIGINALS: dict[str, Callable[..., Any]] = {
|
|
165
|
+
name: getattr(_module(p.provider), p.attribute) for name, p in HOOK_POINTS.items()
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
_ACTIVE: dict[str, Callable[..., Any]] = {}
|
|
169
|
+
|
|
170
|
+
#: The :class:`Reconstruction` the live substitution was installed with, or ``None`` -- either
|
|
171
|
+
#: because nothing is substituted or because the installer supplied no way to rebuild it.
|
|
172
|
+
_PLAN: Reconstruction | None = None
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _get(name: str) -> HookPoint:
|
|
176
|
+
try:
|
|
177
|
+
return HOOK_POINTS[name]
|
|
178
|
+
except KeyError:
|
|
179
|
+
raise HookError(f"unknown SODA-2 hook point {name!r}; "
|
|
180
|
+
f"known: {sorted(HOOK_POINTS)}") from None
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def original(name: str) -> Callable[..., Any]:
|
|
184
|
+
"""The port's own function behind point ``name``."""
|
|
185
|
+
_get(name)
|
|
186
|
+
return _ORIGINALS[name]
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def current(name: str) -> Callable[..., Any]:
|
|
190
|
+
"""What the call site behind point ``name`` would call right now."""
|
|
191
|
+
p = _get(name)
|
|
192
|
+
return getattr(_module(p.consumer), p.attribute)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def pristine() -> dict[str, bool]:
|
|
196
|
+
"""Per point: is the consumer's binding the port's own function (identity)?"""
|
|
197
|
+
return {name: current(name) is _ORIGINALS[name] for name in HOOK_POINTS}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def active() -> dict[str, str]:
|
|
201
|
+
"""Points currently overridden -> the replacement's qualified name."""
|
|
202
|
+
return {name: f"{getattr(fn, '__module__', '?')}.{getattr(fn, '__qualname__', repr(fn))}"
|
|
203
|
+
for name, fn in sorted(_ACTIVE.items())}
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def reconstruction_plan() -> Reconstruction | None:
|
|
207
|
+
"""The live substitution's :class:`Reconstruction`, or ``None``.
|
|
208
|
+
|
|
209
|
+
``None`` means one of two different things and the caller must treat them the same way:
|
|
210
|
+
nothing is substituted, or something is and its installer gave no way to rebuild it in a
|
|
211
|
+
fresh interpreter. Only a non-``None`` plan licenses carrying the substitution into a
|
|
212
|
+
``spawn``-started worker (``process_2d_parallel``).
|
|
213
|
+
"""
|
|
214
|
+
return _PLAN
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
@contextmanager
|
|
218
|
+
def overridden(replacements: Mapping[str, Callable[..., Any]], *,
|
|
219
|
+
reconstruction: Reconstruction | None = None) -> Iterator[dict[str, str]]:
|
|
220
|
+
"""Substitute the named bindings for the duration of the ``with`` block.
|
|
221
|
+
|
|
222
|
+
Yields :func:`active`'s view of what was substituted. An empty mapping substitutes nothing
|
|
223
|
+
and is allowed, so a caller need not special-case "every correction off".
|
|
224
|
+
|
|
225
|
+
``reconstruction`` states how a fresh interpreter rebuilds ``replacements`` from
|
|
226
|
+
plain data; it is **checked here, not trusted** -- the factory is imported and called and
|
|
227
|
+
must produce the same point names, so a plan that has drifted from what is being installed
|
|
228
|
+
fails at install time rather than in a worker. Omitted, the substitution is process-local
|
|
229
|
+
and ``jobs > 1`` stays refused.
|
|
230
|
+
"""
|
|
231
|
+
for name in replacements:
|
|
232
|
+
_get(name)
|
|
233
|
+
if name in _ACTIVE:
|
|
234
|
+
raise HookError(f"hook point {name!r} is already overridden by {active()[name]}; "
|
|
235
|
+
"compose the corrections into one callable instead of stacking")
|
|
236
|
+
if current(name) is not _ORIGINALS[name]:
|
|
237
|
+
raise HookError(f"hook point {name!r} is not pristine: "
|
|
238
|
+
f"{HOOK_POINTS[name].consumer}.{HOOK_POINTS[name].attribute} was "
|
|
239
|
+
"rebound outside enhancement_hooks")
|
|
240
|
+
if not callable(replacements[name]):
|
|
241
|
+
raise HookError(f"replacement for hook point {name!r} is not callable")
|
|
242
|
+
if reconstruction is not None:
|
|
243
|
+
rebuilt = reconstruction.build()
|
|
244
|
+
if sorted(rebuilt) != sorted(replacements):
|
|
245
|
+
raise HookError(
|
|
246
|
+
f"reconstruction {reconstruction.factory!r} rebuilds hook points "
|
|
247
|
+
f"{sorted(rebuilt)} but {sorted(replacements)} are being installed; a plan "
|
|
248
|
+
"that does not reproduce the substitution would carry a DIFFERENT set of "
|
|
249
|
+
"corrections into a worker process")
|
|
250
|
+
not_callable = sorted(n for n, fn in rebuilt.items() if not callable(fn))
|
|
251
|
+
if not_callable:
|
|
252
|
+
raise HookError(f"reconstruction {reconstruction.factory!r} rebuilt "
|
|
253
|
+
f"{not_callable} as non-callables")
|
|
254
|
+
global _PLAN
|
|
255
|
+
# Two `overridden` blocks on disjoint points can nest. Neither plan then describes the
|
|
256
|
+
# whole live substitution, so the combined state is declared unrebuildable and `jobs > 1`
|
|
257
|
+
# goes back to being refused -- the conservative direction.
|
|
258
|
+
previous_plan, previous_active = _PLAN, bool(_ACTIVE)
|
|
259
|
+
installed: list[str] = []
|
|
260
|
+
try:
|
|
261
|
+
for name, fn in replacements.items():
|
|
262
|
+
p = HOOK_POINTS[name]
|
|
263
|
+
setattr(_module(p.consumer), p.attribute, fn)
|
|
264
|
+
_ACTIVE[name] = fn
|
|
265
|
+
installed.append(name)
|
|
266
|
+
if installed:
|
|
267
|
+
_PLAN = None if previous_active else reconstruction
|
|
268
|
+
yield active()
|
|
269
|
+
finally:
|
|
270
|
+
for name in reversed(installed):
|
|
271
|
+
p = HOOK_POINTS[name]
|
|
272
|
+
setattr(_module(p.consumer), p.attribute, _ORIGINALS[name])
|
|
273
|
+
del _ACTIVE[name]
|
|
274
|
+
if installed:
|
|
275
|
+
_PLAN = previous_plan
|