Fix broken markup in Finnish user docs translation
[empathy-mirror.git] / tools / manager-file.py
blobe1b51a616a2c53a172f0d3ecb47b6fe1b4b3c239
1 #!/usr/bin/python
3 # manager-file.py: generate .manager files and TpCMParamSpec arrays from the
4 # same data (should be suitable for all connection managers that don't have
5 # plugins)
7 # The master copy of this program is in the telepathy-glib repository -
8 # please make any changes there.
10 # Copyright (c) Collabora Ltd. <http://www.collabora.co.uk/>
12 # This library is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU Lesser General Public
14 # License as published by the Free Software Foundation; either
15 # version 2.1 of the License, or (at your option) any later version.
17 # This library is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # Lesser General Public License for more details.
22 # You should have received a copy of the GNU Lesser General Public
23 # License along with this library; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 import re
27 import sys
29 _NOT_C_STR = re.compile(r'[^A-Za-z0-9_-]')
31 def c_string(x):
32 # whitelist-based brute force and ignorance - escape nearly all punctuation
33 return '"' + _NOT_C_STR.sub(lambda c: r'\x%02x' % ord(c), x) + '"'
35 def desktop_string(x):
36 return x.replace(' ', r'\s').replace('\n', r'\n').replace('\r', r'\r').replace('\t', r'\t')
38 supported = list('sbuiqn')
40 fdefaultencoders = {
41 's': desktop_string,
42 'b': (lambda b: b and '1' or '0'),
43 'u': (lambda n: '%u' % n),
44 'i': (lambda n: '%d' % n),
45 'q': (lambda n: '%u' % n),
46 'n': (lambda n: '%d' % n),
48 for x in supported: assert x in fdefaultencoders
50 gtypes = {
51 's': 'G_TYPE_STRING',
52 'b': 'G_TYPE_BOOLEAN',
53 'u': 'G_TYPE_UINT',
54 'i': 'G_TYPE_INT',
55 'q': 'G_TYPE_UINT',
56 'n': 'G_TYPE_INT',
58 for x in supported: assert x in gtypes
60 gdefaultencoders = {
61 's': c_string,
62 'b': (lambda b: b and 'GINT_TO_POINTER (TRUE)' or 'GINT_TO_POINTER (FALSE)'),
63 'u': (lambda n: 'GUINT_TO_POINTER (%u)' % n),
64 'i': (lambda n: 'GINT_TO_POINTER (%d)' % n),
65 'q': (lambda n: 'GUINT_TO_POINTER (%u)' % n),
66 'n': (lambda n: 'GINT_TO_POINTER (%d)' % n),
68 for x in supported: assert x in gdefaultencoders
70 gdefaultdefaults = {
71 's': 'NULL',
72 'b': 'GINT_TO_POINTER (FALSE)',
73 'u': 'GUINT_TO_POINTER (0)',
74 'i': 'GINT_TO_POINTER (0)',
75 'q': 'GUINT_TO_POINTER (0)',
76 'n': 'GINT_TO_POINTER (0)',
78 for x in supported: assert x in gdefaultdefaults
80 gflags = {
81 'has-default': 'TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT',
82 'register': 'TP_CONN_MGR_PARAM_FLAG_REGISTER',
83 'required': 'TP_CONN_MGR_PARAM_FLAG_REQUIRED',
84 'secret': 'TP_CONN_MGR_PARAM_FLAG_SECRET',
85 'dbus-property': 'TP_CONN_MGR_PARAM_FLAG_DBUS_PROPERTY',
88 def write_manager(f, manager, protos):
89 # pointless backwards compat section
90 print >> f, '[ConnectionManager]'
91 print >> f, 'BusName=org.freedesktop.Telepathy.ConnectionManager.' + manager
92 print >> f, 'ObjectPath=/org/freedesktop/Telepathy/ConnectionManager/' + manager
94 # protocols
95 for proto, params in protos.iteritems():
96 print >> f
97 print >> f, '[Protocol %s]' % proto
99 defaults = {}
101 for param, info in params.iteritems():
102 dtype = info['dtype']
103 flags = info.get('flags', '').split()
104 struct_field = info.get('struct_field', param.replace('-', '_'))
105 filter = info.get('filter', 'NULL')
106 filter_data = info.get('filter_data', 'NULL')
107 setter_data = 'NULL'
109 if 'default' in info:
110 default = fdefaultencoders[dtype](info['default'])
111 defaults[param] = default
113 if flags:
114 flags = ' ' + ' '.join(flags)
115 else:
116 flags = ''
118 print >> f, 'param-%s=%s%s' % (param, desktop_string(dtype), flags)
120 for param, default in defaults.iteritems():
121 print >> f, 'default-%s=%s' % (param, default)
123 def write_c_params(f, manager, proto, struct, params):
124 print >> f, "static const TpCMParamSpec %s_%s_params[] = {" % (manager, proto)
126 for param, info in params.iteritems():
127 dtype = info['dtype']
128 flags = info.get('flags', '').split()
129 struct_field = info.get('struct_field', param.replace('-', '_'))
130 filter = info.get('filter', 'NULL')
131 filter_data = info.get('filter_data', 'NULL')
132 setter_data = 'NULL'
134 if 'default' in info:
135 default = gdefaultencoders[dtype](info['default'])
136 else:
137 default = gdefaultdefaults[dtype]
139 if flags:
140 flags = ' | '.join([gflags[flag] for flag in flags])
141 else:
142 flags = '0'
144 if struct is None or struct_field is None:
145 struct_offset = '0'
146 else:
147 struct_offset = 'G_STRUCT_OFFSET (%s, %s)' % (struct, struct_field)
149 print >> f, (''' { %s, %s, %s,
151 %s, /* default */
152 %s, /* struct offset */
153 %s, /* filter */
154 %s, /* filter data */
155 %s /* setter data */ },''' %
156 (c_string(param), c_string(dtype), gtypes[dtype], flags,
157 default, struct_offset, filter, filter_data, setter_data))
159 print >> f, " { NULL }"
160 print >> f, "};"
162 if __name__ == '__main__':
163 environment = {}
164 execfile(sys.argv[1], environment)
166 filename = '%s/%s.manager' % (sys.argv[2], environment['MANAGER'])
167 try:
168 os.remove(filename)
169 except OSError:
170 pass
171 f = open(filename + '.tmp', 'w')
172 write_manager(f, environment['MANAGER'], environment['PARAMS'])
173 f.close()
174 os.rename(filename + '.tmp', filename)
176 filename = '%s/param-spec-struct.h' % sys.argv[2]
177 try:
178 os.remove(filename)
179 except OSError:
180 pass
181 f = open(filename + '.tmp', 'w')
182 for protocol in environment['PARAMS']:
183 write_c_params(f, environment['MANAGER'], protocol,
184 environment['STRUCTS'][protocol],
185 environment['PARAMS'][protocol])
186 f.close()
187 os.rename(filename + '.tmp', filename)