class ToolPublisher:
    def __init__(self):
        self.project = Project()

    def publish(self):

        selection = hou.selectedNodes()
        if len(selection) != 1:
            qd.error(
                'Please select a single Houdini Digital Asset node to publish.')
            return
        if not selection[0].type().definition():
            qd.error(
                'Please select a Houdini Digital Asset node to publish.')
            return

        hda = selection[0]
        definition = hda.type().definition()
        self.name = definition.nodeTypeName()

        tool = self.project.get_tool(self.name)
        if not tool:
            tool = self.project.create_tool(self.name)
        self.element = tool.get_element(Asset.HDA)
        
        self.filepath = os.path.join(self.element._filepath, self.name)
        definition.save(self.filepath, hda)

        publishes = self.element.list_publishes()
        publishes_string_list = ""
        for publish in publishes:
            label = publish[0] + " " + publish[1] + " " + publish[2] + "\n"
            publishes_string_list += label
        
        self.input = qd.HoudiniInput(
            parent=hou.qt.mainWindow(), title="Comment ", info=publishes_string_list)
        self.input.submitted.connect(self.comment_results)


    def comment_results(self, value):
        comment = str(value)
        username = Environment().get_user().get_username()

        self.element.update_app_ext(".hda")
        self.element.publish(username, self.filepath, comment, self.name)
class ToolCloner:

    def __init__(self):
        self.project = Project()

    def clone(self):
        tool_list = self.project.list_tools()

        self.item_gui = sfl.SelectFromList(
            l=tool_list, parent=hou.ui.mainQtWindow(), title="Select a tool to clone")
        self.item_gui.submitted.connect(self.results)

    def results(self, value):
        name = value[0]

        body = self.project.get_tool(name)
        element = body.get_element(Asset.HDA)

        if element.get_last_version() < 0:
            qd.error("Nothing has been published for this tool")
            return
        filepath = element.get_last_publish()[3]

        panes = self.getCurrentNetworkEditorPane()
        paths = []
        for pane in panes:
            paths.append(pane.pwd())

        try:
            hou.hda.uninstallFile(filepath)
            hou.hda.installFile(filepath)
            #print("no problem here")
        except Exception as e:
            print(e)
            return

        hda = None
        for p in paths:
            try:
                print(p)
                hda = p.createNode(name)
                break
            except Exception as e:
                #qd.error("Couldn't create node of type " + name + ". You should still be able to tab in the node manually.")
                print(e)
                #return

        if not hda:
            qd.error("Couldn't create node of type " + name + ". You should still be able to tab in the node manually.")

        try:
            hda.setName(name, 1)
        except:
            pass

        try:
            hda.allowEditOfContents()
        except:
            pass

    def getCurrentNetworkEditorPane(self):
        editors = [pane for pane in hou.ui.paneTabs() if isinstance(pane, hou.NetworkEditor) and pane.isCurrentTab()]
        return editors