dosage/dosagelib/loader.py

92 lines
2.8 KiB
Python
Raw Normal View History

2012-10-11 10:03:12 +00:00
# -*- coding: iso-8859-1 -*-
2013-01-23 18:34:11 +00:00
# Copyright (C) 2012-2013 Bastian Kleineidam
2012-10-11 16:02:29 +00:00
"""
Functions to load plugin modules.
2013-12-11 16:54:39 +00:00
Example usage:
modules = loader.get_modules('plugins')
plugins = loader.get_plugins(modules, PluginClass)
2012-10-11 16:02:29 +00:00
"""
2012-10-11 10:03:12 +00:00
import os
2012-12-12 22:22:36 +00:00
import sys
import zipfile
2012-11-19 20:20:50 +00:00
import importlib
from .output import out
2012-10-11 10:03:12 +00:00
2012-10-11 16:02:29 +00:00
2012-12-12 22:22:36 +00:00
def is_frozen ():
2013-12-18 19:55:23 +00:00
"""Return True if running inside a py2exe- or py2app-generated
executable."""
2012-12-12 22:22:36 +00:00
return hasattr(sys, "frozen")
2013-12-11 16:54:39 +00:00
def get_modules(folder):
"""Find all valid modules in the given folder which must be in
in the same directory as this loader.py module. A valid module
has a .py extension, and is importable.
2012-10-11 10:03:12 +00:00
@return: all loaded valid modules
@rtype: iterator of module
"""
2012-12-12 22:22:36 +00:00
if is_frozen():
# find modules in library.zip filename
zipname = os.path.dirname(os.path.dirname(__file__))
2013-12-18 19:55:23 +00:00
parentmodule = os.path.basename(os.path.dirname(__file__))
2012-12-12 22:22:36 +00:00
with zipfile.ZipFile(zipname, 'r') as f:
2013-12-11 16:54:39 +00:00
prefix = "%s/%s/" % (parentmodule, folder)
2012-12-12 22:27:03 +00:00
modnames = [os.path.splitext(n[len(prefix):])[0]
2012-12-12 22:22:36 +00:00
for n in f.namelist()
2012-12-12 22:27:03 +00:00
if n.startswith(prefix) and "__init__" not in n]
2012-12-12 22:22:36 +00:00
else:
dirname = os.path.join(os.path.dirname(__file__), folder)
modnames = get_importable_modules(dirname)
for modname in modnames:
2012-10-11 10:03:12 +00:00
try:
2012-11-19 20:20:50 +00:00
name ="..%s.%s" % (folder, modname)
yield importlib.import_module(name, __name__)
except ImportError as msg:
out.error("could not load module %s: %s" % (modname, msg))
2012-10-11 10:03:12 +00:00
def get_importable_modules(folder):
"""Find all module files in the given folder that end with '.py' and
2012-10-11 10:03:12 +00:00
don't start with an underscore.
2012-11-19 20:20:50 +00:00
@return module names
2012-10-11 10:03:12 +00:00
@rtype: iterator of string
"""
2013-02-18 19:40:35 +00:00
for fname in os.listdir(folder):
2012-10-11 10:03:12 +00:00
if fname.endswith('.py') and not fname.startswith('_'):
2012-11-19 20:20:50 +00:00
yield fname[:-3]
2012-10-11 10:03:12 +00:00
def get_plugins(modules, classobj):
2013-12-11 16:54:39 +00:00
"""Find all class objects in all modules.
2012-10-11 10:03:12 +00:00
@param modules: the modules to search
@ptype modules: iterator of modules
2013-12-11 16:54:39 +00:00
@return: found classes
2012-10-11 10:03:12 +00:00
@rytpe: iterator of class objects
"""
for module in modules:
for plugin in get_module_plugins(module, classobj):
yield plugin
def get_module_plugins(module, classobj):
2013-12-11 16:54:39 +00:00
"""Return all subclasses of a class in the module.
2012-10-11 10:03:12 +00:00
If the module defines __all__, only those entries will be searched,
otherwise all objects not starting with '_' will be searched.
"""
try:
names = module.__all__
except AttributeError:
names = [x for x in vars(module) if not x.startswith('_')]
2013-02-18 19:40:35 +00:00
for name in names:
2012-10-11 10:03:12 +00:00
try:
obj = getattr(module, name)
except AttributeError:
continue
try:
if issubclass(obj, classobj):
yield obj
except TypeError:
continue