comparison libpurple/dbus-analyze-functions.py @ 15374:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15373:f79e0f4df793 15374:5fe8042783c1
1 import re
2 import string
3 import sys
4
5
6 # types translated into "int"
7 simpletypes = ["int", "gint", "guint", "gboolean"]
8
9 # List "excluded" contains functions that shouldn't be exported via
10 # DBus. If you remove a function from this list, please make sure
11 # that it does not break "make" with the configure option
12 # "--enable-dbus" turned on.
13
14 excluded = [\
15 # I don't remember why this function is excluded; something to do
16 # with the fact that it takes a (const) GList as a parameter.
17 "gaim_presence_add_list",
18
19 # These functions are excluded because they involve value of the
20 # type GaimConvPlacementFunc, which is a pointer to a function and
21 # (currently?) can't be translated into a DBus type. Normally,
22 # functions with untranslatable types are skipped, but this script
23 # assumes that all non-pointer type names beginning with "Gaim"
24 # are enums, which is not true in this case.
25 "gaim_conv_placement_add_fnc",
26 "gaim_conv_placement_get_fnc",
27 "gaim_conv_placement_get_current_func",
28 "gaim_conv_placement_set_current_func",
29
30 # This is excluded because this script treats GaimLogReadFlags*
31 # as pointer to a struct, instead of a pointer to an enum. This
32 # causes a compilation error. Someone should fix this script.
33 "gaim_log_read",
34 ]
35
36 # This is a list of functions that return a GList* whose elements are
37 # string, not pointers to objects. Don't put any functions here, it
38 # won't work.
39 stringlists = []
40
41 pointer = "#pointer#"
42 myexception = "My Exception"
43
44 def ctopascal(name):
45 newname = ""
46 for word in name.split("_"):
47 newname += word.capitalize()
48 return newname
49
50 class Parameter:
51 def __init__(self, type, name):
52 self.name = name
53 self.type = type
54
55 def fromtokens(tokens, parameternumber = -1):
56 if len(tokens) == 0:
57 raise myexception
58 if (len(tokens) == 1) or (tokens[-1] == pointer):
59 if parameternumber >= 0:
60 return Parameter(tokens, "param%i" % parameternumber)
61 else:
62 raise myexception
63 else:
64 return Parameter(tokens[:-1], tokens[-1])
65
66 fromtokens = staticmethod(fromtokens)
67
68 class Binding:
69 def __init__(self, functiontext, paramtexts):
70 self.function = Parameter.fromtokens(functiontext.split())
71
72 if self.function.name in excluded:
73 raise myexception
74
75 self.params = []
76 for i in range(len(paramtexts)):
77 self.params.append(Parameter.fromtokens(paramtexts[i].split(), i))
78
79 self.call = "%s(%s)" % (self.function.name,
80 ", ".join([param.name for param in self.params]))
81
82
83 def process(self):
84 for param in self.params:
85 self.processinput(param.type, param.name)
86
87 self.processoutput(self.function.type, "RESULT")
88 self.flush()
89
90
91 def processinput(self, type, name):
92 const = False
93 if type[0] == "const":
94 type = type[1:]
95 const = True
96
97 if len(type) == 1:
98 # simple types (int, gboolean, etc.) and enums
99 if (type[0] in simpletypes) or (type[0].startswith("Gaim")):
100 return self.inputsimple(type, name)
101
102 # pointers ...
103 if (len(type) == 2) and (type[1] == pointer):
104 # strings
105 if type[0] == "char":
106 if const:
107 return self.inputstring(type, name)
108 else:
109 raise myexception
110
111 elif type[0] == "GHashTable":
112 return self.inputhash(type, name)
113
114 # known object types are transformed to integer handles
115 elif type[0].startswith("Gaim") or type[0] == "xmlnode":
116 return self.inputgaimstructure(type, name)
117
118 # unknown pointers are always replaced with NULL
119 else:
120 return self.inputpointer(type, name)
121 return
122
123 raise myexception
124
125
126 def processoutput(self, type, name):
127 # the "void" type is simple ...
128 if type == ["void"]:
129 return self.outputvoid(type, name)
130
131 const = False
132 if type[0] == "const":
133 type = type[1:]
134 const = True
135
136 # a string
137 if type == ["char", pointer]:
138 return self.outputstring(type, name, const)
139
140 # simple types (ints, booleans, enums, ...)
141 if (len(type) == 1) and \
142 ((type[0] in simpletypes) or (type[0].startswith("Gaim"))):
143 return self.outputsimple(type, name)
144
145 # pointers ...
146 if (len(type) == 2) and (type[1] == pointer):
147
148 # handles
149 if type[0].startswith("Gaim"):
150 return self.outputgaimstructure(type, name)
151
152 if type[0] in ["GList", "GSList"]:
153 return self.outputlist(type, name)
154
155 raise myexception
156
157
158 class ClientBinding (Binding):
159 def __init__(self, functiontext, paramtexts, knowntypes, headersonly):
160 Binding.__init__(self, functiontext, paramtexts)
161 self.knowntypes = knowntypes
162 self.headersonly = headersonly
163 self.paramshdr = []
164 self.decls = []
165 self.inputparams = []
166 self.outputparams = []
167 self.returncode = []
168
169 def flush(self):
170 print "%s %s(%s)" % (self.functiontype, self.function.name,
171 ", ".join(self.paramshdr)),
172
173 if self.headersonly:
174 print ";"
175 return
176
177 print "{"
178
179 for decl in self.decls:
180 print decl
181
182 print 'dbus_g_proxy_call(gaim_proxy, "%s", NULL,' % ctopascal(self.function.name)
183
184 for type_name in self.inputparams:
185 print "\t%s, %s, " % type_name,
186 print "G_TYPE_INVALID,"
187
188 for type_name in self.outputparams:
189 print "\t%s, &%s, " % type_name,
190 print "G_TYPE_INVALID);"
191
192 for code in self.returncode:
193 print code
194
195 print "}\n"
196
197
198 def definegaimstructure(self, type):
199 if (self.headersonly) and (type[0] not in self.knowntypes):
200 print "struct _%s;" % type[0]
201 print "typedef struct _%s %s;" % (type[0], type[0])
202 self.knowntypes.append(type[0])
203
204 def inputsimple(self, type, name):
205 self.paramshdr.append("%s %s" % (type[0], name))
206 self.inputparams.append(("G_TYPE_INT", name))
207
208 def inputstring(self, type, name):
209 self.paramshdr.append("const char *%s" % name)
210 self.inputparams.append(("G_TYPE_STRING", name))
211
212 def inputgaimstructure(self, type, name):
213 self.paramshdr.append("const %s *%s" % (type[0], name))
214 self.inputparams.append(("G_TYPE_INT", "GPOINTER_TO_INT(%s)" % name))
215 self.definegaimstructure(type)
216
217 def inputpointer(self, type, name):
218 name += "_NULL"
219 self.paramshdr.append("const %s *%s" % (type[0], name))
220 self.inputparams.append(("G_TYPE_INT", "0"))
221
222 def inputhash(self, type, name):
223 self.paramshdr.append("const GHashTable *%s" % name)
224 self.inputparams.append(('dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING)', name))
225
226 def outputvoid(self, type, name):
227 self.functiontype = "void"
228
229 def outputstring(self, type, name, const):
230 self.functiontype = "char*"
231 self.decls.append("char *%s = NULL;" % name)
232 self.outputparams.append(("G_TYPE_STRING", name))
233 # self.returncode.append("NULLIFY(%s);" % name)
234 self.returncode.append("return %s;" % name);
235
236 def outputsimple(self, type, name):
237 self.functiontype = type[0]
238 self.decls.append("%s %s = 0;" % (type[0], name))
239 self.outputparams.append(("G_TYPE_INT", name))
240 self.returncode.append("return %s;" % name);
241
242 # we could add "const" to the return type but this would probably
243 # be a nuisance
244 def outputgaimstructure(self, type, name):
245 name = name + "_ID"
246 self.functiontype = "%s*" % type[0]
247 self.decls.append("int %s = 0;" % name)
248 self.outputparams.append(("G_TYPE_INT", "%s" % name))
249 self.returncode.append("return (%s*) GINT_TO_POINTER(%s);" % (type[0], name));
250 self.definegaimstructure(type)
251
252 def outputlist(self, type, name):
253 self.functiontype = "%s*" % type[0]
254 self.decls.append("GArray *%s;" % name)
255 self.outputparams.append(('dbus_g_type_get_collection("GArray", G_TYPE_INT)', name))
256 self.returncode.append("return garray_int_to_%s(%s);" %
257 (type[0].lower(), name));
258
259
260 class ServerBinding (Binding):
261 def __init__(self, functiontext, paramtexts):
262 Binding.__init__(self, functiontext, paramtexts)
263 self.dparams = ""
264 self.cparams = []
265 self.cdecls = []
266 self.ccode = []
267 self.cparamsout = []
268 self.ccodeout = []
269 self.argfunc = "dbus_message_get_args"
270
271 def flush(self):
272 print "static DBusMessage*"
273 print "%s_DBUS(DBusMessage *message_DBUS, DBusError *error_DBUS) {" % \
274 self.function.name
275
276 print "\tDBusMessage *reply_DBUS;"
277
278 for decl in self.cdecls:
279 print decl
280
281 print "\t%s(message_DBUS, error_DBUS, " % self.argfunc,
282 for param in self.cparams:
283 print "DBUS_TYPE_%s, &%s," % param,
284 print "DBUS_TYPE_INVALID);"
285
286 print "\tCHECK_ERROR(error_DBUS);"
287
288 for code in self.ccode:
289 print code
290
291 print "\treply_DBUS = dbus_message_new_method_return (message_DBUS);"
292
293 print "\tdbus_message_append_args(reply_DBUS, ",
294 for param in self.cparamsout:
295 if type(param) is str:
296 print "%s, " % param
297 else:
298 print "DBUS_TYPE_%s, &%s, " % param,
299 print "DBUS_TYPE_INVALID);"
300
301 for code in self.ccodeout:
302 print code
303
304 print "\treturn reply_DBUS;\n}\n"
305
306
307 def addstring(self, *items):
308 for item in items:
309 self.dparams += item + r"\0"
310
311 def addintype(self, type, name):
312 self.addstring("in", type, name)
313
314 def addouttype(self, type, name):
315 self.addstring("out", type, name)
316
317
318 # input parameters
319
320 def inputsimple(self, type, name):
321 self.cdecls.append("\tdbus_int32_t %s;" % name)
322 self.cparams.append(("INT32", name))
323 self.addintype("i", name)
324
325 def inputstring(self, type, name):
326 self.cdecls.append("\tconst char *%s;" % name)
327 self.cparams.append(("STRING", name))
328 self.ccode .append("\tNULLIFY(%s);" % name)
329 self.addintype("s", name)
330
331 def inputhash(self, type, name):
332 self.argfunc = "gaim_dbus_message_get_args"
333 self.cdecls.append("\tDBusMessageIter %s_ITER;" % name)
334 self.cdecls.append("\tGHashTable *%s;" % name)
335 self.cparams.append(("ARRAY", "%s_ITER" % name))
336 self.ccode.append("\t%s = gaim_dbus_iter_hash_table(&%s_ITER, error_DBUS);" \
337 % (name, name))
338 self.ccode.append("\tCHECK_ERROR(error_DBUS);")
339 self.ccodeout.append("\tg_hash_table_destroy(%s);" % name)
340 self.addintype("a{ss}", name)
341
342 def inputgaimstructure(self, type, name):
343 self.cdecls.append("\tdbus_int32_t %s_ID;" % name)
344 self.cdecls.append("\t%s *%s;" % (type[0], name))
345 self.cparams.append(("INT32", name + "_ID"))
346 self.ccode.append("\tGAIM_DBUS_ID_TO_POINTER(%s, %s_ID, %s, error_DBUS);" % \
347 (name, name, type[0]))
348 self.addintype("i", name)
349
350 def inputpointer(self, type, name):
351 self.cdecls.append("\tdbus_int32_t %s_NULL;" % name)
352 self.cdecls .append("\t%s *%s;" % (type[0], name))
353 self.cparams.append(("INT32", name + "_NULL"))
354 self.ccode .append("\t%s = NULL;" % name)
355 self.addintype("i", name)
356
357 # output parameters
358
359 def outputvoid(self, type, name):
360 self.ccode.append("\t%s;" % self.call) # just call the function
361
362 def outputstring(self, type, name, const):
363 self.cdecls.append("\tconst char *%s;" % name)
364 self.ccode.append("\t%s = null_to_empty(%s);" % (name, self.call))
365 self.cparamsout.append(("STRING", name))
366 self.addouttype("s", name)
367 if not const:
368 self.ccodeout.append("\tg_free(%s);" % name)
369
370 def outputsimple(self, type, name):
371 self.cdecls.append("\tdbus_int32_t %s;" % name)
372 self.ccode.append("\t%s = %s;" % (name, self.call))
373 self.cparamsout.append(("INT32", name))
374 self.addouttype("i", name)
375
376 def outputgaimstructure(self, type, name):
377 self.cdecls.append("\tdbus_int32_t %s;" % name)
378 self.ccode .append("\tGAIM_DBUS_POINTER_TO_ID(%s, %s, error_DBUS);" % (name, self.call))
379 self.cparamsout.append(("INT32", name))
380 self.addouttype("i", name)
381
382 # GList*, GSList*, assume that list is a list of objects
383
384 # fixme: at the moment, we do NOT free the memory occupied by
385 # the list, we should free it if the list has NOT been declared const
386
387 # fixme: we assume that this is a list of objects, not a list
388 # of strings
389
390 def outputlist(self, type, name):
391 self.cdecls.append("\tdbus_int32_t %s_LEN;" % name)
392 self.ccodeout.append("\tg_free(%s);" % name)
393
394 if self.function.name in stringlists:
395 self.cdecls.append("\tchar **%s;" % name)
396 self.ccode.append("\t%s = gaim_%s_to_array(%s, FALSE, &%s_LEN);" % \
397 (name, type[0], self.call, name))
398 self.cparamsout.append("\tDBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &%s, %s_LEN" \
399 % (name, name))
400 self.addouttype("as", name)
401 else:
402 self.cdecls.append("\tdbus_int32_t *%s;" % name)
403 self.ccode.append("\t%s = gaim_dbusify_%s(%s, FALSE, &%s_LEN);" % \
404 (name, type[0], self.call, name))
405 self.cparamsout.append("\tDBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &%s, %s_LEN" \
406 % (name, name))
407 self.addouttype("ai", name)
408
409
410 class BindingSet:
411 regexp = r"^(\w[^()]*)\(([^()]*)\)\s*;\s*$";
412
413 def __init__(self, inputfile, fprefix):
414 self.inputiter = iter(inputfile)
415 self.functionregexp = \
416 re.compile("^%s(\w[^()]*)\(([^()]*)\)\s*;\s*$" % fprefix)
417
418
419
420 def process(self):
421 print "/* Generated by %s. Do not edit! */" % sys.argv[0]
422
423 for line in self.inputiter:
424 words = line.split()
425 if len(words) == 0: # empty line
426 continue
427 if line[0] == "#": # preprocessor directive
428 continue
429 if words[0] in ["typedef", "struct", "enum", "static"]:
430 continue
431
432 # accumulate lines until the parentheses are balance or an
433 # empty line has been encountered
434 myline = line.strip()
435 while myline.count("(") > myline.count(")"):
436 newline = self.inputiter.next().strip()
437 if len(newline) == 0:
438 break
439 myline += " " + newline
440
441 # is this a function declaration?
442 thematch = self.functionregexp.match(
443 myline.replace("*", " " + pointer + " "))
444
445 if thematch is None:
446 continue
447
448 functiontext = thematch.group(1)
449 paramstext = thematch.group(2).strip()
450
451 if (paramstext == "void") or (paramstext == ""):
452 paramtexts = []
453 else:
454 paramtexts = paramstext.split(",")
455
456 try:
457 self.processfunction(functiontext, paramtexts)
458 except myexception:
459 sys.stderr.write(myline + "\n")
460 except:
461 sys.stderr.write(myline + "\n")
462 raise
463
464 self.flush()
465
466 class ServerBindingSet (BindingSet):
467 def __init__(self, inputfile, fprefix):
468 BindingSet.__init__(self, inputfile, fprefix)
469 self.functions = []
470
471
472 def processfunction(self, functiontext, paramtexts):
473 binding = ServerBinding(functiontext, paramtexts)
474 binding.process()
475 self.functions.append((binding.function.name, binding.dparams))
476
477 def flush(self):
478 print "static GaimDBusBinding bindings_DBUS[] = { "
479 for function, params in self.functions:
480 print '{"%s", "%s", %s_DBUS},' % \
481 (ctopascal(function), params, function)
482
483 print "{NULL, NULL, NULL}"
484 print "};"
485
486 print "#define GAIM_DBUS_REGISTER_BINDINGS(handle) gaim_dbus_register_bindings(handle, bindings_DBUS)"
487
488 class ClientBindingSet (BindingSet):
489 def __init__(self, inputfile, fprefix, headersonly):
490 BindingSet.__init__(self, inputfile, fprefix)
491 self.functions = []
492 self.knowntypes = []
493 self.headersonly = headersonly
494
495 def processfunction(self, functiontext, paramtexts):
496 binding = ClientBinding(functiontext, paramtexts, self.knowntypes, self.headersonly)
497 binding.process()
498
499 def flush(self):
500 pass
501
502 # Main program
503
504 options = {}
505
506 for arg in sys.argv[1:]:
507 if arg[0:2] == "--":
508 mylist = arg[2:].split("=",1)
509 command = mylist[0]
510 if len(mylist) > 1:
511 options[command] = mylist[1]
512 else:
513 options[command] = None
514
515 if "export-only" in options:
516 fprefix = "DBUS_EXPORT\s+"
517 else:
518 fprefix = ""
519
520 sys.stderr.write("%s: Functions not exported:\n" % sys.argv[0])
521
522 if "client" in options:
523 bindings = ClientBindingSet(sys.stdin, fprefix,
524 options.has_key("headers"))
525 else:
526 bindings = ServerBindingSet(sys.stdin, fprefix)
527 bindings.process()
528
529
530
531