mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Delay load slow package level imports
This commit is contained in:
parent
d6f161df98
commit
a9cedb0a0b
@ -8,12 +8,13 @@ __docformat__ = 'restructuredtext en'
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from lxml.etree import tostring as _tostring
|
|
||||||
|
|
||||||
def tostring(root, strip_comments=False, pretty_print=False):
|
def tostring(root, strip_comments=False, pretty_print=False):
|
||||||
'''
|
'''
|
||||||
Serialize processed XHTML.
|
Serialize processed XHTML.
|
||||||
'''
|
'''
|
||||||
|
from lxml.etree import tostring as _tostring
|
||||||
|
|
||||||
root.set('xmlns', 'http://www.w3.org/1999/xhtml')
|
root.set('xmlns', 'http://www.w3.org/1999/xhtml')
|
||||||
root.set('{http://www.w3.org/1999/xhtml}xlink', 'http://www.w3.org/1999/xlink')
|
root.set('{http://www.w3.org/1999/xhtml}xlink', 'http://www.w3.org/1999/xlink')
|
||||||
for x in root.iter():
|
for x in root.iter():
|
||||||
|
@ -4,7 +4,6 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
This package contains logic to read and write LRF files.
|
This package contains logic to read and write LRF files.
|
||||||
The LRF file format is documented at U{http://www.sven.de/librie/Librie/LrfFormat}.
|
The LRF file format is documented at U{http://www.sven.de/librie/Librie/LrfFormat}.
|
||||||
"""
|
"""
|
||||||
from uuid import uuid4
|
|
||||||
|
|
||||||
from calibre.ebooks.lrf.pylrs.pylrs import Book as _Book
|
from calibre.ebooks.lrf.pylrs.pylrs import Book as _Book
|
||||||
from calibre.ebooks.lrf.pylrs.pylrs import TextBlock, Header, \
|
from calibre.ebooks.lrf.pylrs.pylrs import TextBlock, Header, \
|
||||||
@ -60,6 +59,7 @@ def find_custom_fonts(options, logger):
|
|||||||
|
|
||||||
def Book(options, logger, font_delta=0, header=None,
|
def Book(options, logger, font_delta=0, header=None,
|
||||||
profile=PRS500_PROFILE, **settings):
|
profile=PRS500_PROFILE, **settings):
|
||||||
|
from uuid import uuid4
|
||||||
ps = {}
|
ps = {}
|
||||||
ps['topmargin'] = options.top_margin
|
ps['topmargin'] = options.top_margin
|
||||||
ps['evensidemargin'] = options.left_margin
|
ps['evensidemargin'] = options.left_margin
|
||||||
|
@ -7,31 +7,38 @@ __docformat__ = 'restructuredtext en'
|
|||||||
class PDBError(Exception):
|
class PDBError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
FORMAT_READERS = None
|
||||||
|
|
||||||
from calibre.ebooks.pdb.ereader.reader import Reader as ereader_reader
|
def _import_readers():
|
||||||
from calibre.ebooks.pdb.palmdoc.reader import Reader as palmdoc_reader
|
global FORMAT_READERS
|
||||||
from calibre.ebooks.pdb.ztxt.reader import Reader as ztxt_reader
|
from calibre.ebooks.pdb.ereader.reader import Reader as ereader_reader
|
||||||
from calibre.ebooks.pdb.pdf.reader import Reader as pdf_reader
|
from calibre.ebooks.pdb.palmdoc.reader import Reader as palmdoc_reader
|
||||||
from calibre.ebooks.pdb.plucker.reader import Reader as plucker_reader
|
from calibre.ebooks.pdb.ztxt.reader import Reader as ztxt_reader
|
||||||
|
from calibre.ebooks.pdb.pdf.reader import Reader as pdf_reader
|
||||||
|
from calibre.ebooks.pdb.plucker.reader import Reader as plucker_reader
|
||||||
|
|
||||||
FORMAT_READERS = {
|
FORMAT_READERS = {
|
||||||
'PNPdPPrs': ereader_reader,
|
'PNPdPPrs': ereader_reader,
|
||||||
'PNRdPPrs': ereader_reader,
|
'PNRdPPrs': ereader_reader,
|
||||||
'zTXTGPlm': ztxt_reader,
|
'zTXTGPlm': ztxt_reader,
|
||||||
'TEXtREAd': palmdoc_reader,
|
'TEXtREAd': palmdoc_reader,
|
||||||
'.pdfADBE': pdf_reader,
|
'.pdfADBE': pdf_reader,
|
||||||
'DataPlkr': plucker_reader,
|
'DataPlkr': plucker_reader,
|
||||||
}
|
}
|
||||||
|
|
||||||
from calibre.ebooks.pdb.palmdoc.writer import Writer as palmdoc_writer
|
ALL_FORMAT_WRITERS = {'doc', 'ztxt', 'ereader'}
|
||||||
from calibre.ebooks.pdb.ztxt.writer import Writer as ztxt_writer
|
FORMAT_WRITERS = None
|
||||||
from calibre.ebooks.pdb.ereader.writer import Writer as ereader_writer
|
def _import_writers():
|
||||||
|
global FORMAT_WRITERS
|
||||||
|
from calibre.ebooks.pdb.palmdoc.writer import Writer as palmdoc_writer
|
||||||
|
from calibre.ebooks.pdb.ztxt.writer import Writer as ztxt_writer
|
||||||
|
from calibre.ebooks.pdb.ereader.writer import Writer as ereader_writer
|
||||||
|
|
||||||
FORMAT_WRITERS = {
|
FORMAT_WRITERS = {
|
||||||
'doc': palmdoc_writer,
|
'doc': palmdoc_writer,
|
||||||
'ztxt': ztxt_writer,
|
'ztxt': ztxt_writer,
|
||||||
'ereader': ereader_writer,
|
'ereader': ereader_writer,
|
||||||
}
|
}
|
||||||
|
|
||||||
IDENTITY_TO_NAME = {
|
IDENTITY_TO_NAME = {
|
||||||
'PNPdPPrs': 'eReader',
|
'PNPdPPrs': 'eReader',
|
||||||
@ -69,11 +76,17 @@ def get_reader(identity):
|
|||||||
'''
|
'''
|
||||||
Returns None if no reader is found for the identity.
|
Returns None if no reader is found for the identity.
|
||||||
'''
|
'''
|
||||||
|
global FORMAT_READERS
|
||||||
|
if FORMAT_READERS is None:
|
||||||
|
_import_readers()
|
||||||
return FORMAT_READERS.get(identity, None)
|
return FORMAT_READERS.get(identity, None)
|
||||||
|
|
||||||
def get_writer(extension):
|
def get_writer(extension):
|
||||||
'''
|
'''
|
||||||
Returns None if no writer is found for extension.
|
Returns None if no writer is found for extension.
|
||||||
'''
|
'''
|
||||||
|
global FORMAT_WRITERS
|
||||||
|
if FORMAT_WRITERS is None:
|
||||||
|
_import_writers()
|
||||||
return FORMAT_WRITERS.get(extension, None)
|
return FORMAT_WRITERS.get(extension, None)
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ import os
|
|||||||
|
|
||||||
from calibre.customize.conversion import OutputFormatPlugin, \
|
from calibre.customize.conversion import OutputFormatPlugin, \
|
||||||
OptionRecommendation
|
OptionRecommendation
|
||||||
from calibre.ebooks.pdb import PDBError, get_writer, FORMAT_WRITERS
|
from calibre.ebooks.pdb import PDBError, get_writer, ALL_FORMAT_WRITERS
|
||||||
|
|
||||||
class PDBOutput(OutputFormatPlugin):
|
class PDBOutput(OutputFormatPlugin):
|
||||||
|
|
||||||
@ -19,9 +19,9 @@ class PDBOutput(OutputFormatPlugin):
|
|||||||
options = set([
|
options = set([
|
||||||
OptionRecommendation(name='format', recommended_value='doc',
|
OptionRecommendation(name='format', recommended_value='doc',
|
||||||
level=OptionRecommendation.LOW,
|
level=OptionRecommendation.LOW,
|
||||||
short_switch='f', choices=FORMAT_WRITERS.keys(),
|
short_switch='f', choices=list(ALL_FORMAT_WRITERS),
|
||||||
help=(_('Format to use inside the pdb container. Choices are:')+\
|
help=(_('Format to use inside the pdb container. Choices are:')+\
|
||||||
' %s' % FORMAT_WRITERS.keys())),
|
' %s' % list(ALL_FORMAT_WRITERS))),
|
||||||
OptionRecommendation(name='pdb_output_encoding', recommended_value='cp1252',
|
OptionRecommendation(name='pdb_output_encoding', recommended_value='cp1252',
|
||||||
level=OptionRecommendation.LOW,
|
level=OptionRecommendation.LOW,
|
||||||
help=_('Specify the character encoding of the output document. ' \
|
help=_('Specify the character encoding of the output document. ' \
|
||||||
|
@ -7,9 +7,6 @@ Contains the logic for parsing feeds.
|
|||||||
'''
|
'''
|
||||||
import time, traceback, copy, re
|
import time, traceback, copy, re
|
||||||
|
|
||||||
from lxml import html
|
|
||||||
|
|
||||||
from calibre.web.feeds.feedparser import parse
|
|
||||||
from calibre.utils.logging import default_log
|
from calibre.utils.logging import default_log
|
||||||
from calibre import entity_to_unicode, strftime
|
from calibre import entity_to_unicode, strftime
|
||||||
from calibre.utils.date import dt_factory, utcnow, local_tz
|
from calibre.utils.date import dt_factory, utcnow, local_tz
|
||||||
@ -18,6 +15,7 @@ from calibre.utils.cleantext import clean_ascii_chars
|
|||||||
class Article(object):
|
class Article(object):
|
||||||
|
|
||||||
def __init__(self, id, title, url, author, summary, published, content):
|
def __init__(self, id, title, url, author, summary, published, content):
|
||||||
|
from lxml import html
|
||||||
self.downloaded = False
|
self.downloaded = False
|
||||||
self.id = id
|
self.id = id
|
||||||
self._title = title.strip() if title else title
|
self._title = title.strip() if title else title
|
||||||
@ -320,6 +318,7 @@ def feed_from_xml(raw_xml, title=None, oldest_article=7,
|
|||||||
max_articles_per_feed=100,
|
max_articles_per_feed=100,
|
||||||
get_article_url=lambda item: item.get('link', None),
|
get_article_url=lambda item: item.get('link', None),
|
||||||
log=default_log):
|
log=default_log):
|
||||||
|
from calibre.web.feeds.feedparser import parse
|
||||||
# Handle unclosed escaped entities. They trip up feedparser and HBR for one
|
# Handle unclosed escaped entities. They trip up feedparser and HBR for one
|
||||||
# generates them
|
# generates them
|
||||||
raw_xml = re.sub(r'(&#\d+)([^0-9;])', r'\1;\2', raw_xml)
|
raw_xml = re.sub(r'(&#\d+)([^0-9;])', r'\1;\2', raw_xml)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user