From 4b818be393123f9605c702e76f1fe609023666ea Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Sun, 9 Jan 2011 19:15:10 +0000 Subject: [PATCH] Added '0install config' sub-command --- zeroinstall/cmd/config.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 zeroinstall/cmd/config.py diff --git a/zeroinstall/cmd/config.py b/zeroinstall/cmd/config.py new file mode 100644 index 0000000..95a1e61 --- /dev/null +++ b/zeroinstall/cmd/config.py @@ -0,0 +1,71 @@ +""" +The B{0install config} command-line interface. +""" + +# Copyright (C) 2011, Thomas Leonard +# See the README file for details, or visit http://0install.net. + +import os, sys +import logging +import ConfigParser + +from zeroinstall import cmd, SafeException, _ +from zeroinstall.support import basedir +from zeroinstall.injector import policy, namespaces, model +from zeroinstall.cmd import UsageError + +syntax = "[NAME [VALUE]]" + +def add_options(parser): + pass + +def handle(options, args): + config = policy.load_config() + if len(args) == 0: + if options.gui is None and os.environ.get('DISPLAY', None): + options.gui = True + if options.gui: + from zeroinstall import helpers + return helpers.get_selections_gui(None, []) + else: + config.write(sys.stdout) + return + elif len(args) > 2: + raise UsageError() + + if '.' not in args[0]: + raise SafeException(_('Missing section name in "%s" (e.g. try "global.freshness")') % args[0]) + section, option = args[0].split('.', 1) + + if len(args) == 1: + try: + print config.get(section, option) + except ConfigParser.NoOptionError, ex: + raise SafeException(str(ex)) + except ConfigParser.NoSectionError, ex: + raise SafeException(str(ex)) + else: + if section != 'global': + raise SafeException(_('Unknown section "%s" (try "global")' % section)) + + value = args[1] + if option == 'freshness': + int(value) + elif option == 'help_with_testing': + if value.lower() == 'true': + value = 'True' + elif value.lower() == 'false': + value = 'False' + else: + raise SafeException(_('Must be True or False, not "%s"') % value) + elif option == 'network_use': + if value not in model.network_levels: + raise SafeException(_("Must be one of %s") % list(model.network_levels)) + else: + raise SafeException(_('Unknown option "%s"') % option) + + config.set(section, option, value) + path = basedir.save_config_path(namespaces.config_site, namespaces.config_prog) + path = os.path.join(path, 'global') + config.write(file(path + '.new', 'w')) + os.rename(path + '.new', path) -- 2.11.4.GIT