示例#1
0
    def on_export_notebook(self, window, notebook):
        """Callback from gui for exporting a notebook"""

        if notebook is None:
            return

        dialog = FileChooserDialog("Export Notebook",
                                   window,
                                   action=gtk.FILE_CHOOSER_ACTION_SAVE,
                                   buttons=("Cancel", gtk.RESPONSE_CANCEL,
                                            "Export", gtk.RESPONSE_OK),
                                   app=self.app,
                                   persistent_path="archive_notebook_path")

        basename = time.strftime(
            os.path.basename(notebook.get_path()) + "-%Y-%m-%d")

        path = self.app.get_default_path("archive_notebook_path")
        if path and os.path.exists(path):
            filename = notebooklib.get_unique_filename(path, basename, "", ".")
        else:
            filename = basename
        dialog.set_current_name(os.path.basename(filename))

        response = dialog.run()

        if response == gtk.RESPONSE_OK and dialog.get_filename():
            filename = unicode_gtk(dialog.get_filename())
            dialog.destroy()
            self.export_notebook(notebook, filename, window=window)
        else:
            dialog.destroy()
示例#2
0
    def on_export_notebook(self, window, notebook):
        """Callback from gui for exporting a notebook"""
        
        if notebook is None:
            return

        dialog = FileChooserDialog("Export Notebook", window, 
            action=gtk.FILE_CHOOSER_ACTION_SAVE,
            buttons=("Cancel", gtk.RESPONSE_CANCEL,
                     "Export", gtk.RESPONSE_OK),
            app=self.app,
            persistent_path="archive_notebook_path")


        basename = time.strftime(os.path.basename(notebook.get_path()) +
                                 "-%Y-%m-%d")

        path = self.app.get_default_path("archive_notebook_path")
        if path and os.path.exists(path):
            filename = notebooklib.get_unique_filename(
                path, basename, "", ".")
        else:
            filename = basename
        dialog.set_current_name(os.path.basename(filename))
        
        response = dialog.run()

        if response == gtk.RESPONSE_OK and dialog.get_filename():
            filename = unicode_gtk(dialog.get_filename())
            dialog.destroy()
            self.export_notebook(notebook, filename, window=window)
        else:
            dialog.destroy()
示例#3
0
    def install_example_file(self, filename):
        """Installs a new example file into the extension"""

        newpath = self.get_data_dir()
        newfilename = os.path.basename(filename)
        newfilename, ext = os.path.splitext(newfilename)
        newfilename = notebooklib.get_unique_filename(newpath, newfilename, ext=ext, sep=u"", number=2)
        shutil.copy(filename, newfilename)
        return os.path.basename(newfilename)
示例#4
0
    def install_example_file(self, filename):
        """Installs a new example file into the extension"""

        newpath = self.get_data_dir()
        newfilename = os.path.basename(filename)
        newfilename, ext = os.path.splitext(newfilename)
        newfilename = notebooklib.get_unique_filename(newpath, newfilename, 
                                                      ext=ext, sep=u"", 
                                                      number=2)
        shutil.copy(filename, newfilename)
        return os.path.basename(newfilename)
示例#5
0
    def on_archive_notebook(self, window, notebook):
        """Callback from gui for archiving a notebook"""

        if notebook is None:
            return

        dialog = FileChooserDialog(
            "Backup Notebook", window, 
            action=gtk.FILE_CHOOSER_ACTION_SAVE,
            buttons=("Cancel", gtk.RESPONSE_CANCEL,
                     "Backup", gtk.RESPONSE_OK),
            app=self.app,
            persistent_path="archive_notebook_path")

        path = self.app.get_default_path("archive_notebook_path")
        if os.path.exists(path):
            filename = notebooklib.get_unique_filename(
                path,
                os.path.basename(notebook.get_path()) +
                time.strftime("-%Y-%m-%d"), ".tar.gz", ".")
        else: 
            filename = os.path.basename(notebook.get_path()) + \
                time.strftime("-%Y-%m-%d") + u".tar.gz"
        
        dialog.set_current_name(os.path.basename(filename))


        file_filter = gtk.FileFilter()
        file_filter.add_pattern("*.tar.gz")
        file_filter.set_name("Archives (*.tar.gz)")
        dialog.add_filter(file_filter)

        file_filter = gtk.FileFilter()
        file_filter.add_pattern("*")
        file_filter.set_name("All files (*.*)")
        dialog.add_filter(file_filter)

        response = dialog.run()

        if response == gtk.RESPONSE_OK and dialog.get_filename():
            filename = unicode_gtk(dialog.get_filename())
            dialog.destroy()

            if u"." not in filename:
                filename += u".tar.gz"

            window.set_status("Archiving...")
            return self.archive_notebook(notebook, filename, window)
            

        elif response == gtk.RESPONSE_CANCEL:
            dialog.destroy()
            return False
示例#6
0
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)