2 Holds user settings and various helper objects.
6 # Copyright (C) 2011, Thomas Leonard
7 # See the README file for details, or visit http://0install.net.
9 from zeroinstall
import support
, _
, logger
15 import configparser
as ConfigParser
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
22 DEFAULT_MIRROR
= "http://roscidus.com/0mirror"
23 DEFAULT_KEY_LOOKUP_SERVER
= 'https://keylookup.appspot.com'
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
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']
42 def __init__(self
, handler
= None):
43 self
.help_with_testing
= False
44 self
.freshness
= 60 * 60 * 24 * 30
45 self
.network_use
= network_full
46 self
._handler
= handler
47 self
._app
_mgr
= self
._fetcher
= self
._stores
= self
._iface
_cache
= self
._trust
_mgr
= self
._trust
_db
= None
48 self
.mirror
= DEFAULT_MIRROR
49 self
.key_info_server
= DEFAULT_KEY_LOOKUP_SERVER
50 self
.auto_approve_keys
= True
52 feed_mirror
= property(lambda self
: self
.mirror
, lambda self
, value
: setattr(self
, 'mirror', value
))
57 self
._stores
= zerostore
.Stores()
61 def iface_cache(self
):
62 if not self
._iface
_cache
:
63 from zeroinstall
.injector
import iface_cache
64 self
._iface
_cache
= iface_cache
.iface_cache
65 #self._iface_cache = iface_cache.IfaceCache()
66 return self
._iface
_cache
71 from zeroinstall
.injector
import fetch
72 self
._fetcher
= fetch
.Fetcher(self
)
77 if not self
._trust
_mgr
:
78 from zeroinstall
.injector
import trust
79 self
._trust
_mgr
= trust
.TrustMgr(self
)
80 return self
._trust
_mgr
84 from zeroinstall
.injector
import trust
85 self
._trust
_db
= trust
.trust_db
90 from zeroinstall
.injector
import handler
92 self
._handler
= handler
.ConsoleHandler()
94 self
._handler
= handler
.Handler()
100 from zeroinstall
import apps
101 self
._app
_mgr
= apps
.AppManager(self
)
104 def save_globals(self
):
105 """Write global settings."""
106 parser
= ConfigParser
.ConfigParser()
107 parser
.add_section('global')
109 parser
.set('global', 'help_with_testing', str(self
.help_with_testing
))
110 parser
.set('global', 'network_use', self
.network_use
)
111 parser
.set('global', 'freshness', str(self
.freshness
))
112 parser
.set('global', 'auto_approve_keys', str(self
.auto_approve_keys
))
114 path
= basedir
.save_config_path(config_site
, config_prog
)
115 path
= os
.path
.join(path
, 'global')
116 with
open(path
+ '.new', 'wt') as stream
:
118 support
.portable_rename(path
+ '.new', path
)
120 def load_config(handler
= None):
121 config
= Config(handler
)
122 parser
= ConfigParser
.RawConfigParser()
123 parser
.add_section('global')
124 parser
.set('global', 'help_with_testing', 'False')
125 parser
.set('global', 'freshness', str(60 * 60 * 24 * 30)) # One month
126 parser
.set('global', 'network_use', 'full')
127 parser
.set('global', 'auto_approve_keys', 'True')
129 path
= basedir
.load_first_config(config_site
, config_prog
, 'global')
131 logger
.info("Loading configuration from %s", path
)
134 except Exception as ex
:
135 logger
.warn(_("Error loading config: %s"), str(ex
) or repr(ex
))
137 config
.help_with_testing
= parser
.getboolean('global', 'help_with_testing')
138 config
.network_use
= parser
.get('global', 'network_use')
139 config
.freshness
= int(parser
.get('global', 'freshness'))
140 config
.auto_approve_keys
= parser
.getboolean('global', 'auto_approve_keys')
142 assert config
.network_use
in network_levels
, config
.network_use