Add wrapper for DBusServer.
[dbus-python-phuang.git] / dbus / _expat_introspect_parser.py
blob96b27ad92596800f3769050b14d17019ef51fa99
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 # Permission is hereby granted, free of charge, to any person
7 # obtaining a copy of this software and associated documentation
8 # files (the "Software"), to deal in the Software without
9 # restriction, including without limitation the rights to use, copy,
10 # modify, merge, publish, distribute, sublicense, and/or sell copies
11 # of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 # DEALINGS IN THE SOFTWARE.
26 from xml.parsers.expat import ExpatError, ParserCreate
27 from dbus.exceptions import IntrospectionParserException
29 class _Parser(object):
30 __slots__ = ('map', 'in_iface', 'in_method', 'sig')
31 def __init__(self):
32 self.map = {}
33 self.in_iface = ''
34 self.in_method = ''
35 self.sig = ''
37 def parse(self, data):
38 parser = ParserCreate('UTF-8', ' ')
39 parser.buffer_text = True
40 parser.StartElementHandler = self.StartElementHandler
41 parser.EndElementHandler = self.EndElementHandler
42 parser.Parse(data)
43 return self.map
45 def StartElementHandler(self, name, attributes):
46 if not self.in_iface:
47 if (not self.in_method and name == 'interface'):
48 self.in_iface = attributes['name']
49 else:
50 if (not self.in_method and name == 'method'):
51 self.in_method = attributes['name']
52 elif (self.in_method and name == 'arg'):
53 if attributes.get('direction', 'in') == 'in':
54 self.sig += attributes['type']
56 def EndElementHandler(self, name):
57 if self.in_iface:
58 if (not self.in_method and name == 'interface'):
59 self.in_iface = ''
60 elif (self.in_method and name == 'method'):
61 self.map[self.in_iface + '.' + self.in_method] = self.sig
62 self.in_method = ''
63 self.sig = ''
65 def process_introspection_data(data):
66 """Return a dict mapping ``interface.method`` strings to the
67 concatenation of all their 'in' parameters, and mapping
68 ``interface.signal`` strings to the concatenation of all their
69 parameters.
71 Example output::
74 'com.example.SignalEmitter.OneString': 's',
75 'com.example.MethodImplementor.OneInt32Argument': 'i',
78 :Parameters:
79 `data` : str
80 The introspection XML. Must be an 8-bit string of UTF-8.
81 """
82 try:
83 return _Parser().parse(data)
84 except Exception, e:
85 raise IntrospectionParserException('%s: %s' % (e.__class__, e))