X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fmanagers%2Fmanagerwrapper.py;h=77af9bf6fdc89b436b3d84ac0b43f3781235b0e7;hb=3147e8e089dd44e40b852cc121483189807ffa9b;hp=218cd4a49f00e805e077ddcb50622917dbf48906;hpb=ee8a376da1107884bee1ea29248a70e4da8410c9;p=sfa.git diff --git a/sfa/managers/managerwrapper.py b/sfa/managers/managerwrapper.py index 218cd4a4..77af9bf6 100644 --- a/sfa/managers/managerwrapper.py +++ b/sfa/managers/managerwrapper.py @@ -1,32 +1,11 @@ -from sfa.util.faults import SfaNotImplemented -from sfa.util.sfalogging import logger +from types import ModuleType, ClassType -## locate the right manager -def import_manager(kind, type): - """ - kind expected in ['registry', 'aggregate', 'slice', 'component'] - type is e.g. 'pl' or 'max' or whatever - """ - basepath = 'sfa.managers' - qualified = "%s.%s_manager_%s"%(basepath,kind,type) - generic = "%s.%s_manager"%(basepath,kind) +from sfa.util.faults import SfaNotImplemented, SfaAPIError +from sfa.util.sfalogging import logger - message="import_manager for kind=%s and type=%s"%(kind,type) - try: - manager = __import__(qualified, fromlist=[basepath]) - logger.info ("%s: loaded %s"%(message,qualified)) - except: - try: - manager = __import__ (generic, fromlist=[basepath]) - if type != 'pl' : - logger.warn ("%s: using generic with type!='pl'"%(message)) - logger.info("%s: loaded %s"%(message,generic)) - except: - manager=None - logger.log_exc("%s: unable to import either %s or %s"%(message,qualified,generic)) - return manager - #################### + + class ManagerWrapper: """ This class acts as a wrapper around an SFA interface manager module, but @@ -38,12 +17,25 @@ class ManagerWrapper: is not implemented by a libarary and will generally be more helpful than the standard AttributeError """ - def __init__(self, manager, interface): - self.manager = manager + + def __init__(self, manager, interface, config): + if isinstance(manager, ModuleType): + # old-fashioned module implementation + self.manager = manager + elif isinstance(manager, ClassType): + # create an instance; we don't pass the api in argument as it is passed + # to the actual method calls anyway + self.manager = manager(config) + else: + # that's what happens when there's something wrong with the db + # or any bad stuff of that kind at startup time + logger.log_exc( + "Failed to create a manager, startup sequence is broken") + raise SfaAPIError( + "Argument to ManagerWrapper must be a module or class") self.interface = interface - + def __getattr__(self, method): if not hasattr(self.manager, method): - raise SfaNotImplemented(method, self.interface) + raise SfaNotImplemented(self.interface, method) return getattr(self.manager, method) -