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
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
29 _NOT_C_STR = re.compile(r'[^A-Za-z0-9_-]')
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')
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
52 'b': 'G_TYPE_BOOLEAN',
58 for x in supported: assert x in gtypes
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
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
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
95 for proto, params in protos.iteritems():
97 print >> f, '[Protocol %s]' % proto
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')
109 if 'default' in info:
110 default = fdefaultencoders[dtype](info['default'])
111 defaults[param] = default
114 flags = ' ' + ' '.join(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')
134 if 'default' in info:
135 default = gdefaultencoders[dtype](info['default'])
137 default = gdefaultdefaults[dtype]
140 flags = ' | '.join([gflags[flag] for flag in flags])
144 if struct is None or struct_field is None:
147 struct_offset = 'G_STRUCT_OFFSET (%s, %s)' % (struct, struct_field)
149 print >> f, (''' { %s, %s, %s,
152 %s, /* struct offset */
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 }"
162 if __name__ == '__main__':
164 execfile(sys.argv[1], environment)
166 filename = '%s/%s.manager' % (sys.argv[2], environment['MANAGER'])
171 f = open(filename + '.tmp', 'w')
172 write_manager(f, environment['MANAGER'], environment['PARAMS'])
174 os.rename(filename + '.tmp', filename)
176 filename = '%s/param-spec-struct.h' % sys.argv[2]
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])
187 os.rename(filename + '.tmp', filename)