Update NEWS, README for 0.80.0
[dbus-python-phuang.git] / dbus / _expat_introspect_parser.py
blob400396a5888f6c4ed36eb8b6b15d125c503d4b7f
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 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 from xml.parsers.expat import ExpatError, ParserCreate
25 from dbus.exceptions import IntrospectionParserException
27 class _Parser(object):
28 __slots__ = ('map', 'in_iface', 'in_method', 'sig')
29 def __init__(self):
30 self.map = {}
31 self.in_iface = ''
32 self.in_method = ''
33 self.sig = ''
35 def parse(self, data):
36 parser = ParserCreate('UTF-8', ' ')
37 parser.buffer_text = True
38 parser.StartElementHandler = self.StartElementHandler
39 parser.EndElementHandler = self.EndElementHandler
40 parser.Parse(data)
41 return self.map
43 def StartElementHandler(self, name, attributes):
44 if not self.in_iface:
45 if (not self.in_method and name == 'interface'):
46 self.in_iface = attributes['name']
47 else:
48 if (not self.in_method and name == 'method'):
49 self.in_method = attributes['name']
50 elif (self.in_method and name == 'arg'):
51 if attributes.get('direction', 'in') == 'in':
52 self.sig += attributes['type']
54 def EndElementHandler(self, name):
55 if self.in_iface:
56 if (not self.in_method and name == 'interface'):
57 self.in_iface = ''
58 elif (self.in_method and name == 'method'):
59 self.map[self.in_iface + '.' + self.in_method] = self.sig
60 self.in_method = ''
61 self.sig = ''
63 def process_introspection_data(data):
64 """Return a dict mapping ``interface.method`` strings to the
65 concatenation of all their 'in' parameters, and mapping
66 ``interface.signal`` strings to the concatenation of all their
67 parameters.
69 Example output::
72 'com.example.SignalEmitter.OneString': 's',
73 'com.example.MethodImplementor.OneInt32Argument': 'i',
76 :Parameters:
77 `data` : str
78 The introspection XML. Must be an 8-bit string of UTF-8.
79 """
80 try:
81 return _Parser().parse(data)
82 except Exception, e:
83 raise IntrospectionParserException('%s: %s' % (e.__class__, e))