]> git.0d.be Git - empathy.git/blob - tools/libglibcodegen.py
3.12.2
[empathy.git] / tools / libglibcodegen.py
1 """Library code for GLib/D-Bus-related code generation.
2
3 The master copy of this library is in the telepathy-glib repository -
4 please make any changes there.
5 """
6
7 # Copyright (C) 2006-2008 Collabora Limited
8 #
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13 #
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
23
24 from libtpcodegen import NS_TP, \
25                          Signature, \
26                          cmp_by_name, \
27                          escape_as_identifier, \
28                          get_by_path, \
29                          get_descendant_text, \
30                          get_docstring, \
31                          xml_escape, \
32                          get_deprecated
33
34 def dbus_gutils_wincaps_to_uscore(s):
35     """Bug-for-bug compatible Python port of _dbus_gutils_wincaps_to_uscore
36     which gets sequences of capital letters wrong in the same way.
37     (e.g. in Telepathy, SendDTMF -> send_dt_mf)
38     """
39     ret = ''
40     for c in s:
41         if c >= 'A' and c <= 'Z':
42             length = len(ret)
43             if length > 0 and (length < 2 or ret[length-2] != '_'):
44                 ret += '_'
45             ret += c.lower()
46         else:
47             ret += c
48     return ret
49
50
51 def signal_to_marshal_type(signal):
52     """
53     return a list of strings indicating the marshalling type for this signal.
54     """
55
56     mtype=[]
57     for i in signal.getElementsByTagName("arg"):
58         name =i.getAttribute("name")
59         type = i.getAttribute("type")
60         mtype.append(type_to_gtype(type)[2])
61
62     return mtype
63
64
65 _glib_marshallers = ['VOID', 'BOOLEAN', 'CHAR', 'UCHAR', 'INT',
66         'STRING', 'UINT', 'LONG', 'ULONG', 'ENUM', 'FLAGS', 'FLOAT',
67         'DOUBLE', 'STRING', 'PARAM', 'BOXED', 'POINTER', 'OBJECT',
68         'UINT_POINTER']
69
70
71 def signal_to_marshal_name(signal, prefix):
72
73     mtype = signal_to_marshal_type(signal)
74     if len(mtype):
75         name = '_'.join(mtype)
76     else:
77         name = 'VOID'
78
79     if name in _glib_marshallers:
80         return 'g_cclosure_marshal_VOID__' + name
81     else:
82         return prefix + '_marshal_VOID__' + name
83
84
85 def method_to_glue_marshal_name(method, prefix):
86
87     mtype = []
88     for i in method.getElementsByTagName("arg"):
89         if i.getAttribute("direction") != "out":
90             type = i.getAttribute("type")
91             mtype.append(type_to_gtype(type)[2])
92
93     mtype.append('POINTER')
94
95     name = '_'.join(mtype)
96
97     if name in _glib_marshallers:
98         return 'g_cclosure_marshal_VOID__' + name
99     else:
100         return prefix + '_marshal_VOID__' + name
101
102
103 def type_to_gtype(s):
104     if s == 'y': #byte
105         return ("guchar ", "G_TYPE_UCHAR","UCHAR", False)
106     elif s == 'b': #boolean
107         return ("gboolean ", "G_TYPE_BOOLEAN","BOOLEAN", False)
108     elif s == 'n': #int16
109         return ("gint ", "G_TYPE_INT","INT", False)
110     elif s == 'q': #uint16
111         return ("guint ", "G_TYPE_UINT","UINT", False)
112     elif s == 'i': #int32
113         return ("gint ", "G_TYPE_INT","INT", False)
114     elif s == 'u': #uint32
115         return ("guint ", "G_TYPE_UINT","UINT", False)
116     elif s == 'x': #int64
117         return ("gint64 ", "G_TYPE_INT64","INT64", False)
118     elif s == 't': #uint64
119         return ("guint64 ", "G_TYPE_UINT64","UINT64", False)
120     elif s == 'd': #double
121         return ("gdouble ", "G_TYPE_DOUBLE","DOUBLE", False)
122     elif s == 's': #string
123         return ("gchar *", "G_TYPE_STRING", "STRING", True)
124     elif s == 'g': #signature - FIXME
125         return ("gchar *", "DBUS_TYPE_G_SIGNATURE", "STRING", True)
126     elif s == 'o': #object path
127         return ("gchar *", "DBUS_TYPE_G_OBJECT_PATH", "BOXED", True)
128     elif s == 'v':  #variant
129         return ("GValue *", "G_TYPE_VALUE", "BOXED", True)
130     elif s == 'as':  #array of strings
131         return ("gchar **", "G_TYPE_STRV", "BOXED", True)
132     elif s == 'ay': #byte array
133         return ("GArray *",
134             "dbus_g_type_get_collection (\"GArray\", G_TYPE_UCHAR)", "BOXED",
135             True)
136     elif s == 'au': #uint array
137         return ("GArray *", "DBUS_TYPE_G_UINT_ARRAY", "BOXED", True)
138     elif s == 'ai': #int array
139         return ("GArray *", "DBUS_TYPE_G_INT_ARRAY", "BOXED", True)
140     elif s == 'ax': #int64 array
141         return ("GArray *", "DBUS_TYPE_G_INT64_ARRAY", "BOXED", True)
142     elif s == 'at': #uint64 array
143         return ("GArray *", "DBUS_TYPE_G_UINT64_ARRAY", "BOXED", True)
144     elif s == 'ad': #double array
145         return ("GArray *", "DBUS_TYPE_G_DOUBLE_ARRAY", "BOXED", True)
146     elif s == 'ab': #boolean array
147         return ("GArray *", "DBUS_TYPE_G_BOOLEAN_ARRAY", "BOXED", True)
148     elif s == 'ao': #object path array
149         return ("GPtrArray *",
150                 'dbus_g_type_get_collection ("GPtrArray",'
151                 ' DBUS_TYPE_G_OBJECT_PATH)',
152                 "BOXED", True)
153     elif s == 'a{ss}': #hash table of string to string
154         return ("GHashTable *", "DBUS_TYPE_G_STRING_STRING_HASHTABLE", "BOXED", False)
155     elif s[:2] == 'a{':  #some arbitrary hash tables
156         if s[2] not in ('y', 'b', 'n', 'q', 'i', 'u', 's', 'o', 'g'):
157             raise Exception("can't index a hashtable off non-basic type " + s)
158         first = type_to_gtype(s[2])
159         second = type_to_gtype(s[3:-1])
160         return ("GHashTable *", "(dbus_g_type_get_map (\"GHashTable\", " + first[1] + ", " + second[1] + "))", "BOXED", False)
161     elif s[:2] in ('a(', 'aa'): # array of structs or arrays, recurse
162         gtype = type_to_gtype(s[1:])[1]
163         return ("GPtrArray *", "(dbus_g_type_get_collection (\"GPtrArray\", "+gtype+"))", "BOXED", True)
164     elif s[:1] == '(': #struct
165         gtype = "(dbus_g_type_get_struct (\"GValueArray\", "
166         for subsig in Signature(s[1:-1]):
167             gtype = gtype + type_to_gtype(subsig)[1] + ", "
168         gtype = gtype + "G_TYPE_INVALID))"
169         return ("GValueArray *", gtype, "BOXED", True)
170
171     # we just don't know ..
172     raise Exception("don't know the GType for " + s)