Compare commits
29 changed files with 915 additions and 2442 deletions
334
MIGRATION.md
334
MIGRATION.md
|
|
@ -1,8 +1,7 @@
|
|||
# Migration Guide
|
||||
|
||||
This guide covers changes between the `master` branch and the current tree.
|
||||
Both `master` and the current tree report `masque.__version__ == '3.4'`; the
|
||||
version string has not yet been bumped for these changes.
|
||||
This guide covers changes between the git tag `release` and the current tree.
|
||||
At `release`, `masque.__version__` was `3.3`; the current tree reports `3.4`.
|
||||
|
||||
Most downstream changes are in `masque/builder/*`, but there are a few other
|
||||
API changes that may require code updates.
|
||||
|
|
@ -127,40 +126,28 @@ builder = Pather(...)
|
|||
deferred = Pather(..., render='deferred')
|
||||
```
|
||||
|
||||
The new `Pather` remains importable from both `masque` and `masque.builder`.
|
||||
The removed `Builder` and `RenderPather` names are no longer exported from
|
||||
either location.
|
||||
Top-level imports from `masque` also continue to work.
|
||||
|
||||
`Pather` now defaults to `render='auto'`, so plain construction replaces the
|
||||
old `Builder` behavior. Use `Pather(..., render='deferred')` where you
|
||||
previously used `RenderPather`.
|
||||
|
||||
## `SimpleTool` was removed and `AutoTool` registration changed
|
||||
## `BasicTool` was replaced
|
||||
|
||||
`SimpleTool` is no longer exported. `AutoTool` remains, but its old public
|
||||
descriptor classes and constructor-oriented configuration were replaced by
|
||||
registration methods. Use it for generated straights and S-bends and reusable
|
||||
bends, U-turns, and transitions.
|
||||
`BasicTool` is no longer exported. Use `AutoTool` for reusable straight, bend,
|
||||
transition, and S-bend primitives.
|
||||
|
||||
### Old `AutoTool`
|
||||
### Old `BasicTool`
|
||||
|
||||
```python
|
||||
from masque.builder import AutoTool
|
||||
from masque.builder.tools import BasicTool
|
||||
|
||||
tool = AutoTool(
|
||||
straights=[
|
||||
AutoTool.Straight('m1wire', make_straight, 'input', 'output'),
|
||||
],
|
||||
bends=[
|
||||
AutoTool.Bend(lib.abstract('bend'), 'input', 'output'),
|
||||
],
|
||||
sbends=[],
|
||||
tool = BasicTool(
|
||||
straight=(make_straight, 'input', 'output'),
|
||||
bend=(lib.abstract('bend'), 'input', 'output'),
|
||||
transitions={
|
||||
('m2wire', 'm1wire'): AutoTool.Transition(
|
||||
lib.abstract('via'), 'top', 'bottom'
|
||||
),
|
||||
'm2wire': (lib.abstract('via'), 'top', 'bottom'),
|
||||
},
|
||||
default_out_ptype='m1wire',
|
||||
)
|
||||
```
|
||||
|
||||
|
|
@ -179,44 +166,12 @@ tool = (
|
|||
|
||||
The key differences are:
|
||||
|
||||
- `SimpleTool` was removed; use `AutoTool` or implement the new `Tool`
|
||||
primitive-offer interface
|
||||
- `AutoTool.Straight(...)` -> `add_straight(fn, ptype, in_name)`
|
||||
- `AutoTool.Bend(...)` -> `add_bend(abstract, in_name, out_name)`
|
||||
- `AutoTool.SBend(...)` -> `add_sbend(fn, ptype, in_name, out_name)`
|
||||
- reusable native U-turns can be registered with `add_uturn(...)`
|
||||
- `BasicTool` -> `AutoTool`
|
||||
- `straight=(fn, in_name, out_name)` -> `add_straight(fn, ptype, in_name)`
|
||||
- `bend=(abstract, in_name, out_name)` -> `add_bend(abstract, in_name, out_name)`
|
||||
- transitions are registered with `add_transition(abstract, external_port, internal_port)`
|
||||
- transitions are bidirectional by default; pass `one_way=True` to inhibit the reverse adapter
|
||||
|
||||
Primitive costs are now explicit and independent of `AutoTool` registration
|
||||
order. Every `add_*()` method accepts `cost=`, as do the concrete offer
|
||||
factories. A numeric value scales the default geometric cost; for example,
|
||||
`cost=2` makes a primitive twice as expensive. A callable receives the
|
||||
canonical primitive parameter and local endpoint and returns the complete
|
||||
cost:
|
||||
|
||||
```python
|
||||
tool.add_straight(make_straight, 'm1wire', 'input', cost=1.5)
|
||||
tool.add_sbend(
|
||||
make_sbend,
|
||||
'm1wire',
|
||||
'input',
|
||||
'output',
|
||||
cost=lambda jog, endpoint: abs(jog) + 2 * abs(endpoint.x),
|
||||
)
|
||||
```
|
||||
|
||||
`PrimitiveOffer.priority_bias` was removed; custom offers should use `cost`
|
||||
instead. Exact equal-cost candidates still use deterministic discovery order
|
||||
as the final tie-break, but registration order no longer changes their
|
||||
reported cost.
|
||||
|
||||
For two-port primitives, `AutoTool` can infer omitted port names and, for
|
||||
generated straight/S-bend primitives, omitted ptype metadata by sampling an
|
||||
in-domain example. Supply those values explicitly when generation requires
|
||||
route-specific keyword arguments, because metadata inference does not receive
|
||||
`tool_options`.
|
||||
|
||||
## Custom `Tool` subclasses
|
||||
|
||||
If you maintain your own `Tool` subclass, the interface changed:
|
||||
|
|
@ -457,268 +412,25 @@ Check code that calls:
|
|||
- `Ref.rotate(...)`
|
||||
- `Ref.mirror(...)`
|
||||
- `Label.rotate_around(...)` / `Label.mirror(...)`
|
||||
- `Abstract.mirror_port_offsets(...)` / `Abstract.mirror_ports(...)`
|
||||
|
||||
If that code expected offsets or repetition grids to move automatically, it
|
||||
needs updating. For whole-pattern transforms, prefer calling `Pattern.mirror()`
|
||||
or `Pattern.rotate_around(...)` at the container level.
|
||||
|
||||
`Abstract.mirror_port_offsets()` and `Abstract.mirror_ports()` were removed.
|
||||
Use `Abstract.mirror(axis)` to mirror both port locations and orientations. If
|
||||
you intentionally need only one half of that operation, update the individual
|
||||
ports explicitly with `Port.flip_across(...)` or `Port.mirror(...)`.
|
||||
|
||||
## Library hierarchy and graph behavior
|
||||
|
||||
`masque/library.py` was split into the `masque.library` package. Imports from
|
||||
the public module remain stable:
|
||||
|
||||
```python
|
||||
from masque import Library, LazyLibrary
|
||||
# or
|
||||
from masque.library import Library, LazyLibrary
|
||||
```
|
||||
|
||||
Code importing the implementation file itself must move to the public package;
|
||||
do not depend on the new internal `base`, `mapping`, or `lazy` module paths.
|
||||
|
||||
Hierarchy helpers now handle dangling references explicitly. The following
|
||||
methods accept `dangling='error' | 'ignore' | 'include'` and default to
|
||||
`'error'`:
|
||||
|
||||
- `child_graph()`
|
||||
- `parent_graph()`
|
||||
- `child_order()`
|
||||
- `find_refs_local()`
|
||||
- `find_refs_global()`
|
||||
- `prune_empty()`
|
||||
|
||||
On `master`, graph construction could expose missing targets implicitly or
|
||||
fail later with a `KeyError`. If dangling refs are intentional, choose the
|
||||
behavior explicitly, for example:
|
||||
|
||||
```python
|
||||
graph = library.child_graph(dangling='include')
|
||||
order = library.child_order(dangling='ignore')
|
||||
```
|
||||
|
||||
Graph cycles and invalid hierarchy states are now reported as `LibraryError`
|
||||
with context. Audit code that caught `KeyError` or `graphlib.CycleError` from
|
||||
these helpers.
|
||||
|
||||
Invalid `dangling=` strings now raise `ValueError`; they no longer fall through
|
||||
to the `include` behavior. Empty lists stored in `Pattern.refs` are consistently
|
||||
treated as absent references by hierarchy and geometry traversal. Code that
|
||||
uses a `defaultdict` lookup such as `pattern.refs[name]` without appending a
|
||||
`Ref` will therefore not create an edge or force that target to be loaded.
|
||||
|
||||
`Library.add()` now resolves the full name plan and remaps references before it
|
||||
starts inserting cells. Name-resolution and preparation failures no longer
|
||||
leave partially-added cells behind. A `rename_theirs` callback now receives an
|
||||
`INameView` containing both existing names and names reserved earlier in the
|
||||
same addition. Use membership, iteration, `len()`, or `get_name()`; the callback
|
||||
argument is not the destination object and does not support pattern lookup or
|
||||
mapping helpers such as `keys()` and `items()`. Update callback annotations from
|
||||
`ILibraryView` to `INameView`. Custom `_merge()` implementations remain
|
||||
responsible for their own rollback if they fail during the final commit.
|
||||
|
||||
Reference transforms returned by `find_refs_local()` and
|
||||
`find_refs_global()` are now Nx5 arrays. The fifth column is the cumulative
|
||||
scale, so rows have the form `(x, y, rotation, mirrored, scale)` rather than the
|
||||
old Nx4 form.
|
||||
|
||||
`BuildReport` mapping fields are now defensively copied and read-only. Copy a
|
||||
field to a new `dict` before adding or removing report entries.
|
||||
|
||||
`PortsLibraryView`, `OverlayLibrary`, and source-backed outputs returned by
|
||||
`LibraryBuilder.build()` borrow their sources. They do not close source resources, and
|
||||
`PortsLibraryView` is not a context manager. Keep each lazy source open and
|
||||
unchanged until every borrowing view or overlay is finished, then close the
|
||||
owning source explicitly. An eager `build(output='library')` result is detached
|
||||
and does not need its sources afterward.
|
||||
|
||||
Read-only `subtree()` results are now borrowed lazy views rather than eager
|
||||
`LibraryView` snapshots. Creating one no longer loads its reachable patterns,
|
||||
and the view preserves source ordering, hierarchy metadata, and lazy GDS
|
||||
copy-through. Mutable `Library`, `LazyLibrary`, and `OverlayLibrary` subtrees
|
||||
return the same writable type as their source. Overlay subtrees retain their
|
||||
source layers and lazy GDS capabilities. Mutable subtree containers are
|
||||
structurally independent, but already-materialized patterns remain shared.
|
||||
Keep borrowed sources open and structurally unchanged for the subtree's
|
||||
lifetime.
|
||||
|
||||
`ILibrary` no longer inherits `collections.abc.MutableMapping`. It remains a
|
||||
readable `Mapping` with explicit insert-only item assignment and deletion, but
|
||||
generic mutation helpers such as `update()`, `setdefault()`, `pop()`,
|
||||
`popitem()`, and `clear()` are no longer supplied. Use `add()`, `rename()`,
|
||||
`delete()`, item insertion, and item deletion so library name and reference
|
||||
invariants remain explicit.
|
||||
|
||||
Library-level `referenced_patterns()` and `dangling_refs()` now report only
|
||||
named cell targets and return `set[str]`. Populated refs whose target is `None`
|
||||
are ignored, matching `child_graph()` and recursive geometry behavior;
|
||||
`Pattern.referenced_patterns()` continues to report `None` locally.
|
||||
|
||||
Port-importing views now always process detached patterns, including when the
|
||||
raw source cell was already cached. Code can safely retain and compare raw and
|
||||
processed views without port overrides leaking back into the raw pattern.
|
||||
Once a processed cell is persistently materialized, lazy GDS writers no longer
|
||||
copy the raw source structure for that cell, so later mutations to the returned
|
||||
`Pattern` are serialized. Non-persistent materialization does not mark the cell
|
||||
as changed.
|
||||
|
||||
Underscore-prefixed declarations work through the attribute authoring surface:
|
||||
`builder.cells._helper = pattern` now declares `_helper`. Only the view's exact
|
||||
internal `_library` attribute is reserved.
|
||||
|
||||
Recursive geometry operations now reject cyclic reference hierarchies with a
|
||||
contextual `PatternError` instead of eventually leaking `RecursionError`. This
|
||||
applies to bounds calculation, flattened layer polygon extraction, and
|
||||
visualization as well as the existing flattening checks.
|
||||
|
||||
`LibraryBuilder.validate(names=...)` now accepts either one string or a sequence
|
||||
of strings. Non-string roots raise `TypeError`, and duplicate roots are reduced
|
||||
to their first occurrence. A recipe may build a different `LibraryBuilder`, but
|
||||
calling `build()` or `validate()` recursively on its own active builder now
|
||||
raises `BuildError` before starting another session.
|
||||
|
||||
`LibraryBuilder` is an authoring registry, not an `ILibraryView` or mapping.
|
||||
Use membership, iteration, `keys()`, `get_name()`, assignment, and deletion to
|
||||
manage declarations, then use the library returned by `build()` for reads and
|
||||
hierarchy operations. Recipes that need an active library must receive the
|
||||
builder-owned placeholder as a direct argument:
|
||||
|
||||
```python
|
||||
def make_top(lib: ILibrary) -> Pattern:
|
||||
return Pather(library=lib, ports='device').pattern
|
||||
|
||||
builder.cells.top = cell(make_top)(builder.library)
|
||||
builder.cells.device = cell(factory)(hole_lib=builder.library)
|
||||
```
|
||||
|
||||
The builder-owned `builder.library` placeholder is read-only. Only direct
|
||||
positional and keyword values equal to it are substituted; placeholders nested
|
||||
inside containers are not interpreted. A placeholder from another builder is
|
||||
rejected when the recipe is assigned.
|
||||
|
||||
`IMaterializable` now identifies libraries which support explicit
|
||||
`materialize()` and `materialize_many()` operations. `LibraryBuilder.add()`
|
||||
borrows these marked inputs, while ordinary mappings and `ILibraryView`
|
||||
instances are copied eagerly. Use `add_source()` to force borrowing of an
|
||||
unmarked view. Wrapping a materializable library in `LibraryView` intentionally
|
||||
erases the marker.
|
||||
|
||||
`IBorrowing` separately identifies composite views which retain direct source
|
||||
libraries and expose them through `borrowed_sources()`. Keep those sources open
|
||||
and unchanged for the lifetime of the borrowing view. Owner libraries such as
|
||||
`LazyLibrary` and the lazy GDS readers are materializable but do not implement
|
||||
`IBorrowing`. GDS raw-structure copy-through remains a separate,
|
||||
format-specific capability.
|
||||
|
||||
`LibraryBuilder`, `OverlayLibrary`, and `PortsLibraryView` are new additive
|
||||
library implementations. `LibraryBuilder` supports declarative `@cell` recipes
|
||||
and dependency-aware builds; `OverlayLibrary` composes source libraries without
|
||||
eagerly copying all patterns; `PortsLibraryView` overlays port metadata on a
|
||||
read-only source.
|
||||
|
||||
Flattening with `flatten_ports=True` now rejects repeated refs whose target has
|
||||
ports, because expanding them would create duplicate port names. Resolve the
|
||||
repetition and assign unique port names before flattening, or use
|
||||
`flatten_ports=False`.
|
||||
|
||||
## GDSII module and lazy-loading changes
|
||||
|
||||
`masque.file.gdsii` changed from a module into a package. The eager klamath
|
||||
API remains available at the old import path, so ordinary `read`, `readfile`,
|
||||
`write`, and `writefile` calls do not need to change:
|
||||
|
||||
```python
|
||||
from masque.file import gdsii
|
||||
library, info = gdsii.readfile('layout.gds')
|
||||
```
|
||||
|
||||
The old `gdsii.load_library()` and `gdsii.load_libraryfile()` entry points were
|
||||
removed. Use the source-backed lazy reader instead:
|
||||
|
||||
```python
|
||||
# old
|
||||
library, info = gdsii.load_libraryfile('layout.gds')
|
||||
|
||||
# new
|
||||
from masque.file.gdsii import lazy
|
||||
|
||||
library, info = lazy.readfile('layout.gds')
|
||||
try:
|
||||
pattern = library['TOP']
|
||||
finally:
|
||||
library.close()
|
||||
```
|
||||
|
||||
`lazy.read(stream)` and `lazy.readfile(path, use_mmap=...)` return a read-only
|
||||
`GdsLibrarySource`. It owns file resources when it opens them and also supports
|
||||
the context-manager protocol. The old `full_load` and `postprocess` arguments
|
||||
are gone; materialize/copy the desired cells and post-process them explicitly.
|
||||
|
||||
An optional Arrow/native backend is available through
|
||||
`masque.file.gdsii.arrow` and `masque.file.gdsii.lazy_arrow`; install the new
|
||||
`arrow` extra to use it. These modules are additive and are not a transparent
|
||||
replacement unless their additional dependencies and native library are
|
||||
available.
|
||||
|
||||
## Shape construction and geometry additions
|
||||
|
||||
The public `raw=True` constructor shortcut was removed from `Arc`, `Circle`,
|
||||
`Ellipse`, `Path`, `Polygon`, `PolyCollection`, and `Text`. Call their normal
|
||||
constructors without `raw`; `_from_raw()` is an internal fast path and is not a
|
||||
compatibility API.
|
||||
|
||||
```python
|
||||
# old
|
||||
polygon = Polygon(vertices, raw=True)
|
||||
|
||||
# new
|
||||
polygon = Polygon(vertices)
|
||||
```
|
||||
|
||||
`Arc` radii must now be strictly positive rather than merely non-negative.
|
||||
`Arc.angle_ref` is additive and defaults to `Arc.AngleRef.Center`, preserving
|
||||
the previous center-referenced angle interpretation.
|
||||
|
||||
`RectCollection` is a new shape for batches of axis-aligned rectangles and is
|
||||
exported from both `masque` and `masque.shapes`. `Polygon.boolean()` and the
|
||||
top-level `masque.boolean()` helper are also new; install the `boolean` extra
|
||||
for their `pyclipper` dependency.
|
||||
|
||||
## Other user-facing changes
|
||||
|
||||
### File writers
|
||||
|
||||
SVG writing no longer polygonizes or flattens caller-owned patterns in place;
|
||||
it works from detached copies. `svg.writefile(..., annotate_ports=True)` can
|
||||
add port arrows. DXF writing now expands shape repetitions into individual DXF
|
||||
entities, so callers no longer need to wrap repeated shapes solely for DXF
|
||||
output.
|
||||
|
||||
### DXF environments
|
||||
|
||||
If you install the DXF extra, the supported `ezdxf` baseline moved from
|
||||
`~=1.0.2` to `~=1.4`. Any pinned environments should be updated accordingly.
|
||||
|
||||
### Optional dependency names
|
||||
|
||||
The misspelled `manhatanize_slow` extra was corrected to
|
||||
`manhattanize_slow`. A separate `manhattanize` extra now installs the
|
||||
scikit-image implementation. The `arrow` and `boolean` extras are also new.
|
||||
|
||||
### New exports
|
||||
|
||||
These are additive, but available now from `masque` and `masque.builder`:
|
||||
|
||||
- from `masque`: `RectCollection`, `boolean`, `OverlayLibrary`,
|
||||
`PortsLibraryView`, `IMaterializable`, `IBorrowing`, `LibraryBuilder`,
|
||||
`BuildReport`, `CellProvenance`, and `cell`
|
||||
- from `masque.builder`: `CostCallable`, the concrete primitive-offer classes,
|
||||
and structured route error/status types
|
||||
- `PortPather`
|
||||
- `AutoTool`
|
||||
- `boolean`
|
||||
|
||||
## Minimal migration checklist
|
||||
|
||||
|
|
@ -726,15 +438,11 @@ If your code uses the routing stack, do these first:
|
|||
|
||||
1. Replace `path`/`path_to`/`mpath`/`path_into` calls with
|
||||
`trace`/`trace_to`/multi-port `trace`/`trace_into`.
|
||||
2. Replace `SimpleTool` and old `AutoTool` descriptor construction with the
|
||||
new `AutoTool.add_*()` methods.
|
||||
2. Replace `BasicTool` with `AutoTool`.
|
||||
3. Fix imports that still reference `masque.builder.builder` or
|
||||
`masque.builder.renderpather`.
|
||||
4. Audit any low-level `mirror()` usage, especially on `Port` and `Ref`.
|
||||
5. Move lazy GDS calls from `gdsii.load_library*()` to
|
||||
`masque.file.gdsii.lazy`.
|
||||
6. Remove `raw=True` from public shape constructors.
|
||||
|
||||
If your code only uses `Pattern`, `Library`, `place()`, and `plug()` without the
|
||||
routing helpers, audit transforms, dangling-reference graph calls, raw shape
|
||||
construction, and any stale imports.
|
||||
routing helpers, you may not need any changes beyond the transform audit and any
|
||||
stale imports.
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ Contents
|
|||
* Use `Pather` to snap ports together into a circuit
|
||||
* Check for dangling references
|
||||
- [library](library.py)
|
||||
* Continue from `devices.py` by declaring a mixed library with `LibraryBuilder`
|
||||
* Continue from `devices.py` by declaring a mixed library with `BuildLibrary`
|
||||
* Import source-backed GDS cells and register python-generated recipes together
|
||||
* Call `build()` to produce a normal library and report for downstream `Pather` usage and writing
|
||||
* Explore alternate ways of specifying a pattern for `.plug()` and `.place()`
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
"""
|
||||
Tutorial: authoring a mixed library with `LibraryBuilder`.
|
||||
Tutorial: authoring a mixed library with `BuildLibrary`.
|
||||
|
||||
This example assumes you have already read `devices.py` and generated the
|
||||
`circuit.gds` file it writes. The goal here is not the photonic-crystal geometry
|
||||
|
|
@ -11,7 +11,7 @@ from typing import Any
|
|||
from pprint import pformat
|
||||
|
||||
|
||||
from masque import ILibrary, LibraryBuilder, Pather, Pattern, cell
|
||||
from masque import BuildLibrary, Pather, Pattern, cell
|
||||
from masque.file.gdsii import writefile
|
||||
from masque.file.gdsii.lazy import readfile
|
||||
|
||||
|
|
@ -20,7 +20,7 @@ import devices
|
|||
from basic_shapes import GDS_OPTS
|
||||
|
||||
|
||||
def make_mixed_waveguide(lib: ILibrary) -> Pattern:
|
||||
def make_mixed_waveguide(lib: BuildLibrary) -> Pattern:
|
||||
"""
|
||||
Recipe which assembles imported and generated cells behind the builder API.
|
||||
"""
|
||||
|
|
@ -42,7 +42,7 @@ def make_mixed_waveguide(lib: ILibrary) -> Pattern:
|
|||
|
||||
|
||||
def main() -> None:
|
||||
builder = LibraryBuilder()
|
||||
builder = BuildLibrary()
|
||||
cells = builder.cells
|
||||
|
||||
#
|
||||
|
|
@ -72,12 +72,8 @@ def main() -> None:
|
|||
cells.tri_wg28 = cell(devices.waveguide)(length=28, mirror_periods=5, **opts)
|
||||
cells.tri_bend0 = cell(devices.bend)(mirror_periods=5, **opts)
|
||||
cells.tri_ysplit = cell(devices.y_splitter)(mirror_periods=5, **opts)
|
||||
cells.tri_l3cav = cell(devices.perturbed_l3)(
|
||||
xy_size=(4, 10),
|
||||
**opts,
|
||||
hole_lib=builder.library,
|
||||
)
|
||||
cells.mixed_wg_cav = cell(make_mixed_waveguide)(builder.library)
|
||||
cells.tri_l3cav = cell(devices.perturbed_l3)(xy_size=(4, 10), **opts, hole_lib=builder)
|
||||
cells.mixed_wg_cav = cell(make_mixed_waveguide)(builder)
|
||||
|
||||
print('Declared cells waiting to be built:\n' + pformat(list(builder.keys())))
|
||||
|
||||
|
|
@ -114,10 +110,6 @@ def main() -> None:
|
|||
print('Writing library to file...')
|
||||
writefile(built, 'library.gds', **GDS_OPTS)
|
||||
|
||||
# The default build output is an overlay which borrows its lazy sources.
|
||||
# Close the owning GDS source only after the overlay is no longer needed.
|
||||
gds_lib.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
|||
|
|
@ -178,7 +178,7 @@ class PrimitiveWireTool(Tool):
|
|||
|
||||
def _transition_offers(self, in_ptype: str | None) -> tuple[StraightOffer, ...]:
|
||||
offers: list[StraightOffer] = []
|
||||
for spec in self.transitions:
|
||||
for index, spec in enumerate(self.transitions):
|
||||
if spec.out_port.ptype != self.ptype:
|
||||
continue
|
||||
if in_ptype not in (None, 'unk', spec.in_port.ptype):
|
||||
|
|
@ -208,6 +208,7 @@ class PrimitiveWireTool(Tool):
|
|||
offers.append(StraightOffer(
|
||||
in_ptype = spec.in_port.ptype,
|
||||
out_ptype = spec.out_port.ptype,
|
||||
priority_bias = index * 1e7,
|
||||
length_domain = (length, length),
|
||||
endpoint_planner = endpoint_planner,
|
||||
commit_planner = commit_planner,
|
||||
|
|
@ -219,7 +220,7 @@ class PrimitiveWireTool(Tool):
|
|||
return ()
|
||||
|
||||
offers: list[StraightOffer] = []
|
||||
for spec in self.transitions:
|
||||
for index, spec in enumerate(self.transitions):
|
||||
if spec.in_port.ptype != self.ptype:
|
||||
continue
|
||||
if out_ptype is not None and spec.out_port.ptype != out_ptype:
|
||||
|
|
@ -254,6 +255,7 @@ class PrimitiveWireTool(Tool):
|
|||
offers.append(StraightOffer(
|
||||
in_ptype = self.ptype,
|
||||
out_ptype = spec.out_port.ptype,
|
||||
priority_bias = index * 1e7,
|
||||
length_domain = (transition_length, numpy.inf),
|
||||
endpoint_planner = endpoint_planner,
|
||||
commit_planner = commit_planner,
|
||||
|
|
|
|||
|
|
@ -59,16 +59,13 @@ from .pattern import (
|
|||
from .utils.boolean import boolean as boolean
|
||||
|
||||
from .library import (
|
||||
INameView as INameView,
|
||||
ILibraryView as ILibraryView,
|
||||
ILibrary as ILibrary,
|
||||
IBorrowing as IBorrowing,
|
||||
IMaterializable as IMaterializable,
|
||||
LibraryView as LibraryView,
|
||||
Library as Library,
|
||||
OverlayLibrary as OverlayLibrary,
|
||||
PortsLibraryView as PortsLibraryView,
|
||||
LibraryBuilder as LibraryBuilder,
|
||||
BuildLibrary as BuildLibrary,
|
||||
BuildReport as BuildReport,
|
||||
CellProvenance as CellProvenance,
|
||||
LazyLibrary as LazyLibrary,
|
||||
|
|
|
|||
|
|
@ -67,7 +67,6 @@ from .tools import (
|
|||
PathTool as PathTool,
|
||||
RenderStep as RenderStep,
|
||||
PrimitiveKind as PrimitiveKind,
|
||||
CostCallable as CostCallable,
|
||||
GeneratedEndpointFn as GeneratedEndpointFn,
|
||||
PrimitiveOffer as PrimitiveOffer,
|
||||
StraightOffer as StraightOffer,
|
||||
|
|
|
|||
|
|
@ -350,16 +350,11 @@ class Solver:
|
|||
)
|
||||
|
||||
def offer_key(offer: PrimitiveOffer) -> tuple[Any, ...]:
|
||||
cost_key: tuple[str, float | int]
|
||||
if callable(offer.cost):
|
||||
cost_key = ('callable', id(offer.cost))
|
||||
else:
|
||||
cost_key = ('factor', round(float(offer.cost), 9))
|
||||
return (
|
||||
type(offer).__qualname__,
|
||||
offer.in_ptype,
|
||||
offer.out_ptype,
|
||||
cost_key,
|
||||
round(float(offer.priority_bias), 9),
|
||||
tuple(round(float(value), 9) for value in offer.parameter_domain),
|
||||
getattr(offer, 'ccw', None),
|
||||
id(offer.endpoint_planner),
|
||||
|
|
|
|||
|
|
@ -112,32 +112,8 @@ CommitCallable = Callable[[float], Any]
|
|||
BBoxCallable = Callable[[float], NDArray[numpy.float64]]
|
||||
DataCallable = Callable[[float], Any]
|
||||
BBoxForDataCallable = Callable[[Any], NDArray[numpy.float64]]
|
||||
CostCallable = Callable[[float, Port], float]
|
||||
PrimitiveKind = Literal['straight', 'bend', 's', 'u']
|
||||
|
||||
|
||||
def _validated_cost(cost: CostCallable | float) -> CostCallable | float:
|
||||
"""Return a canonical primitive cost policy or raise `BuildError`."""
|
||||
if callable(cost):
|
||||
return cost
|
||||
try:
|
||||
factor = float(cost)
|
||||
except (TypeError, ValueError, OverflowError) as err:
|
||||
raise BuildError(f'Primitive cost factor must be a number or callable, got {cost!r}') from err
|
||||
if not scalar_isfinite(factor) or factor < 0:
|
||||
raise BuildError(f'Primitive cost factor must be nonnegative and finite, got {factor:g}')
|
||||
return factor
|
||||
|
||||
|
||||
def _validated_cost_value(value: Any) -> float:
|
||||
"""Return a canonical evaluated cost or raise `BuildError`."""
|
||||
try:
|
||||
cost = float(value)
|
||||
except (TypeError, ValueError, OverflowError) as err:
|
||||
raise BuildError(f'Primitive cost must be a number, got {value!r}') from err
|
||||
if not scalar_isfinite(cost) or cost < 0:
|
||||
raise BuildError(f'Primitive cost must be nonnegative and finite, got {cost:g}')
|
||||
return cost
|
||||
BUILTIN_PRIORITY_STEP = 1e7
|
||||
|
||||
|
||||
def _generated_offer_callbacks(
|
||||
|
|
@ -212,7 +188,7 @@ class PrimitiveOffer(ABC):
|
|||
"""
|
||||
in_ptype: str | None
|
||||
out_ptype: str | None
|
||||
cost: CostCallable | float = 1.0
|
||||
priority_bias: float = 0.0
|
||||
bbox_planner: BBoxCallable | None = None
|
||||
parameterized_bbox: Any | None = None
|
||||
"""Reserved footprint metadata; the current solver does not inspect it."""
|
||||
|
|
@ -225,7 +201,8 @@ class PrimitiveOffer(ABC):
|
|||
|
||||
if has_endpoint != has_commit:
|
||||
raise BuildError('PrimitiveOffer split callbacks require both endpoint_planner and commit_planner')
|
||||
object.__setattr__(self, 'cost', _validated_cost(self.cost))
|
||||
if not numpy.isfinite(self.priority_bias) or self.priority_bias < 0:
|
||||
raise BuildError(f'PrimitiveOffer priority_bias must be nonnegative and finite, got {self.priority_bias:g}')
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
|
|
@ -257,21 +234,15 @@ class PrimitiveOffer(ABC):
|
|||
"""
|
||||
Return this primitive's additive planning cost.
|
||||
|
||||
Lower cost is preferred before internal tie-breakers. A numeric `cost`
|
||||
scales the default local-displacement cost; a callable receives the
|
||||
canonical parameter and local endpoint and returns the complete cost.
|
||||
Lower cost is preferred before internal tie-breakers. The default cost
|
||||
is based on local endpoint displacement plus `priority_bias`.
|
||||
"""
|
||||
selected = self.canonicalize_parameter(parameter)
|
||||
if self.endpoint_planner is not None:
|
||||
out_port = self.endpoint_planner(selected)
|
||||
else:
|
||||
out_port = self.endpoint_at(selected)
|
||||
if callable(self.cost):
|
||||
value = self.cost(selected, out_port)
|
||||
else:
|
||||
default_cost = abs(float(out_port.x)) + (pi / 2) * abs(float(out_port.y))
|
||||
value = self.cost * default_cost
|
||||
return _validated_cost_value(value)
|
||||
return self.priority_bias + abs(float(out_port.x)) + (pi / 2) * abs(float(out_port.y))
|
||||
|
||||
def bbox_at(self, parameter: float) -> NDArray[numpy.float64]:
|
||||
"""
|
||||
|
|
@ -323,7 +294,7 @@ class StraightOffer(PrimitiveOffer):
|
|||
ptype: str | None,
|
||||
data_at: DataCallable,
|
||||
*,
|
||||
cost: CostCallable | float = 1.0,
|
||||
priority_bias: float = 0.0,
|
||||
bbox_for_data: BBoxForDataCallable | None = None,
|
||||
length_domain: tuple[float, float] = (0.0, numpy.inf),
|
||||
) -> Self:
|
||||
|
|
@ -341,7 +312,7 @@ class StraightOffer(PrimitiveOffer):
|
|||
return cls(
|
||||
in_ptype = ptype,
|
||||
out_ptype = ptype,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_planner = bbox_planner,
|
||||
endpoint_planner = endpoint_planner,
|
||||
commit_planner = commit_planner,
|
||||
|
|
@ -356,7 +327,7 @@ class StraightOffer(PrimitiveOffer):
|
|||
endpoint: Port,
|
||||
data: Any,
|
||||
*,
|
||||
cost: CostCallable | float = 1.0,
|
||||
priority_bias: float = 0.0,
|
||||
bbox_for_data: BBoxForDataCallable | None = None,
|
||||
) -> Self:
|
||||
"""
|
||||
|
|
@ -370,7 +341,7 @@ class StraightOffer(PrimitiveOffer):
|
|||
return cls(
|
||||
in_ptype = in_ptype,
|
||||
out_ptype = out_ptype,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_planner = bbox_planner,
|
||||
endpoint_planner = endpoint_planner,
|
||||
commit_planner = commit_planner,
|
||||
|
|
@ -400,7 +371,7 @@ class BendOffer(PrimitiveOffer):
|
|||
data_at: DataCallable,
|
||||
*,
|
||||
ccw: bool,
|
||||
cost: CostCallable | float = 1.0,
|
||||
priority_bias: float = 0.0,
|
||||
bbox_for_data: BBoxForDataCallable | None = None,
|
||||
length_domain: tuple[float, float] = (0.0, numpy.inf),
|
||||
) -> Self:
|
||||
|
|
@ -415,7 +386,7 @@ class BendOffer(PrimitiveOffer):
|
|||
return cls(
|
||||
in_ptype = ptype,
|
||||
out_ptype = ptype,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_planner = bbox_planner,
|
||||
endpoint_planner = endpoint_planner,
|
||||
commit_planner = commit_planner,
|
||||
|
|
@ -432,7 +403,7 @@ class BendOffer(PrimitiveOffer):
|
|||
data: Any,
|
||||
*,
|
||||
ccw: bool,
|
||||
cost: CostCallable | float = 1.0,
|
||||
priority_bias: float = 0.0,
|
||||
bbox_for_data: BBoxForDataCallable | None = None,
|
||||
) -> Self:
|
||||
"""
|
||||
|
|
@ -446,7 +417,7 @@ class BendOffer(PrimitiveOffer):
|
|||
return cls(
|
||||
in_ptype = in_ptype,
|
||||
out_ptype = out_ptype,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_planner = bbox_planner,
|
||||
endpoint_planner = endpoint_planner,
|
||||
commit_planner = commit_planner,
|
||||
|
|
@ -475,7 +446,7 @@ class SOffer(PrimitiveOffer):
|
|||
endpoint_at: EndpointCallable,
|
||||
data_at: DataCallable,
|
||||
*,
|
||||
cost: CostCallable | float = 1.0,
|
||||
priority_bias: float = 0.0,
|
||||
bbox_for_data: BBoxForDataCallable | None = None,
|
||||
jog_domain: tuple[float, float] = (-numpy.inf, numpy.inf),
|
||||
) -> Self:
|
||||
|
|
@ -490,7 +461,7 @@ class SOffer(PrimitiveOffer):
|
|||
return cls(
|
||||
in_ptype = ptype,
|
||||
out_ptype = ptype,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_planner = bbox_planner,
|
||||
endpoint_planner = endpoint_planner,
|
||||
commit_planner = commit_planner,
|
||||
|
|
@ -505,7 +476,7 @@ class SOffer(PrimitiveOffer):
|
|||
endpoint: Port,
|
||||
data: Any,
|
||||
*,
|
||||
cost: CostCallable | float = 1.0,
|
||||
priority_bias: float = 0.0,
|
||||
bbox_for_data: BBoxForDataCallable | None = None,
|
||||
) -> Self:
|
||||
"""
|
||||
|
|
@ -519,7 +490,7 @@ class SOffer(PrimitiveOffer):
|
|||
return cls(
|
||||
in_ptype = in_ptype,
|
||||
out_ptype = out_ptype,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_planner = bbox_planner,
|
||||
endpoint_planner = endpoint_planner,
|
||||
commit_planner = commit_planner,
|
||||
|
|
@ -547,7 +518,7 @@ class UOffer(PrimitiveOffer):
|
|||
endpoint_at: EndpointCallable,
|
||||
data_at: DataCallable,
|
||||
*,
|
||||
cost: CostCallable | float = 1.0,
|
||||
priority_bias: float = 0.0,
|
||||
bbox_for_data: BBoxForDataCallable | None = None,
|
||||
jog_domain: tuple[float, float] = (-numpy.inf, numpy.inf),
|
||||
) -> Self:
|
||||
|
|
@ -562,7 +533,7 @@ class UOffer(PrimitiveOffer):
|
|||
return cls(
|
||||
in_ptype = ptype,
|
||||
out_ptype = ptype,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_planner = bbox_planner,
|
||||
endpoint_planner = endpoint_planner,
|
||||
commit_planner = commit_planner,
|
||||
|
|
@ -577,7 +548,7 @@ class UOffer(PrimitiveOffer):
|
|||
endpoint: Port,
|
||||
data: Any,
|
||||
*,
|
||||
cost: CostCallable | float = 1.0,
|
||||
priority_bias: float = 0.0,
|
||||
bbox_for_data: BBoxForDataCallable | None = None,
|
||||
) -> Self:
|
||||
"""
|
||||
|
|
@ -591,7 +562,7 @@ class UOffer(PrimitiveOffer):
|
|||
return cls(
|
||||
in_ptype = in_ptype,
|
||||
out_ptype = out_ptype,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_planner = bbox_planner,
|
||||
endpoint_planner = endpoint_planner,
|
||||
commit_planner = commit_planner,
|
||||
|
|
@ -793,14 +764,12 @@ class AutoTool(Tool):
|
|||
"""
|
||||
A routing tool assembled from reusable path primitives.
|
||||
|
||||
`AutoTool` chooses among straight generators, pre-rendered bends, optional
|
||||
generated S-bend primitives, pre-rendered U-turns, and
|
||||
`AutoTool` chooses among prioritized straight generators, pre-rendered bends,
|
||||
optional generated S-bend primitives, pre-rendered U-turns, and
|
||||
pre-rendered transitions registered through `add_straight()`,
|
||||
`add_bend()`, `add_sbend()`, `add_uturn()`, and `add_transition()`.
|
||||
|
||||
Primitive selection uses each registration's explicit `cost` policy rather
|
||||
than registration order. Numeric costs scale the default geometric cost;
|
||||
callable costs replace it.
|
||||
Registration call order defines primitive priority.
|
||||
|
||||
Straight and bend offers use one straight and, if turning, one bend.
|
||||
`add_sbend()` exposes generated S-bend primitives. `add_uturn()` exposes
|
||||
|
|
@ -1003,7 +972,6 @@ class AutoTool(Tool):
|
|||
in_port_name: str | None = None,
|
||||
*,
|
||||
length_range: tuple[float, float] = (0, numpy.inf),
|
||||
cost: CostCallable | float = 1.0,
|
||||
) -> Self:
|
||||
"""
|
||||
Register a generated straight primitive.
|
||||
|
|
@ -1012,8 +980,8 @@ class AutoTool(Tool):
|
|||
generated and the missing metadata is inferred from an equivalent
|
||||
two-port straight. Metadata inference does not receive route options.
|
||||
"""
|
||||
cost = _validated_cost(cost)
|
||||
ptype, in_port_name = self._infer_straight_metadata(fn, length_range, ptype, in_port_name)
|
||||
priority_bias = len(self._straight_offers) * BUILTIN_PRIORITY_STEP
|
||||
|
||||
def data_at(length: float) -> AutoTool.GeneratedData:
|
||||
return self.GeneratedData(fn, in_port_name, length)
|
||||
|
|
@ -1021,7 +989,7 @@ class AutoTool(Tool):
|
|||
self._straight_offers.append(StraightOffer.generated(
|
||||
ptype,
|
||||
data_at,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_for_data = self._bbox_for_data,
|
||||
length_domain = length_range,
|
||||
))
|
||||
|
|
@ -1035,7 +1003,6 @@ class AutoTool(Tool):
|
|||
*,
|
||||
clockwise: bool | None = None,
|
||||
mirror: bool = True,
|
||||
cost: CostCallable | float = 1.0,
|
||||
) -> Self:
|
||||
"""
|
||||
Register a reusable L-bend primitive.
|
||||
|
|
@ -1044,7 +1011,6 @@ class AutoTool(Tool):
|
|||
direction is inferred from the selected port orientations; `clockwise`,
|
||||
when provided, is checked against that inferred direction.
|
||||
"""
|
||||
cost = _validated_cost(cost)
|
||||
if (in_port_name is None) != (out_port_name is None):
|
||||
raise BuildError('Bend port names must be provided together')
|
||||
if in_port_name is None or out_port_name is None:
|
||||
|
|
@ -1053,6 +1019,7 @@ class AutoTool(Tool):
|
|||
raise BuildError(f'Bend port names are required for {len(port_names)}-port abstracts')
|
||||
in_port_name, out_port_name = port_names
|
||||
|
||||
priority_bias = len(self._bend_offers[0]) * BUILTIN_PRIORITY_STEP
|
||||
in_port = abstract.ports[in_port_name]
|
||||
out_port = abstract.ports[out_port_name]
|
||||
out_ptype = out_port.ptype
|
||||
|
|
@ -1077,7 +1044,7 @@ class AutoTool(Tool):
|
|||
endpoint = endpoint,
|
||||
data = reusable_data,
|
||||
ccw = ccw,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_for_data = self._bbox_for_data,
|
||||
))
|
||||
return self
|
||||
|
|
@ -1091,7 +1058,6 @@ class AutoTool(Tool):
|
|||
*,
|
||||
jog_range: tuple[float, float] = (0, numpy.inf),
|
||||
endpoint: GeneratedEndpointFn | None = None,
|
||||
cost: CostCallable | float = 1.0,
|
||||
) -> Self:
|
||||
"""
|
||||
Register a generated S-bend primitive.
|
||||
|
|
@ -1104,7 +1070,6 @@ class AutoTool(Tool):
|
|||
and the missing metadata is inferred from an equivalent two-port S-bend.
|
||||
Metadata inference and endpoint planning do not receive route options.
|
||||
"""
|
||||
cost = _validated_cost(cost)
|
||||
ptype, in_port_name, out_port_name = self._infer_sbend_metadata(
|
||||
fn,
|
||||
jog_range,
|
||||
|
|
@ -1125,6 +1090,8 @@ class AutoTool(Tool):
|
|||
return out_port
|
||||
|
||||
for jog_domain in self._signed_jog_domains(jog_range):
|
||||
priority_bias = len(self._s_offers) * BUILTIN_PRIORITY_STEP
|
||||
|
||||
def data_at(jog: float) -> AutoTool.GeneratedData:
|
||||
return self.GeneratedData(
|
||||
fn,
|
||||
|
|
@ -1137,7 +1104,7 @@ class AutoTool(Tool):
|
|||
ptype,
|
||||
endpoint_at,
|
||||
data_at,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_for_data = self._bbox_for_data,
|
||||
jog_domain = jog_domain,
|
||||
))
|
||||
|
|
@ -1150,12 +1117,10 @@ class AutoTool(Tool):
|
|||
out_port_name: str,
|
||||
*,
|
||||
mirror: bool = True,
|
||||
cost: CostCallable | float = 1.0,
|
||||
) -> Self:
|
||||
"""
|
||||
Register a reusable U-turn primitive.
|
||||
"""
|
||||
cost = _validated_cost(cost)
|
||||
in_port = abstract.ports[in_port_name]
|
||||
out_port = abstract.ports[out_port_name]
|
||||
dxy, angle = in_port.measure_travel(out_port)
|
||||
|
|
@ -1175,6 +1140,7 @@ class AutoTool(Tool):
|
|||
mirrored: bool,
|
||||
) -> None:
|
||||
reusable_data = self.ReusableData(abstract, in_port_name, mirrored)
|
||||
priority_bias = len(self._u_offers) * BUILTIN_PRIORITY_STEP
|
||||
endpoint = Port((length, offer_jog), rotation=0, ptype=out_ptype)
|
||||
|
||||
self._u_offers.append(UOffer.prebuilt(
|
||||
|
|
@ -1182,7 +1148,7 @@ class AutoTool(Tool):
|
|||
out_ptype = out_ptype,
|
||||
endpoint = endpoint,
|
||||
data = reusable_data,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_for_data = self._bbox_for_data,
|
||||
))
|
||||
|
||||
|
|
@ -1198,7 +1164,6 @@ class AutoTool(Tool):
|
|||
our_port_name: str | None = None,
|
||||
*,
|
||||
one_way: bool = False,
|
||||
cost: CostCallable | float = 1.0,
|
||||
) -> Self:
|
||||
"""
|
||||
Register a reusable port-type transition and expose it as router-visible adapter offers.
|
||||
|
|
@ -1206,7 +1171,6 @@ class AutoTool(Tool):
|
|||
If the transition has exactly two ports and is bidirectional, port names
|
||||
may be omitted.
|
||||
"""
|
||||
cost = _validated_cost(cost)
|
||||
if (their_port_name is None) != (our_port_name is None):
|
||||
raise BuildError('Transition port names must be provided together')
|
||||
if their_port_name is None or our_port_name is None:
|
||||
|
|
@ -1217,9 +1181,9 @@ class AutoTool(Tool):
|
|||
raise BuildError(f'Transition port names are required for {len(port_names)}-port abstracts')
|
||||
their_port_name, our_port_name = port_names
|
||||
|
||||
self._add_transition_direction(abstract, their_port_name, our_port_name, cost)
|
||||
self._add_transition_direction(abstract, their_port_name, our_port_name)
|
||||
if not one_way:
|
||||
self._add_transition_direction(abstract, our_port_name, their_port_name, cost)
|
||||
self._add_transition_direction(abstract, our_port_name, their_port_name)
|
||||
return self
|
||||
|
||||
@staticmethod
|
||||
|
|
@ -1297,7 +1261,6 @@ class AutoTool(Tool):
|
|||
abstract: Abstract,
|
||||
their_port_name: str,
|
||||
our_port_name: str,
|
||||
cost: CostCallable | float,
|
||||
) -> None:
|
||||
their_port = abstract.ports[their_port_name]
|
||||
our_port = abstract.ports[our_port_name]
|
||||
|
|
@ -1323,13 +1286,14 @@ class AutoTool(Tool):
|
|||
endpoint = Port((dx, dy), rotation=pi, ptype=our_port.ptype)
|
||||
|
||||
offers = self._transition_adapter_offers_by_key.setdefault((kind, in_key), [])
|
||||
priority_bias = len(offers) * BUILTIN_PRIORITY_STEP
|
||||
if kind == 'straight':
|
||||
offers.append(StraightOffer.prebuilt(
|
||||
in_ptype = their_port.ptype,
|
||||
out_ptype = our_port.ptype,
|
||||
endpoint = endpoint,
|
||||
data = transition_data,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_for_data = self._bbox_for_data,
|
||||
))
|
||||
return
|
||||
|
|
@ -1339,7 +1303,7 @@ class AutoTool(Tool):
|
|||
out_ptype = our_port.ptype,
|
||||
endpoint = endpoint,
|
||||
data = transition_data,
|
||||
cost = cost,
|
||||
priority_bias = priority_bias,
|
||||
bbox_for_data = self._bbox_for_data,
|
||||
))
|
||||
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import mmap
|
|||
import pathlib
|
||||
|
||||
import klamath
|
||||
import numpy
|
||||
from klamath import records
|
||||
|
||||
from . import klamath as gdsii
|
||||
|
|
@ -34,16 +35,15 @@ from ..utils import is_gzipped
|
|||
from ...error import LibraryError
|
||||
from ...library import (
|
||||
ILibraryView,
|
||||
IMaterializable,
|
||||
LibraryView,
|
||||
PortsLibraryView,
|
||||
dangling_mode_t,
|
||||
)
|
||||
from ...library.utils import _validate_dangling_mode
|
||||
from ...utils import apply_transforms
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Iterator, Mapping, Sequence
|
||||
|
||||
import numpy
|
||||
from numpy.typing import NDArray
|
||||
|
||||
from ...pattern import Pattern
|
||||
|
|
@ -122,7 +122,7 @@ def _scan_library(
|
|||
return library_info, order, cells
|
||||
|
||||
|
||||
class GdsLibrarySource(ILibraryView, IMaterializable):
|
||||
class GdsLibrarySource(ILibraryView):
|
||||
"""
|
||||
Read-only library backed by a seekable GDS stream.
|
||||
|
||||
|
|
@ -163,7 +163,7 @@ class GdsLibrarySource(ILibraryView, IMaterializable):
|
|||
return cls(source=source, library_info=library_info, cell_order=cell_order, cells=cells)
|
||||
|
||||
def __getitem__(self, key: str) -> Pattern:
|
||||
return self.materialize(key, persist=True)
|
||||
return self._materialize_pattern(key, persist=True)
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
return iter(self._cell_order)
|
||||
|
|
@ -177,7 +177,19 @@ class GdsLibrarySource(ILibraryView, IMaterializable):
|
|||
def source_order(self) -> tuple[str, ...]:
|
||||
return self._cell_order
|
||||
|
||||
def materialize(self, name: str, *, persist: bool = True) -> Pattern:
|
||||
def materialize_many(
|
||||
self,
|
||||
names: Sequence[str],
|
||||
*,
|
||||
persist: bool = True,
|
||||
) -> LibraryView:
|
||||
mats = {
|
||||
name: self._materialize_pattern(name, persist=persist)
|
||||
for name in dict.fromkeys(names)
|
||||
}
|
||||
return LibraryView(mats)
|
||||
|
||||
def _materialize_pattern(self, name: str, *, persist: bool) -> Pattern:
|
||||
if name in self._cache:
|
||||
return self._cache[name]
|
||||
|
||||
|
|
@ -211,7 +223,6 @@ class GdsLibrarySource(ILibraryView, IMaterializable):
|
|||
self,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, set[str]]:
|
||||
_validate_dangling_mode(dangling)
|
||||
graph: dict[str, set[str]] = {}
|
||||
for name in self._cell_order:
|
||||
if name in self._cache:
|
||||
|
|
@ -232,11 +243,41 @@ class GdsLibrarySource(ILibraryView, IMaterializable):
|
|||
graph.setdefault(cast('str', child), set())
|
||||
return graph
|
||||
|
||||
def parent_graph(
|
||||
self,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, set[str]]:
|
||||
child_graph = self.child_graph(dangling='include' if dangling == 'include' else 'ignore')
|
||||
existing = set(self.keys())
|
||||
igraph: dict[str, set[str]] = {name: set() for name in child_graph}
|
||||
for parent, children in child_graph.items():
|
||||
for child in children:
|
||||
if child in existing or dangling == 'include':
|
||||
igraph.setdefault(child, set()).add(parent)
|
||||
if dangling == 'error':
|
||||
raw = self.child_graph(dangling='include')
|
||||
dangling_refs = set().union(*(children - existing for children in raw.values()))
|
||||
if dangling_refs:
|
||||
raise self._dangling_refs_error(cast('set[str]', dangling_refs), 'building parent graph')
|
||||
return igraph
|
||||
|
||||
def subtree(
|
||||
self,
|
||||
tops: str | Sequence[str],
|
||||
) -> ILibraryView:
|
||||
return super().subtree(tops)
|
||||
if isinstance(tops, str):
|
||||
tops = (tops,)
|
||||
keep = cast('set[str]', self.referenced_patterns(tops) - {None})
|
||||
keep |= set(tops)
|
||||
return self.materialize_many(tuple(keep), persist=True)
|
||||
|
||||
def tops(self) -> list[str]:
|
||||
graph = self.child_graph(dangling='ignore')
|
||||
names = set(graph)
|
||||
not_toplevel: set[str] = set()
|
||||
for children in graph.values():
|
||||
not_toplevel |= children
|
||||
return list(names - not_toplevel)
|
||||
|
||||
def with_ports_from_data(
|
||||
self,
|
||||
|
|
@ -274,7 +315,6 @@ class GdsLibrarySource(ILibraryView, IMaterializable):
|
|||
parent_graph: dict[str, set[str]] | None = None,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, list[NDArray[numpy.float64]]]:
|
||||
_validate_dangling_mode(dangling)
|
||||
instances: dict[str, list[NDArray[numpy.float64]]] = defaultdict(list)
|
||||
if parent_graph is None:
|
||||
graph_mode = 'ignore' if dangling == 'ignore' else 'include'
|
||||
|
|
@ -293,11 +333,58 @@ class GdsLibrarySource(ILibraryView, IMaterializable):
|
|||
for ref in self._cache[parent].refs.get(name, []):
|
||||
instances[parent].append(ref.as_transforms())
|
||||
continue
|
||||
pat = self.materialize(parent, persist=False)
|
||||
pat = self._materialize_pattern(parent, persist=False)
|
||||
for ref in pat.refs.get(name, []):
|
||||
instances[parent].append(ref.as_transforms())
|
||||
return instances
|
||||
|
||||
def find_refs_global(
|
||||
self,
|
||||
name: str,
|
||||
order: list[str] | None = None,
|
||||
parent_graph: dict[str, set[str]] | None = None,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[tuple[str, ...], NDArray[numpy.float64]]:
|
||||
graph_mode = 'ignore' if dangling == 'ignore' else 'include'
|
||||
if order is None:
|
||||
order = self.child_order(dangling=graph_mode)
|
||||
if parent_graph is None:
|
||||
parent_graph = self.parent_graph(dangling=graph_mode)
|
||||
|
||||
if name not in self:
|
||||
if name not in parent_graph:
|
||||
return {}
|
||||
if dangling == 'error':
|
||||
raise self._dangling_refs_error({name}, f'finding global refs for {name!r}')
|
||||
if dangling == 'ignore':
|
||||
return {}
|
||||
|
||||
self_keys = set(self.keys())
|
||||
transforms: dict[str, list[tuple[tuple[str, ...], NDArray[numpy.float64]]]]
|
||||
transforms = defaultdict(list)
|
||||
for parent, vals in self.find_refs_local(name, parent_graph=parent_graph, dangling=dangling).items():
|
||||
transforms[parent] = [((name,), numpy.concatenate(vals))]
|
||||
|
||||
for next_name in order:
|
||||
if next_name not in transforms:
|
||||
continue
|
||||
if not parent_graph.get(next_name, set()) & self_keys:
|
||||
continue
|
||||
|
||||
outers = self.find_refs_local(next_name, parent_graph=parent_graph, dangling=dangling)
|
||||
inners = transforms.pop(next_name)
|
||||
for parent, outer in outers.items():
|
||||
outer_tf = numpy.concatenate(outer)
|
||||
for path, inner in inners:
|
||||
combined = apply_transforms(outer_tf, inner)
|
||||
transforms[parent].append(((next_name,) + path, combined))
|
||||
|
||||
result = {}
|
||||
for parent, targets in transforms.items():
|
||||
for path, instances in targets:
|
||||
result[(parent,) + path] = instances
|
||||
return result
|
||||
|
||||
def close(self) -> None:
|
||||
self._source.close()
|
||||
|
||||
|
|
|
|||
|
|
@ -21,12 +21,11 @@ from .lazy_write import write as write, writefile as writefile
|
|||
from ..utils import is_gzipped
|
||||
from ...library import (
|
||||
ILibraryView,
|
||||
IMaterializable,
|
||||
LibraryView,
|
||||
PortsLibraryView,
|
||||
dangling_mode_t,
|
||||
)
|
||||
from ...library.utils import _validate_dangling_mode
|
||||
from ...utils import apply_transforms
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Iterator, Mapping, Sequence
|
||||
|
|
@ -198,7 +197,7 @@ def _expand_aref_row(
|
|||
return rows
|
||||
|
||||
|
||||
class ArrowLibrary(ILibraryView, IMaterializable):
|
||||
class ArrowLibrary(ILibraryView):
|
||||
"""
|
||||
Read-only library backed by the native lazy Arrow scan schema.
|
||||
|
||||
|
|
@ -232,7 +231,7 @@ class ArrowLibrary(ILibraryView, IMaterializable):
|
|||
return cls(path=path, payload=payload, source=source)
|
||||
|
||||
def __getitem__(self, key: str) -> Pattern:
|
||||
return self.materialize(key, persist=True)
|
||||
return self._materialize_pattern(key, persist=True)
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
return iter(self._payload.cell_order)
|
||||
|
|
@ -300,7 +299,7 @@ class ArrowLibrary(ILibraryView, IMaterializable):
|
|||
materialized[name] = self._cache[name]
|
||||
return materialized
|
||||
|
||||
def materialize(self, name: str, *, persist: bool = True) -> Pattern:
|
||||
def _materialize_pattern(self, name: str, *, persist: bool) -> Pattern:
|
||||
return self._materialize_patterns((name,), persist=persist)[name]
|
||||
|
||||
def _raw_children(self, name: str) -> set[str]:
|
||||
|
|
@ -347,7 +346,6 @@ class ArrowLibrary(ILibraryView, IMaterializable):
|
|||
self,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, set[str]]:
|
||||
_validate_dangling_mode(dangling)
|
||||
graph: dict[str, set[str]] = {}
|
||||
for name in self._payload.cell_order:
|
||||
if name in self._cache:
|
||||
|
|
@ -368,11 +366,41 @@ class ArrowLibrary(ILibraryView, IMaterializable):
|
|||
graph.setdefault(cast('str', child), set())
|
||||
return graph
|
||||
|
||||
def parent_graph(
|
||||
self,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, set[str]]:
|
||||
child_graph = self.child_graph(dangling='include' if dangling == 'include' else 'ignore')
|
||||
existing = set(self.keys())
|
||||
igraph: dict[str, set[str]] = {name: set() for name in child_graph}
|
||||
for parent, children in child_graph.items():
|
||||
for child in children:
|
||||
if child in existing or dangling == 'include':
|
||||
igraph.setdefault(child, set()).add(parent)
|
||||
if dangling == 'error':
|
||||
raw = self.child_graph(dangling='include')
|
||||
dangling_refs = set().union(*(children - existing for children in raw.values()))
|
||||
if dangling_refs:
|
||||
raise self._dangling_refs_error(cast('set[str]', dangling_refs), 'building parent graph')
|
||||
return igraph
|
||||
|
||||
def subtree(
|
||||
self,
|
||||
tops: str | Sequence[str],
|
||||
) -> ILibraryView:
|
||||
return super().subtree(tops)
|
||||
if isinstance(tops, str):
|
||||
tops = (tops,)
|
||||
keep = cast('set[str]', self.referenced_patterns(tops) - {None})
|
||||
keep |= set(tops)
|
||||
return self.materialize_many(tuple(keep), persist=True)
|
||||
|
||||
def tops(self) -> list[str]:
|
||||
graph = self.child_graph(dangling='ignore')
|
||||
names = set(graph)
|
||||
not_toplevel: set[str] = set()
|
||||
for children in graph.values():
|
||||
not_toplevel |= children
|
||||
return list(names - not_toplevel)
|
||||
|
||||
def with_ports_from_data(
|
||||
self,
|
||||
|
|
@ -424,7 +452,6 @@ class ArrowLibrary(ILibraryView, IMaterializable):
|
|||
parent_graph: dict[str, set[str]] | None = None,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, list[NDArray[numpy.float64]]]:
|
||||
_validate_dangling_mode(dangling)
|
||||
instances: dict[str, list[NDArray[numpy.float64]]] = defaultdict(list)
|
||||
if parent_graph is None:
|
||||
graph_mode = 'ignore' if dangling == 'ignore' else 'include'
|
||||
|
|
@ -452,6 +479,54 @@ class ArrowLibrary(ILibraryView, IMaterializable):
|
|||
instances[parent].extend(rows)
|
||||
return instances
|
||||
|
||||
def find_refs_global(
|
||||
self,
|
||||
name: str,
|
||||
order: list[str] | None = None,
|
||||
parent_graph: dict[str, set[str]] | None = None,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[tuple[str, ...], NDArray[numpy.float64]]:
|
||||
graph_mode = 'ignore' if dangling == 'ignore' else 'include'
|
||||
if order is None:
|
||||
order = self.child_order(dangling=graph_mode)
|
||||
if parent_graph is None:
|
||||
parent_graph = self.parent_graph(dangling=graph_mode)
|
||||
|
||||
if name not in self:
|
||||
if name not in parent_graph:
|
||||
return {}
|
||||
if dangling == 'error':
|
||||
raise self._dangling_refs_error({name}, f'finding global refs for {name!r}')
|
||||
if dangling == 'ignore':
|
||||
return {}
|
||||
|
||||
self_keys = set(self.keys())
|
||||
transforms: dict[str, list[tuple[tuple[str, ...], NDArray[numpy.float64]]]]
|
||||
transforms = defaultdict(list)
|
||||
for parent, vals in self.find_refs_local(name, parent_graph=parent_graph, dangling=dangling).items():
|
||||
transforms[parent] = [((name,), numpy.concatenate(vals))]
|
||||
|
||||
for next_name in order:
|
||||
if next_name not in transforms:
|
||||
continue
|
||||
if not parent_graph.get(next_name, set()) & self_keys:
|
||||
continue
|
||||
|
||||
outers = self.find_refs_local(next_name, parent_graph=parent_graph, dangling=dangling)
|
||||
inners = transforms.pop(next_name)
|
||||
for parent, outer in outers.items():
|
||||
outer_tf = numpy.concatenate(outer)
|
||||
for path, inner in inners:
|
||||
combined = apply_transforms(outer_tf, inner)
|
||||
transforms[parent].append(((next_name,) + path, combined))
|
||||
|
||||
result = {}
|
||||
for parent, targets in transforms.items():
|
||||
for path, instances in targets:
|
||||
full_path = (parent,) + path
|
||||
result[full_path] = instances
|
||||
return result
|
||||
|
||||
|
||||
def readfile(
|
||||
filename: str | pathlib.Path,
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ or remapped.
|
|||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import IO, TYPE_CHECKING, Any, Protocol, cast, runtime_checkable
|
||||
from typing import IO, TYPE_CHECKING, Any, cast
|
||||
import gzip
|
||||
import logging
|
||||
import pathlib
|
||||
|
|
@ -18,8 +18,8 @@ import klamath
|
|||
from . import klamath as gdsii
|
||||
from ..utils import tmpfile
|
||||
from ...error import LibraryError
|
||||
from ...library import IBorrowing, ILibraryView
|
||||
from ...library.overlay import _materialize_detached_pattern
|
||||
from ...library import ILibraryView, OverlayLibrary
|
||||
from ...library.overlay import _SourceEntry, _materialize_detached_pattern
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Mapping
|
||||
|
|
@ -30,23 +30,6 @@ if TYPE_CHECKING:
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class _GdsInfoSource(Protocol):
|
||||
"""Structural capability for propagating GDS header metadata."""
|
||||
library_info: dict[str, Any]
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class _GdsRawCellSource(Protocol):
|
||||
"""GDS-specific raw-structure copy-through capability."""
|
||||
|
||||
def source_order(self) -> tuple[str, ...]: ...
|
||||
|
||||
def can_copy_raw_struct(self, name: str) -> bool: ...
|
||||
|
||||
def raw_struct_bytes(self, name: str) -> bytes: ...
|
||||
|
||||
|
||||
def _get_write_info(
|
||||
library: Mapping[str, Pattern] | ILibraryView,
|
||||
*,
|
||||
|
|
@ -59,16 +42,13 @@ def _get_write_info(
|
|||
|
||||
infos: list[dict[str, Any]] = []
|
||||
stack: list[Mapping[str, Pattern] | ILibraryView] = [library]
|
||||
seen: set[int] = set()
|
||||
while stack:
|
||||
current = stack.pop()
|
||||
if id(current) in seen:
|
||||
continue
|
||||
seen.add(id(current))
|
||||
if isinstance(current, _GdsInfoSource) and isinstance(current.library_info, dict):
|
||||
infos.append(current.library_info)
|
||||
if isinstance(current, IBorrowing):
|
||||
stack.extend(reversed(current.borrowed_sources()))
|
||||
info = getattr(current, 'library_info', None)
|
||||
if isinstance(info, dict):
|
||||
infos.append(info)
|
||||
if isinstance(current, OverlayLibrary):
|
||||
stack.extend(reversed([layer.library for layer in current._layers]))
|
||||
|
||||
if infos:
|
||||
unit_pairs = {(info['meters_per_unit'], info['logical_units_per_unit']) for info in infos}
|
||||
|
|
@ -85,6 +65,20 @@ def _get_write_info(
|
|||
return meters_per_unit, logical_units_per_unit, library_name
|
||||
|
||||
|
||||
def _can_copy_raw_cell(library: Mapping[str, Pattern] | ILibraryView, name: str) -> bool:
|
||||
can_copy = getattr(library, 'can_copy_raw_struct', None)
|
||||
if not callable(can_copy):
|
||||
return False
|
||||
return bool(can_copy(name))
|
||||
|
||||
|
||||
def _raw_struct_bytes(library: Mapping[str, Pattern] | ILibraryView, name: str) -> bytes:
|
||||
reader = getattr(library, 'raw_struct_bytes', None)
|
||||
if not callable(reader):
|
||||
raise TypeError('raw_struct_bytes')
|
||||
return cast('bytes', reader(name))
|
||||
|
||||
|
||||
def _write_pattern_struct(stream: IO[bytes], name: str, pat: Pattern) -> None:
|
||||
elements: list[klamath.elements.Element] = []
|
||||
elements += gdsii._shapes_to_elements(pat.shapes)
|
||||
|
|
@ -115,10 +109,28 @@ def write(
|
|||
)
|
||||
header.write(stream)
|
||||
|
||||
if isinstance(library, _GdsRawCellSource):
|
||||
if isinstance(library, OverlayLibrary):
|
||||
for name in library.source_order():
|
||||
if library.can_copy_raw_struct(name):
|
||||
stream.write(library.raw_struct_bytes(name))
|
||||
entry = library._entries[name]
|
||||
can_copy_overlay = False
|
||||
if isinstance(entry, _SourceEntry) and name == entry.source_name:
|
||||
layer = library._layers[entry.layer_index]
|
||||
children = layer.child_graph.get(entry.source_name, set())
|
||||
can_copy_overlay = (
|
||||
_can_copy_raw_cell(layer.library, entry.source_name)
|
||||
and all(library._effective_target(layer, child) == child for child in children)
|
||||
)
|
||||
if can_copy_overlay:
|
||||
stream.write(_raw_struct_bytes(layer.library, entry.source_name))
|
||||
else:
|
||||
_write_pattern_struct(stream, name, library._materialize_pattern(name, persist=False))
|
||||
klamath.records.ENDLIB.write(stream, None)
|
||||
return
|
||||
|
||||
if hasattr(library, 'raw_struct_bytes'):
|
||||
for name in library.source_order():
|
||||
if _can_copy_raw_cell(library, name):
|
||||
stream.write(_raw_struct_bytes(library, name))
|
||||
else:
|
||||
_write_pattern_struct(stream, name, _materialize_detached_pattern(cast('ILibraryView', library), name))
|
||||
klamath.records.ENDLIB.write(stream, None)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
"""Library classes for managing name-to-pattern mappings."""
|
||||
from .utils import (
|
||||
INameView as INameView,
|
||||
SINGLE_USE_PREFIX as SINGLE_USE_PREFIX,
|
||||
Tree as Tree,
|
||||
TreeView as TreeView,
|
||||
|
|
@ -13,10 +12,6 @@ from .base import (
|
|||
ILibrary as ILibrary,
|
||||
ILibraryView as ILibraryView,
|
||||
)
|
||||
from .capabilities import (
|
||||
IBorrowing as IBorrowing,
|
||||
IMaterializable as IMaterializable,
|
||||
)
|
||||
from .mapping import (
|
||||
Library as Library,
|
||||
LibraryView as LibraryView,
|
||||
|
|
@ -26,7 +21,7 @@ from .overlay import (
|
|||
PortsLibraryView as PortsLibraryView,
|
||||
)
|
||||
from .build import (
|
||||
LibraryBuilder as LibraryBuilder,
|
||||
BuildLibrary as BuildLibrary,
|
||||
BuildReport as BuildReport,
|
||||
CellProvenance as CellProvenance,
|
||||
cell as cell,
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ from graphlib import CycleError, TopologicalSorter
|
|||
from pprint import pformat
|
||||
from typing import TYPE_CHECKING, Self, cast
|
||||
import copy
|
||||
import logging
|
||||
import re
|
||||
|
||||
import numpy
|
||||
|
||||
|
|
@ -16,18 +18,7 @@ from ..error import LibraryError, PatternError
|
|||
from ..pattern import Pattern, map_layers
|
||||
from ..shapes import Polygon, Shape
|
||||
from ..utils import apply_transforms, layer_t
|
||||
from .utils import (
|
||||
SINGLE_USE_PREFIX,
|
||||
INameView,
|
||||
TreeView,
|
||||
b64suffix,
|
||||
dangling_mode_t,
|
||||
_plan_source_names,
|
||||
_rename_patterns,
|
||||
_source_rename_map,
|
||||
_validate_dangling_mode,
|
||||
visitor_function_t,
|
||||
)
|
||||
from .utils import SINGLE_USE_PREFIX, TreeView, b64suffix, dangling_mode_t, _rename_patterns, visitor_function_t
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Iterator
|
||||
|
|
@ -37,7 +28,10 @@ if TYPE_CHECKING:
|
|||
from ..label import Label
|
||||
|
||||
|
||||
class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
|
||||
"""
|
||||
Interface for a read-only library.
|
||||
|
||||
|
|
@ -84,7 +78,7 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
def dangling_refs(
|
||||
self,
|
||||
tops: str | Sequence[str] | None = None,
|
||||
) -> set[str]:
|
||||
) -> set[str | None]:
|
||||
"""
|
||||
Get the set of all pattern names not present in the library but referenced
|
||||
by `tops`, recursively traversing any refs.
|
||||
|
|
@ -107,8 +101,8 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
def referenced_patterns(
|
||||
self,
|
||||
tops: str | Sequence[str] | None = None,
|
||||
skip: set[str] | None = None,
|
||||
) -> set[str]:
|
||||
skip: set[str | None] | None = None,
|
||||
) -> set[str | None]:
|
||||
"""
|
||||
Get the set of all pattern names referenced by `tops`. Recursively traverses into any refs.
|
||||
|
||||
|
|
@ -122,78 +116,29 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
Returns:
|
||||
Set of all referenced pattern names
|
||||
"""
|
||||
graph = self.child_graph(dangling='include')
|
||||
return self._referenced_patterns_from_graph(graph, tops=tops, skip=skip)
|
||||
|
||||
def _referenced_patterns_from_graph(
|
||||
self,
|
||||
graph: Mapping[str, set[str]],
|
||||
*,
|
||||
tops: str | Sequence[str] | None,
|
||||
skip: set[str] | None = None,
|
||||
) -> set[str]:
|
||||
"""Traverse named references in an already-computed child graph."""
|
||||
existing = set(self.keys())
|
||||
if tops is None:
|
||||
roots = tuple(existing)
|
||||
elif isinstance(tops, str):
|
||||
roots = (tops,)
|
||||
else:
|
||||
roots = tuple(tops)
|
||||
|
||||
for root in roots:
|
||||
if root not in existing:
|
||||
raise KeyError(root)
|
||||
tops = tuple(self.keys())
|
||||
|
||||
if skip is None:
|
||||
skip = set()
|
||||
root_set = set(roots)
|
||||
skip |= root_set
|
||||
skip = {None}
|
||||
|
||||
targets: set[str] = set()
|
||||
stack = list(root_set)
|
||||
while stack:
|
||||
name = stack.pop()
|
||||
children = graph.get(name, set())
|
||||
targets |= children
|
||||
for child in children - skip:
|
||||
skip.add(child)
|
||||
if child in existing:
|
||||
stack.append(child)
|
||||
return targets
|
||||
if isinstance(tops, str):
|
||||
tops = (tops,)
|
||||
tops = set(tops)
|
||||
skip |= tops # don't re-visit tops
|
||||
|
||||
def _referenced_patterns_by_lookup(
|
||||
self,
|
||||
tops: str | Sequence[str] | None = None,
|
||||
skip: set[str] | None = None,
|
||||
) -> set[str]:
|
||||
"""Traverse refs by loading only reachable patterns."""
|
||||
if tops is None:
|
||||
roots = tuple(self.keys())
|
||||
elif isinstance(tops, str):
|
||||
roots = (tops,)
|
||||
else:
|
||||
roots = tuple(tops)
|
||||
# Get referenced patterns for all tops
|
||||
targets = set()
|
||||
for top in set(tops):
|
||||
targets |= self[top].referenced_patterns()
|
||||
|
||||
if skip is None:
|
||||
skip = set()
|
||||
root_set = set(roots)
|
||||
skip |= root_set
|
||||
# Perform recursive lookups, but only once for each name
|
||||
for target in targets - skip:
|
||||
assert target is not None
|
||||
skip.add(target)
|
||||
if target in self:
|
||||
targets |= self.referenced_patterns(target, skip=skip)
|
||||
|
||||
targets: set[str] = set()
|
||||
stack = list(root_set)
|
||||
while stack:
|
||||
name = stack.pop()
|
||||
children = {
|
||||
target
|
||||
for target in self[name].referenced_patterns()
|
||||
if target is not None
|
||||
}
|
||||
targets |= children
|
||||
for child in children - skip:
|
||||
skip.add(child)
|
||||
if child in self:
|
||||
stack.append(child)
|
||||
return targets
|
||||
|
||||
def subtree(
|
||||
|
|
@ -209,22 +154,19 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
tops: Name(s) of patterns to keep
|
||||
|
||||
Returns:
|
||||
A borrowed view containing only `tops` and the patterns they reference.
|
||||
|
||||
The returned view snapshots membership and hierarchy, but borrows patterns
|
||||
and source capabilities from this library. Keep this library open and do
|
||||
not structurally mutate it for the lifetime of the returned view.
|
||||
A `LibraryView` containing only `tops` and the patterns they reference.
|
||||
"""
|
||||
if isinstance(tops, str):
|
||||
tops = (tops,)
|
||||
|
||||
graph = self.child_graph(dangling='include')
|
||||
keep = self._referenced_patterns_from_graph(graph, tops=tops)
|
||||
keep &= set(self.keys())
|
||||
keep = cast('set[str]', self.referenced_patterns(tops) - {None})
|
||||
keep |= set(tops)
|
||||
|
||||
from .mapping import _SubtreeLibraryView # noqa: PLC0415
|
||||
return _SubtreeLibraryView(self, names=keep, child_graph=graph)
|
||||
from .mapping import LibraryView # noqa: PLC0415
|
||||
|
||||
filtered = {kk: vv for kk, vv in self.items() if kk in keep}
|
||||
new = LibraryView(filtered)
|
||||
return new
|
||||
|
||||
def polygonize(
|
||||
self,
|
||||
|
|
@ -300,7 +242,6 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
def flatten_single(name: str) -> None:
|
||||
flattened[name] = None
|
||||
pat = self[name].deepcopy()
|
||||
pat.prune_refs()
|
||||
refs_by_target = tuple((target, tuple(refs)) for target, refs in pat.refs.items())
|
||||
|
||||
for target, refs in refs_by_target:
|
||||
|
|
@ -340,6 +281,59 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
assert None not in flattened.values()
|
||||
return cast('dict[str, Pattern]', flattened)
|
||||
|
||||
def get_name(
|
||||
self,
|
||||
name: str = SINGLE_USE_PREFIX * 2,
|
||||
sanitize: bool = True,
|
||||
max_length: int = 32,
|
||||
quiet: bool | None = None,
|
||||
) -> str:
|
||||
"""
|
||||
Find a unique name for the pattern.
|
||||
|
||||
This function may be overridden in a subclass or monkey-patched to fit the caller's requirements.
|
||||
|
||||
Args:
|
||||
name: Preferred name for the pattern. Default is `SINGLE_USE_PREFIX * 2`.
|
||||
sanitize: Allows only alphanumeric charaters and _?$. Replaces invalid characters with underscores.
|
||||
max_length: Names longer than this will be truncated.
|
||||
quiet: If `True`, suppress log messages. Default `None` suppresses messages only if
|
||||
the name starts with `SINGLE_USE_PREFIX`.
|
||||
|
||||
Returns:
|
||||
Name, unique within this library.
|
||||
"""
|
||||
if quiet is None:
|
||||
quiet = name.startswith(SINGLE_USE_PREFIX)
|
||||
|
||||
if sanitize:
|
||||
# Remove invalid characters
|
||||
sanitized_name = re.compile(r'[^A-Za-z0-9_\?\$]').sub('_', name)
|
||||
else:
|
||||
sanitized_name = name
|
||||
|
||||
suffixed_name = sanitized_name
|
||||
if sanitized_name in self:
|
||||
ii = sum(1 for nn in self.keys() if nn.startswith(sanitized_name))
|
||||
else:
|
||||
ii = 0
|
||||
while suffixed_name in self or suffixed_name == '':
|
||||
suffixed_name = sanitized_name + b64suffix(ii)
|
||||
ii += 1
|
||||
|
||||
if len(suffixed_name) > max_length:
|
||||
if name == '':
|
||||
raise LibraryError(f'No valid pattern names remaining within the specified {max_length=}')
|
||||
|
||||
cropped_name = self.get_name(sanitized_name[:-1], sanitize=sanitize, max_length=max_length, quiet=True)
|
||||
else:
|
||||
cropped_name = suffixed_name
|
||||
|
||||
if not quiet:
|
||||
logger.info(f'Requested name "{name}" changed to "{cropped_name}"')
|
||||
|
||||
return cropped_name
|
||||
|
||||
def tops(self) -> list[str]:
|
||||
"""
|
||||
Return the list of all patterns that are not referenced by any other pattern in the library.
|
||||
|
|
@ -347,10 +341,13 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
Returns:
|
||||
A list of pattern names in which no pattern is referenced by any other pattern.
|
||||
"""
|
||||
graph = self.child_graph(dangling='ignore')
|
||||
names = set(self.keys())
|
||||
referenced = set().union(*graph.values()) if graph else set()
|
||||
return list(names - referenced)
|
||||
not_toplevel: set[str | None] = set()
|
||||
for name in names:
|
||||
not_toplevel |= set(self[name].refs.keys())
|
||||
|
||||
toplevel = list(names - not_toplevel)
|
||||
return toplevel
|
||||
|
||||
def top(self) -> str:
|
||||
"""
|
||||
|
|
@ -415,7 +412,7 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
tuple of all parent-and-higher pattern names. Top pattern name may be
|
||||
`None` if not provided in first call to .dfs()
|
||||
`transform`: numpy.ndarray containing cumulative
|
||||
[x_offset, y_offset, rotation (rad), mirror_x (0 or 1), scale]
|
||||
[x_offset, y_offset, rotation (rad), mirror_x (0 or 1)]
|
||||
for the instance being visited
|
||||
`memo`: Arbitrary dict (not altered except by `visit_before()` and `visit_after()`)
|
||||
|
||||
|
|
@ -453,13 +450,13 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
if visit_before is not None:
|
||||
pattern = visit_before(pattern, hierarchy=hierarchy, memo=memo, transform=transform)
|
||||
|
||||
for target, refs in pattern.refs.items():
|
||||
if target is None or not refs:
|
||||
for target in pattern.refs:
|
||||
if target is None:
|
||||
continue
|
||||
if target in hierarchy:
|
||||
raise LibraryError(f'.dfs() called on pattern with circular reference to "{target}"')
|
||||
|
||||
for ref in refs:
|
||||
for ref in pattern.refs[target]:
|
||||
ref_transforms: list[bool] | NDArray[numpy.float64]
|
||||
if transform is not False:
|
||||
ref_transforms = apply_transforms(transform, ref.as_transforms())
|
||||
|
|
@ -512,7 +509,6 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
Returns:
|
||||
Mapping from pattern name to a set of all pattern names it references.
|
||||
"""
|
||||
_validate_dangling_mode(dangling)
|
||||
graph, dangling_refs = self._raw_child_graph()
|
||||
if dangling == 'error':
|
||||
if dangling_refs:
|
||||
|
|
@ -542,18 +538,13 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
Returns:
|
||||
Mapping from pattern name to a set of all patterns which reference it.
|
||||
"""
|
||||
_validate_dangling_mode(dangling)
|
||||
graph_mode: dangling_mode_t = 'ignore' if dangling == 'ignore' else 'include'
|
||||
child_graph = self.child_graph(dangling=graph_mode)
|
||||
existing = set(self.keys())
|
||||
dangling_refs = set(child_graph) - existing
|
||||
child_graph, dangling_refs = self._raw_child_graph()
|
||||
if dangling == 'error' and dangling_refs:
|
||||
raise self._dangling_refs_error(dangling_refs, 'building parent graph')
|
||||
|
||||
graph_names = set(child_graph) if dangling == 'include' else existing
|
||||
igraph: dict[str, set[str]] = {name: set() for name in graph_names}
|
||||
for parent in existing:
|
||||
children = child_graph.get(parent, set())
|
||||
existing = set(child_graph)
|
||||
igraph: dict[str, set[str]] = {name: set() for name in existing}
|
||||
for parent, children in child_graph.items():
|
||||
for child in children:
|
||||
if child in existing:
|
||||
igraph[child].add(parent)
|
||||
|
|
@ -575,7 +566,6 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
Return:
|
||||
Topologically sorted list of pattern names.
|
||||
"""
|
||||
_validate_dangling_mode(dangling)
|
||||
try:
|
||||
return cast('list[str]', list(TopologicalSorter(self.child_graph(dangling=dangling)).static_order()))
|
||||
except CycleError as exc:
|
||||
|
|
@ -606,10 +596,9 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
|
||||
Returns:
|
||||
Mapping of {parent_name: transform_list}, where transform_list
|
||||
is an Nx5 ndarray with rows
|
||||
`(x_offset, y_offset, rotation_ccw_rad, mirror_across_x, scale)`.
|
||||
is an Nx4 ndarray with rows
|
||||
`(x_offset, y_offset, rotation_ccw_rad, mirror_across_x)`.
|
||||
"""
|
||||
_validate_dangling_mode(dangling)
|
||||
instances = defaultdict(list)
|
||||
if parent_graph is None:
|
||||
graph_mode = 'ignore' if dangling == 'ignore' else 'include'
|
||||
|
|
@ -659,10 +648,9 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
|
||||
Returns:
|
||||
Mapping of `{hierarchy: transform_list}`, where `hierarchy` is a tuple of the form
|
||||
`(toplevel_pattern, lvl1_pattern, ..., name)` and `transform_list` is an Nx5 ndarray
|
||||
with rows `(x_offset, y_offset, rotation_ccw_rad, mirror_across_x, scale)`.
|
||||
`(toplevel_pattern, lvl1_pattern, ..., name)` and `transform_list` is an Nx4 ndarray
|
||||
with rows `(x_offset, y_offset, rotation_ccw_rad, mirror_across_x)`.
|
||||
"""
|
||||
_validate_dangling_mode(dangling)
|
||||
graph_mode = 'ignore' if dangling == 'ignore' else 'include'
|
||||
if order is None:
|
||||
order = self.child_order(dangling=graph_mode)
|
||||
|
|
@ -712,15 +700,11 @@ class ILibraryView(Mapping[str, 'Pattern'], INameView, metaclass=ABCMeta):
|
|||
|
||||
|
||||
|
||||
class ILibrary(ILibraryView, metaclass=ABCMeta):
|
||||
class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta):
|
||||
"""
|
||||
Interface for an insertable and deletable library.
|
||||
Interface for a writeable library.
|
||||
|
||||
A library is a mapping from unique names (str) to collections of geometry (`Pattern`).
|
||||
Assignment inserts new names but does not replace existing ones. `ILibrary`
|
||||
intentionally does not implement `MutableMapping` or its generic mutation
|
||||
helpers; use the library-specific insertion, deletion, rename, and add
|
||||
operations instead.
|
||||
"""
|
||||
# inherited abstract functions
|
||||
#def __getitem__(self, key: str) -> 'Pattern':
|
||||
|
|
@ -869,35 +853,27 @@ class ILibrary(ILibraryView, metaclass=ABCMeta):
|
|||
def add(
|
||||
self,
|
||||
other: Mapping[str, Pattern],
|
||||
rename_theirs: Callable[[INameView, str], str] = _rename_patterns,
|
||||
rename_theirs: Callable[[ILibraryView, str], str] = _rename_patterns,
|
||||
mutate_other: bool = False,
|
||||
) -> dict[str, str]:
|
||||
"""
|
||||
Add items from another library into this one.
|
||||
|
||||
If any name in `other` is already present in the prospective destination,
|
||||
`rename_theirs(view, name)` is called to pick a new name for the newly-added pattern.
|
||||
The name-only `INameView` includes destination names and names reserved earlier in
|
||||
the same addition plan.
|
||||
If the new name still conflicts, a `LibraryError` is raised. All references to the
|
||||
original name within `other` are updated to the new name.
|
||||
If any name in `other` is already present in `self`, `rename_theirs(self, name)` is called
|
||||
to pick a new name for the newly-added pattern. If the new name still conflicts with a name
|
||||
in `self` a `LibraryError` is raised. All references to the original name (within `other)`
|
||||
are updated to the new name.
|
||||
If `mutate_other=False` (default), all changes are made to a deepcopy of `other`.
|
||||
|
||||
Name resolution, copying, and reference remapping complete before cells are inserted,
|
||||
so failures during those phases do not partially modify either library. Custom
|
||||
implementations of `_merge()` may still have partial effects if they raise while cells
|
||||
are being committed.
|
||||
|
||||
By default, `rename_theirs` makes no changes to the name (causing a `LibraryError`) unless the
|
||||
name starts with `SINGLE_USE_PREFIX`. Prefixed names are truncated to before their first
|
||||
non-prefix '$' and then passed to `self.get_name()` to create a new unique name.
|
||||
|
||||
Args:
|
||||
other: The library to insert keys from.
|
||||
rename_theirs: Called as `rename_theirs(view, name)` for each duplicate name
|
||||
encountered in `other`, where `view` supports membership, iteration,
|
||||
`len()`, and `get_name()`, but not pattern lookup. Should return the new
|
||||
name for the pattern in `other`. See above for default behavior.
|
||||
rename_theirs: Called as rename_theirs(self, name) for each duplicate name
|
||||
encountered in `other`. Should return the new name for the pattern in
|
||||
`other`. See above for default behavior.
|
||||
mutate_other: If `True`, modify the original library and its contained patterns
|
||||
(e.g. when renaming patterns and updating refs). Otherwise, operate on a deepcopy
|
||||
(default).
|
||||
|
|
@ -912,34 +888,42 @@ class ILibrary(ILibraryView, metaclass=ABCMeta):
|
|||
from ..pattern import map_targets # noqa: PLC0415
|
||||
from .mapping import Library # noqa: PLC0415
|
||||
|
||||
source_order = tuple(other.keys())
|
||||
source_to_visible = _plan_source_names(
|
||||
self,
|
||||
source_order,
|
||||
rename_theirs = rename_theirs,
|
||||
rename_when = 'conflict',
|
||||
)
|
||||
rename_map = _source_rename_map(source_to_visible)
|
||||
duplicates = set(self.keys()) & set(other.keys())
|
||||
|
||||
if not duplicates:
|
||||
if mutate_other:
|
||||
temp = other
|
||||
else:
|
||||
temp = Library(copy.deepcopy(dict(other)))
|
||||
|
||||
for key in temp:
|
||||
self._merge(key, temp, key)
|
||||
return {}
|
||||
|
||||
if mutate_other:
|
||||
if isinstance(other, Library):
|
||||
temp = other
|
||||
else:
|
||||
temp = Library({name: other[name] for name in source_order})
|
||||
temp = Library(dict(other))
|
||||
else:
|
||||
temp = Library(copy.deepcopy({name: other[name] for name in source_order}))
|
||||
temp = Library(copy.deepcopy(dict(other)))
|
||||
rename_map = {}
|
||||
for old_name in temp:
|
||||
if old_name in self:
|
||||
new_name = rename_theirs(self, old_name)
|
||||
if new_name in self:
|
||||
raise LibraryError(f'Unresolved duplicate key encountered in library merge: {old_name} -> {new_name}')
|
||||
rename_map[old_name] = new_name
|
||||
else:
|
||||
new_name = old_name
|
||||
|
||||
if rename_map:
|
||||
target_map = cast('dict[str | None, str | None]', rename_map)
|
||||
remapped_refs = {
|
||||
source_name: map_targets(temp[source_name].refs, lambda target: target_map.get(target, target))
|
||||
for source_name in source_order
|
||||
}
|
||||
for source_name, refs in remapped_refs.items():
|
||||
temp[source_name].refs = refs
|
||||
self._merge(new_name, temp, old_name)
|
||||
|
||||
for source_name in source_order:
|
||||
self._merge(source_to_visible[source_name], temp, source_name)
|
||||
# Update references in the newly-added cells
|
||||
for old_name in temp:
|
||||
new_name = rename_map.get(old_name, old_name)
|
||||
pat = self[new_name]
|
||||
pat.refs = map_targets(pat.refs, lambda tt: cast('dict[str | None, str | None]', rename_map).get(tt, tt))
|
||||
|
||||
return rename_map
|
||||
|
||||
|
|
@ -950,18 +934,21 @@ class ILibrary(ILibraryView, metaclass=ABCMeta):
|
|||
based on `add()`'s default `rename_theirs` argument).
|
||||
|
||||
Raises:
|
||||
LibraryError if there is not exactly one topcell in `other`.
|
||||
LibraryError if there is more than one topcell in `other`.
|
||||
"""
|
||||
from .mapping import LibraryView # noqa: PLC0415
|
||||
|
||||
if not isinstance(other, ILibraryView):
|
||||
other = LibraryView(other)
|
||||
if len(other) == 1:
|
||||
name = next(iter(other))
|
||||
else:
|
||||
if not isinstance(other, ILibraryView):
|
||||
other = LibraryView(other)
|
||||
|
||||
tops = other.tops()
|
||||
if len(tops) != 1:
|
||||
raise LibraryError(f'Received a library without exactly one topcell: {pformat(tops)}')
|
||||
tops = other.tops()
|
||||
if len(tops) > 1:
|
||||
raise LibraryError('Received a library containing multiple topcells!')
|
||||
|
||||
name = tops[0]
|
||||
name = tops[0]
|
||||
|
||||
rename_map = self.add(other)
|
||||
new_name = rename_map.get(name, name)
|
||||
|
|
@ -973,7 +960,7 @@ class ILibrary(ILibraryView, metaclass=ABCMeta):
|
|||
of just the pattern's name.
|
||||
|
||||
Raises:
|
||||
LibraryError if there is not exactly one topcell in `other`.
|
||||
LibraryError if there is more than one topcell in `other`.
|
||||
"""
|
||||
new_name = self << other
|
||||
return self.abstract(new_name)
|
||||
|
|
@ -1191,7 +1178,7 @@ class ILibrary(ILibraryView, metaclass=ABCMeta):
|
|||
if isinstance(tops, str):
|
||||
tops = (tops,)
|
||||
|
||||
keep = self.referenced_patterns(tops)
|
||||
keep = cast('set[str]', self.referenced_patterns(tops) - {None})
|
||||
keep |= set(tops)
|
||||
|
||||
new = type(self)()
|
||||
|
|
@ -1214,7 +1201,6 @@ class ILibrary(ILibraryView, metaclass=ABCMeta):
|
|||
Returns:
|
||||
A set containing the names of all deleted patterns
|
||||
"""
|
||||
_validate_dangling_mode(dangling)
|
||||
parent_graph = self.parent_graph(dangling=dangling)
|
||||
empty = {name for name, pat in self.items() if pat.is_empty()}
|
||||
trimmed = set()
|
||||
|
|
@ -1271,6 +1257,3 @@ class AbstractView(Mapping[str, Abstract]):
|
|||
|
||||
def __len__(self) -> int:
|
||||
return self.library.__len__()
|
||||
|
||||
def __contains__(self, key: object) -> bool:
|
||||
return key in self.library
|
||||
|
|
|
|||
|
|
@ -2,25 +2,32 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from collections import defaultdict
|
||||
from contextvars import ContextVar
|
||||
from dataclasses import dataclass, replace
|
||||
from functools import wraps
|
||||
from types import MappingProxyType
|
||||
from pprint import pformat
|
||||
from typing import TYPE_CHECKING, Any, Literal, Self, cast
|
||||
import copy
|
||||
|
||||
from ..error import BuildError, LibraryError
|
||||
from .base import ILibrary, ILibraryView
|
||||
from .capabilities import IMaterializable
|
||||
from .utils import INameView, TreeView, dangling_mode_t, _plan_source_names, _rename_patterns, _source_rename_map
|
||||
from .utils import TreeView, dangling_mode_t, _plan_source_names, _rename_patterns, _source_rename_map
|
||||
from .mapping import Library, LibraryView
|
||||
from .overlay import OverlayLibrary
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable, Iterator, KeysView, Mapping, Sequence
|
||||
from collections.abc import Callable, Iterator, Mapping, Sequence
|
||||
|
||||
from ..abstract import Abstract
|
||||
from ..pattern import Pattern
|
||||
|
||||
|
||||
_ACTIVE_BUILD_SESSIONS: ContextVar[dict[int, _BuildSessionLibrary] | None] = ContextVar(
|
||||
'masque_active_build_sessions',
|
||||
default=None,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CellProvenance:
|
||||
"""
|
||||
|
|
@ -48,7 +55,7 @@ class CellProvenance:
|
|||
@dataclass(frozen=True)
|
||||
class BuildReport:
|
||||
"""
|
||||
Immutable summary of one `LibraryBuilder.validate()` or `.build()` run.
|
||||
Immutable summary of one `BuildLibrary.validate()` or `.build()` run.
|
||||
|
||||
The report is designed to answer two questions after a build completes:
|
||||
which declared cells depended on which other declared cells, and where each
|
||||
|
|
@ -65,15 +72,6 @@ class BuildReport:
|
|||
provenance: Mapping[str, CellProvenance]
|
||||
dependency_graph: Mapping[str, frozenset[str]]
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
object.__setattr__(self, 'requested_roots', tuple(self.requested_roots))
|
||||
object.__setattr__(self, 'provenance', MappingProxyType(dict(self.provenance)))
|
||||
object.__setattr__(
|
||||
self,
|
||||
'dependency_graph',
|
||||
MappingProxyType({name: frozenset(deps) for name, deps in self.dependency_graph.items()}),
|
||||
)
|
||||
|
||||
@dataclass
|
||||
class _BuildRecipe:
|
||||
""" Captured deferred call to a pattern factory. """
|
||||
|
|
@ -100,54 +98,42 @@ def cell(func: Callable[..., Pattern]) -> Callable[..., _BuildRecipe]:
|
|||
return wrapper
|
||||
|
||||
|
||||
class _LibraryPlaceholder:
|
||||
"""Identity token replaced by this builder's active build-session library."""
|
||||
__slots__ = ('_builder',)
|
||||
|
||||
def __init__(self, builder: LibraryBuilder) -> None:
|
||||
self._builder = builder
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return '<LibraryBuilder.library>'
|
||||
|
||||
|
||||
class BuildCellsView:
|
||||
"""
|
||||
Attribute-based declaration namespace for `LibraryBuilder`.
|
||||
Attribute-based declaration namespace for `BuildLibrary`.
|
||||
|
||||
This is the ergonomic authoring surface exposed as `builder.cells`. It is
|
||||
intentionally write-focused: attribute assignment and deletion register
|
||||
declarations, while attribute reads fail with guidance to build first and
|
||||
use the returned library.
|
||||
"""
|
||||
__slots__ = ('_library',)
|
||||
|
||||
def __init__(self, library: LibraryBuilder) -> None:
|
||||
def __init__(self, library: BuildLibrary) -> None:
|
||||
object.__setattr__(self, '_library', library)
|
||||
|
||||
def __getattr__(self, name: str) -> Pattern:
|
||||
raise BuildError(
|
||||
f'LibraryBuilder.cells.{name} is write-only during authoring. '
|
||||
f'BuildLibrary.cells.{name} is write-only during authoring. '
|
||||
'Call build() and index the returned library instead.'
|
||||
)
|
||||
|
||||
def __setattr__(self, name: str, value: Pattern | _BuildRecipe) -> None:
|
||||
if name == '_library':
|
||||
if name.startswith('_'):
|
||||
object.__setattr__(self, name, value)
|
||||
return
|
||||
self._library[name] = value
|
||||
|
||||
def __delattr__(self, name: str) -> None:
|
||||
if name == '_library':
|
||||
if name.startswith('_'):
|
||||
raise AttributeError(name)
|
||||
del self._library[name]
|
||||
|
||||
|
||||
class LibraryBuilder(INameView):
|
||||
class BuildLibrary(ILibrary):
|
||||
"""
|
||||
Two-phase declaration surface for mixed imported/generated libraries.
|
||||
|
||||
A `LibraryBuilder` collects three kinds of inputs:
|
||||
A `BuildLibrary` collects three kinds of inputs:
|
||||
- direct declared `Pattern` objects
|
||||
- deferred recipes created with `cell(...)`
|
||||
- imported source-backed library views added with `add_source(...)`
|
||||
|
|
@ -161,82 +147,112 @@ class LibraryBuilder(INameView):
|
|||
|
||||
def __init__(self) -> None:
|
||||
self.cells = BuildCellsView(self)
|
||||
self._library_placeholder = _LibraryPlaceholder(self)
|
||||
self._frozen = False
|
||||
self._building = False
|
||||
self._declarations: dict[str, Pattern | _BuildRecipe] = {}
|
||||
self._sources: list[tuple[ILibraryView, dict[str, str]]] = []
|
||||
self._names: dict[str, None] = {}
|
||||
|
||||
@property
|
||||
def library(self) -> _LibraryPlaceholder:
|
||||
"""Read-only placeholder replaced by the active library in direct recipe arguments."""
|
||||
return self._library_placeholder
|
||||
def _active_session(self) -> _BuildSessionLibrary | None:
|
||||
sessions = _ACTIVE_BUILD_SESSIONS.get()
|
||||
if sessions is None:
|
||||
return None
|
||||
return sessions.get(id(self))
|
||||
|
||||
def _require_active_session(self, operation: str) -> _BuildSessionLibrary:
|
||||
session = self._active_session()
|
||||
if session is None:
|
||||
raise BuildError(
|
||||
f'BuildLibrary.{operation}() is only available while validate() or build() is running. '
|
||||
'Use the built output library for reads.'
|
||||
)
|
||||
return session
|
||||
|
||||
def _assert_editable(self) -> None:
|
||||
if self._frozen:
|
||||
raise BuildError('This LibraryBuilder has already been built successfully and is now frozen.')
|
||||
if self._building:
|
||||
raise BuildError('Cannot modify a LibraryBuilder while validate() or build() is running.')
|
||||
raise BuildError('This BuildLibrary has already been built successfully and is now frozen.')
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
session = self._active_session()
|
||||
if session is not None:
|
||||
return iter(session)
|
||||
return iter(self._names)
|
||||
|
||||
def __len__(self) -> int:
|
||||
session = self._active_session()
|
||||
if session is not None:
|
||||
return len(session)
|
||||
return len(self._names)
|
||||
|
||||
def __contains__(self, key: object) -> bool:
|
||||
session = self._active_session()
|
||||
if session is not None:
|
||||
return key in session
|
||||
return key in self._names
|
||||
|
||||
def keys(self) -> KeysView[str]:
|
||||
return self._names.keys()
|
||||
def __getitem__(self, key: str) -> Pattern:
|
||||
return self._require_active_session('__getitem__')[key]
|
||||
|
||||
def __setitem__(
|
||||
self,
|
||||
key: str,
|
||||
value: Pattern | _BuildRecipe,
|
||||
) -> None:
|
||||
session = self._active_session()
|
||||
if session is not None:
|
||||
session[key] = value
|
||||
return
|
||||
|
||||
self._assert_editable()
|
||||
if key in self._names:
|
||||
raise LibraryError(f'"{key}" already exists in the builder. Overwriting is not allowed!')
|
||||
|
||||
if isinstance(value, _BuildRecipe):
|
||||
placeholders = (
|
||||
arg for arg in (*value.args, *value.kwargs.values())
|
||||
if isinstance(arg, _LibraryPlaceholder)
|
||||
)
|
||||
if any(placeholder is not self.library for placeholder in placeholders):
|
||||
raise BuildError('A recipe cannot use another LibraryBuilder.library placeholder.')
|
||||
declaration = value
|
||||
else:
|
||||
if callable(value):
|
||||
raise TypeError('LibraryBuilder recipes must be wrapped with cell(fn)(...) or @cell.')
|
||||
raise TypeError('BuildLibrary recipes must be wrapped with cell(fn)(...) or @cell.')
|
||||
declaration = value
|
||||
|
||||
self._declarations[key] = declaration
|
||||
self._names[key] = None
|
||||
|
||||
def __delitem__(self, key: str) -> None:
|
||||
session = self._active_session()
|
||||
if session is not None:
|
||||
del session[key]
|
||||
return
|
||||
|
||||
self._assert_editable()
|
||||
if key not in self._declarations:
|
||||
raise KeyError(key)
|
||||
del self._declarations[key]
|
||||
del self._names[key]
|
||||
|
||||
def _merge(self, key_self: str, other: Mapping[str, Pattern], key_other: str) -> None:
|
||||
session = self._active_session()
|
||||
if session is not None:
|
||||
session._merge(key_self, other, key_other)
|
||||
return
|
||||
self[key_self] = copy.deepcopy(other[key_other])
|
||||
|
||||
def add(
|
||||
self,
|
||||
other: Mapping[str, Pattern],
|
||||
rename_theirs: Callable[[INameView, str], str] = _rename_patterns,
|
||||
rename_theirs: Callable[[ILibraryView, str], str] = _rename_patterns,
|
||||
mutate_other: bool = False,
|
||||
) -> dict[str, str]:
|
||||
from ..pattern import map_targets # noqa: PLC0415
|
||||
|
||||
session = self._active_session()
|
||||
if session is not None:
|
||||
return session.add(other, rename_theirs=rename_theirs, mutate_other=mutate_other)
|
||||
|
||||
self._assert_editable()
|
||||
|
||||
materializable = isinstance(other, IMaterializable)
|
||||
if materializable:
|
||||
source_backed = isinstance(other, ILibraryView) and not isinstance(other, Library | LibraryView)
|
||||
if source_backed:
|
||||
if mutate_other:
|
||||
raise BuildError('LibraryBuilder.add(..., mutate_other=True) is not supported for source-backed inputs.')
|
||||
raise BuildError('BuildLibrary.add(..., mutate_other=True) is not supported for source-backed inputs.')
|
||||
return self.add_source(
|
||||
other,
|
||||
rename_theirs = rename_theirs,
|
||||
|
|
@ -247,6 +263,7 @@ class LibraryBuilder(INameView):
|
|||
source_to_visible = _plan_source_names(
|
||||
self,
|
||||
source_order,
|
||||
self._names,
|
||||
rename_theirs = rename_theirs,
|
||||
rename_when = 'conflict',
|
||||
)
|
||||
|
|
@ -255,39 +272,95 @@ class LibraryBuilder(INameView):
|
|||
if mutate_other:
|
||||
temp = other
|
||||
else:
|
||||
temp = Library(copy.deepcopy({name: other[name] for name in source_order}))
|
||||
|
||||
if rename_map:
|
||||
target_map = cast('dict[str | None, str | None]', rename_map)
|
||||
remapped_refs = {
|
||||
source_name: map_targets(temp[source_name].refs, lambda target: target_map.get(target, target))
|
||||
for source_name in source_order
|
||||
}
|
||||
for source_name, refs in remapped_refs.items():
|
||||
temp[source_name].refs = refs
|
||||
temp = Library(copy.deepcopy(dict(other)))
|
||||
|
||||
for source_name in source_order:
|
||||
visible_name = source_to_visible[source_name]
|
||||
pattern = temp[source_name]
|
||||
if rename_map:
|
||||
pattern.refs = map_targets(
|
||||
pattern.refs,
|
||||
lambda target: cast('dict[str | None, str | None]', rename_map).get(target, target),
|
||||
)
|
||||
self[visible_name] = pattern
|
||||
|
||||
return rename_map
|
||||
|
||||
def __lshift__(self, other: TreeView) -> str:
|
||||
session = self._active_session()
|
||||
if session is not None:
|
||||
return session << other
|
||||
|
||||
self._assert_editable()
|
||||
view = other if isinstance(other, ILibraryView) else LibraryView(other)
|
||||
tops = view.tops()
|
||||
if len(tops) != 1:
|
||||
raise LibraryError(f'Received a library without exactly one topcell: {tops}')
|
||||
top = tops[0]
|
||||
rename_map = self.add(view)
|
||||
return rename_map.get(top, top)
|
||||
if len(other) == 1:
|
||||
name = next(iter(other))
|
||||
elif isinstance(other, ILibraryView) and not isinstance(other, Library | LibraryView):
|
||||
source_order = other.source_order()
|
||||
child_graph = other.child_graph(dangling='include')
|
||||
referenced = set().union(*child_graph.values()) if child_graph else set()
|
||||
tops = [candidate for candidate in source_order if candidate not in referenced]
|
||||
if len(tops) != 1:
|
||||
raise LibraryError(f'Asked for the single topcell, but found the following: {pformat(tops)}')
|
||||
name = tops[0]
|
||||
else:
|
||||
return super().__lshift__(other)
|
||||
|
||||
rename_map = self.add(other)
|
||||
return rename_map.get(name, name)
|
||||
|
||||
def __le__(self, other: Mapping[str, Pattern]) -> Abstract:
|
||||
if self._active_session() is not None:
|
||||
return super().__le__(other)
|
||||
raise BuildError('BuildLibrary.__le__() is only available while validate() or build() is running.')
|
||||
|
||||
def rename(
|
||||
self,
|
||||
old_name: str,
|
||||
new_name: str,
|
||||
move_references: bool = False,
|
||||
) -> Self:
|
||||
"""
|
||||
Rename a helper cell during an active build session.
|
||||
|
||||
During authoring, declared cells must be registered under their
|
||||
intended final names and imported source cells must be renamed through
|
||||
`add_source(...)`.
|
||||
"""
|
||||
session = self._active_session()
|
||||
if session is not None:
|
||||
session.rename(old_name, new_name, move_references=move_references)
|
||||
return self
|
||||
|
||||
self._assert_editable()
|
||||
if old_name == new_name:
|
||||
return self
|
||||
if old_name in self._declarations:
|
||||
raise BuildError(
|
||||
f'Cannot rename declared build cell "{old_name}" during authoring. '
|
||||
'Register it under the intended final name instead.'
|
||||
)
|
||||
if old_name not in self._names:
|
||||
raise LibraryError(f'"{old_name}" does not exist in the builder.')
|
||||
raise BuildError(
|
||||
f'Cannot rename imported source cell "{old_name}" during authoring. '
|
||||
'Choose visible source names with add_source(..., rename_theirs=..., rename_when=...).'
|
||||
)
|
||||
|
||||
def abstract(self, name: str) -> Abstract:
|
||||
return self._require_active_session('abstract').abstract(name)
|
||||
|
||||
def resolve(
|
||||
self,
|
||||
other: Abstract | str | Pattern | TreeView,
|
||||
append: bool = False,
|
||||
) -> Abstract | Pattern:
|
||||
return self._require_active_session('resolve').resolve(other, append=append)
|
||||
|
||||
def add_source(
|
||||
self,
|
||||
source: Mapping[str, Pattern] | ILibraryView,
|
||||
*,
|
||||
rename_theirs: Callable[[INameView, str], str] | None = None,
|
||||
rename_theirs: Callable[[ILibraryView, str], str] | None = None,
|
||||
rename_when: Literal['conflict', 'always'] = 'conflict',
|
||||
) -> dict[str, str]:
|
||||
"""
|
||||
|
|
@ -296,19 +369,13 @@ class LibraryBuilder(INameView):
|
|||
The source is not materialized immediately. Its names are scanned once
|
||||
to reserve visible builder names, then the source is read again when a
|
||||
build session starts. The source's cell membership must not be
|
||||
closed or structurally mutated between `add_source()` and
|
||||
`build()`/`validate()`.
|
||||
structurally mutated between `add_source()` and `build()`/`validate()`.
|
||||
Source cells may be renamed on entry to avoid collisions with existing
|
||||
declarations or other imported sources.
|
||||
|
||||
Sources are borrowed rather than owned. An output built with
|
||||
`output='overlay'` remains source-backed, so its sources must also stay
|
||||
open and unchanged for the lifetime of that output.
|
||||
|
||||
Args:
|
||||
rename_theirs: Function used to choose visible names for imported
|
||||
source cells. Its `INameView` argument contains existing and
|
||||
previously reserved names, but does not support pattern lookup.
|
||||
source cells.
|
||||
rename_when: If `'conflict'`, only conflicting names are renamed.
|
||||
If `'always'`, every imported source name is passed through
|
||||
`rename_theirs`.
|
||||
|
|
@ -317,6 +384,8 @@ class LibraryBuilder(INameView):
|
|||
Mapping of `{source_name: visible_name}` for imported names that
|
||||
were renamed while being added.
|
||||
"""
|
||||
if self._active_session() is not None:
|
||||
raise BuildError('BuildLibrary.add_source() is only available while authoring, not during validate() or build().')
|
||||
self._assert_editable()
|
||||
|
||||
view = source if isinstance(source, ILibraryView) else LibraryView(source)
|
||||
|
|
@ -324,6 +393,7 @@ class LibraryBuilder(INameView):
|
|||
source_to_visible = _plan_source_names(
|
||||
self,
|
||||
source_order,
|
||||
self._names,
|
||||
rename_theirs = rename_theirs,
|
||||
rename_when = rename_when,
|
||||
)
|
||||
|
|
@ -336,7 +406,7 @@ class LibraryBuilder(INameView):
|
|||
|
||||
def validate(
|
||||
self,
|
||||
names: str | Sequence[str] | None = None,
|
||||
names: Sequence[str] | None = None,
|
||||
*,
|
||||
allow_dangling: bool = False,
|
||||
) -> BuildReport:
|
||||
|
|
@ -346,13 +416,6 @@ class LibraryBuilder(INameView):
|
|||
This is a dry run over the same dependency resolution and recipe
|
||||
execution path used by `build()`. Any generated library is discarded
|
||||
after validation completes.
|
||||
|
||||
Args:
|
||||
names: Declared root name or names to validate. `None` validates
|
||||
every declaration. Duplicate names are ignored after their
|
||||
first occurrence.
|
||||
allow_dangling: If `False`, fail validation when the generated
|
||||
library contains dangling references.
|
||||
"""
|
||||
_session, report = self._run_build(names=names, allow_dangling=allow_dangling)
|
||||
return report
|
||||
|
|
@ -368,19 +431,13 @@ class LibraryBuilder(INameView):
|
|||
|
||||
Args:
|
||||
output: `'overlay'` preserves imported source-backed cells where
|
||||
possible and continues borrowing their sources. `'library'`
|
||||
eagerly materializes the full result and no longer needs the
|
||||
sources afterward.
|
||||
possible, while `'library'` eagerly materializes the full
|
||||
result.
|
||||
allow_dangling: If `False`, fail the build when the completed
|
||||
library still contains dangling references.
|
||||
"""
|
||||
if output not in ('overlay', 'library'):
|
||||
raise ValueError(f'Unknown build output mode: {output!r}')
|
||||
if self._building:
|
||||
raise BuildError(
|
||||
'Cannot call build() or validate() recursively on a LibraryBuilder '
|
||||
'from one of its own recipes.'
|
||||
)
|
||||
self._assert_editable()
|
||||
session, report = self._run_build(names=None, allow_dangling=allow_dangling)
|
||||
if output == 'library':
|
||||
|
|
@ -393,37 +450,24 @@ class LibraryBuilder(INameView):
|
|||
def _run_build(
|
||||
self,
|
||||
*,
|
||||
names: str | Sequence[str] | None,
|
||||
names: Sequence[str] | None,
|
||||
allow_dangling: bool,
|
||||
) -> tuple[_BuildSessionLibrary, BuildReport]:
|
||||
if self._building:
|
||||
raise BuildError(
|
||||
'Cannot call build() or validate() recursively on a LibraryBuilder '
|
||||
'from one of its own recipes.'
|
||||
)
|
||||
|
||||
if names is None:
|
||||
requested_names = tuple(self._declarations)
|
||||
elif isinstance(names, str):
|
||||
requested_names = (names,)
|
||||
else:
|
||||
requested_names = tuple(names)
|
||||
if any(not isinstance(name, str) for name in requested_names):
|
||||
raise TypeError('Build roots must be strings.')
|
||||
|
||||
roots = tuple(dict.fromkeys(requested_names))
|
||||
roots = tuple(dict.fromkeys(names if names is not None else self._declarations.keys()))
|
||||
unknown = [name for name in roots if name not in self._names]
|
||||
if unknown:
|
||||
raise BuildError(f'Unknown build roots requested: {unknown}')
|
||||
|
||||
self._building = True
|
||||
session = _BuildSessionLibrary(self)
|
||||
sessions = dict(_ACTIVE_BUILD_SESSIONS.get() or {})
|
||||
sessions[id(self)] = session
|
||||
token = _ACTIVE_BUILD_SESSIONS.set(sessions)
|
||||
try:
|
||||
session = _BuildSessionLibrary(self)
|
||||
session.materialize_many(roots)
|
||||
if not allow_dangling:
|
||||
session.child_graph(dangling='error')
|
||||
finally:
|
||||
self._building = False
|
||||
_ACTIVE_BUILD_SESSIONS.reset(token)
|
||||
|
||||
report = session.build_report(roots)
|
||||
return session, report
|
||||
|
|
@ -431,7 +475,7 @@ class LibraryBuilder(INameView):
|
|||
|
||||
class _BuildSessionLibrary(ILibrary):
|
||||
"""
|
||||
Internal overlay-backed library used while a `LibraryBuilder` is executing.
|
||||
Internal overlay-backed library used while a `BuildLibrary` is executing.
|
||||
|
||||
This object provides the mutable-library surface that recipes expect while
|
||||
also tracking declared-cell dependencies, helper-cell provenance, and
|
||||
|
|
@ -439,7 +483,7 @@ class _BuildSessionLibrary(ILibrary):
|
|||
build run.
|
||||
"""
|
||||
|
||||
def __init__(self, builder: LibraryBuilder) -> None:
|
||||
def __init__(self, builder: BuildLibrary) -> None:
|
||||
self._builder = builder
|
||||
self._overlay = OverlayLibrary()
|
||||
self._built: set[str] = set()
|
||||
|
|
@ -468,7 +512,7 @@ class _BuildSessionLibrary(ILibrary):
|
|||
'Do not structurally mutate source libraries between add_source() and build()/validate().'
|
||||
)
|
||||
|
||||
def rename_source(_lib: INameView, name: str, *, mapping: Mapping[str, str] = source_to_visible) -> str:
|
||||
def rename_source(_lib: ILibraryView, name: str, *, mapping: Mapping[str, str] = source_to_visible) -> str:
|
||||
return mapping[name]
|
||||
|
||||
self._overlay.add_source(
|
||||
|
|
@ -542,8 +586,6 @@ class _BuildSessionLibrary(ILibrary):
|
|||
return self
|
||||
|
||||
def __getitem__(self, key: str) -> Pattern:
|
||||
if key not in self._names:
|
||||
raise KeyError(key)
|
||||
if key in self._builder._declarations:
|
||||
self._record_dependency(key)
|
||||
self._ensure_declared(key)
|
||||
|
|
@ -595,7 +637,7 @@ class _BuildSessionLibrary(ILibrary):
|
|||
def add(
|
||||
self,
|
||||
other: Mapping[str, Pattern],
|
||||
rename_theirs: Callable[[INameView, str], str] = _rename_patterns,
|
||||
rename_theirs: Callable[[ILibraryView, str], str] = _rename_patterns,
|
||||
mutate_other: bool = False,
|
||||
) -> dict[str, str]:
|
||||
rename_map = super().add(other, rename_theirs=rename_theirs, mutate_other=mutate_other)
|
||||
|
|
@ -642,15 +684,7 @@ class _BuildSessionLibrary(ILibrary):
|
|||
if isinstance(declaration, _BuildRecipe):
|
||||
for dep in declaration.explicit_dependencies:
|
||||
self._ensure_named(dep)
|
||||
args = tuple(
|
||||
self if arg is self._builder.library else arg
|
||||
for arg in declaration.args
|
||||
)
|
||||
kwargs = {
|
||||
key: self if value is self._builder.library else value
|
||||
for key, value in declaration.kwargs.items()
|
||||
}
|
||||
pattern = declaration.func(*args, **kwargs)
|
||||
pattern = declaration.func(*declaration.args, **declaration.kwargs)
|
||||
if not isinstance(pattern, Pattern):
|
||||
raise BuildError(f'Recipe for "{name}" returned {type(pattern).__name__}, expected Pattern') # noqa: TRY301
|
||||
else:
|
||||
|
|
@ -688,42 +722,6 @@ class _BuildSessionLibrary(ILibrary):
|
|||
) -> dict[str, set[str]]:
|
||||
return self._overlay.parent_graph(dangling=dangling)
|
||||
|
||||
def subtree(
|
||||
self,
|
||||
tops: str | Sequence[str],
|
||||
) -> Self:
|
||||
if isinstance(tops, str):
|
||||
tops = (tops,)
|
||||
|
||||
keep = self._referenced_patterns_by_lookup(tops=tops)
|
||||
keep &= set(self)
|
||||
keep |= set(tops)
|
||||
order = tuple(name for name in self._names if name in keep)
|
||||
|
||||
patterns = {name: self[name] for name in order}
|
||||
new = object.__new__(type(self))
|
||||
new._builder = self._builder
|
||||
new._overlay = OverlayLibrary()
|
||||
for name in order:
|
||||
new._overlay[name] = patterns[name]
|
||||
new._built = {
|
||||
name for name in order
|
||||
if name in self._builder._declarations
|
||||
}
|
||||
new._declared_stack = []
|
||||
new._names = dict.fromkeys(order)
|
||||
new._provenance = {
|
||||
name: self._provenance[name]
|
||||
for name in order
|
||||
if name in self._provenance
|
||||
}
|
||||
new._dependency_graph = defaultdict(set, {
|
||||
name: set(dependencies) & keep
|
||||
for name, dependencies in self._dependency_graph.items()
|
||||
if name in keep
|
||||
})
|
||||
return new
|
||||
|
||||
def build_report(self, requested_roots: Sequence[str]) -> BuildReport:
|
||||
dependency_graph = {
|
||||
name: frozenset(self._dependency_graph.get(name, set()))
|
||||
|
|
|
|||
|
|
@ -1,42 +0,0 @@
|
|||
"""Optional capabilities implemented by lazy and borrowing libraries."""
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Sequence
|
||||
|
||||
from ..pattern import Pattern
|
||||
from .base import ILibraryView
|
||||
from .mapping import LibraryView
|
||||
|
||||
|
||||
class IMaterializable(ABC):
|
||||
"""Capability for libraries which support explicit pattern materialization."""
|
||||
|
||||
@abstractmethod
|
||||
def materialize(self, name: str, *, persist: bool = True) -> Pattern:
|
||||
"""Materialize one pattern, optionally retaining it in the library's cache."""
|
||||
|
||||
def materialize_many(
|
||||
self,
|
||||
names: Sequence[str],
|
||||
*,
|
||||
persist: bool = True,
|
||||
) -> LibraryView:
|
||||
"""Materialize a de-duplicated sequence into a plain read-only view."""
|
||||
from .mapping import LibraryView # noqa: PLC0415
|
||||
|
||||
return LibraryView({
|
||||
name: self.materialize(name, persist=persist)
|
||||
for name in dict.fromkeys(names)
|
||||
})
|
||||
|
||||
|
||||
class IBorrowing(ABC):
|
||||
"""Capability for library views which directly borrow other libraries."""
|
||||
|
||||
@abstractmethod
|
||||
def borrowed_sources(self) -> tuple[ILibraryView, ...]:
|
||||
"""Return the source views directly borrowed by this library."""
|
||||
|
|
@ -7,10 +7,9 @@ import logging
|
|||
|
||||
from ..error import LibraryError
|
||||
from .base import ILibrary
|
||||
from .capabilities import IMaterializable
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable, Iterator, Mapping, Sequence
|
||||
from collections.abc import Callable, Iterator, Mapping
|
||||
|
||||
from ..pattern import Pattern
|
||||
|
||||
|
|
@ -18,7 +17,7 @@ if TYPE_CHECKING:
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LazyLibrary(ILibrary, IMaterializable):
|
||||
class LazyLibrary(ILibrary):
|
||||
"""
|
||||
This class is usually used to create a library of Patterns by mapping names to
|
||||
functions which generate or load the relevant `Pattern` object as-needed.
|
||||
|
|
@ -57,9 +56,6 @@ class LazyLibrary(ILibrary, IMaterializable):
|
|||
del self.cache[key]
|
||||
|
||||
def __getitem__(self, key: str) -> Pattern:
|
||||
return self.materialize(key, persist=True)
|
||||
|
||||
def materialize(self, key: str, *, persist: bool = True) -> Pattern:
|
||||
logger.debug(f'loading {key}')
|
||||
if key in self.cache:
|
||||
logger.debug(f'found {key} in cache')
|
||||
|
|
@ -80,8 +76,7 @@ class LazyLibrary(ILibrary, IMaterializable):
|
|||
pat = func()
|
||||
finally:
|
||||
self._lookups_in_progress.pop()
|
||||
if persist:
|
||||
self.cache[key] = pat
|
||||
self.cache[key] = pat
|
||||
return pat
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
|
|
@ -93,15 +88,6 @@ class LazyLibrary(ILibrary, IMaterializable):
|
|||
def __contains__(self, key: object) -> bool:
|
||||
return key in self.mapping
|
||||
|
||||
def referenced_patterns(
|
||||
self,
|
||||
tops: str | Sequence[str] | None = None,
|
||||
skip: set[str] | None = None,
|
||||
) -> set[str]:
|
||||
# Closure-backed cells do not have hierarchy metadata. Preserve laziness
|
||||
# by loading only patterns reached from the requested roots.
|
||||
return self._referenced_patterns_by_lookup(tops=tops, skip=skip)
|
||||
|
||||
def _merge(self, key_self: str, other: Mapping[str, Pattern], key_other: str) -> None:
|
||||
if isinstance(other, LazyLibrary):
|
||||
self.mapping[key_self] = other.mapping[key_other]
|
||||
|
|
|
|||
|
|
@ -2,19 +2,14 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from pprint import pformat
|
||||
from typing import TYPE_CHECKING, Any, Self, cast
|
||||
from typing import TYPE_CHECKING, Self
|
||||
|
||||
from ..error import LibraryError
|
||||
from .base import ILibrary, ILibraryView
|
||||
from .capabilities import IBorrowing, IMaterializable
|
||||
from .utils import dangling_mode_t, _validate_dangling_mode
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable, Iterator, Mapping, MutableMapping
|
||||
|
||||
import numpy
|
||||
from numpy.typing import NDArray
|
||||
|
||||
from ..pattern import Pattern
|
||||
|
||||
|
||||
|
|
@ -49,106 +44,6 @@ class LibraryView(ILibraryView):
|
|||
return f'<LibraryView ({type(self.mapping)}) with keys\n' + pformat(list(self.keys())) + '>'
|
||||
|
||||
|
||||
class _SubtreeLibraryView(ILibraryView, IMaterializable, IBorrowing):
|
||||
"""Borrowed subtree view with snapshotted membership and hierarchy."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
source: ILibraryView,
|
||||
*,
|
||||
names: set[str],
|
||||
child_graph: Mapping[str, set[str]],
|
||||
) -> None:
|
||||
self._source = source
|
||||
source_order = source.source_order()
|
||||
ordered = list(dict.fromkeys(name for name in source_order if name in names))
|
||||
seen = set(ordered)
|
||||
ordered.extend(name for name in source if name in names and name not in seen)
|
||||
self._order = tuple(ordered)
|
||||
self._names = frozenset(self._order)
|
||||
self._child_graph = {
|
||||
name: set(child_graph.get(name, set()))
|
||||
for name in self._order
|
||||
}
|
||||
if hasattr(source, 'library_info'):
|
||||
self.library_info = cast('dict[str, Any]', source.library_info)
|
||||
|
||||
def __getitem__(self, key: str) -> Pattern:
|
||||
if key not in self._names:
|
||||
raise KeyError(key)
|
||||
return self._source[key]
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
return iter(self._order)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self._order)
|
||||
|
||||
def __contains__(self, key: object) -> bool:
|
||||
return key in self._names
|
||||
|
||||
def borrowed_sources(self) -> tuple[ILibraryView, ...]:
|
||||
return (self._source,)
|
||||
|
||||
def source_order(self) -> tuple[str, ...]:
|
||||
return self._order
|
||||
|
||||
def materialize(self, name: str, *, persist: bool = True) -> Pattern:
|
||||
if name not in self._names:
|
||||
raise KeyError(name)
|
||||
if isinstance(self._source, IMaterializable):
|
||||
return self._source.materialize(name, persist=persist)
|
||||
return self._source[name]
|
||||
|
||||
def child_graph(
|
||||
self,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, set[str]]:
|
||||
_validate_dangling_mode(dangling)
|
||||
graph = {name: set(children) for name, children in self._child_graph.items()}
|
||||
existing = set(graph)
|
||||
dangling_refs = set().union(*(children - existing for children in graph.values())) if graph else set()
|
||||
if dangling == 'error':
|
||||
if dangling_refs:
|
||||
raise self._dangling_refs_error(dangling_refs, 'building child graph')
|
||||
return graph
|
||||
if dangling == 'ignore':
|
||||
return {
|
||||
name: {child for child in children if child in existing}
|
||||
for name, children in graph.items()
|
||||
}
|
||||
for target in dangling_refs:
|
||||
graph.setdefault(target, set())
|
||||
return graph
|
||||
|
||||
def find_refs_local(
|
||||
self,
|
||||
name: str,
|
||||
parent_graph: dict[str, set[str]] | None = None,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, list[NDArray[numpy.float64]]]:
|
||||
_validate_dangling_mode(dangling)
|
||||
if parent_graph is None:
|
||||
graph_mode: dangling_mode_t = 'ignore' if dangling == 'ignore' else 'include'
|
||||
parent_graph = self.parent_graph(dangling=graph_mode)
|
||||
refs = self._source.find_refs_local(name, parent_graph=parent_graph, dangling=dangling)
|
||||
return {parent: transforms for parent, transforms in refs.items() if parent in self._names}
|
||||
|
||||
def raw_struct_bytes(self, name: str) -> bytes:
|
||||
if name not in self._names:
|
||||
raise KeyError(name)
|
||||
reader = getattr(self._source, 'raw_struct_bytes', None)
|
||||
if not callable(reader):
|
||||
raise TypeError('raw_struct_bytes')
|
||||
return cast('bytes', reader(name))
|
||||
|
||||
def can_copy_raw_struct(self, name: str) -> bool:
|
||||
if name not in self._names:
|
||||
return False
|
||||
can_copy = getattr(self._source, 'can_copy_raw_struct', None)
|
||||
return bool(callable(can_copy) and can_copy(name))
|
||||
|
||||
|
||||
class Library(ILibrary):
|
||||
"""
|
||||
Default implementation for a writeable library.
|
||||
|
|
|
|||
|
|
@ -3,32 +3,34 @@ from __future__ import annotations
|
|||
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING, Any, Literal, Self, cast
|
||||
from typing import TYPE_CHECKING, Any, Literal, cast
|
||||
import copy
|
||||
|
||||
import numpy
|
||||
|
||||
from ..error import LibraryError
|
||||
from ..pattern import Pattern, map_targets
|
||||
from ..utils import apply_transforms, layer_t
|
||||
from .base import ILibrary, ILibraryView
|
||||
from .capabilities import IBorrowing, IMaterializable
|
||||
from .utils import INameView, dangling_mode_t, _plan_source_names, _source_rename_map, _validate_dangling_mode
|
||||
from .utils import dangling_mode_t, _plan_source_names, _source_rename_map
|
||||
from .mapping import LibraryView
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable, Iterator, Mapping, Sequence
|
||||
|
||||
import numpy
|
||||
from numpy.typing import NDArray
|
||||
|
||||
from ..ports import Port
|
||||
from ..utils import layer_t
|
||||
|
||||
|
||||
@dataclass
|
||||
class _SourceLayer:
|
||||
""" One imported source layer tracked by an `OverlayLibrary`. """
|
||||
library: ILibraryView
|
||||
source_target_map: dict[str, str]
|
||||
source_to_visible: dict[str, str]
|
||||
visible_to_source: dict[str, str]
|
||||
child_graph: dict[str, set[str]]
|
||||
order: list[str]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
@ -39,19 +41,18 @@ class _SourceEntry:
|
|||
|
||||
|
||||
def _materialize_detached_pattern(view: ILibraryView, name: str) -> Pattern:
|
||||
if isinstance(view, IMaterializable):
|
||||
return view.materialize(name, persist=False).deepcopy()
|
||||
func = getattr(view, '_materialize_pattern', None)
|
||||
if callable(func):
|
||||
return cast('Pattern', func(name, persist=False))
|
||||
return view[name].deepcopy()
|
||||
|
||||
|
||||
class PortsLibraryView(ILibraryView, IMaterializable, IBorrowing):
|
||||
class PortsLibraryView(ILibraryView):
|
||||
"""
|
||||
Read-only view which imports or applies ports on first materialization.
|
||||
|
||||
The wrapped source remains untouched; this view owns a separate processed
|
||||
cache so direct-copy workflows can continue to use the raw source view.
|
||||
The view borrows its source: callers must keep the source open for the
|
||||
lifetime of the view and close the source themselves.
|
||||
|
||||
Graph queries, source ordering, and copy-through capabilities are delegated
|
||||
to the wrapped source whenever possible, while `__getitem__` and
|
||||
|
|
@ -83,7 +84,7 @@ class PortsLibraryView(ILibraryView, IMaterializable, IBorrowing):
|
|||
self.library_info = cast('dict[str, Any]', source.library_info)
|
||||
|
||||
def __getitem__(self, key: str) -> Pattern:
|
||||
return self.materialize(key, persist=True)
|
||||
return self._materialize_pattern(key, persist=True)
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
return iter(self._source)
|
||||
|
|
@ -94,7 +95,7 @@ class PortsLibraryView(ILibraryView, IMaterializable, IBorrowing):
|
|||
def __contains__(self, key: object) -> bool:
|
||||
return key in self._source
|
||||
|
||||
def materialize(self, name: str, *, persist: bool = True) -> Pattern:
|
||||
def _materialize_pattern(self, name: str, *, persist: bool) -> Pattern:
|
||||
from ..utils.ports2data import data_to_ports # noqa: PLC0415
|
||||
|
||||
if name in self._cache:
|
||||
|
|
@ -133,31 +134,72 @@ class PortsLibraryView(ILibraryView, IMaterializable, IBorrowing):
|
|||
self._cache[name] = pat
|
||||
return pat
|
||||
|
||||
def materialize_many(
|
||||
self,
|
||||
names: Sequence[str],
|
||||
*,
|
||||
persist: bool = True,
|
||||
) -> LibraryView:
|
||||
mats = {
|
||||
name: self._materialize_pattern(name, persist=persist)
|
||||
for name in dict.fromkeys(names)
|
||||
}
|
||||
return LibraryView(mats)
|
||||
|
||||
def source_order(self) -> tuple[str, ...]:
|
||||
return self._source.source_order()
|
||||
|
||||
def borrowed_sources(self) -> tuple[ILibraryView, ...]:
|
||||
return (self._source,)
|
||||
|
||||
def child_graph(
|
||||
self,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, set[str]]:
|
||||
_validate_dangling_mode(dangling)
|
||||
return self._source.child_graph(dangling=dangling)
|
||||
|
||||
def parent_graph(
|
||||
self,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, set[str]]:
|
||||
return self._source.parent_graph(dangling=dangling)
|
||||
|
||||
def subtree(
|
||||
self,
|
||||
tops: str | Sequence[str],
|
||||
) -> ILibraryView:
|
||||
if isinstance(tops, str):
|
||||
tops = (tops,)
|
||||
keep = cast('set[str]', self._source.referenced_patterns(tops) - {None})
|
||||
keep |= set(tops)
|
||||
return self.materialize_many(tuple(keep), persist=True)
|
||||
|
||||
def tops(self) -> list[str]:
|
||||
return self._source.tops()
|
||||
|
||||
def find_refs_local(
|
||||
self,
|
||||
name: str,
|
||||
parent_graph: dict[str, set[str]] | None = None,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, list[NDArray[numpy.float64]]]:
|
||||
_validate_dangling_mode(dangling)
|
||||
finder = getattr(self._source, 'find_refs_local', None)
|
||||
if callable(finder):
|
||||
return cast('dict[str, list[NDArray[numpy.float64]]]', finder(name, parent_graph=parent_graph, dangling=dangling))
|
||||
return super().find_refs_local(name, parent_graph=parent_graph, dangling=dangling)
|
||||
|
||||
def find_refs_global(
|
||||
self,
|
||||
name: str,
|
||||
order: list[str] | None = None,
|
||||
parent_graph: dict[str, set[str]] | None = None,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[tuple[str, ...], NDArray[numpy.float64]]:
|
||||
finder = getattr(self._source, 'find_refs_global', None)
|
||||
if callable(finder):
|
||||
return cast(
|
||||
'dict[tuple[str, ...], NDArray[numpy.float64]]',
|
||||
finder(name, order=order, parent_graph=parent_graph, dangling=dangling),
|
||||
)
|
||||
return super().find_refs_global(name, order=order, parent_graph=parent_graph, dangling=dangling)
|
||||
|
||||
def raw_struct_bytes(self, name: str) -> bytes:
|
||||
reader = getattr(self._source, 'raw_struct_bytes', None)
|
||||
if not callable(reader):
|
||||
|
|
@ -165,26 +207,30 @@ class PortsLibraryView(ILibraryView, IMaterializable, IBorrowing):
|
|||
return cast('bytes', reader(name))
|
||||
|
||||
def can_copy_raw_struct(self, name: str) -> bool:
|
||||
if name in self._cache:
|
||||
return False
|
||||
can_copy = getattr(self._source, 'can_copy_raw_struct', None)
|
||||
if not callable(can_copy):
|
||||
return False
|
||||
return bool(can_copy(name))
|
||||
|
||||
def close(self) -> None:
|
||||
closer = getattr(self._source, 'close', None)
|
||||
if callable(closer):
|
||||
closer()
|
||||
|
||||
class OverlayLibrary(ILibrary, IMaterializable, IBorrowing):
|
||||
def __enter__(self) -> PortsLibraryView:
|
||||
return self
|
||||
|
||||
def __exit__(self, *_args: object) -> None:
|
||||
self.close()
|
||||
|
||||
|
||||
class OverlayLibrary(ILibrary):
|
||||
"""
|
||||
Mutable overlay over one or more source libraries.
|
||||
|
||||
Source-backed cells remain lazy until accessed through `__getitem__`, at
|
||||
which point that visible cell is promoted into an overlay-owned materialized
|
||||
`Pattern`.
|
||||
|
||||
Source libraries must remain open and must not be mutated after they are
|
||||
added. The overlay borrows each source and snapshots its names, hierarchy,
|
||||
and initial visible-name mapping while retaining the source itself for lazy
|
||||
pattern materialization.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
|
|
@ -203,7 +249,7 @@ class OverlayLibrary(ILibrary, IMaterializable, IBorrowing):
|
|||
return key in self._entries
|
||||
|
||||
def __getitem__(self, key: str) -> Pattern:
|
||||
return self.materialize(key, persist=True)
|
||||
return self._materialize_pattern(key, persist=True)
|
||||
|
||||
def __setitem__(
|
||||
self,
|
||||
|
|
@ -229,19 +275,15 @@ class OverlayLibrary(ILibrary, IMaterializable, IBorrowing):
|
|||
self,
|
||||
source: Mapping[str, Pattern] | ILibraryView,
|
||||
*,
|
||||
rename_theirs: Callable[[INameView, str], str] | None = None,
|
||||
rename_theirs: Callable[[ILibraryView, str], str] | None = None,
|
||||
rename_when: Literal['conflict', 'always'] = 'conflict',
|
||||
) -> dict[str, str]:
|
||||
"""
|
||||
Add a source-backed library layer.
|
||||
|
||||
The source must remain open, and its names, hierarchy, and pattern
|
||||
contents must remain unchanged for the lifetime of this overlay.
|
||||
|
||||
Args:
|
||||
rename_theirs: Function used to choose visible names for imported
|
||||
source cells. Its `INameView` argument contains existing and
|
||||
previously reserved names, but does not support pattern lookup.
|
||||
source cells.
|
||||
rename_when: If `'conflict'`, only conflicting names are renamed.
|
||||
If `'always'`, every imported source name is passed through
|
||||
`rename_theirs`.
|
||||
|
|
@ -253,13 +295,18 @@ class OverlayLibrary(ILibrary, IMaterializable, IBorrowing):
|
|||
source_to_visible = _plan_source_names(
|
||||
self,
|
||||
source_order,
|
||||
self._entries,
|
||||
rename_theirs = rename_theirs,
|
||||
rename_when = rename_when,
|
||||
)
|
||||
visible_to_source = {visible: source_name for source_name, visible in source_to_visible.items()}
|
||||
|
||||
layer = _SourceLayer(
|
||||
library=view,
|
||||
source_target_map=dict(source_to_visible),
|
||||
source_to_visible=source_to_visible,
|
||||
visible_to_source=visible_to_source,
|
||||
child_graph=child_graph,
|
||||
order=[source_to_visible[name] for name in source_order],
|
||||
)
|
||||
layer_index = len(self._layers)
|
||||
self._layers.append(layer)
|
||||
|
|
@ -286,6 +333,11 @@ class OverlayLibrary(ILibrary, IMaterializable, IBorrowing):
|
|||
|
||||
entry = self._entries.pop(old_name)
|
||||
self._entries[new_name] = entry
|
||||
if isinstance(entry, _SourceEntry):
|
||||
layer = self._layers[entry.layer_index]
|
||||
layer.source_to_visible[entry.source_name] = new_name
|
||||
del layer.visible_to_source[old_name]
|
||||
layer.visible_to_source[new_name] = entry.source_name
|
||||
|
||||
idx = self._order.index(old_name)
|
||||
self._order[idx] = new_name
|
||||
|
|
@ -323,10 +375,10 @@ class OverlayLibrary(ILibrary, IMaterializable, IBorrowing):
|
|||
return self
|
||||
|
||||
def _effective_target(self, layer: _SourceLayer, target: str) -> str:
|
||||
visible = layer.source_target_map.get(target, target)
|
||||
visible = layer.source_to_visible.get(target, target)
|
||||
return self._resolve_target(visible)
|
||||
|
||||
def materialize(self, name: str, *, persist: bool = True) -> Pattern:
|
||||
def _materialize_pattern(self, name: str, *, persist: bool) -> Pattern:
|
||||
if name not in self._entries:
|
||||
raise KeyError(name)
|
||||
entry = self._entries[name]
|
||||
|
|
@ -334,7 +386,7 @@ class OverlayLibrary(ILibrary, IMaterializable, IBorrowing):
|
|||
return entry
|
||||
|
||||
layer = self._layers[entry.layer_index]
|
||||
source_pat = _materialize_detached_pattern(layer.library, entry.source_name)
|
||||
source_pat = layer.library[entry.source_name].deepcopy()
|
||||
|
||||
def remap(target: str | None) -> str | None:
|
||||
return None if target is None else self._effective_target(layer, target)
|
||||
|
|
@ -350,7 +402,6 @@ class OverlayLibrary(ILibrary, IMaterializable, IBorrowing):
|
|||
self,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, set[str]]:
|
||||
_validate_dangling_mode(dangling)
|
||||
graph: dict[str, set[str]] = {}
|
||||
for name in self._order:
|
||||
if name not in self._entries:
|
||||
|
|
@ -376,31 +427,33 @@ class OverlayLibrary(ILibrary, IMaterializable, IBorrowing):
|
|||
graph.setdefault(cast('str', child), set())
|
||||
return graph
|
||||
|
||||
def parent_graph(
|
||||
self,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, set[str]]:
|
||||
child_graph = self.child_graph(dangling='include' if dangling == 'include' else 'ignore')
|
||||
existing = set(self.keys())
|
||||
igraph: dict[str, set[str]] = {name: set() for name in child_graph}
|
||||
for parent, children in child_graph.items():
|
||||
for child in children:
|
||||
if child in existing or dangling == 'include':
|
||||
igraph.setdefault(child, set()).add(parent)
|
||||
if dangling == 'error':
|
||||
raw = self.child_graph(dangling='include')
|
||||
dangling_refs = set().union(*(children - existing for children in raw.values()))
|
||||
if dangling_refs:
|
||||
raise self._dangling_refs_error(cast('set[str]', dangling_refs), 'building parent graph')
|
||||
return igraph
|
||||
|
||||
def subtree(
|
||||
self,
|
||||
tops: str | Sequence[str],
|
||||
) -> Self:
|
||||
) -> ILibraryView:
|
||||
if isinstance(tops, str):
|
||||
tops = (tops,)
|
||||
|
||||
graph = self.child_graph(dangling='include')
|
||||
keep = self._referenced_patterns_from_graph(graph, tops=tops)
|
||||
keep &= set(self)
|
||||
keep = cast('set[str]', self.referenced_patterns(tops) - {None})
|
||||
keep |= set(tops)
|
||||
|
||||
new = type(self)()
|
||||
new._layers = [
|
||||
_SourceLayer(
|
||||
library=layer.library,
|
||||
source_target_map=dict(layer.source_target_map),
|
||||
child_graph={name: set(children) for name, children in layer.child_graph.items()},
|
||||
)
|
||||
for layer in self._layers
|
||||
]
|
||||
new._order = [name for name in self._order if name in keep and name in self._entries]
|
||||
new._entries = {name: self._entries[name] for name in new._order}
|
||||
new._target_remap = dict(self._target_remap)
|
||||
return new
|
||||
return LibraryView({name: self[name] for name in keep})
|
||||
|
||||
def find_refs_local(
|
||||
self,
|
||||
|
|
@ -408,7 +461,6 @@ class OverlayLibrary(ILibrary, IMaterializable, IBorrowing):
|
|||
parent_graph: dict[str, set[str]] | None = None,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[str, list[NDArray[numpy.float64]]]:
|
||||
_validate_dangling_mode(dangling)
|
||||
instances: dict[str, list[NDArray[numpy.float64]]] = defaultdict(list)
|
||||
if parent_graph is None:
|
||||
graph_mode = 'ignore' if dangling == 'ignore' else 'include'
|
||||
|
|
@ -423,34 +475,57 @@ class OverlayLibrary(ILibrary, IMaterializable, IBorrowing):
|
|||
return instances
|
||||
|
||||
for parent in parent_graph.get(name, set()):
|
||||
pat = self.materialize(parent, persist=False)
|
||||
pat = self._materialize_pattern(parent, persist=False)
|
||||
for ref in pat.refs.get(name, []):
|
||||
instances[parent].append(ref.as_transforms())
|
||||
return instances
|
||||
|
||||
def find_refs_global(
|
||||
self,
|
||||
name: str,
|
||||
order: list[str] | None = None,
|
||||
parent_graph: dict[str, set[str]] | None = None,
|
||||
dangling: dangling_mode_t = 'error',
|
||||
) -> dict[tuple[str, ...], NDArray[numpy.float64]]:
|
||||
graph_mode = 'ignore' if dangling == 'ignore' else 'include'
|
||||
if order is None:
|
||||
order = self.child_order(dangling=graph_mode)
|
||||
if parent_graph is None:
|
||||
parent_graph = self.parent_graph(dangling=graph_mode)
|
||||
|
||||
if name not in self:
|
||||
if name not in parent_graph:
|
||||
return {}
|
||||
if dangling == 'error':
|
||||
raise self._dangling_refs_error({name}, f'finding global refs for {name!r}')
|
||||
if dangling == 'ignore':
|
||||
return {}
|
||||
|
||||
self_keys = set(self.keys())
|
||||
transforms: dict[str, list[tuple[tuple[str, ...], NDArray[numpy.float64]]]]
|
||||
transforms = defaultdict(list)
|
||||
for parent, vals in self.find_refs_local(name, parent_graph=parent_graph, dangling=dangling).items():
|
||||
transforms[parent] = [((name,), numpy.concatenate(vals))]
|
||||
|
||||
for next_name in order:
|
||||
if next_name not in transforms:
|
||||
continue
|
||||
if not parent_graph.get(next_name, set()) & self_keys:
|
||||
continue
|
||||
|
||||
outers = self.find_refs_local(next_name, parent_graph=parent_graph, dangling=dangling)
|
||||
inners = transforms.pop(next_name)
|
||||
for parent, outer in outers.items():
|
||||
outer_tf = numpy.concatenate(outer)
|
||||
for path, inner in inners:
|
||||
combined = apply_transforms(outer_tf, inner)
|
||||
transforms[parent].append(((next_name,) + path, combined))
|
||||
|
||||
result = {}
|
||||
for parent, targets in transforms.items():
|
||||
for path, instances in targets:
|
||||
result[(parent,) + path] = instances
|
||||
return result
|
||||
|
||||
def source_order(self) -> tuple[str, ...]:
|
||||
return tuple(name for name in self._order if name in self._entries)
|
||||
|
||||
def borrowed_sources(self) -> tuple[ILibraryView, ...]:
|
||||
return tuple(layer.library for layer in self._layers)
|
||||
|
||||
def can_copy_raw_struct(self, name: str) -> bool:
|
||||
entry = self._entries.get(name)
|
||||
if not isinstance(entry, _SourceEntry) or name != entry.source_name:
|
||||
return False
|
||||
layer = self._layers[entry.layer_index]
|
||||
can_copy = getattr(layer.library, 'can_copy_raw_struct', None)
|
||||
if not callable(can_copy) or not can_copy(entry.source_name):
|
||||
return False
|
||||
children = layer.child_graph.get(entry.source_name, set())
|
||||
return all(self._effective_target(layer, child) == child for child in children)
|
||||
|
||||
def raw_struct_bytes(self, name: str) -> bytes:
|
||||
entry = self._entries.get(name)
|
||||
if not isinstance(entry, _SourceEntry):
|
||||
raise TypeError('raw_struct_bytes')
|
||||
layer = self._layers[entry.layer_index]
|
||||
reader = getattr(layer.library, 'raw_struct_bytes', None)
|
||||
if not callable(reader):
|
||||
raise TypeError('raw_struct_bytes')
|
||||
return cast('bytes', reader(entry.source_name))
|
||||
|
|
|
|||
|
|
@ -1,11 +1,8 @@
|
|||
"""Shared types and helpers for library implementations."""
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC
|
||||
from typing import TYPE_CHECKING, Literal, Protocol, TypeAlias
|
||||
from collections.abc import Callable, Collection, Iterator, Mapping, MutableMapping, Sequence
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import Callable, Container, Mapping, MutableMapping, Sequence
|
||||
|
||||
from ..error import LibraryError
|
||||
|
||||
|
|
@ -14,80 +11,7 @@ if TYPE_CHECKING:
|
|||
from numpy.typing import NDArray
|
||||
|
||||
from ..pattern import Pattern
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
SINGLE_USE_PREFIX = '_'
|
||||
"""
|
||||
Names starting with this prefix are assumed to refer to single-use patterns,
|
||||
which may be renamed automatically by `ILibrary.add()` (via
|
||||
`rename_theirs=_rename_patterns()` )
|
||||
"""
|
||||
# TODO what are the consequences of making '_' special? maybe we can make this decision everywhere?
|
||||
|
||||
|
||||
class INameView(Collection[str], ABC):
|
||||
"""
|
||||
Read-only collection of reserved names with a shared name allocator.
|
||||
|
||||
Name views support membership, iteration, `len()`, and `get_name()`. They
|
||||
do not provide pattern lookup or the other operations of a library mapping.
|
||||
"""
|
||||
|
||||
def get_name(
|
||||
self,
|
||||
name: str = SINGLE_USE_PREFIX * 2,
|
||||
sanitize: bool = True,
|
||||
max_length: int = 32,
|
||||
quiet: bool | None = None,
|
||||
) -> str:
|
||||
"""
|
||||
Find a unique name.
|
||||
|
||||
This function may be overridden in a subclass or monkey-patched to fit
|
||||
the caller's requirements.
|
||||
|
||||
Args:
|
||||
name: Preferred name. Default is `SINGLE_USE_PREFIX * 2`.
|
||||
sanitize: Allow only alphanumeric characters and _?$, replacing
|
||||
invalid characters with underscores.
|
||||
max_length: Truncate names longer than this.
|
||||
quiet: Suppress log messages when `True`. The default suppresses
|
||||
messages only when `name` starts with `SINGLE_USE_PREFIX`.
|
||||
|
||||
Returns:
|
||||
A name unique within this view.
|
||||
"""
|
||||
if quiet is None:
|
||||
quiet = name.startswith(SINGLE_USE_PREFIX)
|
||||
|
||||
if sanitize:
|
||||
sanitized_name = re.compile(r'[^A-Za-z0-9_\?\$]').sub('_', name)
|
||||
else:
|
||||
sanitized_name = name
|
||||
|
||||
suffixed_name = sanitized_name
|
||||
if sanitized_name in self:
|
||||
ii = sum(1 for nn in self if nn.startswith(sanitized_name))
|
||||
else:
|
||||
ii = 0
|
||||
while suffixed_name in self or suffixed_name == '':
|
||||
suffixed_name = sanitized_name + b64suffix(ii)
|
||||
ii += 1
|
||||
|
||||
if len(suffixed_name) > max_length:
|
||||
if name == '':
|
||||
raise LibraryError(f'No valid pattern names remaining within the specified {max_length=}')
|
||||
|
||||
cropped_name = self.get_name(sanitized_name[:-1], sanitize=sanitize, max_length=max_length, quiet=True)
|
||||
else:
|
||||
cropped_name = suffixed_name
|
||||
|
||||
if not quiet:
|
||||
logger.info(f'Requested name "{name}" changed to "{cropped_name}"')
|
||||
|
||||
return cropped_name
|
||||
from .base import ILibraryView
|
||||
|
||||
|
||||
class visitor_function_t(Protocol):
|
||||
|
|
@ -110,9 +34,16 @@ Tree: TypeAlias = MutableMapping[str, 'Pattern']
|
|||
|
||||
dangling_mode_t: TypeAlias = Literal['error', 'ignore', 'include']
|
||||
""" How helpers should handle refs whose targets are not present in the library. """
|
||||
SINGLE_USE_PREFIX = '_'
|
||||
"""
|
||||
Names starting with this prefix are assumed to refer to single-use patterns,
|
||||
which may be renamed automatically by `ILibrary.add()` (via
|
||||
`rename_theirs=_rename_patterns()` )
|
||||
"""
|
||||
# TODO what are the consequences of making '_' special? maybe we can make this decision everywhere?
|
||||
|
||||
|
||||
def _rename_patterns(lib: INameView, name: str) -> str:
|
||||
def _rename_patterns(lib: ILibraryView, name: str) -> str:
|
||||
"""
|
||||
The default `rename_theirs` function for `ILibrary.add`.
|
||||
|
||||
|
|
@ -136,41 +67,12 @@ def _rename_patterns(lib: INameView, name: str) -> str:
|
|||
return lib.get_name(SINGLE_USE_PREFIX + stem)
|
||||
|
||||
|
||||
def _validate_dangling_mode(dangling: dangling_mode_t) -> None:
|
||||
if dangling not in ('error', 'ignore', 'include'):
|
||||
raise ValueError(
|
||||
f'Unknown dangling-reference mode {dangling!r}; '
|
||||
'expected one of "error", "ignore", or "include"'
|
||||
)
|
||||
|
||||
|
||||
class _ProspectiveNames(INameView):
|
||||
"""Target names plus names reserved earlier in an addition plan."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
target: INameView,
|
||||
reserved: set[str],
|
||||
) -> None:
|
||||
self._target = target
|
||||
self._reserved = reserved
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
yield from self._target
|
||||
yield from self._reserved
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self._target) + len(self._reserved)
|
||||
|
||||
def __contains__(self, key: object) -> bool:
|
||||
return key in self._reserved or key in self._target
|
||||
|
||||
|
||||
def _plan_source_names(
|
||||
target: INameView,
|
||||
target: ILibraryView,
|
||||
source_order: Sequence[str],
|
||||
existing_names: Container[str],
|
||||
*,
|
||||
rename_theirs: Callable[[INameView, str], str] | None = None,
|
||||
rename_theirs: Callable[[ILibraryView, str], str] | None = None,
|
||||
rename_when: Literal['conflict', 'always'] = 'conflict',
|
||||
) -> dict[str, str]:
|
||||
if rename_when not in ('conflict', 'always'):
|
||||
|
|
@ -179,22 +81,21 @@ def _plan_source_names(
|
|||
raise TypeError('rename_theirs is required when rename_when="always"')
|
||||
|
||||
source_to_visible: dict[str, str] = {}
|
||||
reserved: set[str] = set()
|
||||
prospective = _ProspectiveNames(target, reserved)
|
||||
visible_names: set[str] = set()
|
||||
|
||||
for name in source_order:
|
||||
visible = name
|
||||
if rename_when == 'always':
|
||||
assert rename_theirs is not None
|
||||
visible = rename_theirs(prospective, name)
|
||||
elif visible in prospective:
|
||||
visible = rename_theirs(target, name)
|
||||
elif visible in existing_names or visible in visible_names:
|
||||
if rename_theirs is None:
|
||||
raise LibraryError(f'Conflicting name while adding source: {name!r}')
|
||||
visible = rename_theirs(prospective, name)
|
||||
if visible in prospective:
|
||||
visible = rename_theirs(target, name)
|
||||
if visible in existing_names or visible in visible_names:
|
||||
raise LibraryError(f'Unresolved duplicate key encountered while adding source: {name!r} -> {visible!r}')
|
||||
source_to_visible[name] = visible
|
||||
reserved.add(visible)
|
||||
visible_names.add(visible)
|
||||
|
||||
return source_to_visible
|
||||
|
||||
|
|
|
|||
|
|
@ -30,41 +30,6 @@ from .ports import Port, PortList
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _check_ref_cycles(
|
||||
pattern: 'Pattern',
|
||||
library: Mapping[str, 'Pattern'],
|
||||
*,
|
||||
operation: str,
|
||||
) -> None:
|
||||
visited: set[str] = set()
|
||||
active: list[str] = []
|
||||
active_set: set[str] = set()
|
||||
|
||||
def visit(current: 'Pattern') -> None:
|
||||
for target, refs in current.refs.items():
|
||||
if target is None or not refs:
|
||||
continue
|
||||
if target in active_set:
|
||||
cycle_start = active.index(target)
|
||||
cycle = active[cycle_start:] + [target]
|
||||
raise PatternError(
|
||||
f'Circular reference while {operation}: {" -> ".join(cycle)}'
|
||||
)
|
||||
if target in visited:
|
||||
continue
|
||||
|
||||
active.append(target)
|
||||
active_set.add(target)
|
||||
try:
|
||||
visit(library[target])
|
||||
finally:
|
||||
active.pop()
|
||||
active_set.remove(target)
|
||||
visited.add(target)
|
||||
|
||||
visit(pattern)
|
||||
|
||||
|
||||
@functools.total_ordering
|
||||
class Pattern(PortList, AnnotatableImpl, Mirrorable):
|
||||
"""
|
||||
|
|
@ -544,8 +509,6 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
|
|||
layer: layer_t,
|
||||
flatten: bool = True,
|
||||
library: Mapping[str, 'Pattern'] | None = None,
|
||||
*,
|
||||
_cycle_checked: bool = False,
|
||||
) -> list[Polygon]:
|
||||
"""
|
||||
Collect all geometry effectively on a given layer as a list of polygons.
|
||||
|
|
@ -561,15 +524,9 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
|
|||
|
||||
Returns:
|
||||
A list of `Polygon` objects.
|
||||
|
||||
Raises:
|
||||
PatternError: If `flatten=True` and the referenced hierarchy contains a cycle.
|
||||
"""
|
||||
if flatten and self.has_refs() and library is None:
|
||||
raise PatternError("Must provide a library to layer_as_polygons() when flatten=True")
|
||||
if flatten and self.has_refs() and not _cycle_checked:
|
||||
assert library is not None
|
||||
_check_ref_cycles(self, library, operation='collecting layer polygons')
|
||||
|
||||
polys: list[Polygon] = []
|
||||
|
||||
|
|
@ -586,17 +543,12 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
|
|||
if flatten and self.has_refs():
|
||||
assert library is not None
|
||||
for target, refs in self.refs.items():
|
||||
if target is None or not refs:
|
||||
if target is None:
|
||||
continue
|
||||
target_pat = library[target]
|
||||
for ref in refs:
|
||||
# Get polygons from target pattern on the same layer
|
||||
ref_polys = target_pat.layer_as_polygons(
|
||||
layer,
|
||||
flatten=True,
|
||||
library=library,
|
||||
_cycle_checked=True,
|
||||
)
|
||||
ref_polys = target_pat.layer_as_polygons(layer, flatten=True, library=library)
|
||||
# Apply ref transformations
|
||||
for p in ref_polys:
|
||||
p_pat = ref.as_pattern(Pattern(shapes={layer: [p]}))
|
||||
|
|
@ -614,15 +566,13 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
|
|||
Returns:
|
||||
A set of all pattern names referenced by this pattern.
|
||||
"""
|
||||
return {target for target, refs in self.refs.items() if refs}
|
||||
return set(self.refs.keys())
|
||||
|
||||
def get_bounds(
|
||||
self,
|
||||
library: Mapping[str, 'Pattern'] | None = None,
|
||||
recurse: bool = True,
|
||||
cache: MutableMapping[str, NDArray[numpy.float64] | None] | None = None,
|
||||
*,
|
||||
_cycle_checked: bool = False,
|
||||
) -> NDArray[numpy.float64] | None:
|
||||
"""
|
||||
Return a `numpy.ndarray` containing `[[x_min, y_min], [x_max, y_max]]`, corresponding to the
|
||||
|
|
@ -639,13 +589,7 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
|
|||
|
||||
Returns:
|
||||
`[[x_min, y_min], [x_max, y_max]]` or `None`
|
||||
|
||||
Raises:
|
||||
PatternError: If recursive bounds are requested for a cyclic hierarchy.
|
||||
"""
|
||||
if recurse and self.has_refs() and library is not None and not _cycle_checked:
|
||||
_check_ref_cycles(self, library, operation='calculating bounds')
|
||||
|
||||
if self.is_empty():
|
||||
return None
|
||||
|
||||
|
|
@ -684,12 +628,7 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
|
|||
if target in cache:
|
||||
unrot_bounds = cache[target]
|
||||
elif any(numpy.isclose(ref.rotation % (pi / 2), 0) for ref in refs):
|
||||
unrot_bounds = library[target].get_bounds(
|
||||
library=library,
|
||||
recurse=recurse,
|
||||
cache=cache,
|
||||
_cycle_checked=True,
|
||||
)
|
||||
unrot_bounds = library[target].get_bounds(library=library, recurse=recurse, cache=cache)
|
||||
cache[target] = unrot_bounds
|
||||
|
||||
for ref in refs:
|
||||
|
|
@ -1188,17 +1127,8 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
|
|||
overdraw: Whether to create a new figure or draw on a pre-existing one.
|
||||
filename: If provided, save the figure to this file instead of showing it.
|
||||
ports: If True, annotate the plot with arrows representing the ports.
|
||||
|
||||
Raises:
|
||||
PatternError: If the referenced hierarchy contains a cycle.
|
||||
"""
|
||||
# TODO: add text labels to visualize()
|
||||
if self.has_refs() and library is None:
|
||||
raise PatternError('Must provide a library when visualizing a pattern with refs')
|
||||
if self.has_refs():
|
||||
assert library is not None
|
||||
_check_ref_cycles(self, library, operation='visualizing pattern hierarchy')
|
||||
|
||||
try:
|
||||
from matplotlib import pyplot # type: ignore #noqa: PLC0415
|
||||
import matplotlib.collections # type: ignore #noqa: PLC0415
|
||||
|
|
@ -1207,6 +1137,9 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
|
|||
+ 'Make sure to install masque with the [visualize] option to pull in the needed dependencies.')
|
||||
raise
|
||||
|
||||
if self.has_refs() and library is None:
|
||||
raise PatternError('Must provide a library when visualizing a pattern with refs')
|
||||
|
||||
# Cache for {Pattern object ID: List of local polygon vertex arrays}
|
||||
# Polygons are stored relative to the pattern's origin (offset included)
|
||||
poly_cache: dict[int, list[NDArray[numpy.float64]]] = {}
|
||||
|
|
@ -1270,7 +1203,7 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
|
|||
|
||||
# 3. Recurse into refs
|
||||
for target, refs in pat.refs.items():
|
||||
if target is None or not refs:
|
||||
if target is None:
|
||||
continue
|
||||
assert library is not None
|
||||
target_pat = library[target]
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue