plugins - Calling instance methods of function caller in python -
hi everyone,
i'm trying create 'plugin' system using python (2.7.6). have class implements basic manipulation on custom hardware board (uses c++ shared library), , i'd developers able extend functionality of class using sort of plugin system. various reasons, functionality should available @ same level (say, 1 class instance), function calls, explicitly implemented or imported plugins, should callable board instance.
additionally, functions in these plugins need use functions defined in calling class. example, channelisation plugin need use read , write calls implemented in board class imports it. i've started abstract base class plugins follows:
class firmwareblock(object): """ abstract super class must used implement firmware block plugins used access layer """ def __init__(self): """ class initialiser """ # define class abstract class __metaclass__ = abcmeta @abstractmethod def initialise(self): """ abstract method firmware block initialisation should performed """ pass @abstractmethod def status_check(self): """ abstract method status checks should performed """ pass @abstractmethod def clean_up(self): """ abstract method cleaning should performed when unloading firmware """ pass
the functions defined in class must implemented subclasses. in turn, subclasses can implement own specific functions must included in board class, example:
class channeliserblock(firmwareblock): def initialise(self): print "channeliserblock has been initialised" return true def status_check(self): print "checking status" return true def clean_up(self): print "performed clean_up" return true def initialise_channeliser(self, *args, **kwargs): value = 1 writeregister('register1', value) if readregister('register1', value) == value: return true else: return false
the board (calling) class should capable of loading in modules, inspecting custom functions , wrapping them callable directly it. have till now:
def loadplugin(self, plugin): # check if module available if plugin not in self._availableplugins: print "module %s not available" % plugin return error.failure # list of class methods , remove availale in superclass methods = [name name, mtype in inspect.getmembers(eval(plugin), predicate=inspect.ismethod) if name not in [a a, b in inspect.getmembers(firmwareblock, predicate=inspect.ismethod)] ] # create plugin instances instance = globals()[plugin] self.__dict__[plugin] = instance # import plugins function class method in methods: # link class method function pointer self.__dict__[method] = getattr(instance, method)
this should allow me call plugin methods, plugins cannot access functions defined in board itself. can, of course, pass instance of board init when instantiating class, tightly couples plugins board classes. wondering whether there's better way (fro example, extracting methods , attaching them directly board class, assuming no instance members being used)
Comments
Post a Comment