Require python 2.7, use importlib.

This commit is contained in:
Bastian Kleineidam 2012-11-19 21:20:50 +01:00
parent 7dbd14f934
commit 64d9fd6ac2
5 changed files with 15 additions and 28 deletions

View file

@ -50,7 +50,7 @@ home directory, usually "C:\Documents and Settings\UserName".
Dependencies Dependencies
------------- -------------
Dosage is requires Python version 2.5 or higher, which can be downloaded Dosage requires Python version 2.7 or higher, which can be downloaded
from http://www.python.org. from http://www.python.org.
No external Python modules are required - only the Python Standard Library No external Python modules are required - only the Python Standard Library
that gets installed with Python. that gets installed with Python.

View file

@ -7,6 +7,7 @@ Features:
Changes: Changes:
- installation: Added support for dynamic configuration values. - installation: Added support for dynamic configuration values.
- installation: Require and use Python 2.7
- comics: Removed the twisted and zope dependencies by adding - comics: Removed the twisted and zope dependencies by adding
an internal plugin search mechanism. an internal plugin search mechanism.
- testing: Refactored the test comic routine in proper unit tests. - testing: Refactored the test comic routine in proper unit tests.

View file

@ -17,5 +17,5 @@ thus making their individual implementations trivial.
""" """
import sys import sys
if not (hasattr(sys, 'version_info') or if not (hasattr(sys, 'version_info') or
sys.version_info < (2, 5, 0, 'final', 0)): sys.version_info < (2, 7, 0, 'final', 0)):
raise SystemExit("This program requires Python 2.5 or later.") raise SystemExit("This program requires Python 2.7 or later.")

View file

@ -4,45 +4,33 @@
Functions to load plugin modules. Functions to load plugin modules.
""" """
import os import os
import sys import importlib
def get_modules(folder, importprefix): def get_modules(folder='plugins'):
"""Find all valid modules in the plugins directory. A valid module """Find all valid modules in the plugins subdirectory. A valid module
must have a .py extension, and is importable. must have a .py extension, and is importable.
@return: all loaded valid modules @return: all loaded valid modules
@rtype: iterator of module @rtype: iterator of module
""" """
for filename in get_importable_modules(folder): dirname = os.path.join(os.path.dirname(__file__), folder)
for modname in get_importable_modules(dirname):
try: try:
module = load_module(filename, importprefix) name ="..%s.%s" % (folder, modname)
if module is not None: yield importlib.import_module(name, __name__)
yield module
except StandardError, msg: except StandardError, msg:
print "ERROR: could not load module %s: %s" % (filename, msg) print "ERROR: could not load module %s: %s" % (modname, msg)
def get_importable_modules(folder): def get_importable_modules(folder):
"""Find all module files in the given folder that end witn '.py' and """Find all module files in the given folder that end witn '.py' and
don't start with an underscore. don't start with an underscore.
@return module filenames @return module names
@rtype: iterator of string @rtype: iterator of string
""" """
for fname in sorted(os.listdir(folder)): for fname in sorted(os.listdir(folder)):
if fname.endswith('.py') and not fname.startswith('_'): if fname.endswith('.py') and not fname.startswith('_'):
yield os.path.join(folder, fname) yield fname[:-3]
def load_module(filename, importprefix):
"""Load and return the module given by the filename.
Other exceptions than ImportError are not catched.
@return: loaded module or None on import errors
@rtype: module or None
"""
name = os.path.splitext(os.path.basename(filename))[0]
modulename = "%s%s" % (importprefix, name)
__import__(modulename)
return sys.modules[modulename]
def get_plugins(modules, classobj): def get_plugins(modules, classobj):

View file

@ -154,9 +154,7 @@ def get_scrapers():
""" """
global _scrapers global _scrapers
if _scrapers is None: if _scrapers is None:
folder = os.path.join(os.path.dirname(__file__), 'plugins') modules = loader.get_modules()
importprefix = 'dosagelib.plugins.'
modules = loader.get_modules(folder, importprefix)
plugins = loader.get_plugins(modules, _BasicScraper) plugins = loader.get_plugins(modules, _BasicScraper)
_scrapers = list(plugins) _scrapers = list(plugins)
_scrapers.sort(key=lambda s: s.get_name()) _scrapers.sort(key=lambda s: s.get_name())