1 """
2 Holds user settings and various helper objects.
3 @since: 0.53
4 """
5
6
7
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'
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
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
57 if not self._stores:
58 self._stores = zerostore.Stores()
59 return self._stores
60
61 @property
68
69 @property
75
76 @property
82
83 @property
87
88 @property
97
98 @property
104
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
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))
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