def archive_notebook_zip(notebook, filename, task=None): """Archive notebook as *.tar.gz filename -- filename of archive to create progress -- callback function that takes arguments (percent, filename) """ if os.path.exists(filename): raise NoteBookError("File '%s' already exists" % filename) # make sure all modifications are saved first try: notebook.save() except Exception, e: raise NoteBookError("Could not save notebook before archiving", e)
def walk(path, arcname): # add to archive archive.add(path, arcname, False) # report progresss if os.path.isfile(path): nfiles2[0] += 1 if task: task.set_message(("detail", truncate_filename(path))) task.set_percent(nfiles2[0] / float(nfiles)) # recurse if os.path.isdir(path): for f in os.listdir(path): # abort archive if task.aborted(): archive.close() os.remove(filename) raise NoteBookError("Backup canceled") if not os.path.islink(f): walk(os.path.join(path, f), os.path.join(arcname, f))
def export_node(node, path, arcname, index=False): # look for aborted export if task.aborted(): raise NoteBookError("Backup canceled") # report progresss nnodes2[0] += 1 task.set_message(("detail", truncate_filename(path))) task.set_percent(nnodes2[0] / float(nnodes[0])) skipfiles = set(child.get_basename() for child in node.get_children()) # make node directory os.mkdir(arcname) if index: write_index(notebook, node, arcname) if node.get_attr("content_type") == "text/xhtml+xml": skipfiles.add("page.html") # export xhtml export_page(node, path, arcname) # recurse files for f in os.listdir(path): if not os.path.islink(f) and f not in skipfiles: export_files(os.path.join(path, f), os.path.join(arcname, f)) # recurse nodes for child in node.get_children(): f = child.get_basename() export_node(child, os.path.join(path, f), os.path.join(arcname, f))
def export_notebook(notebook, filename, task): """Export notebook to HTML filename -- filename of export to create """ if task is None: # create dummy task if needed task = tasklib.Task() if os.path.exists(filename): raise NoteBookError("File '%s' already exists" % filename) # make sure all modifications are saved first try: notebook.save() except Exception, e: raise NoteBookError("Could not save notebook before archiving", e)
def export_files(path, arcname): # look for aborted export if task.aborted(): raise NoteBookError("Backup canceled") if os.path.isfile(path): # copy files shutil.copy(path, arcname) if os.path.isdir(path): # export directory os.mkdir(arcname) # recurse for f in os.listdir(path): if not os.path.islink(f): export_files(os.path.join(path, f), os.path.join(arcname, f))
def write(self): """Write preferences to file""" try: if not os.path.exists(self._pref_dir): init_user_pref_dir(self._pref_dir) out = safefile.open(get_user_pref_file(self._pref_dir), "w", codec="utf-8") out.write(u'<?xml version="1.0" encoding="UTF-8"?>\n' u'<keepnote>\n' u'<pref>\n') plist.dump(self._data, out, indent=4, depth=4) out.write(u'</pref>\n' u'</keepnote>\n') out.close() except (IOError, OSError), e: log_error(e, sys.exc_info()[2]) raise NoteBookError(_("Cannot save preferences"), e)
def load_config(self): config = self.get_config_file() if not os.path.exists(config): self.set_default_file_types() self.save_default_example_files() self.save_config() try: tree = etree.ElementTree(file=config) # check root root = tree.getroot() if root.tag != "file_types": raise NoteBookError("Root tag is not 'file_types'") # iterate children self._file_types = [] for child in root: if child.tag == "file_type": filetype = FileType("", "", "") for child2 in child: if child2.tag == "name": filetype.name = child2.text elif child2.tag == "filename": filetype.filename = child2.text elif child2.tag == "example_file": filetype.example_file = child2.text self._file_types.append(filetype) except: self.app.error("Error reading file type configuration") self.set_default_file_types() self.save_config()
def restore_notebook(filename, path, rename, task=None): """ Restores a archived notebook filename -- filename of archive path -- name of new notebook rename -- if True, path contains notebook name, otherwise path is basedir of new notebook """ if task is None: # create dummy task if needed task = tasklib.Task() if path == "": raise NoteBookError("Must specify a path for restoring notebook") # remove trailing "/" path = re.sub("/+$", "", path) tar = tarfile.open(filename, "r:gz", format=tarfile.PAX_FORMAT) # create new dirctory, if needed if rename: if not os.path.exists(path): tmppath = get_unique_filename(os.path.dirname(path), os.path.basename(path+"-tmp")) else: raise NoteBookError("Notebook path already exists") try: # extract notebook members = list(tar.getmembers()) if task: task.set_message(("text", "Restoring %d files..." % len(members))) for i, member in enumerate(members): # FIX: tarfile does not seem to keep unicode and str straight # make sure member.name is unicode if 'path' in member.pax_headers: member.name = member.pax_headers['path'] if task: if task.aborted(): raise NoteBookError("Restore canceled") task.set_message(("detail", truncate_filename(member.name))) task.set_percent(i / float(len(members))) tar.extract(member, tmppath) files = os.listdir(tmppath) # assert len(files) = 1 extracted_path = os.path.join(tmppath, files[0]) # move extracted files to proper place if task: task.set_message(("text", "Finishing restore...")) shutil.move(extracted_path, path) os.rmdir(tmppath) except NoteBookError, e: raise e except Exception, e: raise NoteBookError("File writing error while extracting notebook", e)
os.rmdir(tmppath) except NoteBookError, e: raise e except Exception, e: raise NoteBookError("File writing error while extracting notebook", e) else: try: if task: task.set_message(("text", "Restoring archive...")) tar.extractall(path) except Exception, e: raise NoteBookError("File writing error while extracting notebook", e) task.finish() #============================================================================= def archive_notebook_zip(notebook, filename, task=None): """Archive notebook as *.tar.gz filename -- filename of archive to create progress -- callback function that takes arguments (percent, filename) """
def archive_notebook_zip(notebook, filename, task=None): """Archive notebook as *.tar.gz filename -- filename of archive to create progress -- callback function that takes arguments (percent, filename) """ if os.path.exists(filename): raise NoteBookError("File '%s' already exists" % filename) # make sure all modifications are saved first try: notebook.save() except Exception as e: raise NoteBookError("Could not save notebook before archiving", e) # perform archiving try: #archive = tarfile.open(filename, "w:gz") archive = zipfile.ZipFile(filename, "w", zipfile.ZIP_DEFLATED, True) path = notebook.get_path() # first count # of files nfiles = 0 for root, dirs, files in os.walk(path): nfiles += len(files) nfiles2 = [0] abort = [False] def walk(path, arcname): # add to archive #archive.add(path, arcname, False) if os.path.isfile(path): archive.write(path, arcname) # report progresss if os.path.isfile(path): nfiles2[0] += 1 if task: task.set_message(path) task.set_percent(nfiles2[0] / float(nfiles)) # recurse if os.path.isdir(path): for f in os.listdir(path): # abort archive if not task.is_running(): abort[0] = True return if not os.path.islink(f): walk(os.path.join(path, f), os.path.join(arcname, f)) walk(path, os.path.basename(path)) archive.close() if abort[0]: os.remove(filename) elif task: task.finish() except Exception as e: raise NoteBookError("Error while archiving notebook", e)
def archive_notebook(notebook, filename, task=None): """Archive notebook as *.tar.gz filename -- filename of archive to create """ if task is None: # create dummy task if needed task = tasklib.Task() if os.path.exists(filename): raise NoteBookError("File '%s' already exists" % filename) # make sure all modifications are saved first try: notebook.save() except Exception as e: raise NoteBookError("Could not save notebook before archiving", e) # perform archiving archive = tarfile.open(filename, "w:gz", format=tarfile.PAX_FORMAT) path = notebook.get_path() # first count # of files nfiles = 0 for root, dirs, files in os.walk(path): nfiles += len(files) task.set_message(("text", "Archiving %d files..." % nfiles)) nfiles2 = [0] def walk(path, arcname): # add to archive archive.add(path, arcname, False) # report progresss if os.path.isfile(path): nfiles2[0] += 1 if task: task.set_message(("detail", truncate_filename(path))) task.set_percent(nfiles2[0] / float(nfiles)) # recurse if os.path.isdir(path): for f in os.listdir(path): # abort archive if task.aborted(): archive.close() os.remove(filename) raise NoteBookError("Backup canceled") if not os.path.islink(f): walk(os.path.join(path, f), os.path.join(arcname, f)) walk(path, os.path.basename(path)) task.set_message(("text", "Closing archive...")) task.set_message(("detail", "")) archive.close() if task: task.finish()
def export_notebook(notebook, filename, task): """Export notebook to HTML filename -- filename of export to create """ if task is None: # create dummy task if needed task = tasklib.Task() if os.path.exists(filename): raise NoteBookError("File '%s' already exists" % filename) # make sure all modifications are saved first try: notebook.save() except Exception as e: raise NoteBookError("Could not save notebook before archiving", e) # first count # of files nnodes = [0] def walk(node): nnodes[0] += 1 for child in node.get_children(): walk(child) walk(notebook) task.set_message(("text", "Exporting %d notes..." % nnodes[0])) nnodes2 = [0] def export_page(node, path, arcname): filename = os.path.join(path, "page.html") filename2 = os.path.join(arcname, "page.html") try: dom = minidom.parse(filename) except Exception as e: # error parsing file, use simple file export export_files(filename, filename2) else: translate_links(notebook, path, dom.documentElement) # avoid writing <?xml> header # (provides compatiability with browsers) out = codecs.open(filename2, "wb", "utf-8") if dom.doctype: dom.doctype.writexml(out) dom.documentElement.writexml(out) out.close() def export_node(node, path, arcname, index=False): # look for aborted export if task.aborted(): raise NoteBookError("Backup canceled") # report progresss nnodes2[0] += 1 task.set_message(("detail", truncate_filename(path))) task.set_percent(nnodes2[0] / float(nnodes[0])) skipfiles = set(child.get_basename() for child in node.get_children()) # make node directory os.mkdir(arcname) if index: write_index(notebook, node, arcname) if node.get_attr("content_type") == "text/xhtml+xml": skipfiles.add("page.html") # export xhtml export_page(node, path, arcname) # recurse files for f in os.listdir(path): if not os.path.islink(f) and f not in skipfiles: export_files(os.path.join(path, f), os.path.join(arcname, f)) # recurse nodes for child in node.get_children(): f = child.get_basename() export_node(child, os.path.join(path, f), os.path.join(arcname, f)) def export_files(path, arcname): # look for aborted export if task.aborted(): raise NoteBookError("Backup canceled") if os.path.isfile(path): # copy files shutil.copy(path, arcname) if os.path.isdir(path): # export directory os.mkdir(arcname) # recurse for f in os.listdir(path): if not os.path.islink(f): export_files(os.path.join(path, f), os.path.join(arcname, f)) export_node(notebook, notebook.get_path(), filename, True) task.set_message(("text", "Closing export...")) task.set_message(("detail", "")) if task: task.finish()