1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-01-01 15:32:26 +01:00

Replace Calibre path resolve system for some of the modules.

In Calibre there is a module, which registers two functions in global
scope, one for filepath and one for images path. It relies on additional
three variables, registered during application startup. I found it
overcomplicated, and couple of the resource paths has been calculated
using pkg_resource module.
This commit is contained in:
2020-04-12 13:58:53 +02:00
parent f488bc386e
commit bd9cb86c1c
4 changed files with 58 additions and 8 deletions

View File

@@ -4,6 +4,7 @@ __copyright__ = '2008, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import sys, os, re, time, random, warnings
import pkg_resources
from ebook_converter.polyglot.builtins import codepoint_to_chr, unicode_type, range, hasenv, native_string_type
from math import floor
from functools import partial
@@ -34,7 +35,8 @@ _mt_inited = False
def _init_mimetypes():
global _mt_inited
import mimetypes
mimetypes.init([P('mime.types')])
mimetypes.init([pkg_resources.resource_filename('ebook_converter',
'data/mime.types')])
_mt_inited = True

View File

@@ -3,6 +3,7 @@ __license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
import os, glob, re, textwrap
import pkg_resources
from ebook_converter.customize.conversion import InputFormatPlugin, OptionRecommendation
from ebook_converter.polyglot.builtins import iteritems, filter, getcwd, as_bytes
@@ -282,7 +283,9 @@ class RTFInput(InputFormatPlugin):
self.log('Converting XML to HTML...')
inline_class = InlineClass(self.log)
styledoc = safe_xml_fromstring(P('templates/rtf.xsl', data=True), recover=False)
with open(pkg_resources.resource_filename('ebook_converter',
'data/rtf.xsl')) as fobj:
styledoc = safe_xml_fromstring(fobj.read())
extensions = {('calibre', 'inline-class') : inline_class}
transform = etree.XSLT(styledoc, extensions=extensions)
result = transform(doc)

View File

@@ -9,6 +9,7 @@ __license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
import os, re, logging, copy, unicodedata, numbers
import pkg_resources
from operator import itemgetter
from weakref import WeakKeyDictionary
from xml.dom import SyntaxErr as CSSSyntaxError
@@ -32,7 +33,8 @@ _html_css_stylesheet = None
def html_css_stylesheet():
global _html_css_stylesheet
if _html_css_stylesheet is None:
with open(P('templates/html.css'), 'rb') as f:
with open(pkg_resources.resource_filename('ebook_converter',
'data/html.css'), 'rb') as f:
html_css = f.read().decode('utf-8')
_html_css_stylesheet = parseString(html_css, validate=False)
return _html_css_stylesheet

View File

@@ -3,7 +3,9 @@ __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import os, locale, re, io, sys
import json
from gettext import GNUTranslations, NullTranslations
import pkg_resources
from ebook_converter.polyglot.builtins import is_py3, iteritems, unicode_type
@@ -361,12 +363,53 @@ for k in _extra_lang_codes:
def _load_iso639():
global _iso639
# NOTE(gryf): msgpacked data was originally added for speed purposes. In
# my tests, I cannot see any speed gain either on python2 or python3. It
# is even slower (around 4-8 times), than just using code below (which is
# excerpt form Calibre transform code which is executed during Calibre
# build).
if _iso639 is None:
ip = P('localization/iso639.calibre_msgpack', allow_user_override=False, data=True)
from ebook_converter.utils.serialize import msgpack_loads
_iso639 = msgpack_loads(ip)
if 'by_3' not in _iso639:
_iso639['by_3'] = _iso639['by_3t']
src = pkg_resources.resource_filename('ebook_converter',
'data/iso_639-3.json')
with open(src, 'rb') as f:
root = json.load(f)
entries = root['639-3']
by_2 = {}
by_3 = {}
m2to3 = {}
m3to2 = {}
nm = {}
codes2, codes3 = set(), set()
for x in entries:
two = x.get('alpha_2')
threeb = x.get('alpha_3')
if threeb is None:
continue
name = x.get('inverted_name') or x.get('name')
if not name or name[0] in '!~=/\'"':
continue
if two is not None:
by_2[two] = name
codes2.add(two)
m2to3[two] = threeb
m3to2[threeb] = two
codes3.add(threeb)
by_3[threeb] = name
base_name = name.lower()
nm[base_name] = threeb
_iso639 = {'by_2': by_2,
'by_3': by_3,
'codes2': codes2,
'codes3': codes3,
'2to3': m2to3,
'3to2': m3to2,
'name_map': nm}
return _iso639