From a9cedb0a0ba888a2fffc5bb5a3e49efc1a640937 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 5 Feb 2012 21:41:09 +0530 Subject: [PATCH] Delay load slow package level imports --- src/calibre/ebooks/html/__init__.py | 3 +- src/calibre/ebooks/lrf/__init__.py | 2 +- src/calibre/ebooks/pdb/__init__.py | 55 ++++++++++++++++++----------- src/calibre/ebooks/pdb/output.py | 6 ++-- src/calibre/web/feeds/__init__.py | 5 ++- 5 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/calibre/ebooks/html/__init__.py b/src/calibre/ebooks/html/__init__.py index d026256ee8..00afa6d6b6 100644 --- a/src/calibre/ebooks/html/__init__.py +++ b/src/calibre/ebooks/html/__init__.py @@ -8,12 +8,13 @@ __docformat__ = 'restructuredtext en' import re -from lxml.etree import tostring as _tostring def tostring(root, strip_comments=False, pretty_print=False): ''' Serialize processed XHTML. ''' + from lxml.etree import tostring as _tostring + root.set('xmlns', 'http://www.w3.org/1999/xhtml') root.set('{http://www.w3.org/1999/xhtml}xlink', 'http://www.w3.org/1999/xlink') for x in root.iter(): diff --git a/src/calibre/ebooks/lrf/__init__.py b/src/calibre/ebooks/lrf/__init__.py index e4a18a1f91..b12c0d6b34 100644 --- a/src/calibre/ebooks/lrf/__init__.py +++ b/src/calibre/ebooks/lrf/__init__.py @@ -4,7 +4,6 @@ __copyright__ = '2008, Kovid Goyal ' 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}. """ -from uuid import uuid4 from calibre.ebooks.lrf.pylrs.pylrs import Book as _Book 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, profile=PRS500_PROFILE, **settings): + from uuid import uuid4 ps = {} ps['topmargin'] = options.top_margin ps['evensidemargin'] = options.left_margin diff --git a/src/calibre/ebooks/pdb/__init__.py b/src/calibre/ebooks/pdb/__init__.py index c8089297db..428cbe82ab 100644 --- a/src/calibre/ebooks/pdb/__init__.py +++ b/src/calibre/ebooks/pdb/__init__.py @@ -7,31 +7,38 @@ __docformat__ = 'restructuredtext en' class PDBError(Exception): pass +FORMAT_READERS = None -from calibre.ebooks.pdb.ereader.reader import Reader as ereader_reader -from calibre.ebooks.pdb.palmdoc.reader import Reader as palmdoc_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 +def _import_readers(): + global FORMAT_READERS + from calibre.ebooks.pdb.ereader.reader import Reader as ereader_reader + from calibre.ebooks.pdb.palmdoc.reader import Reader as palmdoc_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 = { - 'PNPdPPrs': ereader_reader, - 'PNRdPPrs': ereader_reader, - 'zTXTGPlm': ztxt_reader, - 'TEXtREAd': palmdoc_reader, - '.pdfADBE': pdf_reader, - 'DataPlkr': plucker_reader, -} + FORMAT_READERS = { + 'PNPdPPrs': ereader_reader, + 'PNRdPPrs': ereader_reader, + 'zTXTGPlm': ztxt_reader, + 'TEXtREAd': palmdoc_reader, + '.pdfADBE': pdf_reader, + 'DataPlkr': plucker_reader, + } -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 +ALL_FORMAT_WRITERS = {'doc', 'ztxt', 'ereader'} +FORMAT_WRITERS = None +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 = { - 'doc': palmdoc_writer, - 'ztxt': ztxt_writer, - 'ereader': ereader_writer, -} + FORMAT_WRITERS = { + 'doc': palmdoc_writer, + 'ztxt': ztxt_writer, + 'ereader': ereader_writer, + } IDENTITY_TO_NAME = { 'PNPdPPrs': 'eReader', @@ -69,11 +76,17 @@ def get_reader(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) def get_writer(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) diff --git a/src/calibre/ebooks/pdb/output.py b/src/calibre/ebooks/pdb/output.py index 7bca4e5c5d..b80f9958ef 100644 --- a/src/calibre/ebooks/pdb/output.py +++ b/src/calibre/ebooks/pdb/output.py @@ -8,7 +8,7 @@ import os from calibre.customize.conversion import OutputFormatPlugin, \ 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): @@ -19,9 +19,9 @@ class PDBOutput(OutputFormatPlugin): options = set([ OptionRecommendation(name='format', recommended_value='doc', 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:')+\ - ' %s' % FORMAT_WRITERS.keys())), + ' %s' % list(ALL_FORMAT_WRITERS))), OptionRecommendation(name='pdb_output_encoding', recommended_value='cp1252', level=OptionRecommendation.LOW, help=_('Specify the character encoding of the output document. ' \ diff --git a/src/calibre/web/feeds/__init__.py b/src/calibre/web/feeds/__init__.py index 14c0085f85..746afefaef 100644 --- a/src/calibre/web/feeds/__init__.py +++ b/src/calibre/web/feeds/__init__.py @@ -7,9 +7,6 @@ Contains the logic for parsing feeds. ''' 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 import entity_to_unicode, strftime 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): def __init__(self, id, title, url, author, summary, published, content): + from lxml import html self.downloaded = False self.id = id 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, get_article_url=lambda item: item.get('link', None), log=default_log): + from calibre.web.feeds.feedparser import parse # Handle unclosed escaped entities. They trip up feedparser and HBR for one # generates them raw_xml = re.sub(r'(&#\d+)([^0-9;])', r'\1;\2', raw_xml)