xstack 2.0.2__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.
- xstack/__init__.py +167 -0
- xstack/address.py +137 -0
- xstack/app/__init__.py +36 -0
- xstack/app/__main__.py +5 -0
- xstack/app/application.py +692 -0
- xstack/app/config.py +139 -0
- xstack/app/delegate.py +292 -0
- xstack/app/demo.py +42 -0
- xstack/app/dialogs.py +492 -0
- xstack/app/log_view.py +632 -0
- xstack/app/menu.py +362 -0
- xstack/app/options.py +664 -0
- xstack/app/resources.py +20 -0
- xstack/app/runner.py +35 -0
- xstack/app/settings.py +89 -0
- xstack/app/tree.py +582 -0
- xstack/attributes.py +212 -0
- xstack/compat.py +96 -0
- xstack/component.py +1175 -0
- xstack/constants.py +16 -0
- xstack/stack.py +1078 -0
- xstack/test/__init__.py +0 -0
- xstack/test/components.py +360 -0
- xstack/test/test_stack.py +1314 -0
- xstack-2.0.2.dist-info/METADATA +162 -0
- xstack-2.0.2.dist-info/RECORD +29 -0
- xstack-2.0.2.dist-info/WHEEL +5 -0
- xstack-2.0.2.dist-info/licenses/LICENSE +21 -0
- xstack-2.0.2.dist-info/top_level.txt +1 -0
xstack/component.py
ADDED
|
@@ -0,0 +1,1175 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import uuid
|
|
3
|
+
import json
|
|
4
|
+
import copy
|
|
5
|
+
import typing
|
|
6
|
+
import logging
|
|
7
|
+
import traceback
|
|
8
|
+
import signalling
|
|
9
|
+
|
|
10
|
+
from . import stack
|
|
11
|
+
from .attributes import Option
|
|
12
|
+
from .attributes import Input
|
|
13
|
+
from .attributes import Output
|
|
14
|
+
from .constants import Status
|
|
15
|
+
|
|
16
|
+
log = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
# -- Default for Component.duplicate's parent argument, meaning "the same parent
|
|
19
|
+
# -- as the original". None cannot be used, as it means the root of the stack.
|
|
20
|
+
_SIBLING = object()
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# --------------------------------------------------------------------------------------
|
|
24
|
+
# noinspection PyUnresolvedReferences,PyMethodMayBeStatic
|
|
25
|
+
class Component:
|
|
26
|
+
"""
|
|
27
|
+
A component is a an item that can be added to the stack. Typically, a component
|
|
28
|
+
will expose various options and inputs and then execute some code utilising
|
|
29
|
+
those options and inputs.
|
|
30
|
+
|
|
31
|
+
When implementing a component, you should subclass from this Component class and
|
|
32
|
+
provide a unique identifier. Components are stored in a class factory thus the
|
|
33
|
+
identifier is used to differentiate the different components.
|
|
34
|
+
|
|
35
|
+
Key things you should re-implement when subclassing a component:
|
|
36
|
+
|
|
37
|
+
All the functions you may re-implement are at the top of this class. Please
|
|
38
|
+
do not alter/modify/re-implement any functionality below the warning line.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
# -- Please provide a unique identifier
|
|
42
|
+
identifier = ""
|
|
43
|
+
|
|
44
|
+
# -- This should resolve to an absolute path to a png file. If none is provide
|
|
45
|
+
# -- then a default icon will be given.
|
|
46
|
+
# -- To prevent hard coding the absolute path, you can use this syntax if your
|
|
47
|
+
# -- icon is in the same folder as your component .py:
|
|
48
|
+
# -- icon = os.path.join(os.path.dirname(__file__), "my_icon_name.png")
|
|
49
|
+
icon = ""
|
|
50
|
+
|
|
51
|
+
# -- Any component which is marked as deprecated will still be available
|
|
52
|
+
# -- from the factory but will typically not be visible in any ui's as
|
|
53
|
+
# -- a new component which can be added.
|
|
54
|
+
deprecated = False
|
|
55
|
+
|
|
56
|
+
# -- Previous identifiers of this component. Data saved with (and code asking
|
|
57
|
+
# -- for) any of these names resolves to this component, which always shows
|
|
58
|
+
# -- and saves with its current identifier. Add the old identifier here
|
|
59
|
+
# -- whenever you rename a component. This is unrelated to the deprecated
|
|
60
|
+
# -- flag above, which hides the component from the ui's.
|
|
61
|
+
deprecated_names: typing.List[str] = []
|
|
62
|
+
|
|
63
|
+
# -- The version of this component's serialised data. Increase it whenever
|
|
64
|
+
# -- a change to the component's inputs, options or additional data means
|
|
65
|
+
# -- older data needs converting, and implement migrate to convert it.
|
|
66
|
+
# -- Data saved before versioning existed is treated as version 1.
|
|
67
|
+
version = 1
|
|
68
|
+
|
|
69
|
+
# ----------------------------------------------------------------------------------
|
|
70
|
+
# This MUST be re-implemented
|
|
71
|
+
def run(self) -> bool:
|
|
72
|
+
"""
|
|
73
|
+
This should be re-implemented in your component and should contain all the
|
|
74
|
+
code which you want to trigger during the build.
|
|
75
|
+
|
|
76
|
+
Returning exactly False marks the component as Failed and stops the
|
|
77
|
+
build. Returning None or True (or anything else) is a success. Raising
|
|
78
|
+
an exception is also a failure.
|
|
79
|
+
"""
|
|
80
|
+
return True
|
|
81
|
+
|
|
82
|
+
# ----------------------------------------------------------------------------------
|
|
83
|
+
# You may re-implement this
|
|
84
|
+
@classmethod
|
|
85
|
+
def migrate(cls, data: dict, from_version: int) -> dict:
|
|
86
|
+
"""
|
|
87
|
+
Re-implement this to convert serialised data saved by an older version
|
|
88
|
+
of this component. It is called before the data is applied whenever
|
|
89
|
+
the stored version is lower than the class's version attribute.
|
|
90
|
+
|
|
91
|
+
The data is this component's serialised dictionary (component_type,
|
|
92
|
+
label, inputs, options, additional_data, children and so on). Change
|
|
93
|
+
only this component's own entries, as each child migrates itself.
|
|
94
|
+
|
|
95
|
+
:param data: A copy of the component's serialised data
|
|
96
|
+
:type data: dict
|
|
97
|
+
|
|
98
|
+
:param from_version: The version the data was saved with
|
|
99
|
+
:type from_version: int
|
|
100
|
+
|
|
101
|
+
:return: The data in the form the current version expects
|
|
102
|
+
:rtype: dict
|
|
103
|
+
"""
|
|
104
|
+
return data
|
|
105
|
+
|
|
106
|
+
# ----------------------------------------------------------------------------------
|
|
107
|
+
# You may re-implement this
|
|
108
|
+
def is_valid(self) -> bool:
|
|
109
|
+
"""
|
|
110
|
+
You can use this to test the validation of the options and requirements.
|
|
111
|
+
"""
|
|
112
|
+
return True
|
|
113
|
+
|
|
114
|
+
# ----------------------------------------------------------------------------------
|
|
115
|
+
# You may re-implement this
|
|
116
|
+
def option_widget(self, option_name: str) -> "PySide6.QWidget":
|
|
117
|
+
"""
|
|
118
|
+
This allows you to return a specific (or custom) QWidget to represent the
|
|
119
|
+
given option in any ui's.
|
|
120
|
+
|
|
121
|
+
This requires Qt, which is optional.
|
|
122
|
+
|
|
123
|
+
For example, your code here might look like this:
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
if option_name == "foobar":
|
|
127
|
+
return qute.QLineEdit()
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
If you do not want a specific option to be shown in the ui, you can do:
|
|
131
|
+
|
|
132
|
+
if option_name == "foobar":
|
|
133
|
+
return self.IGNORE_OPTION_FOR_UI
|
|
134
|
+
"""
|
|
135
|
+
return None
|
|
136
|
+
|
|
137
|
+
# ----------------------------------------------------------------------------------
|
|
138
|
+
# You may re-implement this
|
|
139
|
+
def input_widget(self, requirement_name: str) -> "PySide6.QWidget":
|
|
140
|
+
"""
|
|
141
|
+
This allows you to return a specific (or custom) QWidget to represent the
|
|
142
|
+
given requirement in any ui's.
|
|
143
|
+
|
|
144
|
+
This requires Qt, which is optional.
|
|
145
|
+
|
|
146
|
+
For example, your code here might look like this:
|
|
147
|
+
|
|
148
|
+
```
|
|
149
|
+
if requirement_name == "foobar":
|
|
150
|
+
return qute.QLineEdit()
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
If you do not want a specific requirement to be shown in the ui, you can do:
|
|
154
|
+
|
|
155
|
+
if requirement_name == "foobar":
|
|
156
|
+
return self.IGNORE_OPTION_FOR_UI
|
|
157
|
+
"""
|
|
158
|
+
return None
|
|
159
|
+
|
|
160
|
+
# ----------------------------------------------------------------------------------
|
|
161
|
+
def on_add_to_stack(self):
|
|
162
|
+
"""
|
|
163
|
+
Triggered when the component is first added to the stack
|
|
164
|
+
"""
|
|
165
|
+
return None
|
|
166
|
+
|
|
167
|
+
# ----------------------------------------------------------------------------------
|
|
168
|
+
def on_enter_stack(self):
|
|
169
|
+
"""
|
|
170
|
+
This allows you to run any code when this is added to the stack
|
|
171
|
+
|
|
172
|
+
:return:
|
|
173
|
+
"""
|
|
174
|
+
return None
|
|
175
|
+
|
|
176
|
+
# ----------------------------------------------------------------------------------
|
|
177
|
+
def on_removed_from_stack(self):
|
|
178
|
+
"""
|
|
179
|
+
This is triggered if this component is removed from the stack
|
|
180
|
+
|
|
181
|
+
:return:
|
|
182
|
+
"""
|
|
183
|
+
return None
|
|
184
|
+
|
|
185
|
+
# ----------------------------------------------------------------------------------
|
|
186
|
+
def on_serialise(self) -> dict:
|
|
187
|
+
"""
|
|
188
|
+
Re-implement this to store additional data alongside the component's
|
|
189
|
+
inputs and options when it is serialised. The returned dictionary must
|
|
190
|
+
be json serialisable.
|
|
191
|
+
|
|
192
|
+
It is stored in the component's serialised data under the
|
|
193
|
+
"additional_data" key, and passed back to on_deserialise when the
|
|
194
|
+
component is loaded.
|
|
195
|
+
|
|
196
|
+
:return: dict
|
|
197
|
+
"""
|
|
198
|
+
return dict()
|
|
199
|
+
|
|
200
|
+
# ----------------------------------------------------------------------------------
|
|
201
|
+
def on_deserialise(self, additional_data: dict) -> None:
|
|
202
|
+
"""
|
|
203
|
+
Re-implement this to restore the data returned by on_serialise.
|
|
204
|
+
|
|
205
|
+
It is called once the component has been added to the stack during a
|
|
206
|
+
deserialise (before its children are added), and when settings are
|
|
207
|
+
loaded onto the component.
|
|
208
|
+
|
|
209
|
+
:param additional_data: The "additional_data" dictionary stored for
|
|
210
|
+
this component. This is an empty dictionary if there is none, such
|
|
211
|
+
as for data saved before this was supported.
|
|
212
|
+
:type additional_data: dict
|
|
213
|
+
|
|
214
|
+
:return: None
|
|
215
|
+
"""
|
|
216
|
+
return None
|
|
217
|
+
|
|
218
|
+
# ----------------------------------------------------------------------------------
|
|
219
|
+
def on_build_finished(self, successful: bool) -> None:
|
|
220
|
+
"""
|
|
221
|
+
This is run when the build is complete and will pass bool declaring whether the
|
|
222
|
+
build was successful or not.
|
|
223
|
+
|
|
224
|
+
Args:
|
|
225
|
+
successful (bool): Whether the build was successful or not.
|
|
226
|
+
"""
|
|
227
|
+
pass
|
|
228
|
+
|
|
229
|
+
# ----------------------------------------------------------------------------------
|
|
230
|
+
def on_build_started(self) -> None:
|
|
231
|
+
"""
|
|
232
|
+
This is run immediately at the start of the build before any components have
|
|
233
|
+
their run functions called.
|
|
234
|
+
"""
|
|
235
|
+
pass
|
|
236
|
+
|
|
237
|
+
# ----------------------------------------------------------------------------------
|
|
238
|
+
def help(self):
|
|
239
|
+
"""
|
|
240
|
+
This function should be used to integrate any help mechanism you want to utilise
|
|
241
|
+
for your component.
|
|
242
|
+
|
|
243
|
+
Examples could include opening a url to a help webpage. Or loading a video or
|
|
244
|
+
other document.
|
|
245
|
+
"""
|
|
246
|
+
return None
|
|
247
|
+
|
|
248
|
+
# ----------------------------------------------------------------------------------
|
|
249
|
+
def user_functions(self) -> typing.Dict[str, callable]:
|
|
250
|
+
return dict()
|
|
251
|
+
|
|
252
|
+
# ----------------------------------------------------------------------------------
|
|
253
|
+
def mixin_functions(self) -> typing.Dict[str, callable]:
|
|
254
|
+
"""
|
|
255
|
+
Mixins implement this to contribute user facing actions to the component.
|
|
256
|
+
|
|
257
|
+
Do not call this to gather a component's actions - use
|
|
258
|
+
collated_mixin_functions, which combines the contributions of every
|
|
259
|
+
mixin in the hierarchy rather than just the first one the method
|
|
260
|
+
resolution order happens to find.
|
|
261
|
+
|
|
262
|
+
:return: Mapping of menu label to callable
|
|
263
|
+
:rtype: dict(str, callable)
|
|
264
|
+
"""
|
|
265
|
+
return dict()
|
|
266
|
+
|
|
267
|
+
# ---------------------------------------------------------------------------------#
|
|
268
|
+
# DO NOT RE-IMPLEMENT ANY FUNCTIONS BELOW THIS LINE #
|
|
269
|
+
# ---------------------------------------------------------------------------------#
|
|
270
|
+
|
|
271
|
+
# ----------------------------------------------------------------------------------
|
|
272
|
+
def collated_mixin_functions(self) -> typing.Dict[str, callable]:
|
|
273
|
+
"""
|
|
274
|
+
Returns the combined mixin_functions of every class in this component's
|
|
275
|
+
hierarchy.
|
|
276
|
+
|
|
277
|
+
Each class is asked for its own contribution rather than letting the
|
|
278
|
+
method resolution order pick a single winner, so a component can compose
|
|
279
|
+
any number of mixins, in any base order, without the mixins having to
|
|
280
|
+
know about one another.
|
|
281
|
+
|
|
282
|
+
:return: Mapping of menu label to callable
|
|
283
|
+
:rtype: dict(str, callable)
|
|
284
|
+
"""
|
|
285
|
+
collated = dict()
|
|
286
|
+
|
|
287
|
+
# -- Walk from the most base class forwards so that more derived
|
|
288
|
+
# -- contributions take precedence if two mixins offer the same label
|
|
289
|
+
for class_ in reversed(type(self).__mro__):
|
|
290
|
+
|
|
291
|
+
# -- Read straight off the class rather than using getattr, as that
|
|
292
|
+
# -- would find inherited implementations and call the same one once
|
|
293
|
+
# -- per subclass
|
|
294
|
+
function = class_.__dict__.get("mixin_functions")
|
|
295
|
+
|
|
296
|
+
if not function:
|
|
297
|
+
continue
|
|
298
|
+
|
|
299
|
+
collated.update(function(self))
|
|
300
|
+
|
|
301
|
+
return collated
|
|
302
|
+
|
|
303
|
+
# ----------------------------------------------------------------------------------
|
|
304
|
+
def __init__(self, label: str, stack: "stack.Stack", uuid_: typing.Optional[str] = None):
|
|
305
|
+
super().__init__()
|
|
306
|
+
|
|
307
|
+
self.stack: "stack.Stack" = stack
|
|
308
|
+
|
|
309
|
+
self._label: str = label
|
|
310
|
+
self._uuid: str = uuid_ or str(uuid.uuid4())
|
|
311
|
+
self._inputs: typing.List[Input] = list()
|
|
312
|
+
self._options: typing.List[Option] = list()
|
|
313
|
+
self._outputs: typing.List[Output] = list()
|
|
314
|
+
self._status: str = "not executed"
|
|
315
|
+
self._enabled = True
|
|
316
|
+
self._last_error: str = ""
|
|
317
|
+
|
|
318
|
+
# -- Hierarchy attributes
|
|
319
|
+
self.parent = None
|
|
320
|
+
self.children = []
|
|
321
|
+
|
|
322
|
+
# -- Declare our signals so that other mechanisms can tie into the events
|
|
323
|
+
# -- of our component
|
|
324
|
+
self.build_started = signalling.Signal()
|
|
325
|
+
self.build_complete = signalling.Signal()
|
|
326
|
+
self.changed = signalling.Signal()
|
|
327
|
+
|
|
328
|
+
def set_parent(self, parent=None, child_index=None):
|
|
329
|
+
|
|
330
|
+
# -- A component which already has a place in the stack is being moved,
|
|
331
|
+
# -- which changes the build order. That has to be reported as a change,
|
|
332
|
+
# -- or listeners which save the stack (such as aniseed's recipe on the
|
|
333
|
+
# -- rig) never hear of it. A component being placed for the first time
|
|
334
|
+
# -- is not reported here, as add_component reports the addition itself.
|
|
335
|
+
moving = self.parent is not None or self in self.stack.root_components
|
|
336
|
+
|
|
337
|
+
# -- Ensure we're removed as a root component
|
|
338
|
+
if self in self.stack.root_components:
|
|
339
|
+
self.stack.root_components.remove(self)
|
|
340
|
+
|
|
341
|
+
# -- Remove ourself from our current parent if we have
|
|
342
|
+
# -- one
|
|
343
|
+
if self.parent:
|
|
344
|
+
self.parent.children.remove(self)
|
|
345
|
+
|
|
346
|
+
self.parent = parent
|
|
347
|
+
# -- If we're given a component to be a child of, then
|
|
348
|
+
# -- we perform our reparenting otherwise we add ourselves
|
|
349
|
+
# -- as a root component
|
|
350
|
+
if isinstance(parent, Component):
|
|
351
|
+
# -- Add ourself as a child of the component
|
|
352
|
+
if child_index is not None:
|
|
353
|
+
parent.children.insert(child_index, self)
|
|
354
|
+
else:
|
|
355
|
+
parent.children.append(self)
|
|
356
|
+
else:
|
|
357
|
+
if child_index is not None:
|
|
358
|
+
self.stack.root_components.insert(child_index, self)
|
|
359
|
+
else:
|
|
360
|
+
self.stack.root_components.append(self)
|
|
361
|
+
|
|
362
|
+
if moving:
|
|
363
|
+
self.changed.emit()
|
|
364
|
+
|
|
365
|
+
def child_index(self):
|
|
366
|
+
if self.parent:
|
|
367
|
+
return self.parent.children.index(self)
|
|
368
|
+
else:
|
|
369
|
+
return self.stack.root_components.index(self)
|
|
370
|
+
|
|
371
|
+
# ----------------------------------------------------------------------------------
|
|
372
|
+
def wrapped_run(self) -> bool:
|
|
373
|
+
"""
|
|
374
|
+
This should NOT be re-implemented. This is used specifically by the Stack class
|
|
375
|
+
to instigate your implement function whilst wrapped in the signal and
|
|
376
|
+
logging mechanisms.
|
|
377
|
+
|
|
378
|
+
The component is marked as Failed if run raises an exception or returns
|
|
379
|
+
exactly False. Anything else (including None) is a success. The reason
|
|
380
|
+
for a failure is available from last_error.
|
|
381
|
+
"""
|
|
382
|
+
self.build_started.emit()
|
|
383
|
+
|
|
384
|
+
self._last_error = ""
|
|
385
|
+
result = True
|
|
386
|
+
|
|
387
|
+
try:
|
|
388
|
+
if self.run() is False:
|
|
389
|
+
self._last_error = f"{self.label()} reported that it failed (its run returned False)"
|
|
390
|
+
log.warning(self._last_error)
|
|
391
|
+
result = False
|
|
392
|
+
|
|
393
|
+
except Exception:
|
|
394
|
+
self._last_error = traceback.format_exc()
|
|
395
|
+
log.exception(f"{self.label()} raised an exception during its run")
|
|
396
|
+
result = False
|
|
397
|
+
|
|
398
|
+
self.set_status(Status.Success if result else Status.Failed)
|
|
399
|
+
self.build_complete.emit()
|
|
400
|
+
|
|
401
|
+
return result
|
|
402
|
+
|
|
403
|
+
# ----------------------------------------------------------------------------------
|
|
404
|
+
def last_error(self) -> str:
|
|
405
|
+
"""
|
|
406
|
+
Returns the reason the last run of this component failed. This is the
|
|
407
|
+
traceback text if run raised, a short message if run returned False,
|
|
408
|
+
or an empty string if the last run succeeded (or it has not run).
|
|
409
|
+
"""
|
|
410
|
+
return self._last_error
|
|
411
|
+
|
|
412
|
+
# ----------------------------------------------------------------------------------
|
|
413
|
+
def is_enabled(self):
|
|
414
|
+
"""
|
|
415
|
+
This will return whether the component is flagged as enabled. Any component
|
|
416
|
+
that is not enabled will not be executed.
|
|
417
|
+
"""
|
|
418
|
+
|
|
419
|
+
return self._enabled
|
|
420
|
+
|
|
421
|
+
# ----------------------------------------------------------------------------------
|
|
422
|
+
def set_enabled(self, value):
|
|
423
|
+
"""
|
|
424
|
+
Set the enabled state of the component. Any component that has this set to
|
|
425
|
+
false will not be executed.
|
|
426
|
+
|
|
427
|
+
A disabled component will also not execute any of its children.
|
|
428
|
+
"""
|
|
429
|
+
self._enabled = value
|
|
430
|
+
self.changed.emit()
|
|
431
|
+
|
|
432
|
+
# ----------------------------------------------------------------------------------
|
|
433
|
+
def status(self) -> str:
|
|
434
|
+
"""
|
|
435
|
+
This will return the current status string for the component
|
|
436
|
+
"""
|
|
437
|
+
if not self.is_enabled():
|
|
438
|
+
return Status.Disabled
|
|
439
|
+
|
|
440
|
+
return self._status
|
|
441
|
+
|
|
442
|
+
# ----------------------------------------------------------------------------------
|
|
443
|
+
def set_status(self, status: str):
|
|
444
|
+
"""
|
|
445
|
+
This will set the status of this component. Generally you should leave the Stack
|
|
446
|
+
class to call this.
|
|
447
|
+
"""
|
|
448
|
+
self._status = status
|
|
449
|
+
|
|
450
|
+
# ----------------------------------------------------------------------------------
|
|
451
|
+
def uuid(self) -> str:
|
|
452
|
+
"""
|
|
453
|
+
All component instances have a completely unique identifier - allowing the build
|
|
454
|
+
process and the Stack to keep track of all component instances. This function will
|
|
455
|
+
return this.
|
|
456
|
+
"""
|
|
457
|
+
return self._uuid
|
|
458
|
+
|
|
459
|
+
# ----------------------------------------------------------------------------------
|
|
460
|
+
def label(self) -> str:
|
|
461
|
+
"""
|
|
462
|
+
Returns the label for the component. The label generally has no bearing on the
|
|
463
|
+
building of the component, but it allows for the user to keep track of what
|
|
464
|
+
components are and their use cases.
|
|
465
|
+
"""
|
|
466
|
+
return self._label
|
|
467
|
+
|
|
468
|
+
# ----------------------------------------------------------------------------------
|
|
469
|
+
def set_label(self, label: str):
|
|
470
|
+
"""
|
|
471
|
+
Sets the label of the component. Note that this will trigger a change event
|
|
472
|
+
"""
|
|
473
|
+
self._label = label
|
|
474
|
+
self.changed.emit()
|
|
475
|
+
|
|
476
|
+
# ----------------------------------------------------------------------------------
|
|
477
|
+
def suggested_label(self):
|
|
478
|
+
return self.identifier
|
|
479
|
+
|
|
480
|
+
# ----------------------------------------------------------------------------------
|
|
481
|
+
def declare_input(
|
|
482
|
+
self, name: str,
|
|
483
|
+
value: typing.Any = None,
|
|
484
|
+
description: str = "",
|
|
485
|
+
validate=True,
|
|
486
|
+
group=None,
|
|
487
|
+
should_inherit=False,
|
|
488
|
+
pre_expose=False,
|
|
489
|
+
hidden=False,
|
|
490
|
+
):
|
|
491
|
+
"""
|
|
492
|
+
This will add a requirement to the component, which will allow a user to
|
|
493
|
+
set the requirement and allow for your inmplement function to query that
|
|
494
|
+
value.
|
|
495
|
+
|
|
496
|
+
Args:
|
|
497
|
+
name (str): The name of the requirement.
|
|
498
|
+
|
|
499
|
+
value (Any): The value of the requirement.
|
|
500
|
+
|
|
501
|
+
description (str): The description of the requirement to be presented to
|
|
502
|
+
the user.
|
|
503
|
+
|
|
504
|
+
validate (bool): Whether or not the requirement should be validated prior
|
|
505
|
+
to its component being executed.
|
|
506
|
+
|
|
507
|
+
group (str): If given, this requirement will be grouped together in any
|
|
508
|
+
ui's with requirements of the same group.
|
|
509
|
+
|
|
510
|
+
should_inherit (bool): Whether or not the requirement should inherit
|
|
511
|
+
its value from its parent when it is initialised.
|
|
512
|
+
|
|
513
|
+
pre_expose (bool): Whether or not the requirement should be exposed. This
|
|
514
|
+
is to help inform ui's as to whether the user should be presented
|
|
515
|
+
with this requirement before the component is created.
|
|
516
|
+
|
|
517
|
+
hidden (bool): Whether or not the requirement should be hidden.
|
|
518
|
+
"""
|
|
519
|
+
input_ = Input(
|
|
520
|
+
name=name,
|
|
521
|
+
value=value,
|
|
522
|
+
validate=validate,
|
|
523
|
+
description=description,
|
|
524
|
+
group=group,
|
|
525
|
+
should_inherit=should_inherit,
|
|
526
|
+
pre_expose=pre_expose,
|
|
527
|
+
hidden=hidden,
|
|
528
|
+
component=self,
|
|
529
|
+
)
|
|
530
|
+
|
|
531
|
+
self._inputs.append(input_)
|
|
532
|
+
|
|
533
|
+
# -- Trickle the change event of this requirement to the component level
|
|
534
|
+
input_.value_changed.connect(self.changed.emit)
|
|
535
|
+
|
|
536
|
+
# ----------------------------------------------------------------------------------
|
|
537
|
+
def declare_option(
|
|
538
|
+
self,
|
|
539
|
+
name: str,
|
|
540
|
+
value: typing.Any,
|
|
541
|
+
description: str = "",
|
|
542
|
+
group=None,
|
|
543
|
+
should_inherit=False,
|
|
544
|
+
pre_expose=False,
|
|
545
|
+
hidden=False,
|
|
546
|
+
):
|
|
547
|
+
"""
|
|
548
|
+
This will add an option to the component, which will allow a user to
|
|
549
|
+
set the option and allow for your inmplement function to query that
|
|
550
|
+
value.
|
|
551
|
+
|
|
552
|
+
Args:
|
|
553
|
+
name (str): The name of the option.
|
|
554
|
+
|
|
555
|
+
value (Any): The value of the option.
|
|
556
|
+
|
|
557
|
+
description (str): The description of the option to be presented to
|
|
558
|
+
the user.
|
|
559
|
+
|
|
560
|
+
group (str): If given, this option will be grouped together in any
|
|
561
|
+
ui's with options of the same group.
|
|
562
|
+
|
|
563
|
+
should_inherit (bool): Whether or not the option should inherit
|
|
564
|
+
its value from its parent when it is initialised.
|
|
565
|
+
|
|
566
|
+
pre_expose (bool): Whether or not the option should be exposed. This
|
|
567
|
+
is to help inform ui's as to whether the user should be presented
|
|
568
|
+
with this option before the component is created.
|
|
569
|
+
|
|
570
|
+
hidden (bool): Whether or not the option should be hidden.
|
|
571
|
+
"""
|
|
572
|
+
option = Option(
|
|
573
|
+
name=name,
|
|
574
|
+
value=value,
|
|
575
|
+
description=description,
|
|
576
|
+
group=group,
|
|
577
|
+
should_inherit=should_inherit,
|
|
578
|
+
pre_expose=pre_expose,
|
|
579
|
+
hidden=hidden,
|
|
580
|
+
component=self,
|
|
581
|
+
)
|
|
582
|
+
|
|
583
|
+
self._options.append(option)
|
|
584
|
+
|
|
585
|
+
# -- Trickle the change event of the option to the component level
|
|
586
|
+
option.value_changed.connect(self.changed.emit)
|
|
587
|
+
|
|
588
|
+
# ----------------------------------------------------------------------------------
|
|
589
|
+
def declare_output(self, name: str, description: str = "", group=None, is_default=False):
|
|
590
|
+
"""
|
|
591
|
+
An output is a guaranteed value that a component will fill in during its
|
|
592
|
+
run, and can be looked up by other components.
|
|
593
|
+
|
|
594
|
+
This mechanism allows for components to dynamically resolve values from
|
|
595
|
+
other components rather than have them explicitely set
|
|
596
|
+
"""
|
|
597
|
+
output = Output(
|
|
598
|
+
name=name,
|
|
599
|
+
description=description,
|
|
600
|
+
component=self,
|
|
601
|
+
is_default=is_default,
|
|
602
|
+
group=group,
|
|
603
|
+
)
|
|
604
|
+
self._outputs.append(output)
|
|
605
|
+
|
|
606
|
+
# ----------------------------------------------------------------------------------
|
|
607
|
+
def default_output(self):
|
|
608
|
+
"""
|
|
609
|
+
This will return the output that is marked as default. If one is not defined then
|
|
610
|
+
None will be returned.
|
|
611
|
+
"""
|
|
612
|
+
for output_plug in self._outputs:
|
|
613
|
+
if output_plug.is_default():
|
|
614
|
+
return output_plug
|
|
615
|
+
return None
|
|
616
|
+
|
|
617
|
+
# ----------------------------------------------------------------------------------
|
|
618
|
+
def inputs(self) -> typing.List[Input]:
|
|
619
|
+
"""
|
|
620
|
+
Returns a list of the inputs for this component
|
|
621
|
+
"""
|
|
622
|
+
return self._inputs
|
|
623
|
+
|
|
624
|
+
# ----------------------------------------------------------------------------------
|
|
625
|
+
def input(self, name: str) -> typing.Optional[Input]:
|
|
626
|
+
"""
|
|
627
|
+
Returns the Input object for the given requirement name
|
|
628
|
+
"""
|
|
629
|
+
for input_ in self._inputs:
|
|
630
|
+
if input_.name() == name:
|
|
631
|
+
return input_
|
|
632
|
+
|
|
633
|
+
return None
|
|
634
|
+
|
|
635
|
+
# ----------------------------------------------------------------------------------
|
|
636
|
+
def options(self) -> typing.List[Option]:
|
|
637
|
+
"""
|
|
638
|
+
Returns a list of theoptions for this component
|
|
639
|
+
"""
|
|
640
|
+
return self._options
|
|
641
|
+
|
|
642
|
+
# ----------------------------------------------------------------------------------
|
|
643
|
+
def option(self, name: str) -> typing.Optional[Option]:
|
|
644
|
+
"""
|
|
645
|
+
Returns the Option object for the given option name
|
|
646
|
+
"""
|
|
647
|
+
for option_ in self._options:
|
|
648
|
+
if option_.name() == name:
|
|
649
|
+
return option_
|
|
650
|
+
|
|
651
|
+
return None
|
|
652
|
+
|
|
653
|
+
# ----------------------------------------------------------------------------------
|
|
654
|
+
def outputs(self) -> typing.List[Output]:
|
|
655
|
+
"""
|
|
656
|
+
Returns a list of outputs for this component
|
|
657
|
+
Returns:
|
|
658
|
+
|
|
659
|
+
"""
|
|
660
|
+
return self._outputs
|
|
661
|
+
|
|
662
|
+
# ----------------------------------------------------------------------------------
|
|
663
|
+
def output(self, name: str) -> typing.Optional[Output]:
|
|
664
|
+
"""
|
|
665
|
+
Returns the Option object for the given option name
|
|
666
|
+
"""
|
|
667
|
+
for output_ in self._outputs:
|
|
668
|
+
if output_.name() == name:
|
|
669
|
+
return output_
|
|
670
|
+
|
|
671
|
+
return None
|
|
672
|
+
|
|
673
|
+
def remove_output(self, name: str) -> None:
|
|
674
|
+
output_ = self.output(name)
|
|
675
|
+
self._outputs.remove(output_)
|
|
676
|
+
|
|
677
|
+
# ----------------------------------------------------------------------------------
|
|
678
|
+
def describe(self):
|
|
679
|
+
"""
|
|
680
|
+
This will print a nicely formatted description of the component
|
|
681
|
+
"""
|
|
682
|
+
lines = [
|
|
683
|
+
f"Component Type : {self.identifier}",
|
|
684
|
+
f" Identifier : {self.uuid()}",
|
|
685
|
+
" Options :",
|
|
686
|
+
]
|
|
687
|
+
|
|
688
|
+
value_limit = 200
|
|
689
|
+
|
|
690
|
+
option_len = max(
|
|
691
|
+
[
|
|
692
|
+
len(option.name())
|
|
693
|
+
for option in self.options()
|
|
694
|
+
] or [0]
|
|
695
|
+
)
|
|
696
|
+
|
|
697
|
+
for option in self.options():
|
|
698
|
+
resolved_value = str(self.option(option.name()).get())
|
|
699
|
+
if len(resolved_value) > value_limit:
|
|
700
|
+
resolved_value = resolved_value[:value_limit - 3] + "..."
|
|
701
|
+
|
|
702
|
+
lines.append(f" {option.name().ljust(option_len + 2, ' ')} : {resolved_value}")
|
|
703
|
+
|
|
704
|
+
lines.append(" Inputs :")
|
|
705
|
+
|
|
706
|
+
inputs_len = max(
|
|
707
|
+
[
|
|
708
|
+
len(input_.name())
|
|
709
|
+
for input_ in self.inputs()
|
|
710
|
+
] or [0]
|
|
711
|
+
)
|
|
712
|
+
|
|
713
|
+
for input_ in self.inputs():
|
|
714
|
+
resolved_value = str(self.input(input_.name()).get())
|
|
715
|
+
if len(resolved_value) > value_limit:
|
|
716
|
+
resolved_value = resolved_value[:value_limit - 3] + "..."
|
|
717
|
+
|
|
718
|
+
lines.append(f" {input_.name().ljust(inputs_len + 2, ' ')} : {resolved_value}")
|
|
719
|
+
|
|
720
|
+
# -- This is part of the build report rather than diagnostics, so it is
|
|
721
|
+
# -- printed as is. A log record only prefixes its first line, which
|
|
722
|
+
# -- breaks the column alignment.
|
|
723
|
+
print("\n".join(lines))
|
|
724
|
+
|
|
725
|
+
# ----------------------------------------------------------------------------------
|
|
726
|
+
def describe_outputs(self):
|
|
727
|
+
"""
|
|
728
|
+
This will print the current value of each output, as part of the build
|
|
729
|
+
report (see describe)
|
|
730
|
+
"""
|
|
731
|
+
lines = [" Outputs :"]
|
|
732
|
+
value_limit = 200
|
|
733
|
+
|
|
734
|
+
output_len = max(
|
|
735
|
+
[
|
|
736
|
+
len(output.name())
|
|
737
|
+
for output in self.outputs()
|
|
738
|
+
] or [0]
|
|
739
|
+
)
|
|
740
|
+
|
|
741
|
+
for output in self.outputs():
|
|
742
|
+
resolved_value = str(self.output(output.name()).get())
|
|
743
|
+
if len(resolved_value) > value_limit:
|
|
744
|
+
resolved_value = resolved_value[:value_limit - 3] + "..."
|
|
745
|
+
|
|
746
|
+
lines.append(f" {output.name().ljust(output_len + 2, ' ')} : {resolved_value}")
|
|
747
|
+
|
|
748
|
+
print("\n".join(lines))
|
|
749
|
+
|
|
750
|
+
# ----------------------------------------------------------------------------------
|
|
751
|
+
def serialise(self) -> dict:
|
|
752
|
+
"""
|
|
753
|
+
This will serialise down the complete state of the component into a dictionary
|
|
754
|
+
format that is json serialisable
|
|
755
|
+
"""
|
|
756
|
+
option_data = dict()
|
|
757
|
+
input_data = dict()
|
|
758
|
+
|
|
759
|
+
for option in self.options():
|
|
760
|
+
serialised_data = option.serialise()
|
|
761
|
+
option_data[serialised_data["name"]] = serialised_data["value"]
|
|
762
|
+
|
|
763
|
+
for input_ in self.inputs():
|
|
764
|
+
serialised_data = input_.serialise()
|
|
765
|
+
input_data[serialised_data["name"]] = serialised_data["value"]
|
|
766
|
+
|
|
767
|
+
return dict(
|
|
768
|
+
component_type=self.identifier,
|
|
769
|
+
version=self.version,
|
|
770
|
+
inputs=input_data,
|
|
771
|
+
options=option_data,
|
|
772
|
+
label=self.label(),
|
|
773
|
+
uuid=self.uuid(),
|
|
774
|
+
enabled=self.is_enabled(),
|
|
775
|
+
additional_data=self.serialise_additional_data(),
|
|
776
|
+
children=[
|
|
777
|
+
child.serialise()
|
|
778
|
+
for child in self.children
|
|
779
|
+
],
|
|
780
|
+
)
|
|
781
|
+
|
|
782
|
+
# ----------------------------------------------------------------------------------
|
|
783
|
+
def serialise_additional_data(self) -> dict:
|
|
784
|
+
"""
|
|
785
|
+
Returns the result of on_serialise. As on_serialise is third party
|
|
786
|
+
code, any failure is printed and an empty dictionary returned, so one
|
|
787
|
+
component cannot stop the whole stack from being serialised.
|
|
788
|
+
|
|
789
|
+
:return: dict
|
|
790
|
+
"""
|
|
791
|
+
# noinspection PyBroadException
|
|
792
|
+
try:
|
|
793
|
+
return self.on_serialise() or dict()
|
|
794
|
+
|
|
795
|
+
except Exception:
|
|
796
|
+
log.exception(f"on_serialise failed for {self.label()} ({self.identifier})")
|
|
797
|
+
return dict()
|
|
798
|
+
|
|
799
|
+
# ----------------------------------------------------------------------------------
|
|
800
|
+
def deserialise_additional_data(self, additional_data: dict) -> None:
|
|
801
|
+
"""
|
|
802
|
+
Passes the given additional data to on_deserialise. As on_deserialise
|
|
803
|
+
is third party code, any failure is printed rather than raised, so one
|
|
804
|
+
component cannot stop the rest of the stack from loading.
|
|
805
|
+
|
|
806
|
+
:param additional_data: The "additional_data" dictionary stored for
|
|
807
|
+
this component, or None
|
|
808
|
+
:type additional_data: dict
|
|
809
|
+
"""
|
|
810
|
+
# noinspection PyBroadException
|
|
811
|
+
try:
|
|
812
|
+
self.on_deserialise(additional_data or dict())
|
|
813
|
+
|
|
814
|
+
except Exception:
|
|
815
|
+
log.exception(f"on_deserialise failed for {self.label()} ({self.identifier})")
|
|
816
|
+
|
|
817
|
+
# ----------------------------------------------------------------------------------
|
|
818
|
+
def save_settings(self, filepath):
|
|
819
|
+
"""
|
|
820
|
+
This will store this components serialised data to a json file
|
|
821
|
+
|
|
822
|
+
Args:
|
|
823
|
+
filepath: Absolute filepath to write to
|
|
824
|
+
"""
|
|
825
|
+
data = self.serialise()
|
|
826
|
+
|
|
827
|
+
with open(filepath, "w") as f:
|
|
828
|
+
json.dump(
|
|
829
|
+
data,
|
|
830
|
+
f,
|
|
831
|
+
indent=4,
|
|
832
|
+
sort_keys=True,
|
|
833
|
+
)
|
|
834
|
+
|
|
835
|
+
# ----------------------------------------------------------------------------------
|
|
836
|
+
def load_settings(self, filepath):
|
|
837
|
+
"""
|
|
838
|
+
This will load a json file of settings and attempt to set its own
|
|
839
|
+
inputs and options based on that data
|
|
840
|
+
|
|
841
|
+
Args:
|
|
842
|
+
filepath: Aboslute filepath to the settings file to load
|
|
843
|
+
"""
|
|
844
|
+
with open(filepath, "r") as f:
|
|
845
|
+
data = json.load(f)
|
|
846
|
+
|
|
847
|
+
for name, value in data.get("inputs", {}).items():
|
|
848
|
+
input_ = self.input(name)
|
|
849
|
+
if input_:
|
|
850
|
+
input_.set(value)
|
|
851
|
+
|
|
852
|
+
for name, value in data.get("options", {}).items():
|
|
853
|
+
option = self.option(name)
|
|
854
|
+
if option:
|
|
855
|
+
option.set(value)
|
|
856
|
+
|
|
857
|
+
self.deserialise_additional_data(data.get("additional_data"))
|
|
858
|
+
|
|
859
|
+
self.changed.emit()
|
|
860
|
+
|
|
861
|
+
# ----------------------------------------------------------------------------------
|
|
862
|
+
def import_subtree(self, filepath: str) -> None:
|
|
863
|
+
"""
|
|
864
|
+
Load a serialised subtree from a JSON file and add the contents
|
|
865
|
+
as children of this component. UUIDs are regenerated to avoid
|
|
866
|
+
collisions; labels are preserved.
|
|
867
|
+
|
|
868
|
+
Convenience wrapper for ``self.stack.import_subtree_under(
|
|
869
|
+
self, filepath)``.
|
|
870
|
+
|
|
871
|
+
:param filepath: Absolute path to a JSON file produced by
|
|
872
|
+
``save_settings()`` or ``Stack.save()``.
|
|
873
|
+
"""
|
|
874
|
+
self.stack.import_subtree_under(parent=self, filepath=filepath)
|
|
875
|
+
|
|
876
|
+
# ----------------------------------------------------------------------------------
|
|
877
|
+
@classmethod
|
|
878
|
+
def _substituted_value(
|
|
879
|
+
cls,
|
|
880
|
+
expression: "re.Pattern",
|
|
881
|
+
replace_with: str,
|
|
882
|
+
value: typing.Any,
|
|
883
|
+
) -> typing.Any:
|
|
884
|
+
"""
|
|
885
|
+
Returns the given value with the expression substituted throughout,
|
|
886
|
+
recursing into lists, tuples and dictionaries. Dictionary keys are
|
|
887
|
+
substituted as well as their values, as names are just as likely to be
|
|
888
|
+
held as keys. Anything which is not a string or a container is returned
|
|
889
|
+
untouched.
|
|
890
|
+
|
|
891
|
+
:param expression: The compiled pattern to search for
|
|
892
|
+
:type expression: re.Pattern
|
|
893
|
+
|
|
894
|
+
:param replace_with: String to substitute each match with
|
|
895
|
+
:type replace_with: str
|
|
896
|
+
|
|
897
|
+
:param value: The value to perform the substitution within
|
|
898
|
+
:type value: any
|
|
899
|
+
|
|
900
|
+
:return: The value with all substitutions applied
|
|
901
|
+
:rtype: any
|
|
902
|
+
"""
|
|
903
|
+
if isinstance(value, str):
|
|
904
|
+
return expression.sub(replace_with, value)
|
|
905
|
+
|
|
906
|
+
if isinstance(value, dict):
|
|
907
|
+
return {
|
|
908
|
+
cls._substituted_value(expression, replace_with, key):
|
|
909
|
+
cls._substituted_value(expression, replace_with, item)
|
|
910
|
+
for key, item in value.items()
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
if isinstance(value, (list, tuple)):
|
|
914
|
+
substituted = [
|
|
915
|
+
cls._substituted_value(expression, replace_with, item)
|
|
916
|
+
for item in value
|
|
917
|
+
]
|
|
918
|
+
|
|
919
|
+
# -- Preserve whether we were given a mutable sequence or not
|
|
920
|
+
return tuple(substituted) if isinstance(value, tuple) else substituted
|
|
921
|
+
|
|
922
|
+
return value
|
|
923
|
+
|
|
924
|
+
# ----------------------------------------------------------------------------------
|
|
925
|
+
def search_and_replace(self, search_for: str, replace_with: str) -> None:
|
|
926
|
+
"""
|
|
927
|
+
Applies a regex search and replace across the values of every input and
|
|
928
|
+
option on this component.
|
|
929
|
+
|
|
930
|
+
Values are traversed recursively, so strings nested within lists, tuples
|
|
931
|
+
and dictionaries are substituted too, including dictionary keys. Values
|
|
932
|
+
which are not strings are left as they are.
|
|
933
|
+
|
|
934
|
+
:param search_for: The pattern to search for. This is treated as a
|
|
935
|
+
regular expression.
|
|
936
|
+
:type search_for: str
|
|
937
|
+
|
|
938
|
+
:param replace_with: The string to substitute each match with
|
|
939
|
+
:type replace_with: str
|
|
940
|
+
|
|
941
|
+
:return: None
|
|
942
|
+
"""
|
|
943
|
+
expression = re.compile(search_for)
|
|
944
|
+
self.set_label(self._substituted_value(expression, replace_with, self.label()))
|
|
945
|
+
|
|
946
|
+
for attribute in self.inputs() + self.options():
|
|
947
|
+
|
|
948
|
+
# -- Read the unresolved value, otherwise an attribute pointing at
|
|
949
|
+
# -- another component would have that component's value baked in
|
|
950
|
+
# -- as a literal rather than keeping its address
|
|
951
|
+
value = attribute.get(resolved=False)
|
|
952
|
+
|
|
953
|
+
substituted = self._substituted_value(expression, replace_with, value)
|
|
954
|
+
|
|
955
|
+
# -- Only write back where something actually changed, so we do not
|
|
956
|
+
# -- emit change events for untouched attributes
|
|
957
|
+
if substituted != value:
|
|
958
|
+
attribute.set(substituted)
|
|
959
|
+
|
|
960
|
+
# ----------------------------------------------------------------------------------
|
|
961
|
+
def duplicate(
|
|
962
|
+
self,
|
|
963
|
+
input_overrides=None,
|
|
964
|
+
option_overrides=None,
|
|
965
|
+
include_children=True,
|
|
966
|
+
search_for=None,
|
|
967
|
+
replace_with=None,
|
|
968
|
+
parent=_SIBLING,
|
|
969
|
+
):
|
|
970
|
+
"""
|
|
971
|
+
Create a duplicate of this component in the stack.
|
|
972
|
+
|
|
973
|
+
By default the entire subtree is duplicated — this component and all
|
|
974
|
+
its descendants — and parented as a sibling of the original. Pass
|
|
975
|
+
``include_children=False`` to copy only this component without its
|
|
976
|
+
children.
|
|
977
|
+
|
|
978
|
+
``input_overrides`` and ``option_overrides`` apply only to the
|
|
979
|
+
top-level duplicate; descendants keep their original values.
|
|
980
|
+
|
|
981
|
+
``search_for`` and ``replace_with`` apply a regex search and replace
|
|
982
|
+
across the duplicate's label, input and option values once the copy
|
|
983
|
+
and any overrides are in place. It is applied to every duplicated
|
|
984
|
+
descendant too.
|
|
985
|
+
|
|
986
|
+
``parent`` is the component the duplicate is parented under (None for
|
|
987
|
+
the root of the stack). By default it is the original's parent, so the
|
|
988
|
+
duplicate is a sibling of the original.
|
|
989
|
+
|
|
990
|
+
Each duplicate is parented under its final parent before its
|
|
991
|
+
on_enter_stack runs, so anything on_enter_stack does with the parent
|
|
992
|
+
(such as Attribute.hook_to_parent) sees the duplicated parent rather
|
|
993
|
+
than the original.
|
|
994
|
+
"""
|
|
995
|
+
if parent is _SIBLING:
|
|
996
|
+
parent = self.parent
|
|
997
|
+
|
|
998
|
+
# -- Instance the new component
|
|
999
|
+
new_component = self.stack.add_component(
|
|
1000
|
+
component_type=self.identifier,
|
|
1001
|
+
label=self.label(),
|
|
1002
|
+
suppress_events=True,
|
|
1003
|
+
)
|
|
1004
|
+
|
|
1005
|
+
# -- Copy all the data
|
|
1006
|
+
new_component.copy(self)
|
|
1007
|
+
|
|
1008
|
+
# -- Apply any overrides (top-level only)
|
|
1009
|
+
for name, value in (input_overrides or dict()).items():
|
|
1010
|
+
new_component.input(name).set(value)
|
|
1011
|
+
for name, value in (option_overrides or dict()).items():
|
|
1012
|
+
new_component.option(name).set(value)
|
|
1013
|
+
|
|
1014
|
+
# -- Re-name any data carried over from the source, now that the copy
|
|
1015
|
+
# -- and any overrides are in place
|
|
1016
|
+
if search_for is not None and replace_with is not None:
|
|
1017
|
+
new_component.search_and_replace(
|
|
1018
|
+
search_for=search_for,
|
|
1019
|
+
replace_with=replace_with,
|
|
1020
|
+
)
|
|
1021
|
+
|
|
1022
|
+
# -- Parent the component before its events are triggered, so that
|
|
1023
|
+
# -- on_enter_stack sees the parent it will actually live under
|
|
1024
|
+
new_component.set_parent(
|
|
1025
|
+
parent=parent,
|
|
1026
|
+
)
|
|
1027
|
+
|
|
1028
|
+
# -- Trigger its events
|
|
1029
|
+
new_component.on_enter_stack()
|
|
1030
|
+
|
|
1031
|
+
self.stack.component_added.emit(new_component)
|
|
1032
|
+
|
|
1033
|
+
# -- Recursively duplicate the children directly under the new
|
|
1034
|
+
# -- component. Snapshot self.children so the iteration is not affected
|
|
1035
|
+
# -- by any changes to the stack during the duplication.
|
|
1036
|
+
if include_children:
|
|
1037
|
+
for child in list(self.children):
|
|
1038
|
+
child.duplicate(
|
|
1039
|
+
include_children=True,
|
|
1040
|
+
search_for=search_for,
|
|
1041
|
+
replace_with=replace_with,
|
|
1042
|
+
parent=new_component,
|
|
1043
|
+
)
|
|
1044
|
+
|
|
1045
|
+
return new_component
|
|
1046
|
+
|
|
1047
|
+
# ----------------------------------------------------------------------------------
|
|
1048
|
+
def copy(self, other_component):
|
|
1049
|
+
"""
|
|
1050
|
+
This method will copy all the settings from the given component to this
|
|
1051
|
+
component.
|
|
1052
|
+
|
|
1053
|
+
Args:
|
|
1054
|
+
other_component: Stack Component to copy settings from
|
|
1055
|
+
|
|
1056
|
+
Returns:
|
|
1057
|
+
|
|
1058
|
+
"""
|
|
1059
|
+
for input_ in self.inputs():
|
|
1060
|
+
input_to_copy = other_component.input(input_.name())
|
|
1061
|
+
|
|
1062
|
+
if input_to_copy:
|
|
1063
|
+
self.input(input_.name()).set(
|
|
1064
|
+
copy.deepcopy(input_to_copy.get(resolved=False)),
|
|
1065
|
+
)
|
|
1066
|
+
|
|
1067
|
+
for option in self.options():
|
|
1068
|
+
option_to_copy = other_component.option(option.name())
|
|
1069
|
+
|
|
1070
|
+
if option_to_copy:
|
|
1071
|
+
self.option(option.name()).set(
|
|
1072
|
+
copy.deepcopy(option_to_copy.get(resolved=False)),
|
|
1073
|
+
)
|
|
1074
|
+
|
|
1075
|
+
self.set_label(other_component.label())
|
|
1076
|
+
self.set_enabled(other_component.is_enabled())
|
|
1077
|
+
|
|
1078
|
+
# ----------------------------------------------------------------------------------
|
|
1079
|
+
def documentation(self):
|
|
1080
|
+
if not self.__doc__:
|
|
1081
|
+
return ""
|
|
1082
|
+
|
|
1083
|
+
lines = []
|
|
1084
|
+
|
|
1085
|
+
for line in self.__doc__.split("\n"):
|
|
1086
|
+
line = line.strip()
|
|
1087
|
+
|
|
1088
|
+
if not line:
|
|
1089
|
+
line = "\n"
|
|
1090
|
+
|
|
1091
|
+
lines.append(line)
|
|
1092
|
+
|
|
1093
|
+
return " ".join(lines)
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
# --------------------------------------------------------------------------------------
|
|
1097
|
+
class MissingComponent(Component):
|
|
1098
|
+
"""
|
|
1099
|
+
This stands in for a component whose type is not available in the component
|
|
1100
|
+
library, such as one from a package that is not installed or one that has
|
|
1101
|
+
since been removed.
|
|
1102
|
+
|
|
1103
|
+
It keeps all of the data it was loaded from and serialises it back unchanged,
|
|
1104
|
+
so saving the stack does not lose anything. It cannot be built: it fails
|
|
1105
|
+
validation, naming the missing component type. Remove it, switch it to an
|
|
1106
|
+
available component type, or make the missing type available to build.
|
|
1107
|
+
"""
|
|
1108
|
+
|
|
1109
|
+
identifier = "Missing Component"
|
|
1110
|
+
|
|
1111
|
+
# -- This is never offered as a component to add
|
|
1112
|
+
deprecated = True
|
|
1113
|
+
|
|
1114
|
+
# ----------------------------------------------------------------------------------
|
|
1115
|
+
def __init__(
|
|
1116
|
+
self,
|
|
1117
|
+
label: str,
|
|
1118
|
+
stack: "stack.Stack",
|
|
1119
|
+
uuid_: typing.Optional[str] = None,
|
|
1120
|
+
missing_identifier: str = "",
|
|
1121
|
+
data: typing.Optional[dict] = None,
|
|
1122
|
+
):
|
|
1123
|
+
super().__init__(label=label, stack=stack, uuid_=uuid_)
|
|
1124
|
+
|
|
1125
|
+
# -- Show the missing type wherever a component's type is displayed,
|
|
1126
|
+
# -- whilst keeping it distinct from the real type so lookups by type
|
|
1127
|
+
# -- never mistake this for a working component
|
|
1128
|
+
self.identifier = f"{MissingComponent.identifier} : {missing_identifier}"
|
|
1129
|
+
|
|
1130
|
+
self.missing_identifier: str = missing_identifier
|
|
1131
|
+
self._data: dict = copy.deepcopy(data or dict())
|
|
1132
|
+
|
|
1133
|
+
# ----------------------------------------------------------------------------------
|
|
1134
|
+
def is_valid(self) -> bool:
|
|
1135
|
+
log.warning(
|
|
1136
|
+
f"{self.label()} cannot be built because its component type "
|
|
1137
|
+
f"'{self.missing_identifier}' is not available. Remove it, switch it "
|
|
1138
|
+
f"to an available component type, or install what provides it."
|
|
1139
|
+
)
|
|
1140
|
+
return False
|
|
1141
|
+
|
|
1142
|
+
# ----------------------------------------------------------------------------------
|
|
1143
|
+
def run(self) -> bool:
|
|
1144
|
+
raise RuntimeError(
|
|
1145
|
+
f"{self.label()} cannot be built because its component type "
|
|
1146
|
+
f"'{self.missing_identifier}' is not available."
|
|
1147
|
+
)
|
|
1148
|
+
|
|
1149
|
+
# ----------------------------------------------------------------------------------
|
|
1150
|
+
def serialise(self) -> dict:
|
|
1151
|
+
"""
|
|
1152
|
+
Returns the data this was loaded from, updated with anything that can
|
|
1153
|
+
be changed on a missing component: its label, uuid, enabled state and
|
|
1154
|
+
children.
|
|
1155
|
+
"""
|
|
1156
|
+
data = copy.deepcopy(self._data)
|
|
1157
|
+
data["label"] = self.label()
|
|
1158
|
+
data["uuid"] = self.uuid()
|
|
1159
|
+
|
|
1160
|
+
if "enabled" in data or not self.is_enabled():
|
|
1161
|
+
data["enabled"] = self.is_enabled()
|
|
1162
|
+
|
|
1163
|
+
data["children"] = [
|
|
1164
|
+
child.serialise()
|
|
1165
|
+
for child in self.children
|
|
1166
|
+
]
|
|
1167
|
+
return data
|
|
1168
|
+
|
|
1169
|
+
# ----------------------------------------------------------------------------------
|
|
1170
|
+
def duplicate(self, *args, **kwargs):
|
|
1171
|
+
log.warning(
|
|
1172
|
+
f"{self.label()} cannot be duplicated because its component type "
|
|
1173
|
+
f"'{self.missing_identifier}' is not available."
|
|
1174
|
+
)
|
|
1175
|
+
return None
|