Exemple #1
0
def polish_one(ebook, opts, report):
    rt = lambda x: report('\n### ' + x)
    jacket = None

    if opts.subset or opts.embed:
        stats = StatsCollector(ebook, do_embed=opts.embed)

    if opts.opf:
        rt(_('Updating metadata'))
        update_metadata(ebook, opts.opf)
        jacket = find_existing_jacket(ebook)
        if jacket is not None:
            replace_jacket(ebook, jacket)
            report(_('Updated metadata jacket'))
        report(_('Metadata updated\n'))

    if opts.cover:
        rt(_('Setting cover'))
        set_cover(ebook, opts.cover, report)
        report('')

    if opts.jacket:
        rt(_('Inserting metadata jacket'))
        if jacket is None:
            if add_or_replace_jacket(ebook):
                report(_('Existing metadata jacket replaced'))
            else:
                report(_('Metadata jacket inserted'))
        else:
            report(_('Existing metadata jacket replaced'))
        report('')

    if opts.remove_jacket:
        rt(_('Removing metadata jacket'))
        if remove_jacket(ebook):
            report(_('Metadata jacket removed'))
        else:
            report(_('No metadata jacket found'))
        report('')

    if opts.smarten_punctuation:
        rt(_('Smartening punctuation'))
        smarten_punctuation(ebook, report)
        report('')

    if opts.embed:
        rt(_('Embedding referenced fonts'))
        embed_all_fonts(ebook, stats, report)
        report('')

    if opts.subset:
        rt(_('Subsetting embedded fonts'))
        subset_all_fonts(ebook, stats.font_stats, report)
        report('')

    if opts.remove_unused_css:
        rt(_('Removing unused CSS rules'))
        remove_unused_css(ebook, report)
        report('')
Exemple #2
0
def polish(file_map, opts, log, report):
    rt = lambda x: report('\n### ' + x)
    st = time.time()
    for inbook, outbook in file_map.iteritems():
        report(_('## Polishing: %s') % (inbook.rpartition('.')[-1].upper()))
        ebook = get_container(inbook, log)
        jacket = None

        if opts.subset:
            stats = StatsCollector(ebook)

        if opts.opf:
            rt(_('Updating metadata'))
            update_metadata(ebook, opts.opf)
            jacket = find_existing_jacket(ebook)
            if jacket is not None:
                replace_jacket(ebook, jacket)
                report(_('Updated metadata jacket'))
            report(_('Metadata updated\n'))

        if opts.cover:
            rt(_('Setting cover'))
            set_cover(ebook, opts.cover, report)
            report('')

        if opts.jacket:
            rt(_('Inserting metadata jacket'))
            if jacket is None:
                if add_or_replace_jacket(ebook):
                    report(_('Existing metadata jacket replaced'))
                else:
                    report(_('Metadata jacket inserted'))
            else:
                report(_('Existing metadata jacket replaced'))
            report('')

        if opts.remove_jacket:
            rt(_('Removing metadata jacket'))
            if remove_jacket(ebook):
                report(_('Metadata jacket removed'))
            else:
                report(_('No metadata jacket found'))
            report('')

        if opts.smarten_punctuation:
            rt(_('Smartening punctuation'))
            smarten_punctuation(ebook, report)
            report('')

        if opts.subset:
            rt(_('Subsetting embedded fonts'))
            subset_all_fonts(ebook, stats.font_stats, report)
            report('')

        ebook.commit(outbook)
        report('-' * 70)
    report(_('Polishing took: %.1f seconds') % (time.time() - st))
Exemple #3
0
def polish(file_map, opts, log, report):
    rt = lambda x: report('\n### ' + x)
    st = time.time()
    for inbook, outbook in file_map.iteritems():
        report(_('## Polishing: %s')%(inbook.rpartition('.')[-1].upper()))
        ebook = get_container(inbook, log)
        jacket = None

        if opts.subset:
            stats = StatsCollector(ebook)

        if opts.opf:
            rt(_('Updating metadata'))
            update_metadata(ebook, opts.opf)
            jacket = find_existing_jacket(ebook)
            if jacket is not None:
                replace_jacket(ebook, jacket)
                report(_('Updated metadata jacket'))
            report(_('Metadata updated\n'))

        if opts.cover:
            rt(_('Setting cover'))
            set_cover(ebook, opts.cover, report)
            report('')

        if opts.jacket:
            rt(_('Inserting metadata jacket'))
            if jacket is None:
                if add_or_replace_jacket(ebook):
                    report(_('Existing metadata jacket replaced'))
                else:
                    report(_('Metadata jacket inserted'))
            else:
                report(_('Existing metadata jacket replaced'))
            report('')

        if opts.remove_jacket:
            rt(_('Removing metadata jacket'))
            if remove_jacket(ebook):
                report(_('Metadata jacket removed'))
            else:
                report(_('No metadata jacket found'))
            report('')

        if opts.smarten_punctuation:
            rt(_('Smartening punctuation'))
            smarten_punctuation(ebook, report)
            report('')

        if opts.subset:
            rt(_('Subsetting embedded fonts'))
            subset_all_fonts(ebook, stats.font_stats, report)
            report('')

        ebook.commit(outbook)
        report('-'*70)
    report(_('Polishing took: %.1f seconds')%(time.time()-st))
Exemple #4
0
def polish_one(ebook, opts, report):
    rt = lambda x: report('\n### ' + x)
    jacket = None

    if opts.subset or opts.embed:
        stats = StatsCollector(ebook, do_embed=opts.embed)

    if opts.opf:
        rt(_('Updating metadata'))
        update_metadata(ebook, opts.opf)
        jacket = find_existing_jacket(ebook)
        if jacket is not None:
            replace_jacket(ebook, jacket)
            report(_('Updated metadata jacket'))
        report(_('Metadata updated\n'))

    if opts.cover:
        rt(_('Setting cover'))
        set_cover(ebook, opts.cover, report)
        report('')

    if opts.jacket:
        rt(_('Inserting metadata jacket'))
        if jacket is None:
            if add_or_replace_jacket(ebook):
                report(_('Existing metadata jacket replaced'))
            else:
                report(_('Metadata jacket inserted'))
        else:
            report(_('Existing metadata jacket replaced'))
        report('')

    if opts.remove_jacket:
        rt(_('Removing metadata jacket'))
        if remove_jacket(ebook):
            report(_('Metadata jacket removed'))
        else:
            report(_('No metadata jacket found'))
        report('')

    if opts.smarten_punctuation:
        rt(_('Smartening punctuation'))
        smarten_punctuation(ebook, report)
        report('')

    if opts.embed:
        rt(_('Embedding referenced fonts'))
        embed_all_fonts(ebook, stats, report)
        report('')

    if opts.subset:
        rt(_('Subsetting embedded fonts'))
        subset_all_fonts(ebook, stats.font_stats, report)
        report('')
Exemple #5
0
def update_metadata(ebook, new_opf):
    from calibre.ebooks.metadata.opf2 import OPF
    from calibre.ebooks.metadata.epub import update_metadata

    opfpath = ebook.name_to_abspath(ebook.opf_name)
    with ebook.open(ebook.opf_name, "r+b") as stream, open(new_opf, "rb") as ns:
        opf = OPF(stream, basedir=os.path.dirname(opfpath), populate_spine=False, unquote_urls=False)
        mi = OPF(ns, unquote_urls=False, populate_spine=False).to_book_metadata()
        mi.cover, mi.cover_data = None, (None, None)

        update_metadata(opf, mi, apply_null=True, update_timestamp=True)
        stream.seek(0)
        stream.truncate()
        stream.write(opf.render())
Exemple #6
0
def update_metadata(ebook, new_opf):
    from calibre.ebooks.metadata.opf2 import OPF
    from calibre.ebooks.metadata.epub import update_metadata
    opfpath = ebook.name_to_abspath(ebook.opf_name)
    with ebook.open(ebook.opf_name, 'r+b') as stream, open(new_opf, 'rb') as ns:
        opf = OPF(stream, basedir=os.path.dirname(opfpath), populate_spine=False,
                  unquote_urls=False)
        mi = OPF(ns, unquote_urls=False,
                      populate_spine=False).to_book_metadata()
        mi.cover, mi.cover_data = None, (None, None)

        update_metadata(opf, mi, apply_null=True, update_timestamp=True)
        stream.seek(0)
        stream.truncate()
        stream.write(opf.render())
Exemple #7
0
def polish_one(ebook, opts, report, customization=None):
    rt = lambda x: report('\n### ' + x)
    jacket = None
    changed = False
    customization = customization or CUSTOMIZATION.copy()

    if opts.subset or opts.embed:
        stats = StatsCollector(ebook, do_embed=opts.embed)

    if opts.opf:
        changed = True
        rt(_('Updating metadata'))
        update_metadata(ebook, opts.opf)
        jacket = find_existing_jacket(ebook)
        if jacket is not None:
            replace_jacket(ebook, jacket)
            report(_('Updated metadata jacket'))
        report(_('Metadata updated\n'))

    if opts.cover:
        changed = True
        rt(_('Setting cover'))
        set_cover(ebook, opts.cover, report)
        report('')

    if opts.jacket:
        changed = True
        rt(_('Inserting metadata jacket'))
        if jacket is None:
            if add_or_replace_jacket(ebook):
                report(_('Existing metadata jacket replaced'))
            else:
                report(_('Metadata jacket inserted'))
        else:
            report(_('Existing metadata jacket replaced'))
        report('')

    if opts.remove_jacket:
        rt(_('Removing metadata jacket'))
        if remove_jacket(ebook):
            report(_('Metadata jacket removed'))
            changed = True
        else:
            report(_('No metadata jacket found'))
        report('')

    if opts.smarten_punctuation:
        rt(_('Smartening punctuation'))
        if smarten_punctuation(ebook, report):
            changed = True
        report('')

    if opts.embed:
        rt(_('Embedding referenced fonts'))
        if embed_all_fonts(ebook, stats, report):
            changed = True
        report('')

    if opts.subset:
        rt(_('Subsetting embedded fonts'))
        if subset_all_fonts(ebook, stats.font_stats, report):
            changed = True
        report('')

    if opts.remove_unused_css:
        rt(_('Removing unused CSS rules'))
        if remove_unused_css(
                ebook,
                report,
                remove_unused_classes=customization['remove_unused_classes']):
            changed = True
        report('')

    return changed
Exemple #8
0
def polish_one(ebook, opts, report, customization=None):
    rt = lambda x: report("\n### " + x)
    jacket = None
    changed = False
    customization = customization or CUSTOMIZATION.copy()

    if opts.subset or opts.embed:
        stats = StatsCollector(ebook, do_embed=opts.embed)

    if opts.opf:
        changed = True
        rt(_("Updating metadata"))
        update_metadata(ebook, opts.opf)
        jacket = find_existing_jacket(ebook)
        if jacket is not None:
            replace_jacket(ebook, jacket)
            report(_("Updated metadata jacket"))
        report(_("Metadata updated\n"))

    if opts.cover:
        changed = True
        rt(_("Setting cover"))
        set_cover(ebook, opts.cover, report)
        report("")

    if opts.jacket:
        changed = True
        rt(_("Inserting metadata jacket"))
        if jacket is None:
            if add_or_replace_jacket(ebook):
                report(_("Existing metadata jacket replaced"))
            else:
                report(_("Metadata jacket inserted"))
        else:
            report(_("Existing metadata jacket replaced"))
        report("")

    if opts.remove_jacket:
        rt(_("Removing metadata jacket"))
        if remove_jacket(ebook):
            report(_("Metadata jacket removed"))
            changed = True
        else:
            report(_("No metadata jacket found"))
        report("")

    if opts.smarten_punctuation:
        rt(_("Smartening punctuation"))
        if smarten_punctuation(ebook, report):
            changed = True
        report("")

    if opts.embed:
        rt(_("Embedding referenced fonts"))
        if embed_all_fonts(ebook, stats, report):
            changed = True
        report("")

    if opts.subset:
        rt(_("Subsetting embedded fonts"))
        if subset_all_fonts(ebook, stats.font_stats, report):
            changed = True
        report("")

    if opts.remove_unused_css:
        rt(_("Removing unused CSS rules"))
        if remove_unused_css(ebook, report, remove_unused_classes=customization["remove_unused_classes"]):
            changed = True
        report("")

    return changed
Exemple #9
0
def polish_one(ebook, opts, report, customization=None):
    rt = lambda x: report('\n### ' + x)
    jacket = None
    changed = False
    customization = customization or CUSTOMIZATION.copy()

    if opts.subset or opts.embed:
        stats = StatsCollector(ebook, do_embed=opts.embed)

    if opts.opf:
        changed = True
        rt(_('Updating metadata'))
        update_metadata(ebook, opts.opf)
        jacket = find_existing_jacket(ebook)
        if jacket is not None:
            replace_jacket(ebook, jacket)
            report(_('Updated metadata jacket'))
        report(_('Metadata updated\n'))

    if opts.cover:
        changed = True
        rt(_('Setting cover'))
        set_cover(ebook, opts.cover, report)
        report('')

    if opts.jacket:
        changed = True
        rt(_('Inserting metadata jacket'))
        if jacket is None:
            if add_or_replace_jacket(ebook):
                report(_('Existing metadata jacket replaced'))
            else:
                report(_('Metadata jacket inserted'))
        else:
            report(_('Existing metadata jacket replaced'))
        report('')

    if opts.remove_jacket:
        rt(_('Removing metadata jacket'))
        if remove_jacket(ebook):
            report(_('Metadata jacket removed'))
            changed = True
        else:
            report(_('No metadata jacket found'))
        report('')

    if opts.smarten_punctuation:
        rt(_('Smartening punctuation'))
        if smarten_punctuation(ebook, report):
            changed = True
        report('')

    if opts.embed:
        rt(_('Embedding referenced fonts'))
        if embed_all_fonts(ebook, stats, report):
            changed = True
        report('')

    if opts.subset:
        rt(_('Subsetting embedded fonts'))
        if subset_all_fonts(ebook, stats.font_stats, report):
            changed = True
        report('')

    if opts.remove_unused_css:
        rt(_('Removing unused CSS rules'))
        if remove_unused_css(ebook, report, remove_unused_classes=customization['remove_unused_classes']):
            changed = True
        report('')

    if opts.compress_images:
        rt(_('Losslessly compressing images'))
        if compress_images(ebook, report)[0]:
            changed = True
        report('')

    return changed