[PATCH phosphor-objmgr 2/2] Move introspection parser to OpenBMCMapper

OpenBMC Patches patches at stwcx.xyz
Thu Oct 29 13:06:12 AEDT 2015


From: Brad Bishop <bradleyb at us.ibm.com>

Doing this so rest-dbus can use it.
---
 OpenBMCMapper.py | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 phosphor-mapper  | 107 +------------------------------------------------------
 2 files changed, 108 insertions(+), 106 deletions(-)

diff --git a/OpenBMCMapper.py b/OpenBMCMapper.py
index 1862a46..30a93fb 100644
--- a/OpenBMCMapper.py
+++ b/OpenBMCMapper.py
@@ -16,6 +16,9 @@
 # implied. See the License for the specific language governing
 # permissions and limitations under the License.
 
+from xml.etree import ElementTree
+import dbus
+
 MAPPER_NAME = 'org.openbmc.objectmapper'
 MAPPER_IFACE = MAPPER_NAME + '.ObjectMapper'
 MAPPER_PATH = '/org/openbmc/objectmapper/objectmapper'
@@ -41,3 +44,107 @@ class Path:
 		if not last:
 			last = self.depth()
 		return prefix + '/'.join(self.parts[first:last])
+
+class IntrospectionNodeParser:
+	def __init__(self, data):
+		self.data = data
+		self.cache = {}
+
+	def parse_args(self):
+		return [ x.attrib for x in self.data.findall('arg') ]
+
+	def parse_children(self):
+		return [ x.attrib['name'] for x in self.data.findall('node') ]
+
+	def parse_method_or_signal(self):
+		name = self.data.attrib['name']
+		return name, self.parse_args()
+
+	def parse_interface(self):
+		iface = {}
+		iface['method'] = {}
+		iface['signal'] = {}
+		name = self.data.attrib['name']
+
+		for node in self.data:
+			p = IntrospectionNodeParser(node)
+			if node.tag not in ['method', 'signal']:
+				continue
+			n, element = p.parse_method_or_signal()
+			iface[node.tag][n] = element
+
+		return name, iface
+
+	def parse_node(self):
+		if self.cache:
+			return self.cache
+
+		self.cache['interfaces'] = {}
+		self.cache['children'] = []
+
+		for node in self.data:
+			p = IntrospectionNodeParser(node)
+			if node.tag == 'interface':
+				name, ifaces = p.parse_interface()
+				self.cache['interfaces'][name] = ifaces
+			elif node.tag == 'node':
+				self.cache['children'] = self.parse_children()
+
+		return self.cache
+
+	def get_interfaces(self):
+		return self.parse_node()['interfaces']
+
+	def get_children(self):
+		return self.parse_node()['children']
+
+	def recursive_binding(self):
+		return any('/' in s for s in self.get_children())
+
+class IntrospectionParser:
+	def __init__(self, name, bus):
+		self.name = name
+		self.bus = bus
+
+	def _introspect(self, path):
+		try:
+			obj = self.bus.get_object(self.name, path)
+			iface = dbus.Interface(obj, dbus.BUS_DAEMON_IFACE + '.Introspectable')
+			data = iface.Introspect()
+		except dbus.DBusException:
+			return None
+
+		return IntrospectionNodeParser(ElementTree.fromstring(data))
+
+	def _discover_flat(self, path, parser):
+		items = {}
+		interfaces = parser.get_interfaces().keys()
+		if interfaces:
+			items[path] = {}
+			items[path]['interfaces'] = interfaces
+
+		return items
+
+	def introspect(self, path = '/', parser = None):
+		items = {}
+		if not parser:
+			parser = self._introspect(path)
+		if not parser:
+			return {}
+		items.update(self._discover_flat(path, parser))
+
+		if path != '/':
+			path += '/'
+
+		if parser.recursive_binding():
+			callback = self._discover_flat
+		else:
+			callback = self.introspect
+
+		for k in parser.get_children():
+			parser = self._introspect(path + k)
+			if not parser:
+				continue
+			items.update(callback(path + k, parser))
+
+		return items
diff --git a/phosphor-mapper b/phosphor-mapper
index 890e546..313baa2 100644
--- a/phosphor-mapper
+++ b/phosphor-mapper
@@ -21,8 +21,7 @@ import dbus
 import dbus.service
 import dbus.mainloop.glib
 import gobject
-from xml.etree import ElementTree
-from OpenBMCMapper import Path
+from OpenBMCMapper import Path, IntrospectionParser
 import OpenBMCMapper
 
 class DictionaryCache:
@@ -260,110 +259,6 @@ class ObjectMapper(dbus.service.Object):
 			values.update(self.GetTree(x, depth, match_type))
 		return values
 
-class IntrospectionNodeParser:
-	def __init__(self, data):
-		self.data = data
-		self.cache = {}
-
-	def parse_args(self):
-		return [ x.attrib for x in self.data.findall('arg') ]
-
-	def parse_children(self):
-		return [ x.attrib['name'] for x in self.data.findall('node') ]
-
-	def parse_method_or_signal(self):
-		name = self.data.attrib['name']
-		return name, self.parse_args()
-
-	def parse_interface(self):
-		iface = {}
-		iface['method'] = {}
-		iface['signal'] = {}
-		name = self.data.attrib['name']
-
-		for node in self.data:
-			p = IntrospectionNodeParser(node)
-			if node.tag not in ['method', 'signal']:
-				continue
-			n, element = p.parse_method_or_signal()
-			iface[node.tag][n] = element
-
-		return name, iface
-
-	def parse_node(self):
-		if self.cache:
-			return self.cache
-
-		self.cache['interfaces'] = {}
-		self.cache['children'] = []
-
-		for node in self.data:
-			p = IntrospectionNodeParser(node)
-			if node.tag == 'interface':
-				name, ifaces = p.parse_interface()
-				self.cache['interfaces'][name] = ifaces
-			elif node.tag == 'node':
-				self.cache['children'] = self.parse_children()
-
-		return self.cache
-
-	def get_interfaces(self):
-		return self.parse_node()['interfaces']
-
-	def get_children(self):
-		return self.parse_node()['children']
-
-	def recursive_binding(self):
-		return any('/' in s for s in self.get_children())
-
-class IntrospectionParser:
-	def __init__(self, name, bus):
-		self.name = name
-		self.bus = bus
-
-	def _introspect(self, path):
-		try:
-			obj = self.bus.get_object(self.name, path)
-			iface = dbus.Interface(obj, dbus.BUS_DAEMON_IFACE + '.Introspectable')
-			data = iface.Introspect()
-		except dbus.DBusException:
-			return None
-
-		return IntrospectionNodeParser(ElementTree.fromstring(data))
-
-	def _discover_flat(self, path, parser):
-		items = {}
-		interfaces = parser.get_interfaces().keys()
-		if interfaces:
-			items[path] = {}
-			items[path]['interfaces'] = interfaces
-
-		return items
-
-	def introspect(self, path = '/', parser = None):
-		items = {}
-		if not parser:
-			parser = self._introspect(path)
-		if not parser:
-			return {}
-		items.update(self._discover_flat(path, parser))
-
-		if path != '/':
-			path += '/'
-
-		if parser.recursive_binding():
-			callback = self._discover_flat
-		else:
-			callback = self.introspect
-
-		for k in parser.get_children():
-			parser = self._introspect(path + k)
-			if not parser:
-				continue
-			items.update(callback(path + k, parser))
-
-		return items
-
 class BusWrapper:
 	def __init__(self, bus):
 		self.dbus = bus
-- 
2.6.0




More information about the openbmc mailing list