1 """
2 The B{0install config} command-line interface.
3 """
4
5
6
7
8 from __future__ import print_function
9
10 from zeroinstall import SafeException, _
11 from zeroinstall.injector import model
12 from zeroinstall.cmd import UsageError
13
14 syntax = "[NAME [VALUE]]"
18
20 @staticmethod
25
26 @staticmethod
28 """@type value: str
29 @rtype: str"""
30 return value
31
33 @staticmethod
42 value = float(value)
43 if value < 60:
44 return s(value) + "s"
45 value /= 60
46 if value < 60:
47 return s(value) + "m"
48 value /= 60
49 if value < 24:
50 return s(value) + "h"
51 value /= 24
52 return s(value) + "d"
53
54 @staticmethod
56 """@type value: str
57 @rtype: int"""
58 v = float(value[:-1])
59 unit = value[-1]
60 if unit == 's':
61 return int(v)
62 v *= 60
63 if unit == 'm':
64 return int(v)
65 v *= 60
66 if unit == 'h':
67 return int(v)
68 v *= 24
69 if unit == 'd':
70 return int(v)
71 raise SafeException(_('Unknown unit "%s" - use e.g. 5d for 5 days') % unit)
72
74 @staticmethod
79
80 @staticmethod
82 """@type value: str
83 @rtype: bool"""
84 if value.lower() == 'true':
85 return True
86 elif value.lower() == 'false':
87 return False
88 else:
89 raise SafeException(_('Must be True or False, not "%s"') % value)
90
91 settings = {
92 'network_use': String,
93 'freshness': TimeInterval,
94 'help_with_testing': Boolean,
95 'auto_approve_keys': Boolean,
96 }
97
98 -def handle(config, options, args):
126
128 """@type completion: L{zeroinstall.cmd._Completion}
129 @type args: [str]
130 @type cword: int"""
131 if cword == 0:
132 for name in settings:
133 completion.add_filtered(name)
134 elif cword == 1:
135 option = args[0]
136 if settings.get(option, None) == Boolean:
137 completion.add_filtered("true")
138 completion.add_filtered("false")
139 elif option == 'network_use':
140 for level in model.network_levels:
141 completion.add_filtered(level)
142 elif option in settings:
143 value = getattr(completion.config, option)
144 completion.add_filtered(settings[option].format(value))
145