~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to setup/configure.py

setup.configure: Replaced the use of an intermediate dictionary for storing
    config options. Now all configuration is done directly in the
    ivle.config.Config object, and written back out from the same object.

ivle.config: Config.set_by_path, value is now optional, to let you just set
    the comment field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
import configobj
40
40
 
41
 
# conf_options maps option names to values
42
 
conf_options = {}
43
 
 
44
41
class ConfigOption:
45
42
    """A configuration option; one of the things written to conf.py."""
46
43
    def __init__(self, option_name, default, prompt, comment, ask=True):
243
240
        # the default values.
244
241
        conf = ivle.config.Config(blank=True)
245
242
 
 
243
    # Check that all the options are present, and if not, load the default
246
244
    for opt in config_options:
247
245
        try:
248
 
            conf_options[opt.option_name] = conf.get_by_path(opt.option_name)
 
246
            conf.get_by_path(opt.option_name)
249
247
        except KeyError:
250
 
            conf_options[opt.option_name] = opt.default
 
248
            conf.set_by_path(opt.option_name, opt.default)
 
249
 
 
250
    # Store comments in the conf object
 
251
    for opt in config_options:
 
252
        conf.set_by_path(opt.option_name, comment=opt.comment)
251
253
 
252
254
    # Set up some variables
253
255
    cwd = os.getcwd()
287
289
 
288
290
        for opt in config_options:
289
291
            if opt.ask:
290
 
                conf_options[opt.option_name] = \
291
 
                    query_user(conf_options[opt.option_name], opt.prompt)
 
292
                conf.set_by_path(opt.option_name,
 
293
                    query_user(conf.get_by_path(opt.option_name), opt.prompt))
292
294
    else:
293
295
        opts = dict(opts)
294
296
        # Non-interactive mode. Parse the options.
295
297
        for opt in config_options:
296
298
            if '--' + opt.option_name in opts:
297
 
                conf_options[opt.option_name] = opts['--' + opt.option_name]
 
299
                conf.set_by_path(opt.option_name,
 
300
                                 opts['--' + opt.option_name])
298
301
 
299
302
    # Error handling on input values
300
303
    try:
301
304
        allowed_uids_list = map(int,
302
 
                                conf_options['os/allowed_uids'].split(','))
 
305
                                conf['os']['allowed_uids'].split(','))
303
306
    except ValueError:
304
307
        print >>sys.stderr, (
305
308
        "Invalid UID list (%s).\n"
306
309
        "Must be a comma-separated list of integers." %
307
 
            conf_options['os/allowed_uids'])
 
310
            conf['os']['allowed_uids'])
308
311
        return 1
309
312
    try:
310
 
        conf_options['database/port'] = int(conf_options['database/port'])
311
 
        if (conf_options['database/port'] < 0
312
 
            or conf_options['database/port'] >= 65536):
 
313
        conf['database']['port'] = int(conf['database']['port'])
 
314
        if (conf['database']['port'] < 0
 
315
            or conf['database']['port'] >= 65536):
313
316
            raise ValueError()
314
317
    except ValueError:
315
318
        print >>sys.stderr, (
316
319
        "Invalid DB port (%s).\n"
317
320
        "Must be an integer between 0 and 65535." %
318
 
            repr(conf_options['database/port']))
 
321
            repr(conf['database']['port']))
319
322
        return 1
320
323
    try:
321
 
        conf_options['usrmgt/port'] = int(conf_options['usrmgt/port'])
322
 
        if (conf_options['usrmgt/port'] < 0
323
 
            or conf_options['usrmgt/port'] >= 65536):
 
324
        conf['usrmgt']['port'] = int(conf['usrmgt']['port'])
 
325
        if (conf['usrmgt']['port'] < 0 or conf['usrmgt']['port'] >= 65536):
324
326
            raise ValueError()
325
327
    except ValueError:
326
328
        print >>sys.stderr, (
327
329
        "Invalid user management port (%s).\n"
328
330
        "Must be an integer between 0 and 65535." %
329
 
            repr(conf_options['usrmgt/port']))
 
331
            repr(conf['usrmgt']['port']))
330
332
        return 1
331
333
 
332
334
    # By default we generate the magic randomly.
333
 
    if conf_options['usrmgt/magic'] is None:
334
 
        conf_options['usrmgt/magic'] = \
335
 
            hashlib.md5(uuid.uuid4().bytes).hexdigest()
 
335
    if conf['usrmgt']['magic'] is None:
 
336
        conf['usrmgt']['magic'] = hashlib.md5(uuid.uuid4().bytes).hexdigest()
336
337
 
337
338
    # Generate the forum secret
338
339
    forum_secret = hashlib.md5(uuid.uuid4().bytes).hexdigest()
344
345
 
345
346
    # Add the forum secret to the config file (regenerated each config)
346
347
    config_options.append(ConfigOption('plugins/forum/secret', None, '', ''))
347
 
    conf_options['plugins/forum/secret'] = forum_secret
348
 
 
349
 
    for opt in config_options:
350
 
        value = conf_options[opt.option_name]
351
 
        if value is not None:
352
 
            conf.set_by_path(opt.option_name, value, opt.comment)
 
348
    conf['plugins']['forum']['secret'] = forum_secret
353
349
 
354
350
    conf.write()
355
351
 
362
358
    # XXX Compute jail_base, jail_src_base and jail_system. These will
363
359
    # ALSO be done by the boilerplate code, but we need them here in order
364
360
    # to write to the C file.
365
 
    jail_base = os.path.join(conf_options['paths/data'], 'jailmounts')
366
 
    jail_src_base = os.path.join(conf_options['paths/data'], 'jails')
 
361
    jail_base = os.path.join(conf['paths']['data'], 'jailmounts')
 
362
    jail_src_base = os.path.join(conf['paths']['data'], 'jails')
367
363
    jail_system = os.path.join(jail_src_base, '__base__')
368
364
 
369
365
    conf_h.write("""/* IVLE Configuration File
403
399
    conf_php = open(phpBBconffile, "w")
404
400
    
405
401
    # php-pg work around
406
 
    if conf_options['database/host'] == 'localhost':
 
402
    if conf['database']['host'] == 'localhost':
407
403
        forumdb_host = '127.0.0.1'
408
404
    else:
409
 
        forumdb_host = conf_options['database/host']
 
405
        forumdb_host = conf['database']['host']
410
406
 
411
407
    conf_php.write( """<?php
412
408
// phpBB 3.0.x auto-generated configuration file
413
409
// Do not change anything in this file!
414
410
$dbms = 'postgres';
415
411
$dbhost = '""" + forumdb_host + """';
416
 
$dbport = '""" + str(conf_options['database/port']) + """';
417
 
$dbname = '""" + conf_options['plugins/forum/dbname'] + """';
418
 
$dbuser = '""" + conf_options['database/username'] + """';
419
 
$dbpasswd = '""" + conf_options['database/password'] + """';
 
412
$dbport = '""" + str(conf['database']['port']) + """';
 
413
$dbname = '""" + conf['plugins']['forum']['dbname'] + """';
 
414
$dbuser = '""" + conf['database']['username'] + """';
 
415
$dbpasswd = '""" + conf['database']['password'] + """';
420
416
 
421
417
$table_prefix = 'phpbb_';
422
418
$acm_type = 'file';