jitx 4.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jitx/__init__.py +212 -0
- jitx/__main__.py +165 -0
- jitx/_instantiation.py +105 -0
- jitx/_structural.py +913 -0
- jitx/_translate/board.py +250 -0
- jitx/_translate/bundle.py +39 -0
- jitx/_translate/circuit.py +825 -0
- jitx/_translate/component.py +330 -0
- jitx/_translate/copper.py +20 -0
- jitx/_translate/design.py +151 -0
- jitx/_translate/dispatch.py +80 -0
- jitx/_translate/enums.py +89 -0
- jitx/_translate/feature.py +127 -0
- jitx/_translate/fileinfo.py +33 -0
- jitx/_translate/idmap.py +211 -0
- jitx/_translate/landpattern.py +230 -0
- jitx/_translate/layerindex.py +11 -0
- jitx/_translate/mapping.py +13 -0
- jitx/_translate/routing.py +297 -0
- jitx/_translate/rules.py +153 -0
- jitx/_translate/schematic.py +102 -0
- jitx/_translate/shape.py +154 -0
- jitx/_translate/signal_models.py +16 -0
- jitx/_translate/symbol.py +204 -0
- jitx/_translate/via.py +236 -0
- jitx/_websocket.py +207 -0
- jitx/anchor.py +128 -0
- jitx/board.py +26 -0
- jitx/circuit.py +444 -0
- jitx/common.py +116 -0
- jitx/compat/__init__.py +0 -0
- jitx/compat/altium.py +41 -0
- jitx/component.py +175 -0
- jitx/constraints.py +828 -0
- jitx/container.py +168 -0
- jitx/context.py +107 -0
- jitx/copper.py +83 -0
- jitx/decorators.py +21 -0
- jitx/design.py +135 -0
- jitx/error.py +69 -0
- jitx/events.py +100 -0
- jitx/feature.py +156 -0
- jitx/fileinfo.py +23 -0
- jitx/inspect.py +277 -0
- jitx/interval.py +82 -0
- jitx/landpattern.py +214 -0
- jitx/layerindex.py +195 -0
- jitx/logo.py +125 -0
- jitx/memo.py +31 -0
- jitx/model3d.py +99 -0
- jitx/net.py +852 -0
- jitx/paper.py +24 -0
- jitx/placement.py +145 -0
- jitx/property.py +65 -0
- jitx/refpath.py +143 -0
- jitx/run/__init__.py +469 -0
- jitx/run/dependencies.py +267 -0
- jitx/run/pyproject.py +75 -0
- jitx/sample/__init__.py +193 -0
- jitx/schematic.py +159 -0
- jitx/shapes/__init__.py +171 -0
- jitx/shapes/composites.py +649 -0
- jitx/shapes/primitive.py +374 -0
- jitx/shapes/shapely.py +678 -0
- jitx/si.py +1024 -0
- jitx/stackup.py +167 -0
- jitx/substrate.py +207 -0
- jitx/symbol.py +212 -0
- jitx/test.py +46 -0
- jitx/toleranced.py +323 -0
- jitx/transform.py +510 -0
- jitx/units.py +63 -0
- jitx/via.py +115 -0
- jitx/vscode/__init__.py +8 -0
- jitx/vscode/config.py +166 -0
- jitx-4.0.0.dist-info/METADATA +31 -0
- jitx-4.0.0.dist-info/RECORD +78 -0
- jitx-4.0.0.dist-info/WHEEL +4 -0
jitx/__init__.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"""
|
|
2
|
+
JITX Python API
|
|
3
|
+
===============
|
|
4
|
+
|
|
5
|
+
The JITX Python API is a set of classes that can be used to build a design in
|
|
6
|
+
Python. The core principle of the API is that, when run, your code constructs
|
|
7
|
+
an object tree that is then inspected. This means that creating an object and
|
|
8
|
+
attaching them to other objects is what drives the construction of the design,
|
|
9
|
+
and not function calls. The root of the tree is an instance of a
|
|
10
|
+
:py:class:`~jitx.design.Design` subclass. The JITX runtime will inspect your
|
|
11
|
+
project looking for Design subclasses which are then instantiated on request.
|
|
12
|
+
|
|
13
|
+
The inspection mechanism will traverse lists, tuples, and dictionaries, as
|
|
14
|
+
well, thus objects can be added to arrays and dictionaries as needed when
|
|
15
|
+
building the object tree.
|
|
16
|
+
|
|
17
|
+
The API is designed to be as simple as possible, and to be as close to Python
|
|
18
|
+
as possible. The API is designed to be used with a Python language server, such
|
|
19
|
+
as PyLance/pyright, to provide autocompletion and type checking.
|
|
20
|
+
|
|
21
|
+
.. note::
|
|
22
|
+
|
|
23
|
+
There are a few mechanisms designed to make API usage cleaner by allowing
|
|
24
|
+
instantiated objects as class attributes, but there are caveats to be aware
|
|
25
|
+
of when doing more involved logic, and when in doubt, all logic can also be
|
|
26
|
+
placed in the initializer of the class. Runtime errors involving an
|
|
27
|
+
unexpected "Instantiable" object is indicative of this type of error.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
from .anchor import Anchor as Anchor
|
|
31
|
+
from .board import Board as Board
|
|
32
|
+
from .circuit import Circuit as Circuit, CurrentCircuit
|
|
33
|
+
from .component import Component as Component, CurrentComponent
|
|
34
|
+
from .constraints import (
|
|
35
|
+
IsBoardEdge as IsBoardEdge,
|
|
36
|
+
IsCopper as IsCopper,
|
|
37
|
+
IsHole as IsHole,
|
|
38
|
+
IsNeckdown as IsNeckdown,
|
|
39
|
+
IsPad as IsPad,
|
|
40
|
+
IsPour as IsPour,
|
|
41
|
+
IsThroughHole as IsThroughHole,
|
|
42
|
+
IsTrace as IsTrace,
|
|
43
|
+
IsVia as IsVia,
|
|
44
|
+
SquareViaStitchGrid as SquareViaStitchGrid,
|
|
45
|
+
Tag as Tag,
|
|
46
|
+
Tags as Tags,
|
|
47
|
+
TriangularViaStitchGrid as TriangularViaStitchGrid,
|
|
48
|
+
ViaFencePattern as ViaFencePattern,
|
|
49
|
+
design_constraint as design_constraint,
|
|
50
|
+
)
|
|
51
|
+
from .container import Composite as Composite, Container as Container
|
|
52
|
+
from .context import ContextProperty
|
|
53
|
+
from .copper import Copper as Copper, Pour as Pour
|
|
54
|
+
from .design import Design as Design, DesignContext
|
|
55
|
+
from .error import UserCodeException as UserCodeException
|
|
56
|
+
from .feature import (
|
|
57
|
+
Courtyard as Courtyard,
|
|
58
|
+
Custom as Custom,
|
|
59
|
+
Cutout as Cutout,
|
|
60
|
+
Finish as Finish,
|
|
61
|
+
Glue as Glue,
|
|
62
|
+
KeepOut as KeepOut,
|
|
63
|
+
Paste as Paste,
|
|
64
|
+
Silkscreen as Silkscreen,
|
|
65
|
+
Soldermask as Soldermask,
|
|
66
|
+
)
|
|
67
|
+
from .inspect import decompose as decompose, extract as extract, visit as visit
|
|
68
|
+
from .landpattern import (
|
|
69
|
+
Landpattern as Landpattern,
|
|
70
|
+
Pad as Pad,
|
|
71
|
+
PadMapping as PadMapping,
|
|
72
|
+
)
|
|
73
|
+
from .layerindex import LayerSet as LayerSet, Side as Side
|
|
74
|
+
from .net import (
|
|
75
|
+
DiffPair as DiffPair,
|
|
76
|
+
Net as Net,
|
|
77
|
+
Port as Port,
|
|
78
|
+
provide as provide,
|
|
79
|
+
Provide as Provide,
|
|
80
|
+
)
|
|
81
|
+
from .placement import Placement as Placement, Positionable as Positionable
|
|
82
|
+
from .shapes.composites import capsule as capsule, rectangle as rectangle
|
|
83
|
+
from .shapes.primitive import (
|
|
84
|
+
Arc as Arc,
|
|
85
|
+
ArcPolygon as ArcPolygon,
|
|
86
|
+
ArcPolyline as ArcPolyline,
|
|
87
|
+
Circle as Circle,
|
|
88
|
+
Polygon as Polygon,
|
|
89
|
+
Polyline as Polyline,
|
|
90
|
+
)
|
|
91
|
+
from .substrate import Substrate as Substrate, SubstrateContext
|
|
92
|
+
from .symbol import Pin as Pin, Symbol as Symbol, SymbolMapping as SymbolMapping
|
|
93
|
+
from .toleranced import Toleranced as Toleranced
|
|
94
|
+
from .transform import Point as Point, Transform as Transform
|
|
95
|
+
from .via import Via as Via
|
|
96
|
+
|
|
97
|
+
# the pattern used above
|
|
98
|
+
#
|
|
99
|
+
# from X import Class as Class
|
|
100
|
+
#
|
|
101
|
+
# is a convention to indicate to language servers that the name should be
|
|
102
|
+
# reexported from the module and offered during autocompletion, as well as not
|
|
103
|
+
# flagged as an unused import.
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class Current:
|
|
107
|
+
"""
|
|
108
|
+
Context registry object, where each field is a property that will get
|
|
109
|
+
the currently registered context value. If no such context is currently
|
|
110
|
+
active, it will raise an exception. This is purely for convenience, the
|
|
111
|
+
contexts can be accessed directly.
|
|
112
|
+
|
|
113
|
+
The main purpose of this is to allow introspection of the environment. Note
|
|
114
|
+
that any accessed context value will be part of the memoization key, and
|
|
115
|
+
thus if the environment changes a memoized element will be reevaluated, and
|
|
116
|
+
thus use of current circuit or component should be used with care.
|
|
117
|
+
|
|
118
|
+
There is already an instance of this class, :py:data:`current`, which can
|
|
119
|
+
be used directly.
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
design = ContextProperty(DesignContext, lambda x: x.design)
|
|
123
|
+
"The current design being built"
|
|
124
|
+
substrate = ContextProperty(SubstrateContext, lambda x: x.substrate)
|
|
125
|
+
"The substrate of the current design"
|
|
126
|
+
circuit = ContextProperty(CurrentCircuit, lambda x: x.circuit)
|
|
127
|
+
"The current circuit being constructed"
|
|
128
|
+
component = ContextProperty(CurrentComponent, lambda x: x.component)
|
|
129
|
+
"The current component being constructed"
|
|
130
|
+
|
|
131
|
+
def __repr__(self):
|
|
132
|
+
return "Current"
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
current = Current()
|
|
136
|
+
"""Singleton, the "current" context registry."""
|
|
137
|
+
|
|
138
|
+
# this can't be autogenerated at runtime, since the typechecker won't know what is
|
|
139
|
+
# actually imported if someone does from jitx import *, thus they need to be
|
|
140
|
+
# listed in the source file; please remember to update this if you add new
|
|
141
|
+
# imports that should be reexported
|
|
142
|
+
|
|
143
|
+
# grep '^\(from\| \)' __init__.py | grep -o 'as [^, ]*\(,\|$\)' | sed -e 's/^as \([^,]*\).*$/ "\1",/'
|
|
144
|
+
__all__ = [
|
|
145
|
+
"current",
|
|
146
|
+
# list below is generated with the grep command
|
|
147
|
+
"Anchor",
|
|
148
|
+
"Board",
|
|
149
|
+
"Circuit",
|
|
150
|
+
"Component",
|
|
151
|
+
"IsBoardEdge",
|
|
152
|
+
"IsCopper",
|
|
153
|
+
"IsHole",
|
|
154
|
+
"IsNeckdown",
|
|
155
|
+
"IsPad",
|
|
156
|
+
"IsPour",
|
|
157
|
+
"IsThroughHole",
|
|
158
|
+
"IsTrace",
|
|
159
|
+
"IsVia",
|
|
160
|
+
"SquareViaStitchGrid",
|
|
161
|
+
"Tag",
|
|
162
|
+
"Tags",
|
|
163
|
+
"TriangularViaStitchGrid",
|
|
164
|
+
"ViaFencePattern",
|
|
165
|
+
"design_constraint",
|
|
166
|
+
"Composite",
|
|
167
|
+
"Container",
|
|
168
|
+
"Copper",
|
|
169
|
+
"Pour",
|
|
170
|
+
"Design",
|
|
171
|
+
"UserCodeException",
|
|
172
|
+
"Courtyard",
|
|
173
|
+
"Custom",
|
|
174
|
+
"Cutout",
|
|
175
|
+
"Finish",
|
|
176
|
+
"Glue",
|
|
177
|
+
"KeepOut",
|
|
178
|
+
"Paste",
|
|
179
|
+
"Silkscreen",
|
|
180
|
+
"Soldermask",
|
|
181
|
+
"decompose",
|
|
182
|
+
"extract",
|
|
183
|
+
"visit",
|
|
184
|
+
"Landpattern",
|
|
185
|
+
"Pad",
|
|
186
|
+
"PadMapping",
|
|
187
|
+
"LayerSet",
|
|
188
|
+
"Side",
|
|
189
|
+
"DiffPair",
|
|
190
|
+
"Net",
|
|
191
|
+
"Port",
|
|
192
|
+
"provide",
|
|
193
|
+
"Provide",
|
|
194
|
+
"Placement",
|
|
195
|
+
"Positionable",
|
|
196
|
+
"capsule",
|
|
197
|
+
"rectangle",
|
|
198
|
+
"Arc",
|
|
199
|
+
"ArcPolygon",
|
|
200
|
+
"ArcPolyline",
|
|
201
|
+
"Circle",
|
|
202
|
+
"Polygon",
|
|
203
|
+
"Polyline",
|
|
204
|
+
"Substrate",
|
|
205
|
+
"Pin",
|
|
206
|
+
"Symbol",
|
|
207
|
+
"SymbolMapping",
|
|
208
|
+
"Toleranced",
|
|
209
|
+
"Point",
|
|
210
|
+
"Transform",
|
|
211
|
+
"Via",
|
|
212
|
+
]
|
jitx/__main__.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# jitx/__main__.py
|
|
2
|
+
from argparse import ArgumentParser
|
|
3
|
+
from logging import basicConfig
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
from jitx.run import (
|
|
7
|
+
DesignBuilder,
|
|
8
|
+
DesignFactory,
|
|
9
|
+
DesignFinder,
|
|
10
|
+
DryRunBuilder,
|
|
11
|
+
json_formatter,
|
|
12
|
+
text_formatter,
|
|
13
|
+
)
|
|
14
|
+
from jitx.run.dependencies import sync_venv
|
|
15
|
+
|
|
16
|
+
parser = ArgumentParser("jitx")
|
|
17
|
+
parser.add_argument("--format", choices=["json", "text"])
|
|
18
|
+
parser.add_argument(
|
|
19
|
+
"--logging",
|
|
20
|
+
type=str,
|
|
21
|
+
default="INFO",
|
|
22
|
+
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
|
|
23
|
+
help="Logging level",
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
subparser = parser.add_subparsers(dest="cmd")
|
|
27
|
+
|
|
28
|
+
# find
|
|
29
|
+
find_design_parser = subparser.add_parser("find")
|
|
30
|
+
find_design_parser.add_argument("path", nargs="*")
|
|
31
|
+
|
|
32
|
+
# build
|
|
33
|
+
build_design_parser = subparser.add_parser("build")
|
|
34
|
+
build_design_parser.add_argument("--dump")
|
|
35
|
+
build_design_parser.add_argument("--dry", default=False, action="store_true")
|
|
36
|
+
build_design_parser.add_argument("--host", type=str, default="localhost")
|
|
37
|
+
build_design_parser.add_argument("--port", type=int)
|
|
38
|
+
build_design_parser.add_argument("--uri", type=str)
|
|
39
|
+
build_design_parser.add_argument("--socket", metavar="FILE", default=None, type=str)
|
|
40
|
+
build_design_parser.add_argument("--no-dependency-check", action="store_true")
|
|
41
|
+
build_design_parser.add_argument("--check-include-group", action="append", default=None)
|
|
42
|
+
build_design_parser.add_argument("design", nargs="+")
|
|
43
|
+
|
|
44
|
+
# build-all
|
|
45
|
+
build_all_design_parser = subparser.add_parser("build-all")
|
|
46
|
+
build_all_design_parser.add_argument("--host", type=str, default="localhost")
|
|
47
|
+
build_all_design_parser.add_argument("--port", type=int)
|
|
48
|
+
build_all_design_parser.add_argument("--uri", type=str)
|
|
49
|
+
build_all_design_parser.add_argument("--socket", metavar="FILE", default=None, type=str)
|
|
50
|
+
build_all_design_parser.add_argument("--generate-config", action="store_true")
|
|
51
|
+
build_all_design_parser.add_argument("--no-dependency-check", action="store_true")
|
|
52
|
+
build_all_design_parser.add_argument(
|
|
53
|
+
"--check-include-group", action="append", default=None
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
# upgrade-dependencies
|
|
57
|
+
dependency_parser = subparser.add_parser(
|
|
58
|
+
"dependencies", help="Check or upgrade Python dependencies"
|
|
59
|
+
)
|
|
60
|
+
dependency_parser.add_argument(
|
|
61
|
+
"--upgrade",
|
|
62
|
+
action="store_true",
|
|
63
|
+
help="Upgrade to highest allowed (otherwise just check)",
|
|
64
|
+
)
|
|
65
|
+
dependency_parser.add_argument(
|
|
66
|
+
"--allow-prereleases",
|
|
67
|
+
"--pre",
|
|
68
|
+
action="store_true",
|
|
69
|
+
help="Include prereleases during resolution",
|
|
70
|
+
)
|
|
71
|
+
dependency_parser.add_argument(
|
|
72
|
+
"--check-include-group",
|
|
73
|
+
action="append",
|
|
74
|
+
default=None,
|
|
75
|
+
help="Optional-dependency group(s) to include (repeatable)",
|
|
76
|
+
)
|
|
77
|
+
dependency_parser.add_argument(
|
|
78
|
+
"--editable-install",
|
|
79
|
+
action="store_true",
|
|
80
|
+
help="Reinstall the current project in editable mode after upgrading dependencies",
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
build_config_parser = subparser.add_parser(
|
|
84
|
+
"run-config", help="Generate VSCode run and debug configuration"
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
args = parser.parse_args()
|
|
88
|
+
basicConfig(level=args.logging)
|
|
89
|
+
|
|
90
|
+
formatter = {"json": json_formatter, "text": text_formatter}.get(
|
|
91
|
+
args.format, text_formatter
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
if args.cmd == "find":
|
|
95
|
+
DesignFactory(DesignFinder(args.path), DryRunBuilder(), formatter=formatter).list()
|
|
96
|
+
elif args.cmd == "build":
|
|
97
|
+
if not args.no_dependency_check:
|
|
98
|
+
sync_venv(
|
|
99
|
+
mode="check",
|
|
100
|
+
allow_prereleases=False,
|
|
101
|
+
include_optional_groups=args.check_include_group,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
if args.dry:
|
|
105
|
+
builder = DryRunBuilder()
|
|
106
|
+
elif args.uri:
|
|
107
|
+
builder = DesignBuilder(uri=args.uri)
|
|
108
|
+
elif args.port:
|
|
109
|
+
builder = DesignBuilder(port=args.port, host=args.host)
|
|
110
|
+
elif args.socket:
|
|
111
|
+
builder = DesignBuilder(spec=args.socket)
|
|
112
|
+
else:
|
|
113
|
+
builder = DesignBuilder()
|
|
114
|
+
|
|
115
|
+
finder = DesignFinder(".")
|
|
116
|
+
factory = DesignFactory(finder, builder, formatter=formatter, dump=args.dump)
|
|
117
|
+
for design in args.design:
|
|
118
|
+
if design.endswith(".py"):
|
|
119
|
+
factory.by_file(design)
|
|
120
|
+
else:
|
|
121
|
+
factory.by_name(design)
|
|
122
|
+
factory.build()
|
|
123
|
+
if not factory.success:
|
|
124
|
+
sys.exit(1)
|
|
125
|
+
|
|
126
|
+
elif args.cmd == "build-all":
|
|
127
|
+
if not args.no_dependency_check:
|
|
128
|
+
sync_venv(
|
|
129
|
+
mode="check",
|
|
130
|
+
allow_prereleases=False,
|
|
131
|
+
include_optional_groups=args.check_include_group,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
finder = DesignFinder(".")
|
|
135
|
+
builder = (
|
|
136
|
+
DesignBuilder(uri=args.uri)
|
|
137
|
+
if args.uri
|
|
138
|
+
else DesignBuilder(port=args.port, host=args.host)
|
|
139
|
+
if args.port
|
|
140
|
+
else DesignBuilder()
|
|
141
|
+
)
|
|
142
|
+
factory = DesignFactory(finder, builder, formatter=formatter)
|
|
143
|
+
factory.add_all()
|
|
144
|
+
factory.build()
|
|
145
|
+
if args.generate_config:
|
|
146
|
+
from jitx.vscode.config import generate_config
|
|
147
|
+
|
|
148
|
+
generate_config()
|
|
149
|
+
if not factory.success:
|
|
150
|
+
sys.exit(1)
|
|
151
|
+
|
|
152
|
+
elif args.cmd == "dependencies":
|
|
153
|
+
mode = "upgrade" if args.upgrade else "check"
|
|
154
|
+
sync_venv(
|
|
155
|
+
mode=mode,
|
|
156
|
+
allow_prereleases=bool(args.allow_prereleases),
|
|
157
|
+
include_optional_groups=args.check_include_group,
|
|
158
|
+
editable_install=args.editable_install,
|
|
159
|
+
)
|
|
160
|
+
elif args.cmd == "run-config":
|
|
161
|
+
from jitx.vscode.config import generate_config
|
|
162
|
+
|
|
163
|
+
generate_config()
|
|
164
|
+
else:
|
|
165
|
+
parser.print_help()
|
jitx/_instantiation.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
from threading import local
|
|
2
|
+
from typing import Self, Any
|
|
3
|
+
from contextlib import contextmanager
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class InstantiationStructureException(Exception):
|
|
9
|
+
pass
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class InstantiationStructure:
|
|
13
|
+
class Frame:
|
|
14
|
+
def __init__(self, parent: Self | None = None):
|
|
15
|
+
self.parent = parent
|
|
16
|
+
self.context: dict[Any, Any] = {}
|
|
17
|
+
self.tracker: list[dict[Any, Any]] = []
|
|
18
|
+
|
|
19
|
+
def get(self, key):
|
|
20
|
+
if key in self.context:
|
|
21
|
+
value = self.context[key]
|
|
22
|
+
elif self.parent:
|
|
23
|
+
value = self.parent.get(key)
|
|
24
|
+
else:
|
|
25
|
+
value = None
|
|
26
|
+
if self.tracker:
|
|
27
|
+
self.tracker[-1][key] = value
|
|
28
|
+
return value
|
|
29
|
+
|
|
30
|
+
class InstantiationData(local):
|
|
31
|
+
def __init__(self):
|
|
32
|
+
super().__init__()
|
|
33
|
+
self.stack: list[InstantiationStructure.Frame] = []
|
|
34
|
+
|
|
35
|
+
def __init__(self):
|
|
36
|
+
self.data = self.InstantiationData()
|
|
37
|
+
self.generation = 0
|
|
38
|
+
|
|
39
|
+
def active(self):
|
|
40
|
+
return bool(self.data.stack)
|
|
41
|
+
|
|
42
|
+
@contextmanager
|
|
43
|
+
def activate(self):
|
|
44
|
+
if self.active():
|
|
45
|
+
raise InstantiationStructureException("Structure already active")
|
|
46
|
+
# for tracking lost objects
|
|
47
|
+
self.generation += 1
|
|
48
|
+
self.data.stack.append(self.Frame())
|
|
49
|
+
try:
|
|
50
|
+
yield
|
|
51
|
+
finally:
|
|
52
|
+
self.data.stack.clear()
|
|
53
|
+
|
|
54
|
+
@contextmanager
|
|
55
|
+
def require(self):
|
|
56
|
+
if self.active():
|
|
57
|
+
yield
|
|
58
|
+
else:
|
|
59
|
+
with self.activate():
|
|
60
|
+
yield
|
|
61
|
+
|
|
62
|
+
@contextmanager
|
|
63
|
+
def frame(self, frame: Frame | None = None):
|
|
64
|
+
self.push(frame)
|
|
65
|
+
try:
|
|
66
|
+
yield
|
|
67
|
+
finally:
|
|
68
|
+
self.pop()
|
|
69
|
+
|
|
70
|
+
def push(self, frame: Frame | None = None):
|
|
71
|
+
if not self.active():
|
|
72
|
+
raise InstantiationStructureException("Structure not active")
|
|
73
|
+
if frame is None:
|
|
74
|
+
frame = self.Frame(self.data.stack[-1])
|
|
75
|
+
self.data.stack.append(frame)
|
|
76
|
+
|
|
77
|
+
def pop(self):
|
|
78
|
+
self.data.stack.pop()
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def current_frame(self) -> Frame:
|
|
82
|
+
if not self.active():
|
|
83
|
+
raise InstantiationStructureException("Structure not active")
|
|
84
|
+
return self.data.stack[-1]
|
|
85
|
+
|
|
86
|
+
def get(self, key):
|
|
87
|
+
if not self.active():
|
|
88
|
+
raise InstantiationStructureException("Structure not active")
|
|
89
|
+
return self.data.stack[-1].get(key)
|
|
90
|
+
|
|
91
|
+
def set(self, key, value):
|
|
92
|
+
if not self.active():
|
|
93
|
+
raise InstantiationStructureException("Structure not active")
|
|
94
|
+
self.data.stack[-1].context[key] = value
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
instantiation = InstantiationStructure()
|
|
98
|
+
passive_representation = False
|
|
99
|
+
if os.environ.get("JITX_PASSIVE_INSTANTIATION"):
|
|
100
|
+
# this is used to show representation of instantiable objects as their class
|
|
101
|
+
# name, rather than their "Instantiable" wrapper. The purpose of this is to
|
|
102
|
+
# get legible documentation. Enabling this during a debug sessions will
|
|
103
|
+
# likely be confusing, if there's a spurious "Instantiable" object causing
|
|
104
|
+
# whatever bug is being debugged.
|
|
105
|
+
passive_representation = True
|