Package Products :: Package Zuul :: Package routers :: Module introspection
[hide private]
[frames] | no frames]

Source Code for Module Products.Zuul.routers.introspection

  1  ############################################################################## 
  2  #  
  3  # Copyright (C) Zenoss, Inc. 2012, all rights reserved. 
  4  #  
  5  # This content is made available according to terms specified in 
  6  # License.zenoss under the directory where your Zenoss product is installed. 
  7  #  
  8  ############################################################################## 
  9   
 10  import inspect 
 11   
 12  from Products import Zuul 
 13  from Products.ZenUtils.Ext import DirectRouter 
 14  from Products.ZenUtils.extdirect.router import DirectResponse 
 15  from Products.ZenUtils.extdirect.zope.metaconfigure import allDirectRouters 
 16   
 17   
18 -class IntrospectionRouter(DirectRouter):
19 """ 20 Provide a JSON API to explore the available routers and their methods. 21 22 from Products.Zuul.routers.introspection import IntrospectionRouter 23 zz = IntrospectionRouter(dmd) 24 """ 25
26 - def _getAllRouters(self):
27 return allDirectRouters
28
29 - def getAllRouters(self):
30 """ 31 Return a description of the Zenoss routers available. 32 33 from Products.Zuul.routers.introspection import IntrospectionRouter 34 zz = IntrospectionRouter(dmd) 35 pprint(zz.getAllRouters().data) 36 """ 37 routers = self._getAllRouters() 38 data = map(self._getRouterInfo, routers) 39 return DirectResponse(data=Zuul.marshal(data))
40
41 - def _getRouterInfo(self, router=None):
42 klass = self.__class__ if router is None else router 43 filename = inspect.getfile(klass) 44 data = allDirectRouters.get(klass, {}).copy() 45 data.update( dict(action=klass.__name__, filename=filename, 46 documentation=inspect.getdoc(router), 47 urlpath='/zport/dmd/'+data['name'], 48 )) 49 return data
50
51 - def getRouterInfo(self, router=None):
52 """ 53 Return information about the router 54 """ 55 data = self._getRouterInfo(router) 56 return DirectResponse(data=Zuul.marshal(data))
57
58 - def _getRouterByName(self, router):
59 routers = [x for x in self._getAllRouters() if router in x.__name__] 60 return routers
61
62 - def getRouterMethods(self, router=None):
63 """ 64 Return a JSON list of methods, arguments and documentation 65 66 Example usage from zendmd: 67 68 from Products.Zuul.routers.introspection import IntrospectionRouter 69 zz = IntrospectionRouter(dmd) 70 pprint(zz.getRouterMethods('DeviceRouter').data) 71 """ 72 if router is not None: 73 klasses = self._getRouterByName(router) 74 if klasses: 75 # TODO: log something? 76 klass = klasses[0] 77 else: 78 return DirectResponse.fail(msg="No router named '%s' found" % router) 79 else: 80 klass = self.__class__ 81 82 methods = {} 83 for name, code in inspect.getmembers(klass): 84 if name.startswith('_'): 85 continue 86 if not inspect.ismethod(code): 87 continue 88 89 argspec = inspect.getargspec(code) 90 if argspec.defaults is None: 91 args = argspec.args[1:] # Ignore 'self' 92 kwargs = {} 93 else: 94 n = len(argspec.defaults) 95 args = argspec.args[1:-n] # Ignore 'self' 96 kwargs = dict(zip(argspec.args[-n:], argspec.defaults)) 97 98 methods[name] = dict( 99 documentation=inspect.getdoc(code), 100 kwargs=kwargs, 101 args=args, 102 ) 103 104 return DirectResponse(data=Zuul.marshal(methods))
105