Stop using interactive-Python syntax in tutorial to reduce user confusion.
[dbus-python-phuang.git] / dbus / _expat_introspect_parser.py
blobb86b2cabcd89b9d0b26fc6becd958e40a461235e
1 # Copyright (C) 2003, 2004, 2005, 2006 Red Hat Inc. <http://www.redhat.com/>
2 # Copyright (C) 2003 David Zeuthen
3 # Copyright (C) 2004 Rob Taylor
4 # Copyright (C) 2005, 2006 Collabora Ltd. <http://www.collabora.co.uk/>
6 # Licensed under the Academic Free License version 2.1
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 from xml.parsers.expat import ExpatError, ParserCreate
23 from dbus.exceptions import IntrospectionParserException
25 class _Parser(object):
26 __slots__ = ('map', 'in_iface', 'in_method', 'sig')
27 def __init__(self):
28 self.map = {}
29 self.in_iface = ''
30 self.in_method = ''
31 self.sig = ''
33 def parse(self, data):
34 parser = ParserCreate('UTF-8', ' ')
35 parser.buffer_text = True
36 parser.StartElementHandler = self.StartElementHandler
37 parser.EndElementHandler = self.EndElementHandler
38 parser.Parse(data)
39 return self.map
41 def StartElementHandler(self, name, attributes):
42 if not self.in_iface:
43 if (not self.in_method and name == 'interface'):
44 self.in_iface = attributes['name']
45 else:
46 if (not self.in_method and name == 'method'):
47 self.in_method = attributes['name']
48 elif (self.in_method and name == 'arg'):
49 if attributes.get('direction', 'in') == 'in':
50 self.sig += attributes['type']
52 def EndElementHandler(self, name):
53 if self.in_iface:
54 if (not self.in_method and name == 'interface'):
55 self.in_iface = ''
56 elif (self.in_method and name == 'method'):
57 self.map[self.in_iface + '.' + self.in_method] = self.sig
58 self.in_method = ''
59 self.sig = ''
61 def process_introspection_data(data):
62 """Return a dict mapping ``interface.method`` strings to the
63 concatenation of all their 'in' parameters, and mapping
64 ``interface.signal`` strings to the concatenation of all their
65 parameters.
67 Example output::
70 'com.example.SignalEmitter.OneString': 's',
71 'com.example.MethodImplementor.OneInt32Argument': 'i',
74 :Parameters:
75 `data` : str
76 The introspection XML. Must be an 8-bit string of UTF-8.
77 """
78 try:
79 return _Parser().parse(data)
80 except Exception, e:
81 raise IntrospectionParserException('%s: %s' % (e.__class__, e))