def attach_files(self, filenames, parent, index=None, parent_window=None): if parent_window is None: parent_window = self.get_current_window() # def func(task): # for filename in filenames: # task.set_message(("detail", _("attaching %s") % # os.path.basename(filename))) # notebooklib.attach_file(filename, parent, index) # if not task.is_running(): # task.abort() # task = tasklib.Task(func) try: for filename in filenames: notebooklib.attach_file(filename, parent, index) # dialog = keepnote.gui.dialog_wait.WaitDialog(parent_window) # dialog.show(_("Attach File"), _("Attaching files to notebook."), # task, cancel=False) # if task.aborted(): # raise task.exc_info()[1] except Exception, e: if len(filenames) > 1: self.error( _("Error while attaching files %s." % ", ".join(["'%s'" % f for f in filenames])), e, sys.exc_info()[2], ) else: self.error(_("Error while attaching file '%s'." % filenames[0]), e, sys.exc_info()[2])
def attach_files(self, filenames, parent, index=None, parent_window=None): if parent_window is None: parent_window = self.get_current_window() #def func(task): # for filename in filenames: # task.set_message(("detail", _("attaching %s") % # os.path.basename(filename))) # notebooklib.attach_file(filename, parent, index) # if not task.is_running(): # task.abort() #task = tasklib.Task(func) try: for filename in filenames: notebooklib.attach_file(filename, parent, index) #dialog = keepnote.gui.dialog_wait.WaitDialog(parent_window) #dialog.show(_("Attach File"), _("Attaching files to notebook."), # task, cancel=False) #if task.aborted(): # raise task.exc_info()[1] except Exception, e: if len(filenames) > 1: self.error( _("Error while attaching files %s." % ", ".join(["'%s'" % f for f in filenames])), e, sys.exc_info()[2]) else: self.error( _("Error while attaching file '%s'." % filenames[0]), e, sys.exc_info()[2])
def on_new_file(self, window, file_type): """Callback from gui to add a new file""" notebook = window.get_notebook() if notebook is None: return nodes = window.get_selected_nodes() if len(nodes) == 0: parent = notebook else: sibling = nodes[0] if sibling.get_parent(): parent = sibling.get_parent() index = sibling.get_attr("order") + 1 else: parent = sibling try: uri = os.path.join(self.get_data_dir(), file_type.example_file) node = notebooklib.attach_file(uri, parent) node.rename(file_type.filename) window.get_viewer().goto_node(node) except Exception, e: window.error("Error while attaching file '%s'." % uri, e)
def import_folder(node, filename, task=None): """ Import a folder tree as a subfolder of the current item node -- node to attach folder to filename -- filename of folder to import task -- Task object to track progress """ # TODO: Exceptions, intelligent error handling # For windows: # Deep paths are handled by unicode "\\?\" extension to filename. if task is None: # create dummy task if needed task = tasklib.Task() # Determine number of files in advance so we can have a progress bar nfiles = 0 nfilescomplete = 0 # updates progress bar for root, dirs, files in os.walk(filename): nfiles += len(files) # Add files found in current dir task.set_message(("text", "Found %i files..." % nfiles)) # Make a node based on the root - so we have an origin to import to rootnode = node.new_child(CONTENT_TYPE_DIR, os.path.basename(filename)) rootnode.set_attr("title", os.path.basename(filename)) filename2node = {filename: rootnode} # Walk directory we're importing and create nodes for root, dirs, files in os.walk(filename): # create node for directory if root == filename: parent = rootnode else: parent2 = filename2node.get(os.path.dirname(root), None) if parent2 is None: continue parent = parent2.new_child(CONTENT_TYPE_DIR, os.path.basename(root)) parent.set_attr("title", os.path.basename(root)) filename2node[root] = parent # create nodes for files for fn in files: if keepnote.get_platform() is "windows": fn = "\\\\?\\" + os.path.join(root, fn) else: fn = os.path.join(root, fn) child = attach_file(fn, parent) nfilescomplete += 1 task.set_message(("text", "Imported %i / %i files..." % (nfilescomplete, nfiles))) task.set_percent(float(nfilescomplete) / float(nfiles)) task.finish()
def import_folder(node, filename, task=None): """ Import a folder tree as a subfolder of the current item node -- node to attach folder to filename -- filename of folder to import task -- Task object to track progress """ # TODO: Exceptions, intelligent error handling # For windows: # Deep paths are handled by unicode "\\?\" extension to filename. if task is None: # create dummy task if needed task = tasklib.Task() # Determine number of files in advance so we can have a progress bar nfiles = 0 nfilescomplete = 0 # updates progress bar for root, dirs, files in os.walk(filename): nfiles += len(files) # Add files found in current dir task.set_message(("text", "Found %i files..." % nfiles)) # Make a node based on the root - so we have an origin to import to rootnode = node.new_child(CONTENT_TYPE_DIR, os.path.basename(filename)) rootnode.set_attr("title", os.path.basename(filename)) filename2node = {filename: rootnode} # Walk directory we're importing and create nodes for root, dirs, files in os.walk(filename): # create node for directory if root == filename: parent = rootnode else: parent2 = filename2node.get(os.path.dirname(root), None) if parent2 is None: continue parent = parent2.new_child(CONTENT_TYPE_DIR, os.path.basename(root)) parent.set_attr("title", os.path.basename(root)) filename2node[root] = parent # create nodes for files for fn in files: if keepnote.get_platform() is "windows": fn = "\\\\?\\" + os.path.join(root, fn) else: fn = os.path.join(root, fn) child = attach_file(fn, parent) nfilescomplete += 1 task.set_message( ("text", "Imported %i / %i files..." % (nfilescomplete, nfiles))) task.set_percent(float(nfilescomplete) / float(nfiles)) task.finish()