Skip to content

Commit

Permalink
optionally add commandtasks and processors to generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
rkoschmitzky committed Feb 9, 2021
1 parent c78acf3 commit 1802d80
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
63 changes: 48 additions & 15 deletions dispatch/trixterdispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __repr__(self):
code += "\n argument_processors = {}".format(self.argument_processors) if self.argument_processors else ""
code += "\n flags = Task.Flags.PER_ELEMENT" if self.per_element else ""
code += "\n required_tasks = {}".format(self.required_tasks)
code += "\n"

return code

Expand All @@ -105,8 +106,8 @@ def __init__(self, name):
def __repr__(self):
code = "ProcessorDefinition("
code += "name='{}'".format(self.name)
code += ",scope={}".format(self.scope) if self.scope else ""
code += ",parameters={}".format(self.parameters) if self.parameters else ""
code += ", scope={}".format(self.scope) if self.scope else ""
code += ", parameters={}".format(self.parameters) if self.parameters else ""
code += ")"

return code
Expand All @@ -131,12 +132,20 @@ def __init__(self, name="Jobtronaut"):
Gaffer.Metadata.registerPlugValue(self.__class__, "jobsDirectory", "plugValueWidget:type", "")

# Add custom plugs to the Dispatcher UI
taskfile_location_plug = Gaffer.StringPlug("taskfile", Gaffer.Plug.Direction.In)
taskfile_location_plug.setValue("/tmp/temptasks.py")
Gaffer.Metadata.registerPlugValue(taskfile_location_plug, "nodule:type", "")
Gaffer.Metadata.registerPlugValue(taskfile_location_plug, "plugValueWidget:type", "GafferUI.FileSystemPathPlugValueWidget")
Gaffer.Metadata.registerPlugValue(taskfile_location_plug, "path:leaf", False)
self.addChild(taskfile_location_plug)
plugins_location_plug = Gaffer.StringPlug("PluginsFile", Gaffer.Plug.Direction.In)
plugins_location_plug.setValue("/tmp/temptasks.py")
Gaffer.Metadata.registerPlugValue(plugins_location_plug, "nodule:type", "")
Gaffer.Metadata.registerPlugValue(plugins_location_plug, "plugValueWidget:type", "GafferUI.FileSystemPathPlugValueWidget")
Gaffer.Metadata.registerPlugValue(plugins_location_plug, "path:leaf", False)
self.addChild(plugins_location_plug)

add_commandtasks_plug = Gaffer.BoolPlug("AddCommandtasks", Gaffer.Plug.Direction.In, defaultValue=True)
Gaffer.Metadata.registerPlugValue(add_commandtasks_plug, "node:type", "")
self.addChild(add_commandtasks_plug)

add_processors_plug = Gaffer.BoolPlug("AddProcessors", Gaffer.Plug.Direction.In, defaultValue=True)
Gaffer.Metadata.registerPlugValue(add_processors_plug, "node:type", "")
self.addChild(add_processors_plug)

@staticmethod
def _get_named_values(parent, plug_name, ignore_if_default=False):
Expand Down Expand Up @@ -172,9 +181,11 @@ def dispatch(self, nodes):
# filename = os.path.splitext(os.path.basename(scriptnode.getChild("fileName").getValue()))[0]
# self.getChild("taskfile").setValue("/tmp/jobtronaut_plugins/{}.py".format(filename))

all_hierarchy_nodes = JobtronautDispatcher.get_hierarchy_nodes(submitting_node, scriptnode)
all_hierarchy_nodes = self.get_hierarchy_nodes(submitting_node, scriptnode)
all_command_nodes = self.get_command_nodes(submitting_node, scriptnode)
used_processor_nodes = []

code = "from jobtronaut.author import (Task, ProcessorDefinition)"
code = "from jobtronaut.author import (Task, TaskWithOverrides, ProcessorDefinition, BaseProcessor, supported_schemas, ProcessorSchemas)\n"

for hierarchy_node in all_hierarchy_nodes:

Expand All @@ -189,14 +200,15 @@ def dispatch(self, nodes):
name = node.getChild("type").getValue()

template = TaskTemplate(name)
template.required_tasks = JobtronautDispatcher.get_required_tasks(hierarchy_node, scriptnode)
template.required_tasks = self.get_required_tasks(hierarchy_node, scriptnode)

for processor_node in JobtronautDispatcher.get_processors(hierarchy_node):
for processor_node in self.get_processors(hierarchy_node):
processor = ProcessorDefinitionTemplate(processor_node.getChild("type").getValue())
processor.scope = list(processor_node.getChild("scope").getValue())
processor.parameters = self._get_named_values(processor_node, "parameters", ignore_if_default=True)

template.argument_processors.append(processor)
used_processor_nodes.append(processor_node)

argument_defaults = self._get_named_values(hierarchy_node, "argument_defaults")

Expand All @@ -206,7 +218,15 @@ def dispatch(self, nodes):
template.elements_id = hierarchy_node.getChild("elements_id").getValue()
template.per_element = hierarchy_node.getChild("per_element").getValue()

code += "\n\n\n{}".format(template)
code += "\n\n{}".format(template)

if self.getChild("AddCommandtasks").getValue():
for command_node in all_command_nodes:
code += "\n\n{}".format(command_node.getChild("source").getValue())

if self.getChild("AddProcessors").getValue():
processor_nodes_code = list(set([_.getChild("source").getValue() for _ in used_processor_nodes]))
code += "".join(["\n\n{}".format(_) for _ in processor_nodes_code])

code = textwrap.dedent(code)

Expand All @@ -218,12 +238,25 @@ def dispatch(self, nodes):
"max_line_length": 80
})

filepath = self.getChild("taskfile").getValue()
filepath = self.getChild("PluginsFile").getValue()
with open(filepath, "w+") as fp:
fp.write(code)

def get_hierarchy_nodes(self, startnode, scriptnode):
return self.get_nodes(startnode, scriptnode, type_filter=HierarchyTask)

def get_command_nodes(self, startnode, scriptnode):
all_command_nodes = self.get_nodes(startnode, scriptnode, type_filter=JobtronautTask)
command_node_types = {}
for command_node in all_command_nodes:
type = command_node.getChild("type").getValue()
if type not in command_node_types:
command_node_types[type] = command_node

return command_node_types.values()

@staticmethod
def get_hierarchy_nodes(startnode, scriptnode, type_filter=HierarchyTask):
def get_nodes(startnode, scriptnode, type_filter):
connected = Gaffer.StandardSet()
graphgadget = GafferUI.GraphGadget(scriptnode)
if graphgadget is not None:
Expand Down
3 changes: 1 addition & 2 deletions startup/missioncontrol/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ def append_jobtronaut_plugins_to_menu(menu):
nodeMenu.append("/Utility/Dot", Gaffer.Dot)

nodeMenu.append("/Utility/Dot", Gaffer.Dot)
# nodeMenu.append("/FTrack/InitializeFTrackSession", nodes.InitializeFTrackSession, searchText="InitializeFTrackSession")
# append_compounds_to_menu(nodeMenu)

append_jobtronaut_plugins_to_menu(nodeMenu)

# nodeMenu.definition().append("/Utility/Backdrop", {"command": GafferUI.BackdropUI.nodeMenuCreateCommand})
Expand Down

0 comments on commit 1802d80

Please sign in to comment.