blob: cefed52485f57aed18d29e7ec15ea6ce59a38ad3 [file] [log] [blame]
Brad Bishop732c6db2015-10-19 14:49:21 -04001#!/usr/bin/env python
2
3# Contributors Listed Below - COPYRIGHT 2015
4# [+] International Business Machines Corp.
5#
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16# implied. See the License for the specific language governing
17# permissions and limitations under the License.
18
Brad Bishop3f43cdb2015-10-28 22:02:09 -040019from xml.etree import ElementTree
20import dbus
21
Brad Bishop732c6db2015-10-19 14:49:21 -040022MAPPER_NAME = 'org.openbmc.objectmapper'
23MAPPER_IFACE = MAPPER_NAME + '.ObjectMapper'
24MAPPER_PATH = '/org/openbmc/objectmapper/objectmapper'
25
26class Path:
27 def __init__(self, path):
28 self.parts = filter(bool, path.split('/'))
29
30 def rel(self, first = None, last = None):
31 # relative
32 return self.get('', first, last)
33
34 def fq(self, first = None, last = None):
35 # fully qualified
36 return self.get('/', first, last)
37
38 def depth(self):
39 return len(self.parts)
40
41 def get(self, prefix = '/', first = None, last = None):
42 if not first:
43 first = 0
44 if not last:
45 last = self.depth()
46 return prefix + '/'.join(self.parts[first:last])
Brad Bishop3f43cdb2015-10-28 22:02:09 -040047
Brad Bishop6fb84612015-11-01 00:06:24 -040048def org_dot_openbmc_match(name):
49 return 'org.openbmc' in name
50
51class TagListMatch(object):
52 def __init__(self, tag_list):
53 self.tag_list = tag_list
54
55 def __call__(self, tag):
56 return tag in self.tag_list
57
Brad Bishop3f43cdb2015-10-28 22:02:09 -040058class IntrospectionNodeParser:
Brad Bishop6fb84612015-11-01 00:06:24 -040059 def __init__(self, data, tag_match = bool, intf_match = bool):
Brad Bishop3f43cdb2015-10-28 22:02:09 -040060 self.data = data
61 self.cache = {}
Brad Bishop6fb84612015-11-01 00:06:24 -040062 self.tag_match = tag_match
63 self.intf_match = intf_match
Brad Bishop3f43cdb2015-10-28 22:02:09 -040064
65 def parse_args(self):
66 return [ x.attrib for x in self.data.findall('arg') ]
67
68 def parse_children(self):
69 return [ x.attrib['name'] for x in self.data.findall('node') ]
70
71 def parse_method_or_signal(self):
72 name = self.data.attrib['name']
73 return name, self.parse_args()
74
75 def parse_interface(self):
76 iface = {}
77 iface['method'] = {}
78 iface['signal'] = {}
Brad Bishop3f43cdb2015-10-28 22:02:09 -040079
80 for node in self.data:
Brad Bishop3f43cdb2015-10-28 22:02:09 -040081 if node.tag not in ['method', 'signal']:
82 continue
Brad Bishop6fb84612015-11-01 00:06:24 -040083 if not self.tag_match(node.tag):
84 continue
85 p = IntrospectionNodeParser(
86 node, self.tag_match, self.intf_match)
Brad Bishop3f43cdb2015-10-28 22:02:09 -040087 n, element = p.parse_method_or_signal()
88 iface[node.tag][n] = element
89
Brad Bishop6fb84612015-11-01 00:06:24 -040090 return iface
Brad Bishop3f43cdb2015-10-28 22:02:09 -040091
92 def parse_node(self):
93 if self.cache:
94 return self.cache
95
96 self.cache['interfaces'] = {}
97 self.cache['children'] = []
98
99 for node in self.data:
Brad Bishop3f43cdb2015-10-28 22:02:09 -0400100 if node.tag == 'interface':
Brad Bishop6fb84612015-11-01 00:06:24 -0400101 p = IntrospectionNodeParser(
102 node, self.tag_match, self.intf_match)
103 name = p.data.attrib['name']
104 if not self.intf_match(name):
105 continue
106 self.cache['interfaces'][name] = p.parse_interface()
Brad Bishop3f43cdb2015-10-28 22:02:09 -0400107 elif node.tag == 'node':
108 self.cache['children'] = self.parse_children()
109
110 return self.cache
111
112 def get_interfaces(self):
113 return self.parse_node()['interfaces']
114
115 def get_children(self):
116 return self.parse_node()['children']
117
118 def recursive_binding(self):
119 return any('/' in s for s in self.get_children())
120
121class IntrospectionParser:
Brad Bishop6fb84612015-11-01 00:06:24 -0400122 def __init__(self, name, bus, tag_match = bool, intf_match = bool):
Brad Bishop3f43cdb2015-10-28 22:02:09 -0400123 self.name = name
124 self.bus = bus
Brad Bishop6fb84612015-11-01 00:06:24 -0400125 self.tag_match = tag_match
126 self.intf_match = intf_match
Brad Bishop3f43cdb2015-10-28 22:02:09 -0400127
128 def _introspect(self, path):
129 try:
130 obj = self.bus.get_object(self.name, path)
131 iface = dbus.Interface(obj, dbus.BUS_DAEMON_IFACE + '.Introspectable')
132 data = iface.Introspect()
133 except dbus.DBusException:
134 return None
135
Brad Bishop6fb84612015-11-01 00:06:24 -0400136 return IntrospectionNodeParser(
137 ElementTree.fromstring(data),
138 self.tag_match,
139 self.intf_match)
Brad Bishop3f43cdb2015-10-28 22:02:09 -0400140
141 def _discover_flat(self, path, parser):
142 items = {}
143 interfaces = parser.get_interfaces().keys()
144 if interfaces:
145 items[path] = {}
146 items[path]['interfaces'] = interfaces
147
148 return items
149
150 def introspect(self, path = '/', parser = None):
151 items = {}
152 if not parser:
153 parser = self._introspect(path)
154 if not parser:
155 return {}
156 items.update(self._discover_flat(path, parser))
157
158 if path != '/':
159 path += '/'
160
161 if parser.recursive_binding():
162 callback = self._discover_flat
163 else:
164 callback = self.introspect
165
166 for k in parser.get_children():
167 parser = self._introspect(path + k)
168 if not parser:
169 continue
170 items.update(callback(path + k, parser))
171
Brad Bishop6fb84612015-11-01 00:06:24 -0400172 if path == '/':
173 print items
174
Brad Bishop3f43cdb2015-10-28 22:02:09 -0400175 return items