2 Holds user settings and various helper objects.
5 # Copyright (C) 2011, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from zeroinstall
import _
10 from logging
import info
, warn
13 from zeroinstall
import zerostore
14 from zeroinstall
.injector
.model
import network_levels
, network_full
15 from zeroinstall
.injector
.namespaces
import config_site
, config_prog
16 from zeroinstall
.support
import basedir
18 DEFAULT_FEED_MIRROR
= "http://roscidus.com/0mirror"
19 DEFAULT_KEY_LOOKUP_SERVER
= 'https://keylookup.appspot.com'
23 @ivar auto_approve_keys: whether to approve known keys automatically
24 @type auto_approve_keys: bool
25 @ivar handler: handler for main-loop integration
26 @type handler: L{handler.Handler}
27 @ivar key_info_server: the base URL of a key information server
28 @type key_info_server: str
29 @ivar feed_mirror: the base URL of a mirror site for keys and feeds
30 @type feed_mirror: str | None
31 @ivar freshness: seconds since a feed was last checked before it is considered stale
35 __slots__
= ['help_with_testing', 'freshness', 'network_use', 'feed_mirror', 'key_info_server', 'auto_approve_keys',
36 '_fetcher', '_stores', '_iface_cache', '_handler', '_trust_mgr', '_trust_db', '_app_mgr']
38 def __init__(self
, handler
= None):
39 self
.help_with_testing
= False
40 self
.freshness
= 60 * 60 * 24 * 30
41 self
.network_use
= network_full
42 self
._handler
= handler
43 self
._app
_mgr
= self
._fetcher
= self
._stores
= self
._iface
_cache
= self
._trust
_mgr
= self
._trust
_db
= None
44 self
.feed_mirror
= DEFAULT_FEED_MIRROR
45 self
.key_info_server
= DEFAULT_KEY_LOOKUP_SERVER
46 self
.auto_approve_keys
= True
51 self
._stores
= zerostore
.Stores()
55 def iface_cache(self
):
56 if not self
._iface
_cache
:
57 from zeroinstall
.injector
import iface_cache
58 self
._iface
_cache
= iface_cache
.iface_cache
59 #self._iface_cache = iface_cache.IfaceCache()
60 return self
._iface
_cache
65 from zeroinstall
.injector
import fetch
66 self
._fetcher
= fetch
.Fetcher(self
)
71 if not self
._trust
_mgr
:
72 from zeroinstall
.injector
import trust
73 self
._trust
_mgr
= trust
.TrustMgr(self
)
74 return self
._trust
_mgr
78 from zeroinstall
.injector
import trust
79 self
._trust
_db
= trust
.trust_db
84 from zeroinstall
.injector
import handler
86 self
._handler
= handler
.ConsoleHandler()
88 self
._handler
= handler
.Handler()
94 from zeroinstall
import apps
95 self
._app
_mgr
= apps
.AppManager(self
)
98 def save_globals(self
):
99 """Write global settings."""
100 parser
= ConfigParser
.ConfigParser()
101 parser
.add_section('global')
103 parser
.set('global', 'help_with_testing', self
.help_with_testing
)
104 parser
.set('global', 'network_use', self
.network_use
)
105 parser
.set('global', 'freshness', self
.freshness
)
106 parser
.set('global', 'auto_approve_keys', self
.auto_approve_keys
)
108 path
= basedir
.save_config_path(config_site
, config_prog
)
109 path
= os
.path
.join(path
, 'global')
110 parser
.write(open(path
+ '.new', 'w'))
111 os
.rename(path
+ '.new', path
)
113 def load_config(handler
= None):
114 config
= Config(handler
)
115 parser
= ConfigParser
.RawConfigParser()
116 parser
.add_section('global')
117 parser
.set('global', 'help_with_testing', 'False')
118 parser
.set('global', 'freshness', str(60 * 60 * 24 * 30)) # One month
119 parser
.set('global', 'network_use', 'full')
120 parser
.set('global', 'auto_approve_keys', 'True')
122 path
= basedir
.load_first_config(config_site
, config_prog
, 'global')
124 info("Loading configuration from %s", path
)
127 except Exception as ex
:
128 warn(_("Error loading config: %s"), str(ex
) or repr(ex
))
130 config
.help_with_testing
= parser
.getboolean('global', 'help_with_testing')
131 config
.network_use
= parser
.get('global', 'network_use')
132 config
.freshness
= int(parser
.get('global', 'freshness'))
133 config
.auto_approve_keys
= parser
.getboolean('global', 'auto_approve_keys')
135 assert config
.network_use
in network_levels
, config
.network_use