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
, _
11 from logging
import info
, warn
16 import configparser
as ConfigParser
18 from zeroinstall
import zerostore
19 from zeroinstall
.injector
.model
import network_levels
, network_full
20 from zeroinstall
.injector
.namespaces
import config_site
, config_prog
21 from zeroinstall
.support
import basedir
23 DEFAULT_FEED_MIRROR
= "http://roscidus.com/0mirror"
24 DEFAULT_KEY_LOOKUP_SERVER
= 'https://keylookup.appspot.com'
28 @ivar auto_approve_keys: whether to approve known keys automatically
29 @type auto_approve_keys: bool
30 @ivar handler: handler for main-loop integration
31 @type handler: L{handler.Handler}
32 @ivar key_info_server: the base URL of a key information server
33 @type key_info_server: str
34 @ivar feed_mirror: the base URL of a mirror site for keys and feeds
35 @type feed_mirror: str | None
36 @ivar freshness: seconds since a feed was last checked before it is considered stale
40 __slots__
= ['help_with_testing', 'freshness', 'network_use', 'feed_mirror', 'key_info_server', 'auto_approve_keys',
41 '_fetcher', '_stores', '_iface_cache', '_handler', '_trust_mgr', '_trust_db', '_app_mgr']
43 def __init__(self
, 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
.feed_mirror
= DEFAULT_FEED_MIRROR
50 self
.key_info_server
= DEFAULT_KEY_LOOKUP_SERVER
51 self
.auto_approve_keys
= True
56 self
._stores
= zerostore
.Stores()
60 def iface_cache(self
):
61 if not self
._iface
_cache
:
62 from zeroinstall
.injector
import iface_cache
63 self
._iface
_cache
= iface_cache
.iface_cache
64 #self._iface_cache = iface_cache.IfaceCache()
65 return self
._iface
_cache
70 from zeroinstall
.injector
import fetch
71 self
._fetcher
= fetch
.Fetcher(self
)
76 if not self
._trust
_mgr
:
77 from zeroinstall
.injector
import trust
78 self
._trust
_mgr
= trust
.TrustMgr(self
)
79 return self
._trust
_mgr
83 from zeroinstall
.injector
import trust
84 self
._trust
_db
= trust
.trust_db
89 from zeroinstall
.injector
import handler
91 self
._handler
= handler
.ConsoleHandler()
93 self
._handler
= handler
.Handler()
99 from zeroinstall
import apps
100 self
._app
_mgr
= apps
.AppManager(self
)
103 def save_globals(self
):
104 """Write global settings."""
105 parser
= ConfigParser
.ConfigParser()
106 parser
.add_section('global')
108 parser
.set('global', 'help_with_testing', str(self
.help_with_testing
))
109 parser
.set('global', 'network_use', self
.network_use
)
110 parser
.set('global', 'freshness', str(self
.freshness
))
111 parser
.set('global', 'auto_approve_keys', str(self
.auto_approve_keys
))
113 path
= basedir
.save_config_path(config_site
, config_prog
)
114 path
= os
.path
.join(path
, 'global')
115 with
open(path
+ '.new', 'wt') as stream
:
117 support
.portable_rename(path
+ '.new', path
)
119 def load_config(handler
= None):
120 config
= Config(handler
)
121 parser
= ConfigParser
.RawConfigParser()
122 parser
.add_section('global')
123 parser
.set('global', 'help_with_testing', 'False')
124 parser
.set('global', 'freshness', str(60 * 60 * 24 * 30)) # One month
125 parser
.set('global', 'network_use', 'full')
126 parser
.set('global', 'auto_approve_keys', 'True')
128 path
= basedir
.load_first_config(config_site
, config_prog
, 'global')
130 info("Loading configuration from %s", path
)
133 except Exception as ex
:
134 warn(_("Error loading config: %s"), str(ex
) or repr(ex
))
136 config
.help_with_testing
= parser
.getboolean('global', 'help_with_testing')
137 config
.network_use
= parser
.get('global', 'network_use')
138 config
.freshness
= int(parser
.get('global', 'freshness'))
139 config
.auto_approve_keys
= parser
.getboolean('global', 'auto_approve_keys')
141 assert config
.network_use
in network_levels
, config
.network_use