e8058322725ba050014777ee2484f7e833ab1e3aLukas Slebodnik#!/usr/bin/env python
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Authors:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Stef Walter <stefw@redhat.com>
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Copyright (C) 2014 Red Hat
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# This program is free software; you can redistribute it and/or modify
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# it under the terms of the GNU General Public License as published by
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# the Free Software Foundation; either version 3 of the License, or
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# (at your option) any later version.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# This program is distributed in the hope that it will be useful,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# but WITHOUT ANY WARRANTY; without even the implied warranty of
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# GNU General Public License for more details.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# You should have received a copy of the GNU General Public License
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# along with this program. If not, see <http://www.gnu.org/licenses/>.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Some parser code from GLib
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Copyright (C) 2008-2011 Red Hat, Inc.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# This library is free software; you can redistribute it and/or
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# modify it under the terms of the GNU Lesser General Public
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# License as published by the Free Software Foundation; either
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# version 2 of the License, or (at your option) any later version.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# This library is distributed in the hope that it will be useful,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# but WITHOUT ANY WARRANTY; without even the implied warranty of
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Lesser General Public License for more details.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# You should have received a copy of the GNU Lesser General
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Public License along with this library; if not, write to the
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Boston, MA 02111-1307, USA.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Portions by: David Zeuthen <davidz@redhat.com>
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# DBus interfaces are defined here:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# The introspection data format has become the standard way to represent a
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# DBus interface. For many examples see /usr/share/dbus-1/interfaces/ on a
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# typical linux machine.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# A word about annotations. These are extra flags or values that can be
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# assigned to anything. So far, the codegen supports this annotation:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# org.freedesktop.DBus.GLib.CSymbol
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# - An annotation specified in the specification that tells us what C symbol
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# to generate for a given interface or method. By default the codegen will
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# build up a symbol name from the DBus name.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnikfrom __future__ import print_function
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterimport optparse
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterimport os
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterimport re
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterimport sys
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterimport xml.parsers.expat
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnikif sys.version_info[0] > 2:
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik import io as StringIO
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnikelse:
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik import StringIO
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# -----------------------------------------------------------------------------
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Objects
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterclass DBusXmlException(Exception):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter line = 0
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter file = None
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter # Lets us print problems like a compiler would
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def __str__(self):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter message = Exception.__str__(self)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if self.file and self.line:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return "%s:%d: %s" % (self.file, self.line, message)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif self.file:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return "%s: %s" % (self.file, message)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return message
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b07852825eeb63a78e1b3863e42b3f328430da18amitkumaclass Base(object):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def __init__(self, name):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if not name:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter raise DBusXmlException('No name on element')
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.name = name
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.annotations = { }
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter def validate(self):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter pass
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def c_name(self):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return self.annotations.get("org.freedesktop.DBus.GLib.CSymbol", self.name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter# The basic types that we support marshalling right now. These
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter# are the ones we can pass as basic arguments to libdbus directly.
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter# If the dbus and sssd types are identical we pass things directly.
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter# otherwise some copying is necessary.
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef WalterBASIC_TYPES = {
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'y': ( "DBUS_TYPE_BYTE", "uint8_t", "uint8_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'b': ( "DBUS_TYPE_BOOLEAN", "dbus_bool_t", "bool" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'n': ( "DBUS_TYPE_INT16", "int16_t", "int16_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'q': ( "DBUS_TYPE_UINT16", "uint16_t", "uint16_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'i': ( "DBUS_TYPE_INT32", "int32_t", "int32_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'u': ( "DBUS_TYPE_UINT32", "uint32_t", "uint32_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'x': ( "DBUS_TYPE_INT64", "int64_t", "int64_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 't': ( "DBUS_TYPE_UINT64", "uint64_t", "uint64_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'd': ( "DBUS_TYPE_DOUBLE", "double", "double" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 's': ( "DBUS_TYPE_STRING", "const char *", "const char *" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'o': ( "DBUS_TYPE_OBJECT_PATH", "const char *", "const char *" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter}
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walterclass Typed(Base):
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walter def __init__(self, name, type):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter Base.__init__(self, name)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter self.type = type
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter self.is_basic = False
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter self.is_array = False
ac744223411099a862a747e7168a30255c003bf7Pavel Březina self.is_dictionary = False
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter self.dbus_constant = None
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter self.dbus_type = None
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter self.sssd_type = None
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if type[0] == 'a':
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter type = type[1:]
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter self.is_array = True
ac744223411099a862a747e7168a30255c003bf7Pavel Březina if "{" in type:
ac744223411099a862a747e7168a30255c003bf7Pavel Březina self.is_dictionary = True
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if type in BASIC_TYPES:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter (self.dbus_constant, self.dbus_type, self.sssd_type) = BASIC_TYPES[type]
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter # If types are not identical, we can't do array (yet)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if self.is_array:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter self.is_basic = (self.dbus_type == self.sssd_type)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter else:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter self.is_basic = True
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walterclass Arg(Typed):
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walter def __init__(self, method, name, type):
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walter Typed.__init__(self, name, type)
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walter self.method = method
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterclass Method(Base):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def __init__(self, iface, name):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter Base.__init__(self, name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.iface = iface
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.in_args = []
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.out_args = []
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter def validate(self):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if not self.only_basic_args() and not self.use_raw_handler():
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter raise DBusXmlException("Method has complex arguments and requires " +
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter "the 'org.freedesktop.sssd.RawHandler' annotation")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def fq_c_name(self):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return "%s_%s" % (self.iface.c_name(), self.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter def use_raw_handler(self):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter anno = 'org.freedesktop.sssd.RawHandler'
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return self.annotations.get(anno, self.iface.annotations.get(anno)) == 'true'
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter def in_signature(self):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return "".join([arg.type for arg in self.in_args])
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter def only_basic_args(self):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for arg in self.in_args + self.out_args:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if not arg.is_basic:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return False
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return True
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterclass Signal(Base):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def __init__(self, iface, name):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter Base.__init__(self, name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.iface = iface
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.args = []
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def fq_c_name(self):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return "%s_%s" % (self.iface.c_name(), self.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walterclass Property(Typed):
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walter def __init__(self, iface, name, type, access):
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walter Typed.__init__(self, name, type)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.iface = iface
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.readable = False
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.writable = False
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if access == 'readwrite':
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.readable = True
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.writable = True
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif access == 'read':
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.readable = True
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif access == 'write':
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.writable = True
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter raise DBusXmlException('Invalid access type %s'%self.access)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def fq_c_name(self):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return "%s_%s" % (self.iface.c_name(), self.c_name())
ac744223411099a862a747e7168a30255c003bf7Pavel Březina def get_invoker_name(self):
ac744223411099a862a747e7168a30255c003bf7Pavel Březina type = self.type
ac744223411099a862a747e7168a30255c003bf7Pavel Březina type = type.replace("{", "DO")
ac744223411099a862a747e7168a30255c003bf7Pavel Březina type = type.replace("}", "DE")
ac744223411099a862a747e7168a30255c003bf7Pavel Březina return type
ac744223411099a862a747e7168a30255c003bf7Pavel Březina def get_invoker_signature(self, name):
ac744223411099a862a747e7168a30255c003bf7Pavel Březina sig = "void (*%s)(struct sbus_request *, void *data, " % (name)
ac744223411099a862a747e7168a30255c003bf7Pavel Březina if self.is_dictionary:
ac744223411099a862a747e7168a30255c003bf7Pavel Březina sig += "hash_table_t **"
ac744223411099a862a747e7168a30255c003bf7Pavel Březina elif self.is_array:
ac744223411099a862a747e7168a30255c003bf7Pavel Březina sig += "%s**, int *" % (self.sssd_type)
ac744223411099a862a747e7168a30255c003bf7Pavel Březina else:
ac744223411099a862a747e7168a30255c003bf7Pavel Březina sig += "%s*" % (self.sssd_type)
ac744223411099a862a747e7168a30255c003bf7Pavel Březina sig += ")"
ac744223411099a862a747e7168a30255c003bf7Pavel Březina return sig
ac744223411099a862a747e7168a30255c003bf7Pavel Březina
ac744223411099a862a747e7168a30255c003bf7Pavel Březina
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek def getter_name(self):
4e5d19f659d8c545c4ed3c307c95cfe4f2ca33cbPavel Březina return "get_%s" % self.c_name()
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek def getter_invoker_name(self):
ac744223411099a862a747e7168a30255c003bf7Pavel Březina return "sbus_invoke_get_%s" % self.get_invoker_name()
ac744223411099a862a747e7168a30255c003bf7Pavel Březina def getter_signature(self):
ac744223411099a862a747e7168a30255c003bf7Pavel Březina return self.get_invoker_signature(self.getter_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterclass Interface(Base):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def __init__(self, name):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter Base.__init__(self, name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.methods = []
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.signals = []
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.properties = []
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def c_name(self):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return self.annotations.get("org.freedesktop.DBus.GLib.CSymbol",
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.name.replace(".", "_"))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# -----------------------------------------------------------------------------
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Code Generation
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walterdef out(format, *args, **kwargs):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter str = format % args
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter sys.stdout.write(str)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter # NOTE: Would like to use the following syntax for this function
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter # but need to wait until python3 until it is supported:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter # def out(format, *args, new_line=True)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if kwargs.pop("new_line", True):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter sys.stdout.write("\n")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter assert not kwargs, "unknown keyword argument(s): %s" % str(kwargs)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walterdef method_arg_types(args, with_names=False):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter str = ""
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for arg in args:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter str += ", "
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter str += arg.sssd_type
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if with_names:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if str[-1] != '*':
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter str += " "
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter str += "arg_"
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter str += arg.c_name()
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if arg.is_array:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter str += "[], int"
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if with_names:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter str += " len_"
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter str += arg.c_name()
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return str
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walterdef method_function_pointer(meth, name, with_names=False):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if meth.use_raw_handler():
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return "sbus_msg_handler_fn " + name
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter else:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return "int (*%s)(struct sbus_request *%s, void *%s%s)" % \
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter (name, with_names and "req" or "",
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter with_names and "data" or "",
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter method_arg_types(meth.in_args, with_names))
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozekdef property_handlers(prop):
ac744223411099a862a747e7168a30255c003bf7Pavel Březina return prop.getter_signature()
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walterdef forward_method_invoker(signature, args):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("/* invokes a handler with a '%s' DBus signature */", signature)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("static int invoke_%s_method(struct sbus_request *dbus_req, void *function_ptr);", signature)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walterdef source_method_invoker(signature, args):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("/* invokes a handler with a '%s' DBus signature */", signature)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("static int invoke_%s_method(struct sbus_request *dbus_req, void *function_ptr)", signature)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("{")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for i in range(0, len(args)):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter arg = args[i]
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if arg.is_array:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" %s *arg_%d;", arg.dbus_type, i)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" int len_%d;", i)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter else:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" %s arg_%d;", arg.dbus_type, i)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" int (*handler)(struct sbus_request *, void *%s) = function_ptr;", method_arg_types(args))
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" if (!sbus_request_parse_or_finish(dbus_req,")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for i in range(0, len(args)):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter arg = args[i]
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if arg.is_array:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" DBUS_TYPE_ARRAY, %s, &arg_%d, &len_%d,",
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter arg.dbus_constant, i, i)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter else:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" %s, &arg_%d,", arg.dbus_constant, i)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" DBUS_TYPE_INVALID)) {")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" return EOK; /* request handled */")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" }")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
66277b21179d95f6e96abed01a20ccbccf27ce99Pavel Březina out(" return (handler)(dbus_req, dbus_req->intf->handler_data", new_line=False)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for i in range(0, len(args)):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter arg = args[i]
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(",\n arg_%d", i, new_line=False)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if arg.is_array:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(",\n len_%d", i, new_line=False)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(");")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("}")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozekdef source_prop_types(prop, type_prefix=False):
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek prefix = "%s_" % prop.type if type_prefix else ""
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek if prop.is_array:
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek out(" %s *%sprop_val;", prop.sssd_type, prefix)
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek out(" int %sprop_len;", prefix)
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek out(" %s *%sout_val;", prop.dbus_type, prefix)
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek else:
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek out(" %s %sprop_val;", prop.sssd_type, prefix)
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek out(" %s %sout_val;", prop.dbus_type, prefix)
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozekdef source_prop_handler(prop, type_prefix=False):
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek prefix = "%s_" % prop.type if type_prefix else ""
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek out(" %s", prop.getter_signature("%shandler" % prefix), new_line=False)
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek out(";")
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walterdef forward_method_invokers(ifaces):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter invokers = { }
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for iface in ifaces:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for meth in iface.methods:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if meth.use_raw_handler() or not meth.in_args:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter continue
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter signature = meth.in_signature()
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if signature in invokers:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter continue
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walter forward_method_invoker(signature, meth.in_args)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter invokers[signature] = meth
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return invokers
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walterdef source_method_invokers(invokers):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for (signature, meth) in invokers.items():
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter source_method_invoker(signature, meth.in_args)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walterdef source_finisher(meth):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("int %s_finish(struct sbus_request *req%s)",
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter meth.fq_c_name(), method_arg_types(meth.out_args, with_names=True))
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("{")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for arg in meth.out_args:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if arg.dbus_type != arg.sssd_type:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" %s cast_%s = arg_%s;", arg.dbus_type, arg.c_name(), arg.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" return sbus_request_return_and_finish(req,")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for arg in meth.out_args:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" ", new_line=False)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if arg.is_array:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("DBUS_TYPE_ARRAY, %s, &arg_%s, len_%s,",
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter arg.dbus_constant, arg.c_name(), arg.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter elif arg.dbus_type != arg.sssd_type:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("%s, &cast_%s,", arg.dbus_constant, arg.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter else:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("%s, &arg_%s,", arg.dbus_constant, arg.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" DBUS_TYPE_INVALID);")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("}")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walterdef header_reply(meth):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for arg in meth.out_args:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if arg.is_array:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" %s *%s", arg.dbus_type, arg.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" int %s__len", arg.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter else:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" %s %s;", arg.dbus_type, arg.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter types = [arg.sssd_type for arg in meth.in_args]
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterdef source_args(parent, args, suffix):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* arguments for %s.%s */", parent.iface.name, parent.name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("const struct sbus_arg_meta %s%s[] = {", parent.fq_c_name(), suffix)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter for arg in args:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" { \"%s\", \"%s\" },", arg.name, arg.type)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" { NULL, }")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("};")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterdef source_methods(iface, methods):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter for meth in methods:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if meth.in_args:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter source_args(meth, meth.in_args, "__in")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if meth.out_args:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter source_args(meth, meth.out_args, "__out")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if not meth.use_raw_handler():
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter source_finisher(meth)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* methods for %s */", iface.name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("const struct sbus_method_meta %s__methods[] = {", iface.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter for meth in methods:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" {")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" \"%s\", /* name */", meth.name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if meth.in_args:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" %s__in,", meth.fq_c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" NULL, /* no in_args */")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if meth.out_args:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" %s__out,", meth.fq_c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" NULL, /* no out_args */")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" offsetof(struct %s, %s),", iface.c_name(), meth.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if meth.use_raw_handler() or not meth.in_args:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" NULL, /* no invoker */")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter else:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" invoke_%s_method,", meth.in_signature())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" },")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" { NULL, }")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("};")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterdef source_signals(iface, signals):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter for sig in iface.signals:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if sig.args:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter source_args(sig, sig.args, "__args")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* signals for %s */", iface.name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("const struct sbus_signal_meta %s__signals[] = {", iface.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter for sig in signals:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" {")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" \"%s\", /* name */", sig.name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if sig.args:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" %s__args", sig.fq_c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" NULL, /* no args */")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" },")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" { NULL, }")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("};")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterdef source_properties(iface, properties):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* property info for %s */", iface.name)
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("const struct sbus_property_meta %s__properties[] = {", iface.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter for prop in properties:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" {")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" \"%s\", /* name */", prop.name)
1319e71fd1680ca4864afe0b1aca2b8c8e4a1ee4Stef Walter out(" \"%s\", /* type */", prop.type)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if prop.readable and prop.writable:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" SBUS_PROPERTY_READABLE | SBUS_PROPERTY_WRITABLE,")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif prop.readable:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" SBUS_PROPERTY_READABLE,")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif prop.writable:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" SBUS_PROPERTY_WRITABLE,")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter assert False, "should not be reached"
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek if prop.readable:
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek out(" offsetof(struct %s, %s),", iface.c_name(), prop.getter_name())
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek out(" %s,", prop.getter_invoker_name())
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek else:
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek out(" 0, /* not readable */")
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek out(" NULL, /* no invoker */")
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek out(" 0, /* not writable */")
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek out(" NULL, /* no invoker */")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" },")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" { NULL, }")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("};")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterdef header_interface(iface):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* interface info for %s */", iface.name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("extern const struct sbus_interface_meta %s_meta;", iface.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterdef source_interface(iface):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* interface info for %s */", iface.name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("const struct sbus_interface_meta %s_meta = {", iface.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" \"%s\", /* name */", iface.name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if iface.methods:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" %s__methods,", iface.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" NULL, /* no methods */")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if iface.signals:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" %s__signals,", iface.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" NULL, /* no signals */")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if iface.properties:
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek out(" %s__properties,", iface.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek out(" NULL, /* no properties */")
df4e1db5d41c903ae57fd880acc76a0ad84aa7b2Pavel Březina out(" sbus_invoke_get_all, /* GetAll invoker */")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("};")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walterdef generate_source(ifaces, filename, include_header=None):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter basename = os.path.basename(filename)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* The following definitions are auto-generated from %s */", basename)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
054900ab42a8b865a2257f94c66484e0a022a90cLukas Slebodnik out("#include <stddef.h>")
054900ab42a8b865a2257f94c66484e0a022a90cLukas Slebodnik out("")
054900ab42a8b865a2257f94c66484e0a022a90cLukas Slebodnik out("#include \"dbus/dbus-protocol.h\"")
054900ab42a8b865a2257f94c66484e0a022a90cLukas Slebodnik out("#include \"util/util_errors.h\"")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("#include \"sbus/sssd_dbus.h\"")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("#include \"sbus/sssd_dbus_meta.h\"")
df4e1db5d41c903ae57fd880acc76a0ad84aa7b2Pavel Březina out("#include \"sbus/sssd_dbus_invokers.h\"")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter if include_header:
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out("#include \"%s\"", os.path.basename(include_header))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek meth_invokers = forward_method_invokers(ifaces)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter for iface in ifaces:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter # The methods
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if iface.methods:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter source_methods(iface, iface.methods)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter # The signals array
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if iface.signals:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter source_signals(iface, iface.signals)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter # The properties array
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if iface.properties:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter source_properties(iface, iface.properties)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter # The sbus_interface structure
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter source_interface(iface)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek source_method_invokers(meth_invokers)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walterdef header_finisher(iface, meth):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if meth.use_raw_handler():
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("/* finish function for %s */", meth.name)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("int %s_finish(struct sbus_request *req%s);",
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter meth.fq_c_name(), method_arg_types(meth.out_args, with_names=True))
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walterdef header_vtable(iface, methods):
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out("")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out("/* vtable for %s */", iface.name)
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out("struct %s {", iface.c_name())
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" struct sbus_vtable vtable; /* derive from sbus_vtable */")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter # All methods
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter for meth in iface.methods:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" %s;", method_function_pointer(meth, meth.c_name(), with_names=True))
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek for prop in iface.properties:
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek out(" %s;", property_handlers(prop))
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out("};")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter
c2cc119de8eac712c040b3993f41c967ff2278deStef Walterdef header_constants(iface):
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("")
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("/* constants for %s */", iface.name)
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("#define %s \"%s\"", iface.c_name().upper(), iface.name)
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter for meth in iface.methods:
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("#define %s \"%s\"", meth.fq_c_name().upper(), meth.name)
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter for sig in iface.signals:
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("#define %s \"%s\"", sig.fq_c_name().upper(), sig.name)
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter for prop in iface.properties:
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("#define %s \"%s\"", prop.fq_c_name().upper(), prop.name)
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterdef generate_header(ifaces, filename):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter basename = os.path.basename(filename)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter guard = "__%s__" % re.sub(r'([^_A-Z0-9])', "_", basename.upper())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* The following declarations are auto-generated from %s */", basename)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("#ifndef %s", guard)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("#define %s", guard)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("#include \"sbus/sssd_dbus.h\"")
054900ab42a8b865a2257f94c66484e0a022a90cLukas Slebodnik out("#include \"sbus/sssd_dbus_meta.h\"")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("")
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("/* ------------------------------------------------------------------------")
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out(" * DBus Constants")
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out(" *")
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out(" * Various constants of interface and method names mostly for use by clients")
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out(" */")
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter for iface in ifaces:
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter header_constants(iface)
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out("")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out("/* ------------------------------------------------------------------------")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * DBus handlers")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" *")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" * These structures are filled in by implementors of the different")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" * dbus interfaces to handle method calls.")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" *")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" * Handler functions of type sbus_msg_handler_fn accept raw messages,")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * other handlers are typed appropriately. If a handler that is")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" * set to NULL is invoked it will result in a")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" * org.freedesktop.DBus.Error.NotSupported error for the caller.")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" *")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * Handlers have a matching xxx_finish() function (unless the method has")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * accepts raw messages). These finish functions the")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * sbus_request_return_and_finish() with the appropriate arguments to")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * construct a valid reply. Once a finish function has been called, the")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * @dbus_req it was called with is freed and no longer valid.")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" */")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter for iface in ifaces:
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek if iface.methods or iface.properties:
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter header_vtable(iface, iface.methods)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter for meth in iface.methods:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter header_finisher(iface, meth)
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* ------------------------------------------------------------------------")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" * DBus Interface Metadata")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" *")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" * These structure definitions are filled in with the information about")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" * the interfaces, methods, properties and so on.")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" *")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" * The actual definitions are found in the accompanying C file next")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" * to this header.")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" */")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter for iface in ifaces:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter header_interface(iface)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("#endif /* %s */", guard)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# -----------------------------------------------------------------------------
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# XML Interface Parsing
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef WalterSTATE_TOP = 'top'
b699c4d7f85a5404be1d1ee9450331aea869b886Stef WalterSTATE_NODE = 'node'
b699c4d7f85a5404be1d1ee9450331aea869b886Stef WalterSTATE_INTERFACE = 'interface'
b699c4d7f85a5404be1d1ee9450331aea869b886Stef WalterSTATE_METHOD = 'method'
b699c4d7f85a5404be1d1ee9450331aea869b886Stef WalterSTATE_SIGNAL = 'signal'
b699c4d7f85a5404be1d1ee9450331aea869b886Stef WalterSTATE_PROPERTY = 'property'
b699c4d7f85a5404be1d1ee9450331aea869b886Stef WalterSTATE_ARG = 'arg'
b699c4d7f85a5404be1d1ee9450331aea869b886Stef WalterSTATE_ANNOTATION = 'annotation'
b699c4d7f85a5404be1d1ee9450331aea869b886Stef WalterSTATE_IGNORED = 'ignored'
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterdef expect_attr(attrs, name):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if name not in attrs:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter raise DBusXmlException("Missing attribute '%s'" % name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if attrs[name] == "":
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter raise DBusXmlException("Empty attribute '%s'" % name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return attrs[name]
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b07852825eeb63a78e1b3863e42b3f328430da18amitkumaclass DBusXMLParser(object):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def __init__(self, filename):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser = xml.parsers.expat.ParserCreate()
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.CommentHandler = self.handle_comment
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.CharacterDataHandler = self.handle_char_data
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.StartElementHandler = self.handle_start_element
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.EndElementHandler = self.handle_end_element
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.parsed_interfaces = []
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object = None
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_TOP
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state_stack = []
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object = None
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object_stack = []
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.arg_count = 0
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter try:
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik with open(filename, "rb") as f:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.ParseFile(f)
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik except DBusXmlException as ex:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter ex.line = parser.CurrentLineNumber
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter ex.file = filename
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter raise
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik except xml.parsers.expat.ExpatError as ex:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter exc = DBusXmlException(str(ex))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter exc.line = ex.lineno
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter exc.file = filename
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter raise exc
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def handle_comment(self, data):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter pass
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def handle_char_data(self, data):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter pass
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def handle_start_element(self, name, attrs):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter old_state = self.state
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter old_cur_object = self.cur_object
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if self.state == STATE_IGNORED:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_IGNORED
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif self.cur_object and name == STATE_ANNOTATION:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter val = attrs.get('value', '')
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object.annotations[expect_attr(attrs, 'name')] = val
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_IGNORED
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif self.state == STATE_TOP:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if name == STATE_NODE:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_NODE
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_IGNORED
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif self.state == STATE_NODE:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if name == STATE_INTERFACE:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_INTERFACE
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter iface = Interface(expect_attr(attrs, 'name'))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object = iface
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.parsed_interfaces.append(iface)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_IGNORED
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif self.state == STATE_INTERFACE:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if name == STATE_METHOD:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_METHOD
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter method = Method(self.cur_object, expect_attr(attrs, 'name'))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object.methods.append(method)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object = method
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.arg_count = 0
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif name == STATE_SIGNAL:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_SIGNAL
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter signal = Signal(self.cur_object, expect_attr(attrs, 'name'))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object.signals.append(signal)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object = signal
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.arg_count = 0
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif name == STATE_PROPERTY:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_PROPERTY
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter prop = Property(self.cur_object,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter expect_attr(attrs, 'name'),
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter expect_attr(attrs, 'type'),
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter expect_attr(attrs, 'access'))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object.properties.append(prop)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object = prop
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_IGNORED
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif self.state == STATE_METHOD:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if name == STATE_ARG:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_ARG
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter arg = Arg(self.cur_object,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter expect_attr(attrs, 'name'),
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter expect_attr(attrs, 'type'))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter direction = attrs.get('direction', 'in')
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if direction == 'in':
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object.in_args.append(arg)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif direction == 'out':
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object.out_args.append(arg)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter raise DBusXmlException('Invalid direction "%s"' % direction)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object = arg
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_IGNORED
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif self.state == STATE_SIGNAL:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if name == STATE_ARG:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_ARG
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter arg = Arg(self.cur_object,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter expect_attr(attrs, 'name'),
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter expect_attr(attrs, 'type'))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object.args.append(arg)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object = arg
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_IGNORED
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif self.state == STATE_PROPERTY:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_IGNORED
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif self.state == STATE_ARG:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = STATE_IGNORED
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter assert False, 'Unhandled state "%s" while entering element with name "%s"' % (self.state, name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state_stack.append(old_state)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object_stack.append(old_cur_object)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter def handle_end_element(self, name):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if self.cur_object:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter self.cur_object.validate()
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.state = self.state_stack.pop()
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object = self.cur_object_stack.pop()
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterdef parse_options():
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser = optparse.OptionParser("usage: %prog [options] introspect.xml ...")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.set_description("sbus_codegen generates sbus interface structures \
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter from standard XML Introspect data.")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.add_option("--mode",
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter dest="mode", default="header",
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter help="'header' or 'source' (default: header)",
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter metavar="MODE")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.add_option("--output",
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter dest="output", default=None,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter help="Set output file name (default: stdout)",
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter metavar="FILE")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter parser.add_option("--include",
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter dest="include", default=None,
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter help="name of a header to #include",
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter metavar="HEADER")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter (options, args) = parser.parse_args()
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if not args:
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik print("sbus_codegen: no input file specified", file=sys.stderr)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter sys.exit(2)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if options.mode not in ["header", "source"]:
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik print("sbus_codegen: specify --mode=header or --mode=source", file=sys.stderr)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return options, args
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterdef main():
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter options, args = parse_options()
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if options.output:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter sys.stdout = buf = StringIO.StringIO()
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter for filename in args:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser = DBusXMLParser(filename)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if options.mode == "header":
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter generate_header(parser.parsed_interfaces, filename)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif options.mode == "source":
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter generate_source(parser.parsed_interfaces, filename, options.include)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter else:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter assert False, "should not be reached"
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter # Write output at end to be nice to 'make'
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if options.output:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter output = open(options.output, "w")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter output.write(buf.getvalue())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter output.close()
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterif __name__ == "__main__":
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter try:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter main()
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik except DBusXmlException as ex:
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik print(str(ex), file=sys.stderr)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter sys.exit(1)