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/app/menu.py ADDED
@@ -0,0 +1,362 @@
1
+ import functools
2
+ from Qt import QtWidgets, QtGui, QtCore
3
+
4
+
5
+ def show(item, app_config, app, parent):
6
+ menu = TreeMenu(
7
+ item=item,
8
+ app_config=app_config,
9
+ app=app,
10
+ parent=parent,
11
+ )
12
+ menu.popup(QtGui.QCursor().pos())
13
+
14
+
15
+ class TreeMenu(QtWidgets.QMenu):
16
+
17
+ def __init__(self, item=None, app_config=None, app=None, parent=None):
18
+ super().__init__("", parent=parent)
19
+
20
+ # -- Store our inputs
21
+ self._parent = parent
22
+ self.app = app
23
+ self.item = item
24
+ self.component = getattr(item, "component", None)
25
+ self.app_config = app_config
26
+
27
+ # -- Determine our labels
28
+ self.component_label = self.app_config.component_label
29
+ self.execute_label = self.app_config.execute_label
30
+ self.validate_label = "validate"
31
+
32
+ # -- Determine some icons up front
33
+ self.execute_icon = QtGui.QIcon(self.app_config.execute_icon)
34
+ self.validate_icon = QtGui.QIcon(self.app_config.validate_icon)
35
+
36
+ if self.component:
37
+ self.populate_component_menu()
38
+ else:
39
+ self.populate_unfocused_menu()
40
+
41
+ def populate_component_menu(self):
42
+ read_only = self.app_config.read_only_structure
43
+
44
+ # -- Start by adding our build and validation menus
45
+ self.add_build_menu()
46
+ self.add_validate_menu()
47
+
48
+ # -- Add the items which allow a user to edit/alter a component
49
+ if not read_only:
50
+ self.add_component_interaction_items()
51
+
52
+ # -- Add any actions the component provides
53
+ self.add_component_component_actions()
54
+
55
+ # -- Add the menu which exposes the lesser used
56
+ # -- items
57
+ if not read_only:
58
+ self.add_additional_actions()
59
+
60
+ # -- Offer to show the component's part of the latest build's log
61
+ self.add_show_in_log_action()
62
+
63
+ # -- Finally we add the help menu item
64
+ help_action = QtWidgets.QAction(f"Help", self._parent)
65
+ help_action.triggered.connect(
66
+ functools.partial(
67
+ self.app.tree_widget.help_for_component,
68
+ item=self.item,
69
+ ),
70
+ )
71
+ self.addAction(help_action)
72
+
73
+ def add_show_in_log_action(self):
74
+ log_view = getattr(self.app, "log_view", None)
75
+
76
+ # -- Apps built with the log tab turned off have nothing to show
77
+ if not log_view:
78
+ return
79
+
80
+ show_in_log_action = QtWidgets.QAction("Show In Log", self._parent)
81
+ show_in_log_action.triggered.connect(
82
+ functools.partial(
83
+ self.app.show_component_in_log,
84
+ self.component,
85
+ ),
86
+ )
87
+
88
+ # -- Only components the latest build reached are in the log
89
+ show_in_log_action.setEnabled(log_view.has_component(self.component))
90
+ self.addAction(show_in_log_action)
91
+
92
+ def add_additional_actions(self):
93
+
94
+ # -- Add the rename, wrapped in a separator
95
+ self.addSeparator()
96
+
97
+ # -- Build menu
98
+ actions_menu = QtWidgets.QMenu("Actions")
99
+
100
+ # -- Action to enable/disable a component
101
+ enable_disable_action = QtWidgets.QAction(f"Enable/Disable", self._parent)
102
+ enable_disable_action.triggered.connect(
103
+ functools.partial(
104
+ self.app.tree_widget.toggle_enable,
105
+ item=self.item,
106
+ ),
107
+ )
108
+ actions_menu.addAction(enable_disable_action)
109
+
110
+ # -- Action to enable/disable a component
111
+ search_and_replace_action = QtWidgets.QAction(f"Search And Replace", self._parent)
112
+ search_and_replace_action.triggered.connect(
113
+ functools.partial(
114
+ self.app.tree_widget.search_and_replace_component,
115
+ item=self.item,
116
+ ),
117
+ )
118
+ actions_menu.addAction(search_and_replace_action)
119
+
120
+
121
+ # -- Action to export the component (and its subtree) to a JSON file
122
+ export_action = QtWidgets.QAction(f"Export to File", self._parent)
123
+ export_action.triggered.connect(
124
+ functools.partial(
125
+ self.app.tree_widget.save_component_settings,
126
+ item=self.item,
127
+ ),
128
+ )
129
+ actions_menu.addAction(export_action)
130
+
131
+ # -- Action to apply just this component's inputs/options from a JSON file
132
+ apply_settings_action = QtWidgets.QAction(f"Apply Settings from File", self._parent)
133
+ apply_settings_action.triggered.connect(
134
+ functools.partial(
135
+ self.app.tree_widget.load_component_settings,
136
+ item=self.item,
137
+ ),
138
+ )
139
+ actions_menu.addAction(apply_settings_action)
140
+
141
+ # -- Action to load a JSON file and add its contents as children
142
+ # -- of this component, regenerating UUIDs on load.
143
+ import_subtree_action = QtWidgets.QAction(f"Import as Children", self._parent)
144
+ import_subtree_action.triggered.connect(
145
+ functools.partial(
146
+ self.app.tree_widget.import_subtree,
147
+ item=self.item,
148
+ ),
149
+ )
150
+ actions_menu.addAction(import_subtree_action)
151
+
152
+ # -- Finally we add the menu
153
+ self.addMenu(actions_menu)
154
+
155
+ def add_component_component_actions(self):
156
+
157
+ # -- If we have no user facing actions we can skip this
158
+ user_functions = self.component.user_functions()
159
+ mixin_functions = self.component.collated_mixin_functions()
160
+
161
+ # -- If we have no functions from either then we return
162
+ if not user_functions and not mixin_functions:
163
+ return
164
+
165
+ # -- We collate the two dictionaries into one, and we always allow
166
+ # -- the user functions to override the mixin functions
167
+ collated_functions = dict()
168
+
169
+ collated_functions.update(mixin_functions)
170
+ collated_functions.update(user_functions)
171
+
172
+ # -- Start with a separator
173
+ self.addSeparator()
174
+
175
+ # -- Cycle the user functions, adding each one
176
+ for label, func_ in collated_functions.items():
177
+ action = QtWidgets.QAction(label, self)
178
+ action.triggered.connect(func_)
179
+ self.addAction(action)
180
+
181
+ def add_component_interaction_items(self):
182
+
183
+ # -- Add the rename, wrapped in a separator
184
+ self.addSeparator()
185
+ rename_action = QtWidgets.QAction(f"Rename {self.app_config.component_label}", self._parent)
186
+ rename_action.triggered.connect(
187
+ functools.partial(
188
+ self.app.tree_widget.rename_component,
189
+ item=self.item,
190
+ ),
191
+ )
192
+ self.addAction(rename_action)
193
+ self.addSeparator()
194
+
195
+ # -- Add
196
+ add_action = QtWidgets.QAction(f"Add {self.app_config.component_label}", self._parent)
197
+ add_action.triggered.connect(
198
+ functools.partial(
199
+ self.app.tree_widget.add_component,
200
+ parent=self.component,
201
+ ),
202
+ )
203
+ self.addAction(add_action)
204
+
205
+ # -- Remove
206
+ remove_action = QtWidgets.QAction(f"Remove {self.app_config.component_label}", self._parent)
207
+ remove_action.triggered.connect(
208
+ functools.partial(
209
+ self.app.tree_widget.remove_component,
210
+ item=self.item,
211
+ ),
212
+ )
213
+ self.addAction(remove_action)
214
+
215
+ # -- Duplicate
216
+ duplicate_action = QtWidgets.QAction(f"Duplicate {self.app_config.component_label}", self._parent)
217
+ duplicate_action.triggered.connect(
218
+ functools.partial(
219
+ self.app.tree_widget.duplicate_component,
220
+ item=self.item,
221
+ ),
222
+ )
223
+ self.addAction(duplicate_action)
224
+
225
+ # -- Switch Component
226
+ switch_action = QtWidgets.QAction(f"Change {self.app_config.component_label} Type", self._parent)
227
+ switch_action.triggered.connect(
228
+ functools.partial(
229
+ self.app.tree_widget.switch_component_type,
230
+ item=self.item,
231
+ ),
232
+ )
233
+ self.addAction(switch_action)
234
+
235
+ def _add_build_actions(self, menu, label, validate_only=False):
236
+ """
237
+ Adds the build actions the app config allows for the component to the
238
+ given menu. Returns how many were added.
239
+ """
240
+ allowed = self.app_config.component_build_actions
241
+ added = 0
242
+
243
+ # -- Build Below (default)
244
+ if "section" in allowed:
245
+ action = QtWidgets.QAction(f"{label} Section", self._parent)
246
+ action.triggered.connect(
247
+ functools.partial(
248
+ self.app.build,
249
+ build_below=self.component,
250
+ validate_only=validate_only,
251
+ ),
252
+ )
253
+ menu.addAction(action)
254
+ added += 1
255
+
256
+ # -- All other build actions are less used, so lets add a separator
257
+ if added and ("up_to" in allowed or "only" in allowed):
258
+ menu.addSeparator()
259
+
260
+ # -- Build Up To
261
+ if "up_to" in allowed:
262
+ action = QtWidgets.QAction(f"{label} Up To This", self._parent)
263
+ action.triggered.connect(
264
+ functools.partial(
265
+ self.app.build,
266
+ build_up_to=self.component,
267
+ validate_only=validate_only,
268
+ ),
269
+ )
270
+ menu.addAction(action)
271
+ added += 1
272
+
273
+ # -- Build Only.
274
+ if "only" in allowed:
275
+ action = QtWidgets.QAction(f"{label} Just This", self._parent)
276
+ action.triggered.connect(
277
+ functools.partial(
278
+ self.app.build,
279
+ build_only=self.component,
280
+ validate_only=validate_only,
281
+ ),
282
+ )
283
+ menu.addAction(action)
284
+ added += 1
285
+
286
+ return added
287
+
288
+ def add_build_menu(self):
289
+ """
290
+ Creates the menu for building the given component
291
+ """
292
+ # -- Build menu
293
+ build_menu = QtWidgets.QMenu(self.execute_label, self)
294
+
295
+ # -- Add the build menu, if it offers anything
296
+ if self._add_build_actions(build_menu, self.execute_label):
297
+ self.addMenu(build_menu)
298
+
299
+ def add_validate_menu(self):
300
+ """
301
+ Creates the menu for building the given component
302
+ """
303
+ # -- validate menu
304
+ validate_menu = QtWidgets.QMenu(self.validate_label, self)
305
+
306
+ # -- Add the validate menu, if it offers anything
307
+ if self._add_build_actions(validate_menu, self.validate_label, validate_only=True):
308
+ self.addMenu(validate_menu)
309
+
310
+ def populate_unfocused_menu(self):
311
+
312
+ if self.app_config.show_stack_build_actions:
313
+ # -- Item to instigate an execution of the stag
314
+ label = f"{self.execute_label} {self.app_config.label}"
315
+ build_action = QtWidgets.QAction(self.execute_icon, label, self._parent)
316
+ build_action.triggered.connect(functools.partial(self.app.build))
317
+ self.addAction(build_action)
318
+
319
+ # -- This item will allow a user to validate the stack
320
+ label = f"{self.validate_label} {self.app_config.label}"
321
+ validate_action = QtWidgets.QAction(self.validate_icon, label, self._parent)
322
+ validate_action.triggered.connect(
323
+ functools.partial(
324
+ self.app.build,
325
+ validate_only=True,
326
+ ),
327
+ )
328
+ self.addAction(validate_action)
329
+
330
+ # -- Add a separator to visually distinguish the execution
331
+ # -- items
332
+ self.addSeparator()
333
+
334
+ # -- Nothing below changes or describes the structure, which a read
335
+ # -- only structure does not offer
336
+ if self.app_config.read_only_structure:
337
+ return
338
+
339
+ # -- Now add the functionality to add a component
340
+ label = f"Add {self.component_label}"
341
+ add_action = QtWidgets.QAction(QtGui.QIcon(self.app_config.add_icon), label, self._parent)
342
+ add_action.triggered.connect(
343
+ functools.partial(
344
+ self.app.tree_widget.add_component,
345
+ parent=None,
346
+ ),
347
+ )
348
+ self.addAction(add_action)
349
+
350
+ # -- Add a separator to visually distinguish the execution
351
+ # -- items
352
+ self.addSeparator()
353
+
354
+ # -- Now add the functionality to add a component
355
+ label = f"Describe"
356
+ add_action = QtWidgets.QAction("Describe", self._parent)
357
+ add_action.triggered.connect(
358
+ functools.partial(
359
+ self.app.tree_widget.describe,
360
+ ),
361
+ )
362
+ self.addAction(add_action)