Переглянути джерело

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

tags/v0.2
Ben Kurtovic 11 роки тому
джерело
коміт
0ca84ab9bc
5 змінених файлів з 23 додано та 12 видалено
  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 Переглянути файл

@@ -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

+ 9
- 2
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
<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
@@ -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)


+ 6
- 4
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)


+ 5
- 2
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"]



+ 3
- 2
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):


Завантаження…
Відмінити
Зберегти