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 ADDED
@@ -0,0 +1,167 @@
1
+ """
2
+ xstack is a hierarchical component execution framework. Components as classes
3
+ which expose parameters (Requirements and Options) and implement a run method
4
+ which is exectuted.
5
+
6
+ The stack itself is a hierarchical list, where a component can be made a child
7
+ or parent of other components. The execution of the stack will always respect
8
+ the hierarchical order.
9
+
10
+ The stack can be triggered to execute the whole stack, all children of a particular
11
+ component (including itself) or just a single component.
12
+
13
+ Component classes are searched for through an abstract factory. You can declare
14
+ folder paths using the XSTACK_COMPONENT_PATHS environment variable, or you can
15
+ register component classes directly using any of the following:
16
+
17
+ ```
18
+ stack = Stack()
19
+ stack.component_library.register_path(path)
20
+ stack.component_library.register_module(path_to_py_file)
21
+ stack.component_library.register_item(component_class)
22
+ ```
23
+
24
+ Components can be super simple or quite complex depending on its needs. Here is an
25
+ example of a very simple Component:
26
+
27
+ ```
28
+ import xstack
29
+
30
+ class MyComponent(xstack.Component):
31
+
32
+ identifier = "MyCustomComponent"
33
+
34
+ def run(self):
35
+ print("Running in my custom component")
36
+ ```
37
+
38
+ However, a component can also expose Requirements and Options. Any external code
39
+ or tools should only ever interact with the component through these two methods.
40
+
41
+ Requirements and Options are almost identical at a code level, but conceptually they
42
+ are quite different. A Requirement is considered to be an expected input needed in
43
+ order to evaluate the component whilst an Option is considered to be something that
44
+ alters how the end result my appear.
45
+
46
+ At a code level, the only different is that Requirements can be set to always
47
+ validate, and if they do not have a value set for them they will fail validation
48
+ during the building of the stack.
49
+
50
+ Here is an example of a component using options and requirements.
51
+
52
+ ```
53
+ class ResprayVehicle(xstack.Component):
54
+ identifier = "respray_vehicle"
55
+
56
+ def __init__(self, *args, **kwargs):
57
+ super().__init__(*args, **kwargs)
58
+
59
+ self.declare_requirement(
60
+ name="vehicle_instance",
61
+ value=None,
62
+ validate=True,
63
+ )
64
+
65
+ self.declare_option(
66
+ name="color",
67
+ value="orange",
68
+ )
69
+
70
+ def run(self):
71
+ vehicle = self.requirement("vehicle_instance").get()
72
+ color = self.option("color").get()
73
+
74
+ print(f"respraying {vehicle} to be {color}")
75
+ ```
76
+ In this example we see that we have exposed "vehicle_instance" as a requirement as
77
+ in this instance, we always need a vehicle to paint. We then expose the color as an
78
+ option with a default value.
79
+
80
+ Registering the components with the stack does not add them to the stack, it just
81
+ makes them available to the stack. You can add components to the stack using the
82
+ following syntax:
83
+
84
+ ```
85
+ stack = Stack()
86
+
87
+ stack.add_component(
88
+ label="Example",
89
+ component_type="MyCustomComponentIdentifier",
90
+ )
91
+ ```
92
+
93
+ All components must inherit from xstack.Component, and therefore you will need to
94
+ provide your class with an identifier. This is the component_type.
95
+
96
+ Once a component has been added to the stack you can execute that stack using
97
+
98
+ stack.build()
99
+
100
+ Stacks are hierarchical, so you can add multiple components and form a hierarchical
101
+ relationship, such as...
102
+
103
+ ```
104
+ stack = Stack()
105
+
106
+ # -- Create three components, they will execute in the order
107
+ # -- they are added by default
108
+ component_a = stack.add_component(
109
+ label="A",
110
+ component_type="MyCustomComponent",
111
+ )
112
+
113
+ component_b = stack.add_component(
114
+ label="B",
115
+ component_type="MyCustomComponent",
116
+ )
117
+
118
+ component_c = stack.add_component(
119
+ label="C",
120
+ component_type="MyCustomComponent",
121
+ )
122
+
123
+ # -- At the moment, the order is completely flat, and we can choose
124
+ # -- to change that order by doing this. Component A was added first, so it
125
+ # -- would have a build position of zero by default. By setting it to 1 it means
126
+ # -- that component b will now move to the top of the list, and component a will
127
+ # -- execute after it
128
+ stack.set_build_position(
129
+ component_a,
130
+ index=1,
131
+ )
132
+
133
+ # -- As well as changing the order, we can make it hierarchical. Lets make
134
+ # -- component_a be a child of component_c. By doing this we can always guarantee
135
+ # -- that component_a will always execute after component_c, even if component_c
136
+ # -- is moved in the hierarchy.
137
+ stack.set_build_position(
138
+ component_a,
139
+ parent=component_c,
140
+ )
141
+ ```
142
+ Whilst a hierarchical structure in this form can always be flattened - and is always
143
+ executed in a linear fashion, it can make for a much more intuitive format for
144
+ categories of executions.
145
+ """
146
+ # -- Expose the stack. This is the main class of the module and handles the
147
+ # -- execution of the components
148
+ from .stack import Stack
149
+
150
+ # -- Expose the Component. All components must ultimately inherit from this class
151
+ from .component import Component
152
+ from .component import MissingComponent
153
+
154
+ from . import address
155
+ from . import constants
156
+
157
+ # -- Expose the app if Qt is available. We do not fail the module
158
+ # -- load if Qt is not present as we allow xstack to run headless
159
+ try:
160
+ from . import app
161
+ from .app.demo import launch_demo
162
+ from .app import launch
163
+
164
+ except ImportError:
165
+ pass
166
+
167
+ __version__ = "2.0.2"
xstack/address.py ADDED
@@ -0,0 +1,137 @@
1
+ """
2
+ This module handles the logic around resolving and constructing attribute
3
+ addresses within a stack.
4
+
5
+ Addresses always take the form of
6
+
7
+ [COMPONENT LABEL].[CATEGORY].[ATTRIBUTE NAME]
8
+ """
9
+ import re
10
+
11
+ ADDRESS_REGEX = re.compile(r"\[.*\]\.\[(option|input|output)\]\.\[.*\]")
12
+
13
+
14
+ # --------------------------------------------------------------------------------------
15
+ def get_label(address):
16
+ """
17
+ Returns the label part of the address
18
+ """
19
+ try:
20
+ return address.split(".")[0][1:-1]
21
+
22
+ except IndexError:
23
+ return None
24
+
25
+
26
+ # --------------------------------------------------------------------------------------
27
+ def get_category(address):
28
+ """
29
+ Returns the category (or type) part of the address
30
+ """
31
+ try:
32
+ return address.split(".")[1][1:-1]
33
+
34
+ except IndexError:
35
+ return None
36
+
37
+
38
+
39
+ # --------------------------------------------------------------------------------------
40
+ def get_attribute_name(address):
41
+ """
42
+ Returns the attribute name from the address
43
+ """
44
+ try:
45
+ return address.split(".")[2][1:-1]
46
+
47
+ except IndexError:
48
+ return None
49
+
50
+
51
+ # --------------------------------------------------------------------------------------
52
+ def get_attribute(address, stack):
53
+ """
54
+ This will convert an address to an actual attribute within the stack
55
+
56
+ Args:
57
+ address: Address to resolve
58
+ stack: Stack to get attribute from
59
+
60
+ Returns:
61
+ xstack.Attribute
62
+ """
63
+
64
+ # -- Break the address into its component parts
65
+ component_label = get_label(address)
66
+ category = get_category(address)
67
+ attribute_name = get_attribute_name(address)
68
+
69
+ # -- Attempt to find a matching component
70
+ component = _get_component_with_label(
71
+ component_label,
72
+ stack,
73
+ )
74
+
75
+ # -- Switch out what we are looking for based on the classification
76
+ if category == "option":
77
+ return component.option(attribute_name)
78
+
79
+ if category == "input":
80
+ return component.input(attribute_name)
81
+
82
+ if category == "output":
83
+ return component.output(attribute_name)
84
+
85
+ return None
86
+
87
+
88
+ # --------------------------------------------------------------------------------------
89
+ def form_address(attribute, category):
90
+ """
91
+ Given an attribute, we will form an address from it
92
+ """
93
+ return f"[{attribute.component().label()}].[{category}].[{attribute.name()}]"
94
+
95
+
96
+ # --------------------------------------------------------------------------------------
97
+ def is_address(address):
98
+ """
99
+ This will test whether the value being given is recognised as an address
100
+ """
101
+ if isinstance(address, str) and ADDRESS_REGEX.search(address):
102
+ return True
103
+
104
+ return False
105
+
106
+
107
+ # --------------------------------------------------------------------------------------
108
+ def _get_component_with_label(label, stack):
109
+ """
110
+ This is a private funciton that cycles the components of a stack looking
111
+ for the first component with a matching label
112
+ """
113
+ for component in stack.components():
114
+ if component.label() == label:
115
+ return component
116
+
117
+ return None
118
+
119
+
120
+ # --------------------------------------------------------------------------------------
121
+ def _get_uuid_from_label(label, stack):
122
+ """
123
+ Given a component label, this will return the uuid of that component
124
+ """
125
+ component = _get_component_with_label(label, stack)
126
+ return component.uuid() if component else None
127
+
128
+
129
+ # --------------------------------------------------------------------------------------
130
+ def _is_uuid(data):
131
+ """
132
+ Convenience function to check whether the data we are given is a uuid formed string
133
+ """
134
+ if str(data).count("-") == 4 and len(str(data)) == 36:
135
+ return True
136
+
137
+ return False
xstack/app/__init__.py ADDED
@@ -0,0 +1,36 @@
1
+ """
2
+ The xstack_app is a Qt based ui application which allows a user to create
3
+ new stack instances and populate them with components as well as executing
4
+ the stack.
5
+
6
+ xstack is considered a library rather than a tool, so the likelyhood is that
7
+ you will want to import this module into your own and then tailor the ui to
8
+ look at your own locations for components.
9
+
10
+ This can be done using the AppConfig class. You can subclass this and use it
11
+ to tell the AppWindow/AppWidget where it should look for components. The config
12
+ class also allows you to alter the terminology used in the application to be
13
+ more meaningful to your use cases.
14
+
15
+ To test this module you can simply run:
16
+
17
+ ```
18
+ import xstack_app
19
+ xstack_app.launch()
20
+ ```
21
+
22
+ Alternatively, if you want to test the app with some example components which are
23
+ used by the unit tests of xstack, you can run this:
24
+
25
+ ```
26
+ import xstack_app
27
+ xstack_app.launch_demo()
28
+ ```
29
+ """
30
+ from .application import launch
31
+ from .application import AppWindow
32
+ from .application import AppWidget
33
+
34
+ from .config import AppConfig
35
+ from . import application
36
+ from . import resources
xstack/app/__main__.py ADDED
@@ -0,0 +1,5 @@
1
+ import xstack
2
+
3
+
4
+ if __name__ == '__main__':
5
+ xstack.launch_demo()