From 0ca84ab9bced68ff8296fb26bc86f3ec44a7b5c3 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 1 Sep 2012 22:28:30 -0400 Subject: [PATCH] Implement lazy-importing of oauth2, nltk, and bs4. --- earwigbot/__init__.py | 2 -- earwigbot/lazy.py | 11 +++++++++-- earwigbot/wiki/copyvios/__init__.py | 10 ++++++---- earwigbot/wiki/copyvios/parsers.py | 7 +++++-- earwigbot/wiki/copyvios/search.py | 5 +++-- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/earwigbot/__init__.py b/earwigbot/__init__.py index 8dca99f..256837c 100644 --- a/earwigbot/__init__.py +++ b/earwigbot/__init__.py @@ -64,5 +64,3 @@ managers = importer.new("earwigbot.managers") tasks = importer.new("earwigbot.tasks") util = importer.new("earwigbot.util") wiki = importer.new("earwigbot.wiki") - -del importer diff --git a/earwigbot/lazy.py b/earwigbot/lazy.py index 555f039..f9214e0 100644 --- a/earwigbot/lazy.py +++ b/earwigbot/lazy.py @@ -21,8 +21,10 @@ # SOFTWARE. """ -Implements a hierarchy of importing classes as defined in PEP 302 to load -modules in a safe yet lazy manner. +Implements a hierarchy of importing classes as defined in `PEP 302 +`_ to load modules in a safe yet lazy +manner, so that they can be referred to by name but are not actually loaded +until they are used (i.e. their attributes are read or modified). """ from imp import acquire_lock, release_lock @@ -64,6 +66,11 @@ class _LazyModule(type): class LazyImporter(object): + """An importer for modules that are loaded lazily. + + This inserts itself into :py:data:`sys.meta_path`, storing a dictionary of + :py:class:`_LazyModule`\ s (which is added to with :py:meth:`new`). + """ def __init__(self): self._modules = {} sys.meta_path.append(self) diff --git a/earwigbot/wiki/copyvios/__init__.py b/earwigbot/wiki/copyvios/__init__.py index cd643f3..425d99a 100644 --- a/earwigbot/wiki/copyvios/__init__.py +++ b/earwigbot/wiki/copyvios/__init__.py @@ -26,14 +26,14 @@ from StringIO import StringIO from time import sleep, time from urllib2 import build_opener, URLError -import oauth2 as oauth - -from earwigbot import exceptions +from earwigbot import exceptions, importer from earwigbot.wiki.copyvios.markov import MarkovChain, MarkovChainIntersection from earwigbot.wiki.copyvios.parsers import ArticleTextParser, HTMLTextParser from earwigbot.wiki.copyvios.result import CopyvioCheckResult from earwigbot.wiki.copyvios.search import YahooBOSSSearchEngine +oauth = importer.new("oauth2") + __all__ = ["CopyvioMixIn"] class CopyvioMixIn(object): @@ -93,7 +93,9 @@ class CopyvioMixIn(object): credentials = self._search_config["credentials"] if engine == "Yahoo! BOSS": - if not oauth: + try: + oauth.__version__ # Force-load the lazy module + except (ImportError, AttributeError): e = "The package 'oauth2' could not be imported" raise exceptions.UnsupportedSearchEngineError(e) return YahooBOSSSearchEngine(credentials) diff --git a/earwigbot/wiki/copyvios/parsers.py b/earwigbot/wiki/copyvios/parsers.py index 798b2a7..4f8e981 100644 --- a/earwigbot/wiki/copyvios/parsers.py +++ b/earwigbot/wiki/copyvios/parsers.py @@ -22,9 +22,12 @@ from os import path -import bs4 import mwparserfromhell -import nltk + +from earwigbot import importer + +bs4 = importer.new("bs4") +nltk = importer.new("nltk") __all__ = ["BaseTextParser", "ArticleTextParser", "HTMLTextParser"] diff --git a/earwigbot/wiki/copyvios/search.py b/earwigbot/wiki/copyvios/search.py index 0834885..a9afcfb 100644 --- a/earwigbot/wiki/copyvios/search.py +++ b/earwigbot/wiki/copyvios/search.py @@ -23,10 +23,11 @@ from json import loads from urllib import quote_plus, urlencode -import oauth2 as oauth - +from earwigbot import importer from earwigbot.exceptions import SearchQueryError +oauth = importer.new("oauth2") + __all__ = ["BaseSearchEngine", "YahooBOSSSearchEngine"] class BaseSearchEngine(object):