1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
import os import imp import sys
class Plugin(object):
filters = {} actions = {}
@classmethod def folder(cls, path, module_name='pp.plugins'): root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
parent_module = imp.new_module(module_name) sys.modules[module_name] = parent_module
for folder_path, folder_names, filenames in os.walk(path): for filename in filenames: if filename.endswith('.py'): file_path = os.path.join(folder_path, filename) source_name = "{0}.{1}".format(module_name, filename.replace('.py', '')) imp.load_source(source_name, file_path)
@classmethod def add_filter(cls, name, method): cls.filters[name] = method
@classmethod def apply_filter(cls, name, value): filter = cls.filters.get(name)
if not filter: raise ValueError('Can not found filter named: {0}'.format(name)) else: return filter(value)
@classmethod def add_action(cls, name, method): cls.actions[name] = method
@classmethod def do_action(cls, name, value): action = cls.actions.get(name)
if not action: raise ValueError('Can not found action named: {0}'.format(name)) else: action(value)
|