Blame telepathy-account-widgets/tools/libglibcodegen.py

Packit Service c6b9b0
"""Library code for GLib/D-Bus-related code generation.
Packit Service c6b9b0
Packit Service c6b9b0
The master copy of this library is in the telepathy-glib repository -
Packit Service c6b9b0
please make any changes there.
Packit Service c6b9b0
"""
Packit Service c6b9b0
Packit Service c6b9b0
# Copyright (C) 2006-2008 Collabora Limited
Packit Service c6b9b0
#
Packit Service c6b9b0
# This library is free software; you can redistribute it and/or
Packit Service c6b9b0
# modify it under the terms of the GNU Lesser General Public
Packit Service c6b9b0
# License as published by the Free Software Foundation; either
Packit Service c6b9b0
# version 2.1 of the License, or (at your option) any later version.
Packit Service c6b9b0
#
Packit Service c6b9b0
# This library is distributed in the hope that it will be useful,
Packit Service c6b9b0
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service c6b9b0
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service c6b9b0
# Lesser General Public License for more details.
Packit Service c6b9b0
#
Packit Service c6b9b0
# You should have received a copy of the GNU Lesser General Public
Packit Service c6b9b0
# License along with this library; if not, write to the Free Software
Packit Service c6b9b0
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit Service c6b9b0
Packit Service c6b9b0
Packit Service c6b9b0
from libtpcodegen import NS_TP, \
Packit Service c6b9b0
                         Signature, \
Packit Service c6b9b0
                         cmp_by_name, \
Packit Service c6b9b0
                         escape_as_identifier, \
Packit Service c6b9b0
                         get_by_path, \
Packit Service c6b9b0
                         get_descendant_text, \
Packit Service c6b9b0
                         get_docstring, \
Packit Service c6b9b0
                         xml_escape, \
Packit Service c6b9b0
                         get_deprecated
Packit Service c6b9b0
Packit Service c6b9b0
def dbus_gutils_wincaps_to_uscore(s):
Packit Service c6b9b0
    """Bug-for-bug compatible Python port of _dbus_gutils_wincaps_to_uscore
Packit Service c6b9b0
    which gets sequences of capital letters wrong in the same way.
Packit Service c6b9b0
    (e.g. in Telepathy, SendDTMF -> send_dt_mf)
Packit Service c6b9b0
    """
Packit Service c6b9b0
    ret = ''
Packit Service c6b9b0
    for c in s:
Packit Service c6b9b0
        if c >= 'A' and c <= 'Z':
Packit Service c6b9b0
            length = len(ret)
Packit Service c6b9b0
            if length > 0 and (length < 2 or ret[length-2] != '_'):
Packit Service c6b9b0
                ret += '_'
Packit Service c6b9b0
            ret += c.lower()
Packit Service c6b9b0
        else:
Packit Service c6b9b0
            ret += c
Packit Service c6b9b0
    return ret
Packit Service c6b9b0
Packit Service c6b9b0
Packit Service c6b9b0
def signal_to_marshal_type(signal):
Packit Service c6b9b0
    """
Packit Service c6b9b0
    return a list of strings indicating the marshalling type for this signal.
Packit Service c6b9b0
    """
Packit Service c6b9b0
Packit Service c6b9b0
    mtype=[]
Packit Service c6b9b0
    for i in signal.getElementsByTagName("arg"):
Packit Service c6b9b0
        name =i.getAttribute("name")
Packit Service c6b9b0
        type = i.getAttribute("type")
Packit Service c6b9b0
        mtype.append(type_to_gtype(type)[2])
Packit Service c6b9b0
Packit Service c6b9b0
    return mtype
Packit Service c6b9b0
Packit Service c6b9b0
Packit Service c6b9b0
_glib_marshallers = ['VOID', 'BOOLEAN', 'CHAR', 'UCHAR', 'INT',
Packit Service c6b9b0
        'STRING', 'UINT', 'LONG', 'ULONG', 'ENUM', 'FLAGS', 'FLOAT',
Packit Service c6b9b0
        'DOUBLE', 'STRING', 'PARAM', 'BOXED', 'POINTER', 'OBJECT',
Packit Service c6b9b0
        'UINT_POINTER']
Packit Service c6b9b0
Packit Service c6b9b0
Packit Service c6b9b0
def signal_to_marshal_name(signal, prefix):
Packit Service c6b9b0
Packit Service c6b9b0
    mtype = signal_to_marshal_type(signal)
Packit Service c6b9b0
    if len(mtype):
Packit Service c6b9b0
        name = '_'.join(mtype)
Packit Service c6b9b0
    else:
Packit Service c6b9b0
        name = 'VOID'
Packit Service c6b9b0
Packit Service c6b9b0
    if name in _glib_marshallers:
Packit Service c6b9b0
        return 'g_cclosure_marshal_VOID__' + name
Packit Service c6b9b0
    else:
Packit Service c6b9b0
        return prefix + '_marshal_VOID__' + name
Packit Service c6b9b0
Packit Service c6b9b0
Packit Service c6b9b0
def method_to_glue_marshal_name(method, prefix):
Packit Service c6b9b0
Packit Service c6b9b0
    mtype = []
Packit Service c6b9b0
    for i in method.getElementsByTagName("arg"):
Packit Service c6b9b0
        if i.getAttribute("direction") != "out":
Packit Service c6b9b0
            type = i.getAttribute("type")
Packit Service c6b9b0
            mtype.append(type_to_gtype(type)[2])
Packit Service c6b9b0
Packit Service c6b9b0
    mtype.append('POINTER')
Packit Service c6b9b0
Packit Service c6b9b0
    name = '_'.join(mtype)
Packit Service c6b9b0
Packit Service c6b9b0
    if name in _glib_marshallers:
Packit Service c6b9b0
        return 'g_cclosure_marshal_VOID__' + name
Packit Service c6b9b0
    else:
Packit Service c6b9b0
        return prefix + '_marshal_VOID__' + name
Packit Service c6b9b0
Packit Service c6b9b0
Packit Service c6b9b0
def type_to_gtype(s):
Packit Service c6b9b0
    if s == 'y': #byte
Packit Service c6b9b0
        return ("guchar ", "G_TYPE_UCHAR","UCHAR", False)
Packit Service c6b9b0
    elif s == 'b': #boolean
Packit Service c6b9b0
        return ("gboolean ", "G_TYPE_BOOLEAN","BOOLEAN", False)
Packit Service c6b9b0
    elif s == 'n': #int16
Packit Service c6b9b0
        return ("gint ", "G_TYPE_INT","INT", False)
Packit Service c6b9b0
    elif s == 'q': #uint16
Packit Service c6b9b0
        return ("guint ", "G_TYPE_UINT","UINT", False)
Packit Service c6b9b0
    elif s == 'i': #int32
Packit Service c6b9b0
        return ("gint ", "G_TYPE_INT","INT", False)
Packit Service c6b9b0
    elif s == 'u': #uint32
Packit Service c6b9b0
        return ("guint ", "G_TYPE_UINT","UINT", False)
Packit Service c6b9b0
    elif s == 'x': #int64
Packit Service c6b9b0
        return ("gint64 ", "G_TYPE_INT64","INT64", False)
Packit Service c6b9b0
    elif s == 't': #uint64
Packit Service c6b9b0
        return ("guint64 ", "G_TYPE_UINT64","UINT64", False)
Packit Service c6b9b0
    elif s == 'd': #double
Packit Service c6b9b0
        return ("gdouble ", "G_TYPE_DOUBLE","DOUBLE", False)
Packit Service c6b9b0
    elif s == 's': #string
Packit Service c6b9b0
        return ("gchar *", "G_TYPE_STRING", "STRING", True)
Packit Service c6b9b0
    elif s == 'g': #signature - FIXME
Packit Service c6b9b0
        return ("gchar *", "DBUS_TYPE_G_SIGNATURE", "STRING", True)
Packit Service c6b9b0
    elif s == 'o': #object path
Packit Service c6b9b0
        return ("gchar *", "DBUS_TYPE_G_OBJECT_PATH", "BOXED", True)
Packit Service c6b9b0
    elif s == 'v':  #variant
Packit Service c6b9b0
        return ("GValue *", "G_TYPE_VALUE", "BOXED", True)
Packit Service c6b9b0
    elif s == 'as':  #array of strings
Packit Service c6b9b0
        return ("gchar **", "G_TYPE_STRV", "BOXED", True)
Packit Service c6b9b0
    elif s == 'ay': #byte array
Packit Service c6b9b0
        return ("GArray *",
Packit Service c6b9b0
            "dbus_g_type_get_collection (\"GArray\", G_TYPE_UCHAR)", "BOXED",
Packit Service c6b9b0
            True)
Packit Service c6b9b0
    elif s == 'au': #uint array
Packit Service c6b9b0
        return ("GArray *", "DBUS_TYPE_G_UINT_ARRAY", "BOXED", True)
Packit Service c6b9b0
    elif s == 'ai': #int array
Packit Service c6b9b0
        return ("GArray *", "DBUS_TYPE_G_INT_ARRAY", "BOXED", True)
Packit Service c6b9b0
    elif s == 'ax': #int64 array
Packit Service c6b9b0
        return ("GArray *", "DBUS_TYPE_G_INT64_ARRAY", "BOXED", True)
Packit Service c6b9b0
    elif s == 'at': #uint64 array
Packit Service c6b9b0
        return ("GArray *", "DBUS_TYPE_G_UINT64_ARRAY", "BOXED", True)
Packit Service c6b9b0
    elif s == 'ad': #double array
Packit Service c6b9b0
        return ("GArray *", "DBUS_TYPE_G_DOUBLE_ARRAY", "BOXED", True)
Packit Service c6b9b0
    elif s == 'ab': #boolean array
Packit Service c6b9b0
        return ("GArray *", "DBUS_TYPE_G_BOOLEAN_ARRAY", "BOXED", True)
Packit Service c6b9b0
    elif s == 'ao': #object path array
Packit Service c6b9b0
        return ("GPtrArray *",
Packit Service c6b9b0
                'dbus_g_type_get_collection ("GPtrArray",'
Packit Service c6b9b0
                ' DBUS_TYPE_G_OBJECT_PATH)',
Packit Service c6b9b0
                "BOXED", True)
Packit Service c6b9b0
    elif s == 'a{ss}': #hash table of string to string
Packit Service c6b9b0
        return ("GHashTable *", "DBUS_TYPE_G_STRING_STRING_HASHTABLE", "BOXED", False)
Packit Service c6b9b0
    elif s[:2] == 'a{':  #some arbitrary hash tables
Packit Service c6b9b0
        if s[2] not in ('y', 'b', 'n', 'q', 'i', 'u', 's', 'o', 'g'):
Packit Service c6b9b0
            raise Exception("can't index a hashtable off non-basic type " + s)
Packit Service c6b9b0
        first = type_to_gtype(s[2])
Packit Service c6b9b0
        second = type_to_gtype(s[3:-1])
Packit Service c6b9b0
        return ("GHashTable *", "(dbus_g_type_get_map (\"GHashTable\", " + first[1] + ", " + second[1] + "))", "BOXED", False)
Packit Service c6b9b0
    elif s[:2] in ('a(', 'aa'): # array of structs or arrays, recurse
Packit Service c6b9b0
        gtype = type_to_gtype(s[1:])[1]
Packit Service c6b9b0
        return ("GPtrArray *", "(dbus_g_type_get_collection (\"GPtrArray\", "+gtype+"))", "BOXED", True)
Packit Service c6b9b0
    elif s[:1] == '(': #struct
Packit Service c6b9b0
        gtype = "(dbus_g_type_get_struct (\"GValueArray\", "
Packit Service c6b9b0
        for subsig in Signature(s[1:-1]):
Packit Service c6b9b0
            gtype = gtype + type_to_gtype(subsig)[1] + ", "
Packit Service c6b9b0
        gtype = gtype + "G_TYPE_INVALID))"
Packit Service c6b9b0
        return ("GValueArray *", gtype, "BOXED", True)
Packit Service c6b9b0
Packit Service c6b9b0
    # we just don't know ..
Packit Service c6b9b0
    raise Exception("don't know the GType for " + s)