Package Products ::
Package Zuul ::
Package routers ::
Module introspection
|
|
1
2
3
4
5
6
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
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
27 return allDirectRouters
28
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
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
52 """
53 Return information about the router
54 """
55 data = self._getRouterInfo(router)
56 return DirectResponse(data=Zuul.marshal(data))
57
61
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
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:]
92 kwargs = {}
93 else:
94 n = len(argspec.defaults)
95 args = argspec.args[1:-n]
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