From d68f2673dab910ba14b757b6c1273d17e7478404 Mon Sep 17 00:00:00 2001 From: aardschok Date: Mon, 4 Sep 2017 18:05:44 +0200 Subject: [PATCH] updated load_from_configuration function --- python/scriptsmenu/scriptsmenu.py | 112 ++++++++++------------ samples/sample_configuration_a.json | 144 ++++++++++++++++++---------- samples/sample_configuration_b.json | 65 +++++++++---- 3 files changed, 187 insertions(+), 134 deletions(-) diff --git a/python/scriptsmenu/scriptsmenu.py b/python/scriptsmenu/scriptsmenu.py index 750cff0..a704404 100644 --- a/python/scriptsmenu/scriptsmenu.py +++ b/python/scriptsmenu/scriptsmenu.py @@ -1,7 +1,6 @@ import json import logging import os -from collections import OrderedDict from .vendor.Qt import QtWidgets, QtCore @@ -86,7 +85,7 @@ def create_default_items(self): separator = self.addSeparator() separator.setObjectName("separator") - def add_menu(self, parent, title): + def add_menu(self, title, parent=None): """ Create a sub menu for a parent widget @@ -99,6 +98,9 @@ def add_menu(self, parent, title): :return: QtWidget.QMenu instance """ + if not parent: + parent = self + menu = QtWidgets.QMenu(parent, title) menu.setTitle(title) menu.setObjectName(title) @@ -188,6 +190,50 @@ def add_script(self, parent, title, command, sourcetype, icon=None, return script_action + def load_from_configuration(self, parent, configuration): + """Process the configurations and store the configuration + + This creates all submenus from a configuration.json file. + + When the configuration holds the key `main` all scripts under `main` will + be added to the main menu first before adding the rest + + Args: + parent (ScriptsMenu): script menu instance + configuration (list): A ScriptsMenu configuration list + """ + + print "DEBUG : {}".format(parent.title()) + + for item in configuration: + assert isinstance(item, dict), "Configuration is wrong!" + + # skip items which have no `type` key + item_type = item.get('type', None) + if not item_type: + log.warning("Missing 'type' from configuration item") + continue + + # add separator + # Special behavior for separators + if item_type == "separator": + parent.addSeparator() + + # add submenu + # items should hold a collection of submenu items (dict) + elif item_type == "menu": + assert "items" in item, "Menu is missing 'items' key" + menu = self.add_menu(parent=parent, title=item["title"]) + self.load_from_configuration(menu, item["items"]) + + # add script + elif item_type == "action": + # filter out `type` from the item dict + config = {key: value for key, value in + item.items() if key != "type"} + + self.add_script(parent=parent, **config) + def set_update_visible(self, state): self.update_action.setVisible(state) @@ -230,19 +276,6 @@ def _update_search(self, search): action.setVisible(visible) -def create_submenu(menu, script, parent, items): - - title = script["title"] - submenu = menu.add_menu(parent=parent, title=title) - for item in items: - assert isinstance(script, dict), "Configuration is wrong!" - if item['title'] == "separator": - submenu.addSeparator() - continue - - menu.add_script(parent=submenu, **item) - - def load_configuration(path): if not os.path.isfile(path): @@ -256,58 +289,11 @@ def load_configuration(path): # retrieve and store config with open(path, "r") as f: - data = json.load(f) - - # check if configuration has an specific order - order = data.get("order", None) - if order is not None: - configuration = OrderedDict() - for key in order: - configuration[key] = data[key] - else: - configuration = data + configuration = json.load(f) return configuration -def load_from_configuration(menu, configuration): - """Process the configurations and store the configuration - - This creates all submenus from a configuration.json file. - - When the configuration holds the key `main` all scripts under `main` will - be added to the main menu first before adding the rest - - Args: - menu (QtGui.QMenu): menu instance - configuration (dict): A ScriptsMenu configuration dictionary - """ - - # todo: improve for-loop, too complex - - for section, scripts in configuration.items(): - - if section == "main": - parent_menu = menu - else: - parent_menu = menu.add_menu(parent=menu, title=section) - - for script in scripts: - assert isinstance(script, dict), "Configuration is wrong!" - # Special behavior for separators - if script['title'] == "separator": - menu.addSeparator() - continue - - # items should hold a collection of submenu items (dict) - items = script.get("items", None) - if items: - create_submenu(menu, script, parent_menu, items) - continue - - menu.add_script(parent=parent_menu, **script) - - def application(configuration, parent): import sys app = QtWidgets.QApplication(sys.argv) diff --git a/samples/sample_configuration_a.json b/samples/sample_configuration_a.json index 1918163..eeb7991 100644 --- a/samples/sample_configuration_a.json +++ b/samples/sample_configuration_a.json @@ -1,61 +1,105 @@ -{"Animation": [{"title" : "Script A", +[ + { + "title": "Animation", + "type": "menu", + "items": [ + { + "title": "Script A", "tooltip": "Script A", "command": "$SCRIPTMENU/script_a.py", "sourcetype": "file", - "tags": ["test", "script", "cluster"], + "tags": [ + "test", + "script", + "cluster" + ], "icon": "$SCRIPTMENU/resources/script_a.png", - "label": "SCR A"}, - {"title" : "Script B", + "label": "SCR A" + }, + { + "title": "Script B", "tooltip": "Run script B", "command": "$SCRIPTMENU/script_b.py", "sourcetype": "file", - "tags": ["test", "script", "curves"]}, - {"title" : "Script C", + "tags": [ + "test", + "script", + "curves" + ] + }, + { + "title": "Script C", "tooltip": "Run script C", "command": "$SCRIPTMENU/script_c.py", "sourcetype": "file", - "tags": ["test", "script", "joints"], - "icon": ""} - ], - -"Modeling": [{"title" : "Script A", - "tooltip": "Run script A", - "command": "$SCRIPTMENU/script_a.py", - "sourcetype": "file", - "tags": ["test", "script", "model", "blendshapes"]}, - {"title" : "Script B", - "tooltip": "Run script B", - "command": "$SCRIPTMENU/script_b.py", - "sourcetype": "file", - "tags": ["test", "script", "normals", "model"]}, - {"title" : "Script C", - "tooltip": "Run script C", - "command": "$SCRIPTMENU/script_c.py", - "sourcetype": "file", - "tags": ["math", "power", "sum"]} - ], - -"Rigging": [{"title" : "Script A", - "tooltip": "Run script A", - "command": "$SCRIPTMENU/script_a.py", - "sourcetype": "file", - "tags": ["test", "tool", "rig", "skeleton"]}, - {"title" : "Script B", - "tooltip": "Run script A", - "command": "$SCRIPTMENU/script_b.py", - "sourcetype": "file", - "tags": ["test", "script", "cloth", "rig", "setup"]}, - {"title" : "Script C", - "tooltip": "Run script C", - "command": "$SCRIPTMENU/script_c.py", - "sourcetype": "file", - "tags": ["test", "script", "approval"]} - ], - -"MEL": [{"title" : "Create cube", - "tooltip": "Launch character rigging tool", - "command": "polyCube -w 1 -h 1 -d 1;", - "sourcetype": "mel", - "tags": ["test", "script", "mel"]} + "tags": [ + "test", + "script", + "joints" + ], + "icon": "" + } ] -} \ No newline at end of file + }, + { + "title": "Modeling", + "type": "menu", + "items": [ + { + "type": "action", + "title": "Script A", + "tooltip": "Run script A", + "command": "$SCRIPTMENU/script_a.py", + "sourcetype": "file", + "tags": [ + "test", + "script", + "model", + "blendshapes" + ] + }, + { + "type": "action", + "title": "Script B", + "tooltip": "Run script B", + "command": "$SCRIPTMENU/script_b.py", + "sourcetype": "file", + "tags": [ + "test", + "script", + "normals", + "model" + ] + }, + { + "type": "action", + "title": "Script C", + "tooltip": "Run script C", + "command": "$SCRIPTMENU/script_c.py", + "sourcetype": "file", + "tags": [ + "math", + "power", + "sum" + ] + } + ] + }, + { + "type": "menu", + "title": "MEL", + "items": [ + { + "title": "Create cube", + "tooltip": "Launch character rigging tool", + "command": "polyCube -w 1 -h 1 -d 1;", + "sourcetype": "mel", + "tags": [ + "test", + "script", + "mel" + ] + } + ] + } +] \ No newline at end of file diff --git a/samples/sample_configuration_b.json b/samples/sample_configuration_b.json index 48d13ff..a7c19e9 100644 --- a/samples/sample_configuration_b.json +++ b/samples/sample_configuration_b.json @@ -1,23 +1,46 @@ -{ - "Launcher": [ +[ { - "title": "Echo command", - "tooltip": "Test print", - "command": "print('I am a tool launcher!')", - "sourcetype": "python", - "tags": ["test", "script", "echo", "command"] - }, - {"title": "Echo second command", - "tooltip": "Test print", - "command": "print('I am a tool killer!')", - "sourcetype": "python", - "tags": ["test", "script", "echo", "command"] - }, - {"title": "Launch script A", - "tooltip": "Test launcher", - "command": "$SCRIPTMENU/script_a.py", - "sourcetype": "file", - "tags": ["test", "script", "launch"] + "title":"Launcher", + "type": "menu", + "items": [ + { + "type": "action", + "title": "Echo command", + "tooltip": "Test print", + "command": "print('I am a tool launcher!')", + "sourcetype": "python", + "tags": [ + "test", + "script", + "echo", + "command" + ] + }, + { + "type": "action", + "title": "Echo second command", + "tooltip": "Test print", + "command": "print('I am a tool killer!')", + "sourcetype": "python", + "tags": [ + "test", + "script", + "echo", + "command" + ] + }, + { + "type": "action", + "title": "Launch script A", + "tooltip": "Test launcher", + "command": "$SCRIPTMENU/script_a.py", + "sourcetype": "file", + "tags": [ + "test", + "script", + "launch" + ] + } + ] } - ] -} \ No newline at end of file +] \ No newline at end of file