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

Source Code for Module zeroinstall.injector.config

  1  """ 
  2  Holds user settings and various helper objects. 
  3  @since: 0.53 
  4  """ 
  5   
  6  # Copyright (C) 2011, Thomas Leonard 
  7  # See the README file for details, or visit http://0install.net. 
  8   
  9  from zeroinstall import support, _, logger 
 10  import os 
 11   
 12  try: 
 13          import ConfigParser 
 14  except ImportError: 
 15          import configparser as ConfigParser 
 16   
 17  from zeroinstall import zerostore 
 18  from zeroinstall.injector.model import network_levels, network_full 
 19  from zeroinstall.injector.namespaces import config_site, config_prog 
 20  from zeroinstall.support import basedir 
 21   
 22  DEFAULT_MIRROR = "http://roscidus.com/0mirror" 
 23  DEFAULT_KEY_LOOKUP_SERVER = 'https://keylookup.appspot.com' 
24 25 -class Config(object):
26 """ 27 @ivar auto_approve_keys: whether to approve known keys automatically 28 @type auto_approve_keys: bool 29 @ivar handler: handler for main-loop integration 30 @type handler: L{handler.Handler} 31 @ivar key_info_server: the base URL of a key information server 32 @type key_info_server: str 33 @ivar mirror: the base URL of a mirror site for feeds, keys and implementations (since 1.10) 34 @type mirror: str | None 35 @ivar freshness: seconds since a feed was last checked before it is considered stale 36 @type freshness: int 37 """ 38 39 __slots__ = ['help_with_testing', 'freshness', 'network_use', 'mirror', 'key_info_server', 'auto_approve_keys', 40 '_fetcher', '_stores', '_iface_cache', '_handler', '_trust_mgr', '_trust_db', '_app_mgr'] 41
42 - def __init__(self, handler = None):
43 """@type handler: L{zeroinstall.injector.handler.Handler} | None""" 44 self.help_with_testing = False 45 self.freshness = 60 * 60 * 24 * 30 46 self.network_use = network_full 47 self._handler = handler 48 self._app_mgr = self._fetcher = self._stores = self._iface_cache = self._trust_mgr = self._trust_db = None 49 self.mirror = DEFAULT_MIRROR 50 self.key_info_server = DEFAULT_KEY_LOOKUP_SERVER 51 self.auto_approve_keys = True
52 53 feed_mirror = property(lambda self: self.mirror, lambda self, value: setattr(self, 'mirror', value)) 54 55 @property
56 - def stores(self):
57 if not self._stores: 58 self._stores = zerostore.Stores() 59 return self._stores
60 61 @property
62 - def iface_cache(self):
63 if not self._iface_cache: 64 from zeroinstall.injector import iface_cache 65 self._iface_cache = iface_cache.iface_cache 66 #self._iface_cache = iface_cache.IfaceCache() 67 return self._iface_cache
68 69 @property
70 - def fetcher(self):
71 if not self._fetcher: 72 from zeroinstall.injector import fetch 73 self._fetcher = fetch.Fetcher(self) 74 return self._fetcher
75 76 @property
77 - def trust_mgr(self):
78 if not self._trust_mgr: 79 from zeroinstall.injector import trust 80 self._trust_mgr = trust.TrustMgr(self) 81 return self._trust_mgr
82 83 @property
84 - def trust_db(self):
85 from zeroinstall.injector import trust 86 self._trust_db = trust.trust_db
87 88 @property
89 - def handler(self):
90 if not self._handler: 91 from zeroinstall.injector import handler 92 if os.isatty(1): 93 self._handler = handler.ConsoleHandler() 94 else: 95 self._handler = handler.Handler() 96 return self._handler
97 98 @property
99 - def app_mgr(self):
100 if not self._app_mgr: 101 from zeroinstall import apps 102 self._app_mgr = apps.AppManager(self) 103 return self._app_mgr
104
105 - def save_globals(self):
106 """Write global settings.""" 107 parser = ConfigParser.ConfigParser() 108 parser.add_section('global') 109 110 parser.set('global', 'help_with_testing', str(self.help_with_testing)) 111 parser.set('global', 'network_use', self.network_use) 112 parser.set('global', 'freshness', str(self.freshness)) 113 parser.set('global', 'auto_approve_keys', str(self.auto_approve_keys)) 114 115 path = basedir.save_config_path(config_site, config_prog) 116 path = os.path.join(path, 'global') 117 with open(path + '.new', 'wt') as stream: 118 parser.write(stream) 119 support.portable_rename(path + '.new', path)
120
121 -def load_config(handler = None):
122 """@type handler: L{zeroinstall.injector.handler.Handler} | None 123 @rtype: L{Config}""" 124 config = Config(handler) 125 parser = ConfigParser.RawConfigParser() 126 parser.add_section('global') 127 parser.set('global', 'help_with_testing', 'False') 128 parser.set('global', 'freshness', str(60 * 60 * 24 * 30)) # One month 129 parser.set('global', 'network_use', 'full') 130 parser.set('global', 'auto_approve_keys', 'True') 131 132 path = basedir.load_first_config(config_site, config_prog, 'global') 133 if path: 134 logger.info("Loading configuration from %s", path) 135 try: 136 parser.read(path) 137 except Exception as ex: 138 logger.warning(_("Error loading config: %s"), str(ex) or repr(ex)) 139 140 config.help_with_testing = parser.getboolean('global', 'help_with_testing') 141 config.network_use = parser.get('global', 'network_use') 142 config.freshness = int(parser.get('global', 'freshness')) 143 config.auto_approve_keys = parser.getboolean('global', 'auto_approve_keys') 144 145 assert config.network_use in network_levels, config.network_use 146 147 return config
148