Browse Source

Implement lazy-importing of oauth2, nltk, and bs4.

tags/v0.2
Ben Kurtovic 12 years ago
parent
commit
0ca84ab9bc
5 changed files with 23 additions and 12 deletions
  1. +0
    -2
      earwigbot/__init__.py
  2. +9
    -2
      earwigbot/lazy.py
  3. +6
    -4
      earwigbot/wiki/copyvios/__init__.py
  4. +5
    -2
      earwigbot/wiki/copyvios/parsers.py
  5. +3
    -2
      earwigbot/wiki/copyvios/search.py

+ 0
- 2
earwigbot/__init__.py View File

@@ -64,5 +64,3 @@ managers = importer.new("earwigbot.managers")
tasks = importer.new("earwigbot.tasks") tasks = importer.new("earwigbot.tasks")
util = importer.new("earwigbot.util") util = importer.new("earwigbot.util")
wiki = importer.new("earwigbot.wiki") wiki = importer.new("earwigbot.wiki")

del importer

+ 9
- 2
earwigbot/lazy.py View File

@@ -21,8 +21,10 @@
# SOFTWARE. # 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
<http://www.python.org/dev/peps/pep-0302/>`_ 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 from imp import acquire_lock, release_lock
@@ -64,6 +66,11 @@ class _LazyModule(type):




class LazyImporter(object): 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): def __init__(self):
self._modules = {} self._modules = {}
sys.meta_path.append(self) sys.meta_path.append(self)


+ 6
- 4
earwigbot/wiki/copyvios/__init__.py View File

@@ -26,14 +26,14 @@ from StringIO import StringIO
from time import sleep, time from time import sleep, time
from urllib2 import build_opener, URLError 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.markov import MarkovChain, MarkovChainIntersection
from earwigbot.wiki.copyvios.parsers import ArticleTextParser, HTMLTextParser from earwigbot.wiki.copyvios.parsers import ArticleTextParser, HTMLTextParser
from earwigbot.wiki.copyvios.result import CopyvioCheckResult from earwigbot.wiki.copyvios.result import CopyvioCheckResult
from earwigbot.wiki.copyvios.search import YahooBOSSSearchEngine from earwigbot.wiki.copyvios.search import YahooBOSSSearchEngine


oauth = importer.new("oauth2")

__all__ = ["CopyvioMixIn"] __all__ = ["CopyvioMixIn"]


class CopyvioMixIn(object): class CopyvioMixIn(object):
@@ -93,7 +93,9 @@ class CopyvioMixIn(object):
credentials = self._search_config["credentials"] credentials = self._search_config["credentials"]


if engine == "Yahoo! BOSS": 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" e = "The package 'oauth2' could not be imported"
raise exceptions.UnsupportedSearchEngineError(e) raise exceptions.UnsupportedSearchEngineError(e)
return YahooBOSSSearchEngine(credentials) return YahooBOSSSearchEngine(credentials)


+ 5
- 2
earwigbot/wiki/copyvios/parsers.py View File

@@ -22,9 +22,12 @@


from os import path from os import path


import bs4
import mwparserfromhell import mwparserfromhell
import nltk

from earwigbot import importer

bs4 = importer.new("bs4")
nltk = importer.new("nltk")


__all__ = ["BaseTextParser", "ArticleTextParser", "HTMLTextParser"] __all__ = ["BaseTextParser", "ArticleTextParser", "HTMLTextParser"]




+ 3
- 2
earwigbot/wiki/copyvios/search.py View File

@@ -23,10 +23,11 @@
from json import loads from json import loads
from urllib import quote_plus, urlencode from urllib import quote_plus, urlencode


import oauth2 as oauth

from earwigbot import importer
from earwigbot.exceptions import SearchQueryError from earwigbot.exceptions import SearchQueryError


oauth = importer.new("oauth2")

__all__ = ["BaseSearchEngine", "YahooBOSSSearchEngine"] __all__ = ["BaseSearchEngine", "YahooBOSSSearchEngine"]


class BaseSearchEngine(object): class BaseSearchEngine(object):


Loading…
Cancel
Save