本文整理汇总了Python中calibre.ebooks.metadata.opf2.OPFCreator.render方法的典型用法代码示例。如果您正苦于以下问题:Python OPFCreator.render方法的具体用法?Python OPFCreator.render怎么用?Python OPFCreator.render使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类calibre.ebooks.metadata.opf2.OPFCreator
的用法示例。
在下文中一共展示了OPFCreator.render方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def write(self, doc):
toc = create_toc(doc, self.body, self.resolved_link_map, self.styles, self.object_map, self.log)
raw = html.tostring(self.html, encoding='utf-8', doctype='<!DOCTYPE html>')
with open(os.path.join(self.dest_dir, 'index.html'), 'wb') as f:
f.write(raw)
css = self.styles.generate_css(self.dest_dir, self.docx)
if css:
with open(os.path.join(self.dest_dir, 'docx.css'), 'wb') as f:
f.write(css.encode('utf-8'))
opf = OPFCreator(self.dest_dir, self.mi)
opf.toc = toc
opf.create_manifest_from_files_in([self.dest_dir])
for item in opf.manifest:
if item.media_type == 'text/html':
item.media_type = guess_type('a.xhtml')[0]
opf.create_spine(['index.html'])
if self.cover_image is not None:
opf.guide.set_cover(self.cover_image)
toc_file = os.path.join(self.dest_dir, 'toc.ncx')
with open(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(toc_file, 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx')
if os.path.getsize(toc_file) == 0:
os.remove(toc_file)
return os.path.join(self.dest_dir, 'metadata.opf')
示例2: write
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def write(self, doc):
toc = create_toc(doc, self.body, self.resolved_link_map, self.styles, self.object_map, self.log, self.namespace)
raw = html.tostring(self.html, encoding='utf-8', doctype='<!DOCTYPE html>')
with lopen(os.path.join(self.dest_dir, 'index.html'), 'wb') as f:
f.write(raw)
css = self.styles.generate_css(self.dest_dir, self.docx, self.notes_nopb, self.nosupsub)
if css:
with lopen(os.path.join(self.dest_dir, 'docx.css'), 'wb') as f:
f.write(css.encode('utf-8'))
opf = OPFCreator(self.dest_dir, self.mi)
opf.toc = toc
opf.create_manifest_from_files_in([self.dest_dir])
for item in opf.manifest:
if item.media_type == 'text/html':
item.media_type = guess_type('a.xhtml')[0]
opf.create_spine(['index.html'])
if self.cover_image is not None:
opf.guide.set_cover(self.cover_image)
def process_guide(E, guide):
if self.toc_anchor is not None:
guide.append(E.reference(
href='index.html#' + self.toc_anchor, title=_('Table of Contents'), type='toc'))
toc_file = os.path.join(self.dest_dir, 'toc.ncx')
with lopen(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(toc_file, 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx', process_guide=process_guide)
if os.path.getsize(toc_file) == 0:
os.remove(toc_file)
return os.path.join(self.dest_dir, 'metadata.opf')
示例3: convert
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def convert(self, stream, options, file_ext, log,
accelerators):
from calibre.ebooks.metadata.opf2 import OPFCreator
from calibre.ebooks.pdf.pdftohtml import pdftohtml
log.debug('Converting file to html...')
# The main html file will be named index.html
self.opts, self.log = options, log
if options.new_pdf_engine:
return self.convert_new(stream, accelerators)
pdftohtml(os.getcwdu(), stream.name, options.no_images)
from calibre.ebooks.metadata.meta import get_metadata
log.debug('Retrieving document metadata...')
mi = get_metadata(stream, 'pdf')
opf = OPFCreator(os.getcwdu(), mi)
manifest = [(u'index.html', None)]
images = os.listdir(os.getcwdu())
images.remove('index.html')
for i in images:
manifest.append((i, None))
log.debug('Generating manifest...')
opf.create_manifest(manifest)
opf.create_spine([u'index.html'])
log.debug('Rendering manifest...')
with open(u'metadata.opf', 'wb') as opffile:
opf.render(opffile)
return os.path.join(os.getcwdu(), u'metadata.opf')
示例4: write_opf
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def write_opf(self, guide, toc, spine, resource_map):
mi = self.header.exth.mi
if (self.cover_offset is not None and self.cover_offset <
len(resource_map)):
mi.cover = resource_map[self.cover_offset]
if len(list(toc)) < 2:
self.log.warn('KF8 has no metadata Table of Contents')
for ref in guide:
if ref.type == 'toc':
href = ref.href()
href, frag = urldefrag(href)
if os.path.exists(href.replace('/', os.sep)):
try:
toc = self.read_inline_toc(href, frag)
except:
self.log.exception('Failed to read inline ToC')
opf = OPFCreator(os.getcwdu(), mi)
opf.guide = guide
def exclude(path):
return os.path.basename(path) == 'debug-raw.html'
opf.create_manifest_from_files_in([os.getcwdu()], exclude=exclude)
opf.create_spine(spine)
opf.set_toc(toc)
with open('metadata.opf', 'wb') as of, open('toc.ncx', 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx')
return 'metadata.opf'
示例5: main
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def main(args=sys.argv):
parser = option_parser()
opts, args = parser.parse_args(args)
if len(args) < 2:
parser.print_help()
prints(_('No file specified'), file=sys.stderr)
return 1
path = args[1]
stream_type = os.path.splitext(path)[1].replace('.', '').lower()
trying_to_set = False
for pref in config().option_set.preferences:
if pref.name in ('to_opf', 'get_cover'):
continue
if getattr(opts, pref.name) is not None:
trying_to_set = True
break
with open(path, 'rb') as stream:
mi = get_metadata(stream, stream_type, force_read_metadata=True)
if trying_to_set:
prints(_('Original metadata')+'::')
metadata = unicode_type(mi)
if trying_to_set:
metadata = '\t'+'\n\t'.join(metadata.split('\n'))
prints(metadata, safe_encode=True)
if trying_to_set:
with open(path, 'r+b') as stream:
do_set_metadata(opts, mi, stream, stream_type)
stream.seek(0)
stream.flush()
lrf = None
if stream_type == 'lrf':
if opts.lrf_bookid is not None:
lrf = LRFMetaFile(stream)
lrf.book_id = opts.lrf_bookid
mi = get_metadata(stream, stream_type, force_read_metadata=True)
prints('\n' + _('Changed metadata') + '::')
metadata = unicode_type(mi)
metadata = '\t'+'\n\t'.join(metadata.split('\n'))
prints(metadata, safe_encode=True)
if lrf is not None:
prints('\tBookID:', lrf.book_id)
if opts.to_opf is not None:
from calibre.ebooks.metadata.opf2 import OPFCreator
opf = OPFCreator(getcwd(), mi)
with open(opts.to_opf, 'wb') as f:
opf.render(f)
prints(_('OPF created in'), opts.to_opf)
if opts.get_cover is not None:
if mi.cover_data and mi.cover_data[1]:
with open(opts.get_cover, 'wb') as f:
f.write(mi.cover_data[1])
prints(_('Cover saved to'), f.name)
else:
prints(_('No cover found'), file=sys.stderr)
return 0
示例6: do_show_metadata
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def do_show_metadata(db, id, as_opf):
if not db.has_id(id):
raise ValueError('Id #%d is not present in database.'%id)
mi = db.get_metadata(id, index_is_id=True)
if as_opf:
mi = OPFCreator(os.getcwdu(), mi)
mi.render(sys.stdout)
else:
prints(unicode(mi))
示例7: convert
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def convert(self, stream, options, file_ext, log,
accelerators):
from calibre.ebooks.metadata.toc import TOC
from calibre.ebooks.metadata.opf2 import OPFCreator
from calibre.utils.zipfile import ZipFile
self.options = options
self.log = log
pages, images = [], []
toc = TOC()
if file_ext == 'pmlz':
log.debug('De-compressing content to temporary directory...')
with TemporaryDirectory('_unpmlz') as tdir:
zf = ZipFile(stream)
zf.extractall(tdir)
pmls = glob.glob(os.path.join(tdir, '*.pml'))
for pml in pmls:
html_name = os.path.splitext(os.path.basename(pml))[0]+'.html'
html_path = os.path.join(getcwd(), html_name)
pages.append(html_name)
log.debug('Processing PML item %s...' % pml)
ttoc = self.process_pml(pml, html_path)
toc += ttoc
images = self.get_images(stream, tdir, True)
else:
toc = self.process_pml(stream, 'index.html')
pages.append('index.html')
if hasattr(stream, 'name'):
images = self.get_images(stream, os.path.abspath(os.path.dirname(stream.name)))
# We want pages to be orded alphabetically.
pages.sort()
manifest_items = []
for item in pages+images:
manifest_items.append((item, None))
from calibre.ebooks.metadata.meta import get_metadata
log.debug('Reading metadata from input file...')
mi = get_metadata(stream, 'pml')
if 'images/cover.png' in images:
mi.cover = 'images/cover.png'
opf = OPFCreator(getcwd(), mi)
log.debug('Generating manifest...')
opf.create_manifest(manifest_items)
opf.create_spine(pages)
opf.set_toc(toc)
with lopen('metadata.opf', 'wb') as opffile:
with lopen('toc.ncx', 'wb') as tocfile:
opf.render(opffile, tocfile, 'toc.ncx')
return os.path.join(getcwd(), 'metadata.opf')
示例8: write_opf
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def write_opf(self, guide, toc, spine, resource_map):
mi = self.header.exth.mi
if (self.cover_offset is not None and self.cover_offset <
len(resource_map)):
mi.cover = resource_map[self.cover_offset]
if len(list(toc)) < 2:
self.log.warn('KF8 has no metadata Table of Contents')
for ref in guide:
if ref.type == 'toc':
href = ref.href()
href, frag = urldefrag(href)
if os.path.exists(href.replace('/', os.sep)):
try:
toc = self.read_inline_toc(href, frag)
except:
self.log.exception('Failed to read inline ToC')
opf = OPFCreator(os.getcwdu(), mi)
opf.guide = guide
def exclude(path):
return os.path.basename(path) == 'debug-raw.html'
# If there are no images then the azw3 input plugin dumps all
# binary records as .unknown images, remove them
if self.for_tweak and os.path.exists('images') and os.path.isdir('images'):
files = os.listdir('images')
unknown = [x for x in files if x.endswith('.unknown')]
if len(files) == len(unknown):
[os.remove('images/'+f) for f in files]
if self.for_tweak:
try:
os.remove('debug-raw.html')
except:
pass
opf.create_manifest_from_files_in([os.getcwdu()], exclude=exclude)
for entry in opf.manifest:
if entry.mime_type == 'text/html':
entry.mime_type = 'application/xhtml+xml'
opf.create_spine(spine)
opf.set_toc(toc)
ppd = getattr(self.header.exth, 'page_progression_direction', None)
if ppd in {'ltr', 'rtl', 'default'}:
opf.page_progression_direction = ppd
pwm = getattr(self.header.exth, 'primary_writing_mode', None)
if pwm is not None:
opf.primary_writing_mode = pwm
with open('metadata.opf', 'wb') as of, open('toc.ncx', 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx')
return 'metadata.opf'
示例9: convert
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def convert(self, stream, options, file_ext, log, accelerators):
log.debug('Enter convert() ...')
dest_dir = os.getcwdu() # note: temp dir from calibre process
log.debug('dest_dir: ' + dest_dir)
mi = None
# call latex2mobi with markup output only
from subprocess import check_output, STDOUT, CalledProcessError
args = [self.java_exec, '-jar', os.path.join(self.plugin_dir, JAR_FILENAME), '-i', stream.name,
'-n', '-o', dest_dir]
from calibre_plugins.latexformulas_input.config import prefs
if prefs['pandoc_exec'] != None and prefs['pandoc_exec'] != '':
args.append('-p')
args.append(prefs['pandoc_exec'])
try:
log.debug(check_output(args, stderr=STDOUT))
except CalledProcessError as e:
log.debug(e.returncode)
log.debug(e.cmd)
log.debug(e.output)
opf = OPFCreator(dest_dir, mi)
markup_dir = dest_dir + os.path.sep + os.path.basename(stream.name) + '-markup'
log.debug('Markup-dir: ' + markup_dir)
log.debug('CreateManifestFromFilesIn()')
opf.create_manifest_from_files_in([markup_dir])
for item in opf.manifest:
if item.media_type == 'text/html':
log.debug('Item ' + str(item) + ' is of type text/html')
item.media_type = guess_type('a.html')[0]
log.debug('Guess type result: ' + item.media_type)
if item.media_type == 'text/css':
log.debug('Item ' + str(item) + ' is of type text/css')
item.media_type = guess_type('a.css')[0]
log.debug('Guess type result: ' + item.media_type)
log.debug('Create_spine()')
opf.create_spine([os.path.basename(markup_dir) + os.path.sep + 'latex2mobi.html'])
output_path = os.path.join(dest_dir, 'metadata.opf')
with open(output_path, 'wb') as of:
opf.render(of)
log('Exit convert() ...')
return output_path
示例10: write_opf
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def write_opf(self, guide, toc, spine, resource_map):
mi = self.header.exth.mi
if self.cover_offset is not None and self.cover_offset < len(resource_map):
mi.cover = resource_map[self.cover_offset]
if len(list(toc)) < 2:
self.log.warn("KF8 has no metadata Table of Contents")
for ref in guide:
if ref.type == "toc":
href = ref.href()
href, frag = urldefrag(href)
if os.path.exists(href.replace("/", os.sep)):
try:
toc = self.read_inline_toc(href, frag)
except:
self.log.exception("Failed to read inline ToC")
opf = OPFCreator(os.getcwdu(), mi)
opf.guide = guide
def exclude(path):
return os.path.basename(path) == "debug-raw.html"
# If there are no images then the azw3 input plugin dumps all
# binary records as .unknown images, remove them
if self.for_tweak and os.path.exists("images") and os.path.isdir("images"):
files = os.listdir("images")
unknown = [x for x in files if x.endswith(".unknown")]
if len(files) == len(unknown):
[os.remove("images/" + f) for f in files]
if self.for_tweak:
try:
os.remove("debug-raw.html")
except:
pass
opf.create_manifest_from_files_in([os.getcwdu()], exclude=exclude)
for entry in opf.manifest:
if entry.mime_type == "text/html":
entry.mime_type = "application/xhtml+xml"
opf.create_spine(spine)
opf.set_toc(toc)
ppd = getattr(self.header.exth, "page_progression_direction", None)
if ppd in {"ltr", "rtl", "default"}:
opf.page_progression_direction = ppd
with open("metadata.opf", "wb") as of, open("toc.ncx", "wb") as ncx:
opf.render(of, ncx, "toc.ncx")
return "metadata.opf"
示例11: main
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def main(opts, args, dbctx):
if len(args) < 1:
raise SystemExit(_('You must specify an id'))
book_id = int(args[0])
mi = dbctx.run('show_metadata', book_id)
if mi is None:
raise SystemExit('Id #%d is not present in database.' % id)
if opts.as_opf:
mi = OPFCreator(os.getcwdu(), mi)
mi.render(sys.stdout)
else:
prints(unicode(mi))
return 0
示例12: create_opf
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def create_opf(self, output_dir, pages, images):
with CurrentDir(output_dir):
opf = OPFCreator(output_dir, self.mi)
manifest = []
for page in pages+images:
manifest.append((page, None))
opf.create_manifest(manifest)
opf.create_spine(pages)
with open('metadata.opf', 'wb') as opffile:
opf.render(opffile)
return os.path.join(output_dir, 'metadata.opf')
示例13: create_opf
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def create_opf(self, output_dir, images):
with CurrentDir(output_dir):
opf = OPFCreator(output_dir, self.mi)
manifest = [('index.html', None)]
for i in images:
manifest.append((os.path.join('images/', i), None))
opf.create_manifest(manifest)
opf.create_spine(['index.html'])
with open('metadata.opf', 'wb') as opffile:
opf.render(opffile)
return os.path.join(output_dir, 'metadata.opf')
示例14: write
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def write(self):
toc = self.create_toc()
raw = html.tostring(self.html, encoding='utf-8', doctype='<!DOCTYPE html>')
with open(os.path.join(self.dest_dir, 'index.html'), 'wb') as f:
f.write(raw)
css = self.styles.generate_css(self.dest_dir, self.docx)
if css:
with open(os.path.join(self.dest_dir, 'docx.css'), 'wb') as f:
f.write(css.encode('utf-8'))
opf = OPFCreator(self.dest_dir, self.mi)
opf.toc = toc
opf.create_manifest_from_files_in([self.dest_dir])
opf.create_spine(['index.html'])
with open(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(os.path.join(self.dest_dir, 'toc.ncx'), 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx')
return os.path.join(self.dest_dir, 'metadata.opf')
示例15: write
# 需要导入模块: from calibre.ebooks.metadata.opf2 import OPFCreator [as 别名]
# 或者: from calibre.ebooks.metadata.opf2.OPFCreator import render [as 别名]
def write(self, doc):
toc = create_toc(doc, self.body, self.resolved_link_map, self.styles, self.object_map)
raw = html.tostring(self.html, encoding='utf-8', doctype='<!DOCTYPE html>')
with open(os.path.join(self.dest_dir, 'index.html'), 'wb') as f:
f.write(raw)
css = self.styles.generate_css(self.dest_dir, self.docx)
if css:
with open(os.path.join(self.dest_dir, 'docx.css'), 'wb') as f:
f.write(css.encode('utf-8'))
opf = OPFCreator(self.dest_dir, self.mi)
opf.toc = toc
opf.create_manifest_from_files_in([self.dest_dir])
opf.create_spine(['index.html'])
if self.cover_image is not None:
opf.guide.set_cover(self.cover_image)
with open(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(os.path.join(self.dest_dir, 'toc.ncx'), 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx')
return os.path.join(self.dest_dir, 'metadata.opf')