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
20 @ivar handler: handler for main-loop integration
21 @type handler: L{handler.Handler}
24 __slots__
= ['help_with_testing', 'freshness', 'network_use', '_fetcher', '_stores', '_iface_cache', '_handler']
25 def __init__(self
, handler
= None):
26 self
.help_with_testing
= False
27 self
.freshness
= 60 * 60 * 24 * 30
28 self
.network_use
= network_full
29 self
._handler
= handler
30 self
._fetcher
= self
._stores
= self
._iface
_cache
= None
35 self
._stores
= zerostore
.Stores()
39 def iface_cache(self
):
40 if not self
._iface
_cache
:
41 from zeroinstall
.injector
import iface_cache
42 self
._iface
_cache
= iface_cache
.iface_cache
43 #self._iface_cache = iface_cache.IfaceCache()
44 return self
._iface
_cache
49 from zeroinstall
.injector
import fetch
50 self
._fetcher
= fetch
.Fetcher(self
.handler
)
56 from zeroinstall
.injector
import handler
58 self
._handler
= handler
.ConsoleHandler()
60 self
._handler
= handler
.Handler()
63 def save_globals(self
):
64 """Write global settings."""
65 parser
= ConfigParser
.ConfigParser()
66 parser
.add_section('global')
68 parser
.set('global', 'help_with_testing', self
.help_with_testing
)
69 parser
.set('global', 'network_use', self
.network_use
)
70 parser
.set('global', 'freshness', self
.freshness
)
72 path
= basedir
.save_config_path(config_site
, config_prog
)
73 path
= os
.path
.join(path
, 'global')
74 parser
.write(file(path
+ '.new', 'w'))
75 os
.rename(path
+ '.new', path
)
77 def load_config(handler
= None):
78 config
= Config(handler
)
79 parser
= ConfigParser
.RawConfigParser()
80 parser
.add_section('global')
81 parser
.set('global', 'help_with_testing', 'False')
82 parser
.set('global', 'freshness', str(60 * 60 * 24 * 30)) # One month
83 parser
.set('global', 'network_use', 'full')
85 path
= basedir
.load_first_config(config_site
, config_prog
, 'global')
87 info("Loading configuration from %s", path
)
91 warn(_("Error loading config: %s"), str(ex
) or repr(ex
))
93 config
.help_with_testing
= parser
.getboolean('global', 'help_with_testing')
94 config
.network_use
= parser
.get('global', 'network_use')
95 config
.freshness
= int(parser
.get('global', 'freshness'))
97 assert config
.network_use
in network_levels
, config
.network_use