split the various *Api classes into somethin more sensible
[sfa.git] / sfa / managers / managerwrapper.py
1 from sfa.util.faults import SfaNotImplemented
2 from sfa.util.sfalogging import logger
3
4 ## locate the right manager
5 def import_manager(kind, type):
6     """
7     kind expected in ['registry', 'aggregate', 'slice', 'component']
8     type is e.g. 'pl' or 'max' or whatever
9     """
10     basepath = 'sfa.managers'
11     qualified = "%s.%s_manager_%s"%(basepath,kind,type)
12     generic = "%s.%s_manager"%(basepath,kind)
13
14     message="import_manager for kind=%s and type=%s"%(kind,type)
15     try: 
16         manager = __import__(qualified, fromlist=[basepath])
17         logger.info ("%s: loaded %s"%(message,qualified))
18     except:
19         try:
20             manager = __import__ (generic, fromlist=[basepath])
21             if type != 'pl' : 
22                 logger.warn ("%s: using generic with type!='pl'"%(message))
23             logger.info("%s: loaded %s"%(message,generic))
24         except:
25             manager=None
26             logger.log_exc("%s: unable to import either %s or %s"%(message,qualified,generic))
27     return manager
28     
29 ####################
30 class ManagerWrapper:
31     """
32     This class acts as a wrapper around an SFA interface manager module, but
33     can be used with any python module. The purpose of this class is raise a 
34     SfaNotImplemented exception if someone attempts to use an attribute 
35     (could be a callable) thats not available in the library by checking the
36     library using hasattr. This helps to communicate better errors messages 
37     to the users and developers in the event that a specifiec operation 
38     is not implemented by a libarary and will generally be more helpful than
39     the standard AttributeError         
40     """
41     def __init__(self, manager, interface):
42         self.manager = manager
43         self.interface = interface
44         
45     def __getattr__(self, method):
46         if not hasattr(self.manager, method):
47             raise SfaNotImplemented(method, self.interface)
48         return getattr(self.manager, method)
49