File indexing completed on 2024-05-05 17:01:41

0001 #!/usr/bin/python
0002 
0003 # manager-file.py: generate .manager files and TpCMParamSpec arrays from the
0004 # same data (should be suitable for all connection managers that don't have
0005 # plugins)
0006 #
0007 # The master copy of this program is in the telepathy-glib repository -
0008 # please make any changes there.
0009 #
0010 # Copyright (c) Collabora Ltd. <http://www.collabora.co.uk/>
0011 #
0012 # This library is free software; you can redistribute it and/or
0013 # modify it under the terms of the GNU Lesser General Public
0014 # License as published by the Free Software Foundation; either
0015 # version 2.1 of the License, or (at your option) any later version.
0016 #
0017 # This library is distributed in the hope that it will be useful,
0018 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0019 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0020 # Lesser General Public License for more details.
0021 #
0022 # You should have received a copy of the GNU Lesser General Public
0023 # License along with this library; if not, write to the Free Software
0024 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0025 
0026 import re
0027 import sys
0028 
0029 _NOT_C_STR = re.compile(r'[^A-Za-z0-9_-]')
0030 
0031 def c_string(x):
0032     # whitelist-based brute force and ignorance - escape nearly all punctuation
0033     return '"' + _NOT_C_STR.sub(lambda c: r'\x%02x' % ord(c), x) + '"'
0034 
0035 def desktop_string(x):
0036     return x.replace(' ', r'\s').replace('\n', r'\n').replace('\r', r'\r').replace('\t', r'\t')
0037 
0038 supported = list('sbuiqn')
0039 
0040 fdefaultencoders = {
0041         's': desktop_string,
0042         'b': (lambda b: b and '1' or '0'),
0043         'u': (lambda n: '%u' % n),
0044         'i': (lambda n: '%d' % n),
0045         'q': (lambda n: '%u' % n),
0046         'n': (lambda n: '%d' % n),
0047         }
0048 for x in supported: assert x in fdefaultencoders
0049 
0050 gtypes = {
0051         's': 'G_TYPE_STRING',
0052         'b': 'G_TYPE_BOOLEAN',
0053         'u': 'G_TYPE_UINT',
0054         'i': 'G_TYPE_INT',
0055         'q': 'G_TYPE_UINT',
0056         'n': 'G_TYPE_INT',
0057 }
0058 for x in supported: assert x in gtypes
0059 
0060 gdefaultencoders = {
0061         's': c_string,
0062         'b': (lambda b: b and 'GINT_TO_POINTER (TRUE)' or 'GINT_TO_POINTER (FALSE)'),
0063         'u': (lambda n: 'GUINT_TO_POINTER (%u)' % n),
0064         'i': (lambda n: 'GINT_TO_POINTER (%d)' % n),
0065         'q': (lambda n: 'GUINT_TO_POINTER (%u)' % n),
0066         'n': (lambda n: 'GINT_TO_POINTER (%d)' % n),
0067         }
0068 for x in supported: assert x in gdefaultencoders
0069 
0070 gdefaultdefaults = {
0071         's': 'NULL',
0072         'b': 'GINT_TO_POINTER (FALSE)',
0073         'u': 'GUINT_TO_POINTER (0)',
0074         'i': 'GINT_TO_POINTER (0)',
0075         'q': 'GUINT_TO_POINTER (0)',
0076         'n': 'GINT_TO_POINTER (0)',
0077         }
0078 for x in supported: assert x in gdefaultdefaults
0079 
0080 gflags = {
0081         'has-default': 'TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT',
0082         'register': 'TP_CONN_MGR_PARAM_FLAG_REGISTER',
0083         'required': 'TP_CONN_MGR_PARAM_FLAG_REQUIRED',
0084         'secret': 'TP_CONN_MGR_PARAM_FLAG_SECRET',
0085         'dbus-property': 'TP_CONN_MGR_PARAM_FLAG_DBUS_PROPERTY',
0086 }
0087 
0088 def write_manager(f, manager, protos):
0089     # pointless backwards compat section
0090     print >> f, '[ConnectionManager]'
0091     print >> f, 'BusName=org.freedesktop.Telepathy.ConnectionManager.' + manager
0092     print >> f, 'ObjectPath=/org/freedesktop/Telepathy/ConnectionManager/' + manager
0093 
0094     # protocols
0095     for proto, params in protos.iteritems():
0096         print >> f
0097         print >> f, '[Protocol %s]' % proto
0098 
0099         defaults = {}
0100 
0101         for param, info in params.iteritems():
0102             dtype = info['dtype']
0103             flags = info.get('flags', '').split()
0104             struct_field = info.get('struct_field', param.replace('-', '_'))
0105             filter = info.get('filter', 'NULL')
0106             filter_data = info.get('filter_data', 'NULL')
0107             setter_data = 'NULL'
0108 
0109             if 'default' in info:
0110                 default = fdefaultencoders[dtype](info['default'])
0111                 defaults[param] = default
0112 
0113             if flags:
0114                 flags = ' ' + ' '.join(flags)
0115             else:
0116                 flags = ''
0117 
0118             print >> f, 'param-%s=%s%s' % (param, desktop_string(dtype), flags)
0119 
0120         for param, default in defaults.iteritems():
0121             print >> f, 'default-%s=%s' % (param, default)
0122 
0123 def write_c_params(f, manager, proto, struct, params):
0124     print >> f, "static const TpCMParamSpec %s_%s_params[] = {" % (manager, proto)
0125 
0126     for param, info in params.iteritems():
0127         dtype = info['dtype']
0128         flags = info.get('flags', '').split()
0129         struct_field = info.get('struct_field', param.replace('-', '_'))
0130         filter = info.get('filter', 'NULL')
0131         filter_data = info.get('filter_data', 'NULL')
0132         setter_data = 'NULL'
0133 
0134         if 'default' in info:
0135             default = gdefaultencoders[dtype](info['default'])
0136         else:
0137             default = gdefaultdefaults[dtype]
0138 
0139         if flags:
0140             flags = ' | '.join([gflags[flag] for flag in flags])
0141         else:
0142             flags = '0'
0143 
0144         if struct is None or struct_field is None:
0145             struct_offset = '0'
0146         else:
0147             struct_offset = 'G_STRUCT_OFFSET (%s, %s)' % (struct, struct_field)
0148 
0149         print >> f, ('''  { %s, %s, %s,
0150     %s,
0151     %s, /* default */
0152     %s, /* struct offset */
0153     %s, /* filter */
0154     %s, /* filter data */
0155     %s /* setter data */ },''' %
0156                 (c_string(param), c_string(dtype), gtypes[dtype], flags,
0157                     default, struct_offset, filter, filter_data, setter_data))
0158 
0159     print >> f, "  { NULL }"
0160     print >> f, "};"
0161 
0162 if __name__ == '__main__':
0163     environment = {}
0164     execfile(sys.argv[1], environment)
0165 
0166     f = open('%s/%s.manager' % (sys.argv[2], environment['MANAGER']), 'w')
0167     write_manager(f, environment['MANAGER'], environment['PARAMS'])
0168     f.close()
0169 
0170     f = open('%s/param-spec-struct.h' % sys.argv[2], 'w')
0171     for protocol in environment['PARAMS']:
0172         write_c_params(f, environment['MANAGER'], protocol,
0173                 environment['STRUCTS'][protocol],
0174                 environment['PARAMS'][protocol])
0175     f.close()