Esempio n. 1
0
    def save_copy(self):
        c = current_container()
        ext = c.path_to_ebook.rpartition(".")[-1]
        path = choose_save_file(
            self.gui,
            "tweak_book_save_copy",
            _("Choose path"),
            filters=[(_("Book (%s)") % ext.upper(), [ext.lower()])],
            all_files=False,
        )
        if not path:
            return
        tdir = self.mkdtemp(prefix="save-copy-")
        container = clone_container(c, tdir)
        for name, ed in editors.iteritems():
            if ed.is_modified or not ed.is_synced_to_container:
                self.commit_editor_to_container(name, container)

        def do_save(c, path, tdir):
            save_container(c, path)
            shutil.rmtree(tdir, ignore_errors=True)
            return path

        self.gui.blocking_job(
            "save_copy", _("Saving copy, please wait..."), self.copy_saved, do_save, container, path, tdir
        )
Esempio n. 2
0
 def save_book(self):
     c = current_container()
     for name, ed in editors.iteritems():
         if ed.is_modified or not ed.is_synced_to_container:
             self.commit_editor_to_container(name, c)
             ed.is_modified = False
     self.gui.action_save.setEnabled(False)
     tdir = self.mkdtemp(prefix='save-')
     container = clone_container(c, tdir)
     self.save_manager.schedule(tdir, container)
Esempio n. 3
0
 def save_book(self):
     c = current_container()
     for name, ed in editors.iteritems():
         if ed.is_modified or not ed.is_synced_to_container:
             self.commit_editor_to_container(name, c)
             ed.is_modified = False
     self.gui.action_save.setEnabled(False)
     tdir = self.mkdtemp(prefix='save-')
     container = clone_container(c, tdir)
     self.save_manager.schedule(tdir, container)
Esempio n. 4
0
 def save_book(self):
     c = current_container()
     for name, ed in editors.iteritems():
         if ed.is_modified:
             with c.open(name, 'wb') as f:
                 f.write(ed.data)
             ed.is_modified = False
     self.gui.action_save.setEnabled(False)
     tdir = self.mkdtemp(prefix='save-')
     container = clone_container(c, tdir)
     self.save_manager.schedule(tdir, container)
Esempio n. 5
0
    def test_clone(self):
        ' Test cloning of containers '
        for fmt in ('epub', 'azw3'):
            base = os.path.join(self.tdir, fmt + '-')
            book = get_simple_book(fmt)
            tdir = base + 'first'
            os.mkdir(tdir)
            c1 = get_container(book, tdir=tdir)
            tdir = base + 'second'
            os.mkdir(tdir)
            c2 = clone_container(c1, tdir)

            for c in (c1, c2):
                for name, path in iteritems(c.name_path_map):
                    self.assertEqual(2, nlinks_file(path),
                                     'The file %s is not linked' % name)

            for name in c1.name_path_map:
                self.assertIn(name, c2.name_path_map)
                self.assertEqual(
                    c1.open(name).read(),
                    c2.open(name).read(), 'The file %s differs' % name)

            spine_names = tuple(x[0] for x in c1.spine_names)
            text = spine_names[0]
            root = c2.parsed(text)
            root.xpath('//*[local-name()="body"]')[0].set(
                'id', 'changed id for test')
            c2.dirty(text)
            c2.commit_item(text)
            for c in (c1, c2):
                self.assertEqual(1, nlinks_file(c.name_path_map[text]))
            self.assertNotEqual(c1.open(text).read(), c2.open(text).read())

            name = spine_names[1]
            with c1.open(name, mode='r+b') as f:
                f.seek(0, 2)
                f.write(b'    ')
            for c in (c1, c2):
                self.assertEqual(1, nlinks_file(c.name_path_map[name]))
            self.assertNotEqual(c1.open(name).read(), c2.open(name).read())

            x = base + 'out.' + fmt
            for c in (c1, c2):
                c.commit(outpath=x)
Esempio n. 6
0
    def test_clone(self):
        ' Test cloning of containers '
        for fmt in ('epub', 'azw3'):
            base = os.path.join(self.tdir, fmt + '-')
            book = get_simple_book(fmt)
            tdir = base + 'first'
            os.mkdir(tdir)
            c1 = get_container(book, tdir=tdir)
            tdir = base + 'second'
            os.mkdir(tdir)
            c2 = clone_container(c1, tdir)

            for c in (c1, c2):
                for name, path in c.name_path_map.iteritems():
                    self.assertEqual(2, nlinks_file(path), 'The file %s is not linked' % name)

            for name in c1.name_path_map:
                self.assertIn(name, c2.name_path_map)
                self.assertEqual(c1.open(name).read(), c2.open(name).read(), 'The file %s differs' % name)

            spine_names = tuple(x[0] for x in c1.spine_names)
            text = spine_names[0]
            root = c2.parsed(text)
            root.xpath('//*[local-name()="body"]')[0].set('id', 'changed id for test')
            c2.dirty(text)
            c2.commit_item(text)
            for c in (c1, c2):
                self.assertEqual(1, nlinks_file(c.name_path_map[text]))
            self.assertNotEqual(c1.open(text).read(), c2.open(text).read())

            name = spine_names[1]
            with c1.open(name, mode='r+b') as f:
                f.seek(0, 2)
                f.write(b'    ')
            for c in (c1, c2):
                self.assertEqual(1, nlinks_file(c.name_path_map[name]))
            self.assertNotEqual(c1.open(name).read(), c2.open(name).read())

            x = base + 'out.' + fmt
            for c in (c1, c2):
                c.commit(outpath=x)
Esempio n. 7
0
    def test_clone(self):
        " Test cloning of containers "
        for fmt in ("epub", "azw3"):
            base = os.path.join(self.tdir, fmt + "-")
            book = get_simple_book(fmt)
            tdir = base + "first"
            os.mkdir(tdir)
            c1 = get_container(book, tdir=tdir)
            tdir = base + "second"
            os.mkdir(tdir)
            c2 = clone_container(c1, tdir)

            for c in (c1, c2):
                for name, path in c.name_path_map.iteritems():
                    self.assertEqual(2, nlinks_file(path), "The file %s is not linked" % name)

            for name in c1.name_path_map:
                self.assertIn(name, c2.name_path_map)
                self.assertEqual(c1.open(name).read(), c2.open(name).read(), "The file %s differs" % name)

            spine_names = tuple(x[0] for x in c1.spine_names)
            text = spine_names[0]
            root = c2.parsed(text)
            root.xpath('//*[local-name()="body"]')[0].set("id", "changed id for test")
            c2.dirty(text)
            c2.commit_item(text)
            for c in (c1, c2):
                self.assertEqual(1, nlinks_file(c.name_path_map[text]))
            self.assertNotEqual(c1.open(text).read(), c2.open(text).read())

            name = spine_names[1]
            with c1.open(name, mode="r+b") as f:
                f.seek(0, 2)
                f.write(b"    ")
            for c in (c1, c2):
                self.assertEqual(1, nlinks_file(c.name_path_map[name]))
            self.assertNotEqual(c1.open(name).read(), c2.open(name).read())

            x = base + "out." + fmt
            for c in (c1, c2):
                c.commit(outpath=x)
Esempio n. 8
0
 def do_editor_save(self):
     ed = self.gui.central.current_editor
     if ed is None:
         return
     name = None
     for n, x in editors.iteritems():
         if x is ed:
             name = n
             break
     if name is None:
         return
     c = current_container()
     with c.open(name, 'wb') as f:
         f.write(ed.data)
     ed.is_modified = False
     tdir = self.mkdtemp(prefix='save-')
     container = clone_container(c, tdir)
     self.save_manager.schedule(tdir, container)
     is_modified = False
     for ed in editors.itervalues():
         if ed.is_modified:
             is_modified = True
             break
     self.gui.action_save.setEnabled(is_modified)
Esempio n. 9
0
    def save_copy(self):
        c = current_container()
        ext = c.path_to_ebook.rpartition('.')[-1]
        path = choose_save_file(self.gui,
                                'tweak_book_save_copy',
                                _('Choose path'),
                                filters=[(_('Book (%s)') % ext.upper(),
                                          [ext.lower()])],
                                all_files=False)
        if not path:
            return
        tdir = self.mkdtemp(prefix='save-copy-')
        container = clone_container(c, tdir)
        for name, ed in editors.iteritems():
            if ed.is_modified or not ed.is_synced_to_container:
                self.commit_editor_to_container(name, container)

        def do_save(c, path, tdir):
            save_container(c, path)
            shutil.rmtree(tdir, ignore_errors=True)
            return path

        self.gui.blocking_job('save_copy', _('Saving copy, please wait...'),
                              self.copy_saved, do_save, container, path, tdir)
Esempio n. 10
0
 def add_savepoint(self, msg):
     nc = clone_container(current_container(), self.mkdtemp())
     self.global_undo.add_savepoint(nc, msg)
     set_current_container(nc)
     self.update_global_history_actions()
Esempio n. 11
0
 def add_savepoint(self, msg):
     nc = clone_container(current_container(), self.mkdtemp())
     self.global_undo.add_savepoint(nc, msg)
     set_current_container(nc)
     self.update_global_history_actions()
Esempio n. 12
0
 def save_book(self):
     self.gui.action_save.setEnabled(False)
     tdir = tempfile.mkdtemp(prefix='save-%05d-' % self.container_count, dir=self.tdir)
     container = clone_container(current_container(), tdir)
     self.save_manager.schedule(tdir, container)
Esempio n. 13
0
    def initialise_new_file(self, pathtoebook):
        self.meta, self.errors = {}, {}
        self.rename_file_map = {}
        self.is_scrambled = False
        self.dummyimg = None
        self.dummysvg = ''
        self.runButton.setEnabled(True)
        self.buttonBox.button(QDialogButtonBox.Save).setEnabled(False)

        fileok = True
        if not os.path.isfile(pathtoebook):
            fileok = False
        else:
            try:
                self.ebook = get_container(pathtoebook)
            except:
                fileok = False
                msg = "Source ebook must be de-DRM'd and in one of these formats:" \
                    "\n- azw3\n- epub\n- kepub\n- kepub.epub.\n\nPlease select another."
                error_dialog(self,
                             CAPTION,
                             msg,
                             show=True,
                             show_copy_button=True)

        if not fileok:
            self.log.append('No ebook selected yet')
        else:
            self.cleanup_dirs.append(self.ebook.root)
            tdir = PersistentTemporaryDirectory('_scramble_clone_orig')
            self.cleanup_dirs.append(tdir)
            self.eborig = clone_container(self.ebook, tdir)

            dirn, fname, ext, is_kepub_epub = get_fileparts(
                self.ebook.path_to_ebook)
            ext = ext.lower()
            format = 'kepub' if is_kepub_epub else ext

            if self.book_id is not None:
                # calibre library book
                self.cleanup_files.append(self.ebook.path_to_ebook)
            sourcepath = self.ebook.path_to_ebook

            self.dummyimg = get_resources('images/' + format + '.png')
            self.dummysvg = get_resources('images/' + format + '.svg')

            if self.from_calibre:
                # calibre plugin
                self.dirout = ''
            else:
                # standalone version
                self.dirout = dirn
                self.log.append('\n--- New ebook: %s' % sourcepath)

            fn = fname + '_scrambled.'
            fn += 'kepub.' + ext if is_kepub_epub else ext
            self.fname_scrambled_ebook = ascii_text(fn)
            self.sourcefile.setText(sourcepath)
            self.savefile.setText(self.fname_scrambled_ebook)
            self.meta['orig'] = get_metadata(self.ebook)
            self.errors['orig'] = get_run_check_error(self.ebook)

        self.viewlog()