diff options
author | David Zeuthen <davidz@redhat.com> | 2011-02-12 10:40:49 -0500 |
---|---|---|
committer | David Zeuthen <davidz@redhat.com> | 2011-02-12 10:40:49 -0500 |
commit | fc056dfd5dbd87aa40ea20e707d03d8e50600198 (patch) | |
tree | c1dcbe682c7861fde5b313780a36d3227c950586 /src/codegen.py | |
parent | a8fc395fa19caaefaa261bf1a7ef1a793ad99aa8 (diff) |
Move python around some
Signed-off-by: David Zeuthen <davidz@redhat.com>
Diffstat (limited to 'src/codegen.py')
-rw-r--r--[-rwxr-xr-x] | src/codegen.py | 297 |
1 files changed, 10 insertions, 287 deletions
diff --git a/src/codegen.py b/src/codegen.py index ec9919d..88d1451 100755..100644 --- a/src/codegen.py +++ b/src/codegen.py @@ -1,291 +1,13 @@ -#!/usr/bin/python +# -*- Mode: Python -*- import sys import argparse -from gi.repository import GLib, Gio +from gi.repository import Gio import config - -def _strip_dots(s): - ret = '' - force_upper = False - for c in s: - if c == '.': - force_upper = True - else: - if force_upper: - ret += c.upper() - force_upper = False - else: - ret += c - return ret - -def _camel_case_to_uscore(s): - ret = '' - insert_uscore = False - prev_was_lower = False - for c in s: - if c.isupper(): - if prev_was_lower: - insert_uscore = True - prev_was_lower = False - else: - prev_was_lower = True - if insert_uscore: - ret += '_' - ret += c.lower() - insert_uscore = False - return ret - -def _lookup_annotation(annotations, key): - if annotations: - for a in annotations: - if a.key == key: - return a.value - return None - -class Arg: - def __init__(self, name, signature, annotations): - self.name = name - self.annotations = annotations - - self.signature = signature - # default to GVariant - self.ctype_in_g = 'GVariant *' - self.ctype_in = 'GVariant *' - self.ctype_out = 'GVariant **' - self.gtype = 'G_TYPE_VARIANT' - self.free_func = 'g_variant_unref' - self.format_in = '@' + signature - self.format_out = '@' + signature - if not _lookup_annotation(annotations, 'org.gtk.GDBus.UseGVariant'): - if self.signature == 'b': - self.ctype_in_g = 'gboolean ' - self.ctype_in = 'gboolean ' - self.ctype_out = 'gboolean *' - self.gtype = 'G_TYPE_BOOLEAN' - self.free_func = None - self.format_in = 'b' - self.format_out = 'b' - elif self.signature == 'y': - self.ctype_in_g = 'guchar ' - self.ctype_in = 'guchar ' - self.ctype_out = 'guchar *' - self.gtype = 'G_TYPE_UCHAR' - self.free_func = None - self.format_in = 'y' - self.format_out = 'y' - elif self.signature == 'n': - self.ctype_in_g = 'gint ' - self.ctype_in = 'gint16 ' - self.ctype_out = 'gint16 *' - self.gtype = 'G_TYPE_INT' - self.free_func = None - self.format_in = 'n' - self.format_out = 'n' - elif self.signature == 'q': - self.ctype_in_g = 'guint ' - self.ctype_in = 'guint16 ' - self.ctype_out = 'guint16 *' - self.gtype = 'G_TYPE_UINT' - self.free_func = None - self.format_in = 'q' - self.format_out = 'q' - elif self.signature == 'i': - self.ctype_in_g = 'gint ' - self.ctype_in = 'gint ' - self.ctype_out = 'gint *' - self.gtype = 'G_TYPE_INT' - self.free_func = None - self.format_in = 'i' - self.format_out = 'i' - elif self.signature == 'u': - self.ctype_in_g = 'guint ' - self.ctype_in = 'guint ' - self.ctype_out = 'guint *' - self.gtype = 'G_TYPE_UINT' - self.free_func = None - self.format_in = 'u' - self.format_out = 'u' - elif self.signature == 'x': - self.ctype_in_g = 'gint64 ' - self.ctype_in = 'gint64 ' - self.ctype_out = 'gint64 *' - self.gtype = 'G_TYPE_INT64' - self.free_func = None - self.format_in = 'x' - self.format_out = 'x' - elif self.signature == 't': - self.ctype_in_g = 'guint64 ' - self.ctype_in = 'guint64 ' - self.ctype_out = 'guint64 *' - self.gtype = 'G_TYPE_UINT64' - self.free_func = None - self.format_in = 't' - self.format_out = 't' - elif self.signature == 'd': - self.ctype_in_g = 'gdouble ' - self.ctype_in = 'gdouble ' - self.ctype_out = 'gdouble *' - self.gtype = 'G_TYPE_DOUBLE' - self.free_func = None - self.format_in = 'd' - self.format_out = 'd' - elif self.signature == 's': - self.ctype_in_g = 'const gchar *' - self.ctype_in = 'const gchar *' - self.ctype_out = 'gchar **' - self.gtype = 'G_TYPE_STRING' - self.free_func = 'g_free' - self.format_in = 's' - self.format_out = 's' - elif self.signature == 'o': - self.ctype_in_g = 'const gchar *' - self.ctype_in = 'const gchar *' - self.ctype_out = 'gchar **' - self.gtype = 'G_TYPE_STRING' - self.free_func = 'g_free' - self.format_in = 'o' - self.format_out = 'o' - elif self.signature == 'g': - self.ctype_in_g = 'const gchar *' - self.ctype_in = 'const gchar *' - self.ctype_out = 'gchar **' - self.gtype = 'G_TYPE_STRING' - self.free_func = 'g_free' - self.format_in = 'g' - self.format_out = 'g' - elif self.signature == 'ay': - self.ctype_in_g = 'const gchar *' - self.ctype_in = 'const gchar *' - self.ctype_out = 'gchar **' - self.gtype = 'G_TYPE_STRING' - self.format_in = '^ay' - self.format_out = '^ay' - elif self.signature == 'as': - self.ctype_in_g = 'const gchar *const *' - self.ctype_in = 'const gchar *const *' - self.ctype_out = 'gchar ***' - self.gtype = 'G_TYPE_STRV' - self.free_func = 'g_strfreev' - self.format_in = '^as' - self.format_out = '^as' - elif self.signature == 'aay': - self.ctype_in_g = 'const gchar *const *' - self.ctype_in = 'const gchar *const *' - self.ctype_out = 'gchar ***' - self.gtype = 'G_TYPE_STRV' - self.free_func = 'g_strfreev' - self.format_in = '^aay' - self.format_out = '^aay' - - #print ' arg: %s (sig=%s, ctype_in=%s, ctype_out=%s, gtype=%s)'%(self.name, self.signature, self.ctype_in, self.ctype_out, self.gtype) - -class Method: - def __init__(self, gm, i): - self.interface = i - self.name = gm.name - self.annotations = gm.get_annotations() - self.name_lower = _camel_case_to_uscore(self.name).lower() - self.name_hyphen = self.name_lower.replace('_', '-') - #print ' --- method' - #print ' name: ' + self.name - #print ' name_lower: ' + self.name_lower - - self.in_args = [] - gdbus_in_args = gm.get_in_args() - for a in gdbus_in_args: - self.in_args.append(Arg(a.name, a.signature, a.get_annotations())) - - self.out_args = [] - gdbus_out_args = gm.get_out_args() - for a in gdbus_out_args: - self.out_args.append(Arg(a.name, a.signature, a.get_annotations())) - -class Signal: - def __init__(self, gs, i): - self.interface = i - self.name = gs.name - self.annotations = gs.get_annotations() - self.name_lower = _camel_case_to_uscore(self.name).lower() - self.name_hyphen = self.name_lower.replace('_', '-') - #print ' --- signal' - #print ' name: ' + self.name - #print ' name_lower: ' + self.name_lower - - self.args = [] - gdbus_args = gs.get_args() - for a in gdbus_args: - self.args.append(Arg(a.name, a.signature, a.get_annotations())) - -class Property: - def __init__(self, gp, i): - self.interface = i - self.name = gp.name - self.annotations = gp.get_annotations() - self.name_lower = _camel_case_to_uscore(self.name).lower() - self.name_hyphen = self.name_lower.replace('_', '-') - flags = gp.get_flags() - # TODO: use constants from Gio.DBusPropertyInfoFlags bitfield - self.readable = ((flags & 1) != 0) - self.writable = ((flags & 2) != 0) - #print ' --- property' - #print ' name: ' + self.name - #print ' name_lower: ' + self.name_lower - #print ' name_hyphen: ' + self.name_hyphen - #print ' readable: %d'%self.readable - #print ' writable: %d'%self.writable - - self.arg = Arg('value', gp.signature, gp.get_annotations()) - -class Interface: - def __init__(self, gi, strip_prefix, namespace): - self.name = gi.name - self.annotations = gi.get_annotations() - - s = gi.name - if s.startswith(strip_prefix): - s = s[len(strip_prefix):] - s = _strip_dots(s) - name_with_ns = _strip_dots(namespace + '.' + s) - - self.camel_name = name_with_ns - if len(namespace) > 1: - self.ns_upper = _camel_case_to_uscore(namespace).upper() + '_' - else: - self.ns_upper = '' - self.name_upper = _camel_case_to_uscore(s).upper() - self.name_lower = _camel_case_to_uscore(name_with_ns) - - #print '---' - #print 'interface: ' + self.name - #print 'camel_name: ' + self.camel_name - #print 'ns_upper: ' + self.ns_upper - #print 'name_upper: ' + self.name_upper - #print 'name_lower: ' + self.name_lower - # - # interface: org.project.Bar.Frobnicator - # camel_name: FooBarFrobnicator - # ns_upper: FOO_ - # name_upper: BAR_FROBNICATOR - # name_lower: foo_bar_frobnicator - - self.methods = [] - gdbus_methods = gi.get_methods() - for gm in gdbus_methods: - self.methods.append(Method(gm, self)) - - self.signals = [] - gdbus_signals = gi.get_signals() - for gs in gdbus_signals: - self.signals.append(Signal(gs, self)) - - self.properties = [] - gdbus_properties = gi.get_properties() - for gp in gdbus_properties: - self.properties.append(Property(gp, self)) - +import utils +import dbustypes # ---------------------------------------------------------------------------------------------------- @@ -296,8 +18,8 @@ class CodeGenerator: self.c = c self.namespace = namespace if len(namespace) > 1: - self.ns_upper = _camel_case_to_uscore(namespace).upper() + '_' - self.ns_lower = _camel_case_to_uscore(namespace).lower() + '_' + self.ns_upper = utils.camel_case_to_uscore(namespace).upper() + '_' + self.ns_lower = utils.camel_case_to_uscore(namespace).lower() + '_' else: self.ns_upper = '' self.ns_lower = '' @@ -839,7 +561,7 @@ class CodeGenerator: else: self.c.write(' (GDBusAnnotationInfo **) &%s_arg_%s_annotation_info_pointers\n'%(prefix, a.name)) self.c.write(' },\n') - if not _lookup_annotation(a.annotations, 'org.gtk.GDBus.UseGVariant'): + if not utils.lookup_annotation(a.annotations, 'org.gtk.GDBus.UseGVariant'): self.c.write(' FALSE\n') else: self.c.write(' TRUE\n') @@ -958,7 +680,7 @@ class CodeGenerator: self.c.write(' },\n' ' "%s",\n' %(p.name_hyphen)) - if not _lookup_annotation(p.annotations, 'org.gtk.GDBus.UseGVariant'): + if not utils.lookup_annotation(p.annotations, 'org.gtk.GDBus.UseGVariant'): self.c.write(' FALSE\n') else: self.c.write(' TRUE\n') @@ -2552,7 +2274,7 @@ def codegen_main(): if args.annotate != None: apply_annotations(gdbus_interfaces, args.annotate) for gi in gdbus_interfaces: - all_ifaces.append(Interface(gi, args.strip_prefix, args.namespace)) + all_ifaces.append(dbustypes.Interface(gi, args.strip_prefix, args.namespace)) h = file(args.output_hfile, 'w') c = file(args.output_cfile, 'w') @@ -2560,4 +2282,5 @@ def codegen_main(): sys.exit(codegen.generate()) if __name__ == "__main__": + print "foo" codegen_main() |