Example #1
0
 def __call__(self, container):
     changed = False
     if self.change_ext_to is not None:
         from calibre.ebooks.oeb.polish.replace import rename_files
         new_name = self.file_name.rpartition(
             '.')[0] + '.' + self.change_ext_to
         c = 0
         while container.has_name(new_name):
             c += 1
             new_name = self.file_name.rpartition('.')[0] + (
                 '%d.' % c) + self.change_ext_to
         rename_files(container, {self.file_name: new_name})
         changed = True
     else:
         for item in container.opf_xpath(
                 '//opf:manifest/opf:item[@href and @media-type="%s"]' %
                 self.opf_mt):
             name = container.href_to_name(item.get('href'),
                                           container.opf_name)
             if name == self.file_name:
                 changed = True
                 item.set('media-type', self.ext_mt)
                 container.mime_map[name] = self.ext_mt
         if changed:
             container.dirty(container.opf_name)
     return changed
Example #2
0
 def __call__(self, container):
     from calibre.ebooks.oeb.polish.replace import rename_files
     all_names = set(container.name_path_map)
     bn, ext = self.sname.rpartition('.')[0::2]
     c = 0
     while self.sname in all_names:
         c += 1
         self.sname = '%s_%d.%s' % (bn, c, ext)
     rename_files(container, {self.name:self.sname})
     return True
Example #3
0
 def __call__(self, container):
     from calibre.ebooks.oeb.polish.replace import rename_files
     all_names = set(container.name_path_map)
     bn, ext = self.sname.rpartition('.')[0::2]
     c = 0
     while self.sname in all_names:
         c += 1
         self.sname = '%s_%d.%s' % (bn, c, ext)
     rename_files(container, {self.name:self.sname})
     return True
Example #4
0
 def __call__(self, container):
     changed = False
     if self.change_ext_to is not None:
         from calibre.ebooks.oeb.polish.replace import rename_files
         new_name = self.file_name.rpartition('.')[0] + '.' + self.change_ext_to
         c = 0
         while container.has_name(new_name):
             c += 1
             new_name = self.file_name.rpartition('.')[0] + ('%d.' % c) + self.change_ext_to
         rename_files(container, {self.file_name:new_name})
         changed = True
     else:
         for item in container.opf_xpath('//opf:manifest/opf:item[@href and @media-type="%s"]' % self.opf_mt):
             name = container.href_to_name(item.get('href'), container.opf_name)
             if name == self.file_name:
                 changed = True
                 item.set('media-type', self.ext_mt)
                 container.mime_map[name] = self.ext_mt
         if changed:
             container.dirty(container.opf_name)
     return changed
Example #5
0
    def test_file_rename(self):
        ' Test renaming of files '
        book = get_simple_book()
        count = [0]

        def new_container():
            count[0] += 1
            tdir = os.mkdir(os.path.join(self.tdir, unicode_type(count[0])))
            return get_container(book, tdir=tdir)

        # Test simple opf rename
        c = new_container()
        orig_name = c.opf_name
        name = 'renamed opf.opf'
        rename_files(c, {c.opf_name: name})
        self.assertEqual(c.opf_name, name)
        for x in ('name_path_map', 'mime_map'):
            self.assertNotIn(orig_name, getattr(c, x))
            self.assertIn(name, getattr(c, x))
        self.assertNotIn(name, c.dirtied)
        root = c.parsed('META-INF/container.xml')
        vals = set(
            root.xpath(r'child::ocf:rootfiles/ocf:rootfile/@full-path',
                       namespaces={'ocf': OCF_NS}))
        self.assertSetEqual(vals, {name})
        self.check_links(c)

        # Test a rename that moves the OPF into different directory
        c = new_container()
        orig_name = c.opf_name
        name = 'renamed/again/metadata.opf'
        rename_files(c, {c.opf_name: name})
        self.check_links(c)

        # Test that renaming commits dirtied items
        c = new_container()
        name = next(c.spine_names)[0]
        root = c.parsed(name)
        root.xpath('//*[local-name()="body"]')[0].set('id',
                                                      'rename-dirty-test')
        rename_files(c, {name: 'other/' + name})
        with c.open('other/' + name) as f:
            raw = f.read()
        self.assertIn(b'id="rename-dirty-test"', raw)
        self.check_links(c)

        # Test renaming of stylesheets
        c = new_container()
        rename_files(
            c, {
                'stylesheet.css': 'styles/s 1.css',
                'page_styles.css': 'styles/p 1.css'
            })
        self.check_links(c)

        # Test renaming of images
        c = new_container()
        rename_files(
            c, {
                'cover.png': 'images/cover img.png',
                'light_wood.png': 'images/light wood.png',
                'marked.png': 'images/marked img.png'
            })
        self.check_links(c)

        # Test renaming of ToC
        c = new_container()
        rename_files(c, {'toc.ncx': 'toc/toc file.ncx'})
        self.check_links(c)

        # Test renaming of font files
        c = new_container()
        fname = 'LiberationMono-Regular.ttf'
        if fname not in c.name_path_map:
            fname = fname.lower(
            )  # On OS X the font file name is lowercased for some reason (maybe on windows too)
        rename_files(c, {fname: 'fonts/LiberationMono Regular.ttf'})
        self.check_links(c)

        # Test renaming of text files
        c = new_container()
        rename_files(
            c, {
                'index_split_000.html': 'text/page one fällen.html',
                'index_split_001.html': 'text/page two fällen.html'
            })
        self.check_links(c)

        # Test rename with only case change
        c = new_container()
        rename_files(c, {'index_split_000.html': 'Index_split_000.html'})
        self.check_links(c)
Example #6
0
    def test_file_rename(self):
        " Test renaming of files "
        book = get_simple_book()
        count = [0]

        def new_container():
            count[0] += 1
            tdir = os.mkdir(os.path.join(self.tdir, str(count[0])))
            return get_container(book, tdir=tdir)

        # Test simple opf rename
        c = new_container()
        orig_name = c.opf_name
        name = "renamed opf.opf"
        rename_files(c, {c.opf_name: name})
        self.assertEqual(c.opf_name, name)
        for x in ("name_path_map", "mime_map"):
            self.assertNotIn(orig_name, getattr(c, x))
            self.assertIn(name, getattr(c, x))
        self.assertNotIn(name, c.dirtied)
        root = c.parsed("META-INF/container.xml")
        vals = set(root.xpath(r"child::ocf:rootfiles/ocf:rootfile/@full-path", namespaces={"ocf": OCF_NS}))
        self.assertSetEqual(vals, {name})
        self.check_links(c)

        # Test a rename that moves the OPF into different directory
        c = new_container()
        orig_name = c.opf_name
        name = "renamed/again/metadata.opf"
        rename_files(c, {c.opf_name: name})
        self.check_links(c)

        # Test that renaming commits dirtied items
        c = new_container()
        name = next(c.spine_names)[0]
        root = c.parsed(name)
        root.xpath('//*[local-name()="body"]')[0].set("id", "rename-dirty-test")
        rename_files(c, {name: "other/" + name})
        with c.open("other/" + name) as f:
            raw = f.read()
        self.assertIn(b'id="rename-dirty-test"', raw)
        self.check_links(c)

        # Test renaming of stylesheets
        c = new_container()
        rename_files(c, {"stylesheet.css": "styles/s 1.css", "page_styles.css": "styles/p 1.css"})
        self.check_links(c)

        # Test renaming of images
        c = new_container()
        rename_files(
            c,
            {
                "cover.png": "images/cover img.png",
                "light_wood.png": "images/light wood.png",
                "marked.png": "images/marked img.png",
            },
        )
        self.check_links(c)

        # Test renaming of ToC
        c = new_container()
        rename_files(c, {"toc.ncx": "toc/toc file.ncx"})
        self.check_links(c)

        # Test renaming of font files
        c = new_container()
        fname = "LiberationMono-Regular.ttf"
        if fname not in c.name_path_map:
            fname = fname.lower()  # On OS X the font file name is lowercased for some reason (maybe on windows too)
        rename_files(c, {fname: "fonts/LiberationMono Regular.ttf"})
        self.check_links(c)

        # Test renaming of text files
        c = new_container()
        rename_files(
            c,
            {"index_split_000.html": "text/page one fällen.html", "index_split_001.html": "text/page two fällen.html"},
        )
        self.check_links(c)

        # Test rename with only case change
        c = new_container()
        rename_files(c, {"index_split_000.html": "Index_split_000.html"})
        self.check_links(c)
Example #7
0
    def test_file_rename(self):
        ' Test renaming of files '
        book = get_simple_book()
        count = [0]

        def new_container():
            count[0] += 1
            tdir = os.mkdir(os.path.join(self.tdir, str(count[0])))
            return get_container(book, tdir=tdir)

        # Test simple opf rename
        c = new_container()
        orig_name = c.opf_name
        name = 'renamed opf.opf'
        rename_files(c, {c.opf_name: name})
        self.assertEqual(c.opf_name, name)
        for x in ('name_path_map', 'mime_map'):
            self.assertNotIn(orig_name, getattr(c, x))
            self.assertIn(name, getattr(c, x))
        self.assertNotIn(name, c.dirtied)
        root = c.parsed('META-INF/container.xml')
        vals = set(root.xpath(
            r'child::ocf:rootfiles/ocf:rootfile/@full-path',
            namespaces={'ocf':OCF_NS}))
        self.assertSetEqual(vals, {name})
        self.check_links(c)

        # Test a rename that moves the OPF into different directory
        c = new_container()
        orig_name = c.opf_name
        name = 'renamed/again/metadata.opf'
        rename_files(c, {c.opf_name: name})
        self.check_links(c)

        # Test that renaming commits dirtied items
        c = new_container()
        name = next(c.spine_names)[0]
        root = c.parsed(name)
        root.xpath('//*[local-name()="body"]')[0].set('id', 'rename-dirty-test')
        rename_files(c, {name:'other/' + name})
        with c.open('other/' + name) as f:
            raw = f.read()
        self.assertIn(b'id="rename-dirty-test"', raw)
        self.check_links(c)

        # Test renaming of stylesheets
        c = new_container()
        rename_files(c, {'stylesheet.css':'styles/s 1.css', 'page_styles.css':'styles/p 1.css'})
        self.check_links(c)

        # Test renaming of images
        c = new_container()
        rename_files(c, {'cover.png':'images/cover img.png', 'light_wood.png':'images/light wood.png', 'marked.png':'images/marked img.png'})
        self.check_links(c)

        # Test renaming of ToC
        c = new_container()
        rename_files(c, {'toc.ncx': 'toc/toc file.ncx'})
        self.check_links(c)

        # Test renaming of font files
        c = new_container()
        fname = 'LiberationMono-Regular.ttf'
        if fname not in c.name_path_map:
            fname = fname.lower()  # On OS X the font file name is lowercased for some reason (maybe on windows too)
        rename_files(c, {fname: 'fonts/LiberationMono Regular.ttf'})
        self.check_links(c)

        # Test renaming of text files
        c = new_container()
        rename_files(c, {'index_split_000.html':'text/page one fällen.html', 'index_split_001.html':'text/page two fällen.html'})
        self.check_links(c)

        # Test rename with only case change
        c = new_container()
        rename_files(c, {'index_split_000.html':'Index_split_000.html'})
        self.check_links(c)
    def scramble_main(self):
        # NB: an epub3 nav.xhtml file will currently be scrambled by HTML rules not NCX rules
        textnames = get_textnames(self.eb)
        if self.dsettings['x_html']:
            [
                self.scramble_html(n, scramble_dgts=self.dsettings['x_dgts'])
                for n in textnames
            ]
            self.log.append('   Scrambled text content')

        self.ncxnames = get_ncxnames(self.eb)
        # no need to scramble digits in a TOC
        if self.dsettings['x_toc']:
            if len(self.ncxnames) > 0:
                self.scramble_toc(self.ncxnames[0], scramble_dgts=False)
                self.log.append('   Scrambled TOC')

        svgnames = get_imgnames(self.eb, SVG_MIME)
        imgnames = get_imgnames(self.eb, OEB_RASTER_IMAGES)
        if self.dsettings['x_imgs']:
            cover_img_name = find_cover_image(self.eb, strict=True)
            cover_img_names = []
            if self.dsettings['keep_cover']:
                if cover_img_name:
                    cover_img_names.append(cover_img_name)
            [
                self.scramble_img(n) for n in imgnames
                if n not in cover_img_names
            ]
            for svgn in [n for n in svgnames if n not in cover_img_names]:
                #self.eb.remove_item(svgn)
                data = self.eb.parsed(svgn)
                self.eb.replace(svgn, self.dummysvg)
            self.log.append('   Replaced images')

        fontnames = get_fontnames(self.eb)
        if len(fontnames) > 0 and (self.dsettings['x_fontsno']
                                   or self.dsettings['x_fontsob']):
            self.log.append('   Removed these fonts:')
        if self.dsettings['x_fontsno']:
            # remove non-obfuscated embedded fonts
            for name in [
                    n for n in fontnames if n not in self.eb.obfuscated_fonts
            ]:
                self.eb.remove_item(name)
                self.log.append('      - non-obfuscated font: %s' % name)
        if self.dsettings['x_fontsob']:
            # remove obfuscated embedded fonts
            for name in [n for n in self.eb.obfuscated_fonts]:
                self.eb.remove_item(name)
                self.log.append('      - obfuscated font: %s' % name)

        if self.dsettings['x_meta']:
            self.scramble_metadata()
            msg = '   Removed basic metadata'
            if self.dsettings['x_meta_extra']:
                msg += ' & extra metadata'
            self.log.append(msg)

        if self.dsettings['x_fnames']:
            spine_names = tuple([
                n for n in get_spinenames(self.eb)
                if n not in self.eb.names_that_must_not_be_changed
            ])
            self.scramble_filenames(spine_names, 'txcontent_')

            svgnames = get_imgnames(self.eb, SVG_MIME)
            img_names = tuple([
                n for n in imgnames + svgnames
                if n not in self.eb.names_that_must_not_be_changed
            ])
            self.scramble_filenames(img_names, 'img_')

            css_names = tuple([
                n for n in get_cssnames(self.eb)
                if n not in self.eb.names_that_must_not_be_changed
            ])
            self.scramble_filenames(css_names, 'style_')

        if self.file_map:
            rename_files(self.eb, self.file_map)
            self.log.append('   Renamed internal files:')
            [
                self.log.append('      %s \t--> %s' %
                                (old, self.file_map.get(old, old)))
                for old in spine_names + img_names + css_names
            ]