Package zeroinstall :: Package cmd :: Module config
[frames] | no frames]

Source Code for Module zeroinstall.cmd.config

  1  """ 
  2  The B{0install config} command-line interface. 
  3  """ 
  4   
  5  # Copyright (C) 2011, Thomas Leonard 
  6  # See the README file for details, or visit http://0install.net. 
  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]]" 
15 16 -def add_options(parser):
17 pass
18
19 -class String(object):
20 @staticmethod
21 - def format(value):
22 """@type value: str 23 @rtype: str""" 24 return value
25 26 @staticmethod
27 - def parse(value):
28 """@type value: str 29 @rtype: str""" 30 return value
31
32 -class TimeInterval(object):
33 @staticmethod
34 - def format(value):
35 """@type value: float 36 @rtype: str""" 37 def s(v): 38 if int(v) == v: 39 return str(int(v)) 40 else: 41 return str(v)
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
55 - def parse(value):
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
73 -class Boolean(object):
74 @staticmethod
75 - def format(value):
76 """@type value: bool 77 @rtype: str""" 78 return str(value)
79 80 @staticmethod
81 - def parse(value):
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):
99 """@type config: L{zeroinstall.injector.config.Config} 100 @type args: [str]""" 101 if len(args) == 0: 102 from zeroinstall import helpers 103 if helpers.get_selections_gui(None, [], use_gui = options.gui) == helpers.DontUseGUI: 104 for key, setting_type in settings.items(): 105 value = getattr(config, key) 106 print(key, "=", setting_type.format(value)) 107 # (else we displayed the preferences dialog in the GUI) 108 return 109 elif len(args) > 2: 110 raise UsageError() 111 112 option = args[0] 113 if option not in settings: 114 raise SafeException(_('Unknown option "%s"') % option) 115 116 if len(args) == 1: 117 value = getattr(config, option) 118 print(settings[option].format(value)) 119 else: 120 value = settings[option].parse(args[1]) 121 if option == 'network_use' and value not in model.network_levels: 122 raise SafeException(_("Must be one of %s") % list(model.network_levels)) 123 setattr(config, option, value) 124 125 config.save_globals()
126
127 -def complete(completion, args, cword):
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