Example #1
0
 def load_template(self):
     self.template = TemplateFactory().create_template(self.combo_get_path(self.combo))
     return self.template
Example #2
0
class pluginExport:
    def __init__(self):
        """Initialize all the GTK widgets"""
        self.__init_gtk()

    def activate(self, plugin_api):
        """loads the saved preferences"""
        self.plugin_api = plugin_api
        self.preferences_load()
        self.preferences_apply()

    def deactivate(self, plugin_api):
        """Removes the gtk widgets before quitting"""
        self.__gtk_deactivate()

    ## CALLBACK AND CORE FUNCTIONS ################################################

    def load_template(self):
        self.template = TemplateFactory().create_template(self.combo_get_path(self.combo))
        return self.template

    def export_generate(self, document_ready):
        # Template loading and cutting
        timespan = None
        if self.export_all_active.get_active():
            tree = self.plugin_api.get_requester().get_tasks_tree(name="active")
            tree.apply_filter("active")
        elif self.export_all_finished.get_active():
            tree = self.plugin_api.get_requester().get_tasks_tree(name="closed")
            tree.apply_filter("closed")
        elif self.export_finished_last_week.get_active():
            tree = self.plugin_api.get_requester().get_tasks_tree(name="closed")
            tree.apply_filter("closed")
            timespan = -7
        meta_root_node = tree.get_root()
        root_nodes = [meta_root_node.get_child(c) for c in meta_root_node.get_children()]
        tasks_str = tree_to_TaskStr(tree, root_nodes, self.plugin_api, timespan)
        document = str(
            CheetahTemplate(
                file=self.template.get_path(), searchList=[{"tasks": tasks_str, "plugin_api": self.plugin_api}]
            )
        )
        self.__purge_saved_document()
        # we save the created document in a temporary file with the same suffix
        # as the template (it's script-friendly)
        with tempfile.NamedTemporaryFile(suffix=".%s" % self.template.get_suffix(), delete=False) as f:
            f.write(document)
            self.document_path = f.name
        if self.template.get_script_path():

            def __script_worker(self):
                try:
                    self.document_path = subprocess.Popen(
                        args=["/bin/sh", "-c", self.template.get_script_path() + " " + self.document_path],
                        shell=False,
                        stdout=subprocess.PIPE,
                    ).communicate()[0]
                except:
                    pass
                if self.document_path == "ERROR":
                    Log.debug("Document creation failed")
                    self.document_path = None
                document_ready.set()

            worker_thread = threading.Thread(target=__script_worker, args=(self,))
            worker_thread.setDaemon(True)
            worker_thread.start()
        else:
            document_ready.set()

    def export_execute_with_ui(self, document_ready):
        if not self.load_template():
            self.show_error_dialog(_("Template not found"))
            return False
        # REMOVE ME
        try:
            self.export_generate(document_ready)
        except Exception, e:
            self.show_error_dialog(_("Could not generate the document: %s") % e)
            return False
        return True