77af9bf6fdc89b436b3d84ac0b43f3781235b0e7
[sfa.git] / sfa / managers / managerwrapper.py
1 from types import ModuleType, ClassType
2
3 from sfa.util.faults import SfaNotImplemented, SfaAPIError
4 from sfa.util.sfalogging import logger
5
6 ####################
7
8
9 class ManagerWrapper:
10     """
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         
19     """
20
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)
29         else:
30             # that's what happens when there's something wrong with the db
31             # or any bad stuff of that kind at startup time
32             logger.log_exc(
33                 "Failed to create a manager, startup sequence is broken")
34             raise SfaAPIError(
35                 "Argument to ManagerWrapper must be a module or class")
36         self.interface = interface
37
38     def __getattr__(self, method):
39         if not hasattr(self.manager, method):
40             raise SfaNotImplemented(self.interface, method)
41         return getattr(self.manager, method)