1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-04-30 02:34:05 +02:00

Cleanup get_path usage in favor of pkg_resources.

This commit is contained in:
2020-04-12 18:33:23 +02:00
parent 0bf43ec6e8
commit 5aa0b1a0eb
17 changed files with 532 additions and 277 deletions
@@ -6,6 +6,7 @@ __copyright__ = '2008, Anatoly Shipitsin <norguhtar at gmail.com>'
Convert .fb2 files to .lrf
"""
import os, re
import pkg_resources
from ebook_converter.customize.conversion import InputFormatPlugin, OptionRecommendation
from ebook_converter import guess_type
@@ -86,8 +87,9 @@ class FB2Input(InputFormatPlugin):
css = re.sub(r'name\s*=\s*', 'class=', css)
self.extract_embedded_content(doc)
log.debug('Converting XML to HTML...')
with open(P('templates/fb2.xsl'), 'rb') as f:
ss = f.read().decode('utf-8')
with open(pkg_resources.resource_filename('ebook_converter',
'data/fb2.xsl')) as f:
ss = f.read().decode()
ss = ss.replace("__FB_NS__", fb_ns)
if options.no_inline_fb2_toc:
log('Disabling generation of inline FB2 TOC')
@@ -6,6 +6,7 @@ __docformat__ = 'restructuredtext en'
import os, re, shutil
from os.path import dirname, abspath, relpath as _relpath, exists, basename
import pkg_resources
from ebook_converter.customize.conversion import OutputFormatPlugin, OptionRecommendation
from ebook_converter import CurrentDir
@@ -95,19 +96,31 @@ class HTMLOutput(OutputFormatPlugin):
with open(opts.template_html_index, 'rb') as f:
template_html_index_data = f.read()
else:
template_html_index_data = P('templates/html_export_default_index.tmpl', data=True)
with open(pkg_resources.
resource_filename('ebook_converter',
'data/html_export_default_index.tmpl')
) as fobj:
template_html_index_data = fobj.read().decode()
if opts.template_html is not None:
with open(opts.template_html, 'rb') as f:
template_html_data = f.read()
else:
template_html_data = P('templates/html_export_default.tmpl', data=True)
with open(pkg_resources.
resource_filename('ebook_converter',
'data/html_export_default.tmpl')
) as fobj:
template_html_data = fobj.read().decode()
if opts.template_css is not None:
with open(opts.template_css, 'rb') as f:
template_css_data = f.read()
else:
template_css_data = P('templates/html_export_default.css', data=True)
with open(pkg_resources.
resource_filename('ebook_converter',
'data/html_export_default.css')
) as fobj:
template_css_data = fobj.read().decode()
template_html_index_data = template_html_index_data.decode('utf-8')
template_html_data = template_html_data.decode('utf-8')
@@ -7,6 +7,8 @@ __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import os, sys
import pkg_resources
from ebook_converter.customize.conversion import InputFormatPlugin
@@ -54,7 +56,12 @@ class LRFInput(InputFormatPlugin):
plot_map[ro] = imgstr[0].get('file')
self.log('Converting XML to HTML...')
styledoc = safe_xml_fromstring(P('templates/lrf.xsl', data=True))
with open(pkg_resources.
resource_filename('ebook_converter',
'data/lrf.xsl')) as fobj:
# TODO(gryf): change this nonsense to etree.parse() instead.
styledoc = safe_xml_fromstring(fobj.read())
media_type = MediaType()
styles = Styles()
text_block = TextBlock(styles, char_button_map, plot_map, log)
+22 -11
View File
@@ -5,29 +5,40 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
from PIL import ImageFont
from ebook_converter.utils.fonts.scanner import font_scanner
'''
Default fonts used in the PRS500
'''
LIBERATION_FONT_MAP = {
'Swis721 BT Roman' : 'LiberationSans-Regular',
'Dutch801 Rm BT Roman' : 'LiberationSerif-Regular',
'Courier10 BT Roman' : 'LiberationMono-Regular',
}
LIBERATION_FONT_MAP = {'Swis721 BT Roman': 'Liberation Sans Regular',
'Dutch801 Rm BT Roman': 'Liberation Serif Regular',
'Courier10 BT Roman': 'Liberation Mono Regular'}
_LIB_CACHE = {}
FONT_FILE_MAP = {}
def get_font(name, size, encoding='unic'):
'''
"""
Get an ImageFont object by name.
@param size: Font height in pixels. To convert from pts:
sz in pixels = (dpi/72) * size in pts
@param encoding: Font encoding to use. E.g. 'unic', 'symbol', 'ADOB', 'ADBE', 'aprm'
@param manager: A dict that will store the PersistentTemporary
'''
@param encoding: Font encoding to use. E.g. 'unic', 'symbol', 'ADOB',
'ADBE', 'aprm'
"""
if name in LIBERATION_FONT_MAP:
return ImageFont.truetype(P('fonts/liberation/%s.ttf' % LIBERATION_FONT_MAP[name]), size, encoding=encoding)
if not _LIB_CACHE:
for key in font_scanner.cache['fonts']:
record = font_scanner.cache['fonts'][key]
_LIB_CACHE[record['family_name'] + ' ' +
record['subfamily_name']] = record['path']
fpath = _LIB_CACHE.get(LIBERATION_FONT_MAP[name])
if not fpath:
raise ValueError('There is no liberation font existing in the '
'system. Please install them before converter '
'use.')
return ImageFont.truetype(fpath, size, encoding=encoding)
elif name in FONT_FILE_MAP:
return ImageFont.truetype(FONT_FILE_MAP[name], size, encoding=encoding)
+5 -1
View File
@@ -10,6 +10,7 @@ import re
from collections import Counter, OrderedDict
from functools import partial
from operator import itemgetter
import pkg_resources
from lxml import etree
from lxml.builder import ElementMaker
@@ -690,7 +691,10 @@ def commit_nav_toc(container, toc, lang=None, landmarks=None, previous_nav=None)
if previous_nav is not None:
root = previous_nav[1]
else:
root = container.parse_xhtml(P('templates/new_nav.html', data=True).decode('utf-8'))
with open(pkg_resources.
resource_filename('ebook_converter',
'data/new_nav.html')) as fobj:
root = container.parse_xhtml(fobj.read())
container.replace(tocname, root)
else:
root = container.parsed(tocname)
@@ -9,13 +9,13 @@ __docformat__ = 'restructuredtext en'
import sys, os, re
from xml.sax.saxutils import escape
from string import Formatter
import pkg_resources
from ebook_converter import guess_type, strftime
from ebook_converter.constants import iswindows
from ebook_converter.ebooks.oeb.base import XPath, XHTML_NS, XHTML, xml2text, urldefrag, urlnormalize
from ebook_converter.library.comments import comments_to_html, markdown
from ebook_converter.utils.date import is_date_undefined, as_local_time
from ebook_converter.utils.icu import sort_key
from ebook_converter.ebooks.chardet import strip_encoding_declarations
from ebook_converter.ebooks.metadata import fmt_sidx, rating_to_stars
from ebook_converter.polyglot.builtins import unicode_type, map
@@ -196,7 +196,7 @@ class Tags(unicode_type):
def __new__(self, tags, output_profile):
tags = [escape(x) for x in tags or ()]
t = unicode_type.__new__(self, ', '.join(tags))
t.alphabetical = ', '.join(sorted(tags, key=sort_key))
t.alphabetical = ', '.join(sorted(tags))
t.tags_list = tags
return t
@@ -232,8 +232,14 @@ def postprocess_jacket(root, output_profile, has_data):
def render_jacket(mi, output_profile,
alt_title=_('Unknown'), alt_tags=[], alt_comments='',
alt_publisher='', rescale_fonts=False, alt_authors=None):
css = P('jacket/stylesheet.css', data=True).decode('utf-8')
template = P('jacket/template.xhtml', data=True).decode('utf-8')
with open(pkg_resources.resource_filename('ebook_converter',
'data/jacket/stylesheet.css'),
'rb') as fobj:
css = fobj.read().decode()
with open(pkg_resources.resource_filename('ebook_converter',
'data/jacket/template.xhtml'),
'rb') as fobj:
template = fobj.read().decode()
template = re.sub(r'<!--.*?-->', '', template, flags=re.DOTALL)
css = re.sub(r'/\*.*?\*/', '', css, flags=re.DOTALL)