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
-------------
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.
No external Python modules are required - only the Python Standard Library
that gets installed with Python.

View file

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

View file

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

View file

@ -4,45 +4,33 @@
Functions to load plugin modules.
"""
import os
import sys
import importlib
def get_modules(folder, importprefix):
"""Find all valid modules in the plugins directory. A valid module
def get_modules(folder='plugins'):
"""Find all valid modules in the plugins subdirectory. A valid module
must have a .py extension, and is importable.
@return: all loaded valid modules
@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:
module = load_module(filename, importprefix)
if module is not None:
yield module
name ="..%s.%s" % (folder, modname)
yield importlib.import_module(name, __name__)
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):
"""Find all module files in the given folder that end witn '.py' and
don't start with an underscore.
@return module filenames
@return module names
@rtype: iterator of string
"""
for fname in sorted(os.listdir(folder)):
if fname.endswith('.py') and not fname.startswith('_'):
yield os.path.join(folder, fname)
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]
yield fname[:-3]
def get_plugins(modules, classobj):

View file

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