Skip to content

Commit

Permalink
now you can get plugin by name, call all implementations or get resul…
Browse files Browse the repository at this point in the history
…t from them one by one
  • Loading branch information
stanfeldman committed Jul 16, 2012
1 parent 776fe51 commit ff2736d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions example/interface.py
Expand Up @@ -4,3 +4,5 @@
class MyInterface(Interface):
def do_smth(self):
pass
def get_smth(self):
pass
6 changes: 5 additions & 1 deletion example/main.py
Expand Up @@ -10,4 +10,8 @@
project_dir = os.path.dirname(os.path.abspath(__file__))
plugin_dir = os.path.join(project_dir, "plugins")
PluginLoader.load(project_dir, plugin_dir)
MyInterface.do_smth()
for result in MyInterface.get_smth_get_all():
print result
MyInterface.do_smth_call_all()
print MyInterface.get_smth()
print MyInterface.plugins["plugin2"].get_smth()
4 changes: 3 additions & 1 deletion example/plugins/plugin1.py
Expand Up @@ -2,7 +2,6 @@
from interface import MyInterface

class Plugin1(Plugin):
id = "12423"
name = "plugin1"
version = "0.0.1"
author = "Stanislav Feldman"
Expand All @@ -12,3 +11,6 @@ class Plugin1(Plugin):

def do_smth(self):
print "hello from plugin1"

def get_smth(self):
return "result from plugin1"
4 changes: 3 additions & 1 deletion example/plugins/plugin2.py
Expand Up @@ -2,7 +2,6 @@
from interface import MyInterface

class Plugin2(Plugin):
id = "22"
name = "plugin2"
version = "0.0.1"
author = "Stanislav Feldman"
Expand All @@ -12,3 +11,6 @@ class Plugin2(Plugin):

def do_smth(self):
print "hi from plugin2"

def get_smth(self):
return "result from plugin2"
30 changes: 24 additions & 6 deletions pyplug/__init__.py
Expand Up @@ -11,7 +11,7 @@ def __new__(metaclass, classname, bases, attrs):
if "implements" in attrs:
new_obj = new_class()
for iface in attrs["implements"]:
iface.implementors.append(new_obj)
iface.plugins[new_obj.name] = new_obj
return new_class


Expand All @@ -22,20 +22,38 @@ class Plugin(object):
class MetaInterface(type):
def __new__(metaclass, classname, bases, attrs):
new_class = super(MetaInterface, metaclass).__new__(metaclass, classname, bases, attrs)
new_class.implementors = []
for k, v in new_class.__dict__.iteritems():
new_class.plugins = {}
for k, v in attrs.iteritems():
if type(v) is FunctionType:
setattr(new_class, k, classmethod(MetaInterface.meta_method(k)))
setattr(new_class, k+"_get_all", classmethod(MetaInterface.meta_method_get_all(k)))
setattr(new_class, k+"_call_all", classmethod(MetaInterface.meta_method_call_all(k)))
setattr(new_class, k, classmethod(MetaInterface.meta_method_call_first(k)))
return new_class

@staticmethod
def meta_method(method_name):
def meta_method_get_all(method_name):
def wrapper(cls, *args, **kwargs):
for impl in cls.implementors:
for impl in cls.plugins.values():
method = getattr(impl, method_name)
yield method(*args, **kwargs)
return wrapper

@staticmethod
def meta_method_call_all(method_name):
def wrapper(cls, *args, **kwargs):
for impl in cls.plugins.values():
method = getattr(impl, method_name)
method(*args, **kwargs)
return wrapper

@staticmethod
def meta_method_call_first(method_name):
def wrapper(cls, *args, **kwargs):
for impl in cls.plugins.values():
method = getattr(impl, method_name)
return method(*args, **kwargs)
return wrapper


class Interface(object):
__metaclass__ = MetaInterface
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -6,7 +6,7 @@

setup(
name = "pyplug",
version = "0.1.0",
version = "0.1.1",
author = "Stanislav Feldman",
description = ("Python plugin framework"),
url = "https://github.com/stanislavfeldman/pyplug",
Expand Down

0 comments on commit ff2736d

Please sign in to comment.