summaryrefslogtreecommitdiff
path: root/tools/gquark-gen.py
blob: 37d19e07c0205deead22726e6af2650241d94355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python

# glib-client-gen.py: "I Can't Believe It's Not dbus-binding-tool"
#
# Generate GLib client wrappers from the Telepathy specification.
# The master copy of this program is in the telepathy-glib repository -
# please make any changes there.
#
# Copyright (C) 2006-2008 Collabora Ltd. <http://www.collabora.co.uk/>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import sys
import os.path
from getopt import gnu_getopt

from libglibcodegen import Signature, type_to_gtype, cmp_by_name, get_docstring

def camelcase_to_lower(s):
    out ="";
    out += s[0].lower()
    last_upper=False
    if s[0].isupper():
        last_upper=True
    for i in range(1,len(s)):
        if s[i].isupper():
            if last_upper:
                if (i+1) < len(s) and  s[i+1].islower():
                    out += "_" + s[i].lower()
                else:
                    out += s[i].lower()
            else:
                out += "_" + s[i].lower()
            last_upper=True
        else:
            out += s[i]
            last_upper=False
    return out


NS_TP = "http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"

class Generator(object):

    def __init__(self, quark_list, prefix, basename, opts):
        self.__header = []
        self.__body = []

        self.prefix_lc = prefix.lower()
        self.prefix_uc = prefix.upper()
        self.basename = basename
        self.quark_prefix = opts.get('--quark-prefix', None)

        self.quark_list = [ l.split(None, 2) for l in quark_list ]
            

    def h(self, s):
        self.__header.append(s)

    def b(self, s):
        self.__body.append(s)

    def do_quark(self, quark):
        (qname, qstring) = quark

        self.b('GQuark')
        self.b('%s_%s (void)'
                % (self.prefix_lc, qname.lower()))
        self.b('{')
        self.b('    static GQuark quark = 0;')
        self.b('')
        self.b('    if (G_UNLIKELY (quark == 0))')
        self.b('	quark = g_quark_from_static_string ("%s");'
                % (qstring, ))
        self.b('    return quark;')
        self.b('}')
        self.b('')
        self.b('')

        self.h('#define %s_%s %s_%s()' %
                (self.prefix_uc, qname.upper(),
                 self.prefix_lc, qname.lower()))
        self.h('GQuark %s_%s(void);' % (self.prefix_lc, qname.lower()))
        self.h('')

    def __call__(self):

        self.b('#include "%s.h"' % (self.basename))
        self.b('')

        self.h('#include <glib.h>')
        self.h('')
        self.h('G_BEGIN_DECLS')
        self.h('')

        for quark in self.quark_list:
            self.do_quark(quark)


        self.h('G_END_DECLS')
        self.h('')

        open(self.basename + '.h', 'w').write('\n'.join(self.__header))
        open(self.basename + '.c', 'w').write('\n'.join(self.__body))


def types_to_gtypes(types):
    return [type_to_gtype(t)[1] for t in types]


if __name__ == '__main__':
    options, argv = gnu_getopt(sys.argv[1:], '',
                               ['quark-prefix='])

    opts = {}

    for option, value in options:
        opts[option] = value

    quark_list_file = file(argv[0])
    quark_list = quark_list_file.readlines()
    quark_list_file.close()
    Generator(quark_list, argv[1], argv[2], opts)()