1 from types import ModuleType, ClassType
3 from sfa.util.faults import SfaNotImplemented, SfaAPIError
4 from sfa.util.sfalogging import logger
11 This class acts as a wrapper around an SFA interface manager module, but
12 can be used with any python module. The purpose of this class is raise a
13 SfaNotImplemented exception if someone attempts to use an attribute
14 (could be a callable) thats not available in the library by checking the
15 library using hasattr. This helps to communicate better errors messages
16 to the users and developers in the event that a specifiec operation
17 is not implemented by a libarary and will generally be more helpful than
18 the standard AttributeError
21 def __init__(self, manager, interface, config):
22 if isinstance(manager, ModuleType):
23 # old-fashioned module implementation
24 self.manager = manager
25 elif isinstance(manager, ClassType):
26 # create an instance; we don't pass the api in argument as it is passed
27 # to the actual method calls anyway
28 self.manager = manager(config)
30 # that's what happens when there's something wrong with the db
31 # or any bad stuff of that kind at startup time
33 "Failed to create a manager, startup sequence is broken")
35 "Argument to ManagerWrapper must be a module or class")
36 self.interface = interface
38 def __getattr__(self, method):
39 if not hasattr(self.manager, method):
40 raise SfaNotImplemented(self.interface, method)
41 return getattr(self.manager, method)