summaryrefslogtreecommitdiff
path: root/tools/manager-file.py
blob: e1b51a616a2c53a172f0d3ecb47b6fe1b4b3c239 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/python

# manager-file.py: generate .manager files and TpCMParamSpec arrays from the
# same data (should be suitable for all connection managers that don't have
# plugins)
#
# The master copy of this program is in the telepathy-glib repository -
# please make any changes there.
#
# Copyright (c) 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 re
import sys

_NOT_C_STR = re.compile(r'[^A-Za-z0-9_-]')

def c_string(x):
    # whitelist-based brute force and ignorance - escape nearly all punctuation
    return '"' + _NOT_C_STR.sub(lambda c: r'\x%02x' % ord(c), x) + '"'

def desktop_string(x):
    return x.replace(' ', r'\s').replace('\n', r'\n').replace('\r', r'\r').replace('\t', r'\t')

supported = list('sbuiqn')

fdefaultencoders = {
        's': desktop_string,
        'b': (lambda b: b and '1' or '0'),
        'u': (lambda n: '%u' % n),
        'i': (lambda n: '%d' % n),
        'q': (lambda n: '%u' % n),
        'n': (lambda n: '%d' % n),
        }
for x in supported: assert x in fdefaultencoders

gtypes = {
        's': 'G_TYPE_STRING',
        'b': 'G_TYPE_BOOLEAN',
        'u': 'G_TYPE_UINT',
        'i': 'G_TYPE_INT',
        'q': 'G_TYPE_UINT',
        'n': 'G_TYPE_INT',
}
for x in supported: assert x in gtypes

gdefaultencoders = {
        's': c_string,
        'b': (lambda b: b and 'GINT_TO_POINTER (TRUE)' or 'GINT_TO_POINTER (FALSE)'),
        'u': (lambda n: 'GUINT_TO_POINTER (%u)' % n),
        'i': (lambda n: 'GINT_TO_POINTER (%d)' % n),
        'q': (lambda n: 'GUINT_TO_POINTER (%u)' % n),
        'n': (lambda n: 'GINT_TO_POINTER (%d)' % n),
        }
for x in supported: assert x in gdefaultencoders

gdefaultdefaults = {
        's': 'NULL',
        'b': 'GINT_TO_POINTER (FALSE)',
        'u': 'GUINT_TO_POINTER (0)',
        'i': 'GINT_TO_POINTER (0)',
        'q': 'GUINT_TO_POINTER (0)',
        'n': 'GINT_TO_POINTER (0)',
        }
for x in supported: assert x in gdefaultdefaults

gflags = {
        'has-default': 'TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT',
        'register': 'TP_CONN_MGR_PARAM_FLAG_REGISTER',
        'required': 'TP_CONN_MGR_PARAM_FLAG_REQUIRED',
        'secret': 'TP_CONN_MGR_PARAM_FLAG_SECRET',
        'dbus-property': 'TP_CONN_MGR_PARAM_FLAG_DBUS_PROPERTY',
}

def write_manager(f, manager, protos):
    # pointless backwards compat section
    print >> f, '[ConnectionManager]'
    print >> f, 'BusName=org.freedesktop.Telepathy.ConnectionManager.' + manager
    print >> f, 'ObjectPath=/org/freedesktop/Telepathy/ConnectionManager/' + manager

    # protocols
    for proto, params in protos.iteritems():
        print >> f
        print >> f, '[Protocol %s]' % proto

        defaults = {}

        for param, info in params.iteritems():
            dtype = info['dtype']
            flags = info.get('flags', '').split()
            struct_field = info.get('struct_field', param.replace('-', '_'))
            filter = info.get('filter', 'NULL')
            filter_data = info.get('filter_data', 'NULL')
            setter_data = 'NULL'

            if 'default' in info:
                default = fdefaultencoders[dtype](info['default'])
                defaults[param] = default

            if flags:
                flags = ' ' + ' '.join(flags)
            else:
                flags = ''

            print >> f, 'param-%s=%s%s' % (param, desktop_string(dtype), flags)

        for param, default in defaults.iteritems():
            print >> f, 'default-%s=%s' % (param, default)

def write_c_params(f, manager, proto, struct, params):
    print >> f, "static const TpCMParamSpec %s_%s_params[] = {" % (manager, proto)

    for param, info in params.iteritems():
        dtype = info['dtype']
        flags = info.get('flags', '').split()
        struct_field = info.get('struct_field', param.replace('-', '_'))
        filter = info.get('filter', 'NULL')
        filter_data = info.get('filter_data', 'NULL')
        setter_data = 'NULL'

        if 'default' in info:
            default = gdefaultencoders[dtype](info['default'])
        else:
            default = gdefaultdefaults[dtype]

        if flags:
            flags = ' | '.join([gflags[flag] for flag in flags])
        else:
            flags = '0'

        if struct is None or struct_field is None:
            struct_offset = '0'
        else:
            struct_offset = 'G_STRUCT_OFFSET (%s, %s)' % (struct, struct_field)

        print >> f, ('''  { %s, %s, %s,
    %s,
    %s, /* default */
    %s, /* struct offset */
    %s, /* filter */
    %s, /* filter data */
    %s /* setter data */ },''' %
                (c_string(param), c_string(dtype), gtypes[dtype], flags,
                    default, struct_offset, filter, filter_data, setter_data))

    print >> f, "  { NULL }"
    print >> f, "};"

if __name__ == '__main__':
    environment = {}
    execfile(sys.argv[1], environment)

    filename = '%s/%s.manager' % (sys.argv[2], environment['MANAGER'])
    try:
        os.remove(filename)
    except OSError:
        pass
    f = open(filename + '.tmp', 'w')
    write_manager(f, environment['MANAGER'], environment['PARAMS'])
    f.close()
    os.rename(filename + '.tmp', filename)

    filename = '%s/param-spec-struct.h' % sys.argv[2]
    try:
        os.remove(filename)
    except OSError:
        pass
    f = open(filename + '.tmp', 'w')
    for protocol in environment['PARAMS']:
        write_c_params(f, environment['MANAGER'], protocol,
                environment['STRUCTS'][protocol],
                environment['PARAMS'][protocol])
    f.close()
    os.rename(filename + '.tmp', filename)