python3 - 2to3 + miscell obvious tweaks
[sfa.git] / sfa / managers / managerwrapper.py
1 from types import ModuleType
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         # if it's a class
26         elif isinstance(manager, type):
27             # create an instance; we don't pass the api in argument as it is passed
28             # to the actual method calls anyway
29             self.manager = manager(config)
30         else:
31             # that's what happens when there's something wrong with the db
32             # or any bad stuff of that kind at startup time
33             logger.log_exc(
34                 "Failed to create a manager, startup sequence is broken")
35             raise SfaAPIError(
36                 "Argument to ManagerWrapper must be a module or class")
37         self.interface = interface
38
39     def __getattr__(self, method):
40         if not hasattr(self.manager, method):
41             raise SfaNotImplemented(self.interface, method)
42         return getattr(self.manager, method)