2 The B{0install config} command-line interface.
5 # Copyright (C) 2011, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
12 from zeroinstall
import cmd
, SafeException
, _
13 from zeroinstall
.support
import basedir
14 from zeroinstall
.injector
import policy
, namespaces
, model
15 from zeroinstall
.cmd
import UsageError
17 syntax
= "[NAME [VALUE]]"
19 def add_options(parser
):
22 def handle(options
, args
):
23 config
= policy
.load_config()
25 if options
.gui
is None and os
.environ
.get('DISPLAY', None):
28 from zeroinstall
import helpers
29 return helpers
.get_selections_gui(None, [])
31 config
.write(sys
.stdout
)
36 if '.' not in args
[0]:
37 raise SafeException(_('Missing section name in "%s" (e.g. try "global.freshness")') % args
[0])
38 section
, option
= args
[0].split('.', 1)
42 print config
.get(section
, option
)
43 except ConfigParser
.NoOptionError
, ex
:
44 raise SafeException(str(ex
))
45 except ConfigParser
.NoSectionError
, ex
:
46 raise SafeException(str(ex
))
48 if section
!= 'global':
49 raise SafeException(_('Unknown section "%s" (try "global")' % section
))
52 if option
== 'freshness':
54 elif option
== 'help_with_testing':
55 if value
.lower() == 'true':
57 elif value
.lower() == 'false':
60 raise SafeException(_('Must be True or False, not "%s"') % value
)
61 elif option
== 'network_use':
62 if value
not in model
.network_levels
:
63 raise SafeException(_("Must be one of %s") % list(model
.network_levels
))
65 raise SafeException(_('Unknown option "%s"') % option
)
67 config
.set(section
, option
, value
)
68 path
= basedir
.save_config_path(namespaces
.config_site
, namespaces
.config_prog
)
69 path
= os
.path
.join(path
, 'global')
70 config
.write(file(path
+ '.new', 'w'))
71 os
.rename(path
+ '.new', path
)