Blame dbus/_expat_introspect_parser.py

Packit 130fc8
# Copyright (C) 2003, 2004, 2005, 2006 Red Hat Inc. <http://www.redhat.com/>
Packit 130fc8
# Copyright (C) 2003 David Zeuthen
Packit 130fc8
# Copyright (C) 2004 Rob Taylor
Packit 130fc8
# Copyright (C) 2005, 2006 Collabora Ltd. <http://www.collabora.co.uk/>
Packit 130fc8
#
Packit 130fc8
# Permission is hereby granted, free of charge, to any person
Packit 130fc8
# obtaining a copy of this software and associated documentation
Packit 130fc8
# files (the "Software"), to deal in the Software without
Packit 130fc8
# restriction, including without limitation the rights to use, copy,
Packit 130fc8
# modify, merge, publish, distribute, sublicense, and/or sell copies
Packit 130fc8
# of the Software, and to permit persons to whom the Software is
Packit 130fc8
# furnished to do so, subject to the following conditions:
Packit 130fc8
#
Packit 130fc8
# The above copyright notice and this permission notice shall be
Packit 130fc8
# included in all copies or substantial portions of the Software.
Packit 130fc8
#
Packit 130fc8
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 130fc8
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 130fc8
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 130fc8
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Packit 130fc8
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
Packit 130fc8
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Packit 130fc8
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit 130fc8
# DEALINGS IN THE SOFTWARE.
Packit 130fc8
Packit 130fc8
from xml.parsers.expat import ParserCreate
Packit 130fc8
from dbus.exceptions import IntrospectionParserException
Packit 130fc8
Packit 130fc8
class _Parser(object):
Packit 130fc8
    __slots__ = ('map', 'in_iface', 'in_method', 'sig')
Packit 130fc8
    def __init__(self):
Packit 130fc8
        self.map = {}
Packit 130fc8
        self.in_iface = ''
Packit 130fc8
        self.in_method = ''
Packit 130fc8
        self.sig = ''
Packit 130fc8
Packit 130fc8
    def parse(self, data):
Packit 130fc8
        parser = ParserCreate('UTF-8', ' ')
Packit 130fc8
        parser.buffer_text = True
Packit 130fc8
        parser.StartElementHandler = self.StartElementHandler
Packit 130fc8
        parser.EndElementHandler = self.EndElementHandler
Packit 130fc8
        parser.Parse(data)
Packit 130fc8
        return self.map
Packit 130fc8
Packit 130fc8
    def StartElementHandler(self, name, attributes):
Packit 130fc8
        if not self.in_iface:
Packit 130fc8
            if (not self.in_method and name == 'interface'):
Packit 130fc8
                self.in_iface = attributes['name']
Packit 130fc8
        else:
Packit 130fc8
            if (not self.in_method and name == 'method'):
Packit 130fc8
                self.in_method = attributes['name']
Packit 130fc8
            elif (self.in_method and name == 'arg'):
Packit 130fc8
                if attributes.get('direction', 'in') == 'in':
Packit 130fc8
                    self.sig += attributes['type']
Packit 130fc8
Packit 130fc8
    def EndElementHandler(self, name):
Packit 130fc8
        if self.in_iface:
Packit 130fc8
            if (not self.in_method and name == 'interface'):
Packit 130fc8
                self.in_iface = ''
Packit 130fc8
            elif (self.in_method and name == 'method'):
Packit 130fc8
                self.map[self.in_iface + '.' + self.in_method] = self.sig
Packit 130fc8
                self.in_method = ''
Packit 130fc8
                self.sig = ''
Packit 130fc8
Packit 130fc8
def process_introspection_data(data):
Packit 130fc8
    """Return a dict mapping ``interface.method`` strings to the
Packit 130fc8
    concatenation of all their 'in' parameters, and mapping
Packit 130fc8
    ``interface.signal`` strings to the concatenation of all their
Packit 130fc8
    parameters.
Packit 130fc8
Packit 130fc8
    Example output::
Packit 130fc8
Packit 130fc8
        {
Packit 130fc8
            'com.example.SignalEmitter.OneString': 's',
Packit 130fc8
            'com.example.MethodImplementor.OneInt32Argument': 'i',
Packit 130fc8
        }
Packit 130fc8
Packit 130fc8
    :Parameters:
Packit 130fc8
        `data` : str
Packit 130fc8
            The introspection XML. Must be an 8-bit string of UTF-8.
Packit 130fc8
    """
Packit 130fc8
    try:
Packit 130fc8
        return _Parser().parse(data)
Packit 130fc8
    except Exception as e:
Packit 130fc8
        raise IntrospectionParserException('%s: %s' % (e.__class__, e))