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