Skip to content

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
stanfeldman committed Aug 21, 2012
1 parent a5f006a commit 4d8bbd7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pyplug import PluginLoader, Interface
from interface import MyInterface, MyBaseInterface
from putils.dynamics import Importer, Introspector
import inspect


if __name__ == "__main__":
Expand All @@ -23,4 +24,3 @@
print res
#print MyInterface.attr()
print MyInterface.plugins["Plugin2"].get_smth()
print MyBaseInterface.__subclasses__()
49 changes: 26 additions & 23 deletions pyplug/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from putils.dynamics import Importer
from putils.dynamics import Importer, Introspector
from putils.filesystem import Dir
from types import FunctionType
import os, sys
import mimetypes
from copy import copy


class MetaPlugin(type):
Expand All @@ -11,7 +12,7 @@ def __new__(metaclass, classname, bases, attrs):
new_obj = new_class()
if "implements" in attrs:
for iface in attrs["implements"]:
iface.plugins[new_obj.__class__.__name__] = new_obj
iface._plugins[new_obj.__class__.__name__] = new_obj
return new_class


Expand All @@ -26,7 +27,9 @@ class Plugin(object):
class MetaInterface(type):
def __new__(metaclass, classname, bases, attrs):
new_class = super(MetaInterface, metaclass).__new__(metaclass, classname, bases, attrs)
new_class.plugins = {}
new_class._plugins = {}
new_class.plugins = classmethod(MetaInterface.plugins)
new_class.implementations = classmethod(MetaInterface.implementations)
for k, v in attrs.iteritems():
if type(v) is FunctionType:
setattr(new_class, k+"_get_all", classmethod(MetaInterface.meta_method_get_all(k)))
Expand All @@ -36,14 +39,26 @@ def __new__(metaclass, classname, bases, attrs):
setattr(new_class, k+"_get_all", classmethod(MetaInterface.meta_property_all(k)))
setattr(new_class, k, classmethod(MetaInterface.meta_property_first(k)))
return new_class

@staticmethod
def plugins(cls):
results = {}
for pl_name, pl_code in cls._plugins.iteritems():
results[pl_name] = pl_code
for subclass in Introspector.all_subclasses(cls):
for name, code in subclass._plugins.iteritems():
if name not in results:
results[name] = code
return results

@staticmethod
def implementations(cls):
return list(cls.plugins().values())

@staticmethod
def meta_method_get_all(method_name):
def wrapper(cls, *args, **kwargs):
impls = list(cls.plugins.values())
for subclass in cls.__subclasses__():
impls = impls + list(subclass.plugins.values())
for impl in impls:
for impl in cls.implementations():
if hasattr(impl, method_name):
method = getattr(impl, method_name)
yield method(*args, **kwargs)
Expand All @@ -52,10 +67,7 @@ def wrapper(cls, *args, **kwargs):
@staticmethod
def meta_method_call_all(method_name):
def wrapper(cls, *args, **kwargs):
impls = list(cls.plugins.values())
for subclass in cls.__subclasses__():
impls = impls + list(subclass.plugins.values())
for impl in impls:
for impl in cls.implementations():
if hasattr(impl, method_name):
method = getattr(impl, method_name)
method(*args, **kwargs)
Expand All @@ -64,10 +76,7 @@ def wrapper(cls, *args, **kwargs):
@staticmethod
def meta_method_call_first(method_name):
def wrapper(cls, *args, **kwargs):
impls = list(cls.plugins.values())
for subclass in cls.__subclasses__():
impls = impls + list(subclass.plugins.values())
for impl in impls:
for impl in cls.implementations():
if hasattr(impl, method_name):
method = getattr(impl, method_name)
return method(*args, **kwargs)
Expand All @@ -76,21 +85,15 @@ def wrapper(cls, *args, **kwargs):
@staticmethod
def meta_property_all(method_name):
def wrapper(cls):
impls = list(cls.plugins.values())
for subclass in cls.__subclasses__():
impls = impls + list(subclass.plugins.values())
for impl in impls:
for impl in cls.implementations():
if hasattr(impl, method_name):
yield getattr(impl, method_name)
return wrapper

@staticmethod
def meta_property_first(method_name):
def wrapper(cls):
impls = list(cls.plugins.values())
for subclass in cls.__subclasses__():
impls = impls + list(subclass.plugins.values())
for impl in impls:
for impl in cls.implementations():
if hasattr(impl, method_name):
return getattr(impl, method_name)
return wrapper
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

0 comments on commit 4d8bbd7

Please sign in to comment.