Source code for coherence.extern.simple_plugin

# -*- coding: utf-8 -*-

# Licensed under the MIT license
# http://opensource.org/licenses/mit-license.php

# Copyright 2007, Frank Scholz <coherence@beebits.net>

""" real simple plugin system
    meant as a replacement when setuptools/pkg_resources
    are not available
"""

import os
import sys


[docs]class Plugin(object): """ a new style class that betrays all its sub-classes """ pass
[docs]class Reception(object): """ singleton class which holds information about known plugins currently a singleton, and even a class, seems to be overkill for this, but maybe we'll add some more functionality later """ _instance_ = None # Singleton def __new__(cls, *args, **kwargs): """ creates the singleton """ obj = getattr(cls, '_instance_', None) if obj is not None: return obj else: obj = super(Reception, cls).__new__(cls) cls._instance_ = obj return obj
[docs] def __init__(self, plugin_path=None, log=None): """ initializes the class and checks in if a path is provided """ self.log = log if plugin_path is not None: self.checkin(plugin_path)
[docs] def checkin(self, plugin_path): """ import all valid files from plugin_path """ if plugin_path not in sys.path: sys.path.insert(0, plugin_path) for plugin in os.listdir(plugin_path): p = os.path.join(plugin_path, plugin) if plugin != '__init__.py' and os.path.isfile(p) and \ os.path.splitext(p)[1] == '.py': try: __import__(os.path.splitext(plugin)[0], None, None, ['']) except Exception as msg: if self.log is None: print("can't import %r - %s" % ( os.path.splitext(plugin)[0], msg)) else: self.log("can't import %r - %r" % ( os.path.splitext(plugin)[0], msg))
[docs] def guestlist(self, plugin_class=Plugin): """ returns a list of all Plugin subclasses """ found = [] def get_subclass(klass, subclasses): if len(subclasses) == 0: found.append(klass) else: for k in subclasses: get_subclass(k, k.__subclasses__()) get_subclass(plugin_class, plugin_class.__subclasses__()) return found