Moved Config into its own module
[zeroinstall/zeroinstall-afb.git] / zeroinstall / injector / config.py
blob1c9e548cfab4e8ad013109af412f1fe975bae982
1 """
2 Holds user settings and various helper objects.
3 """
5 # Copyright (C) 2011, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from zeroinstall import _
9 import os
10 from logging import info, warn
11 import ConfigParser
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 class Config(object):
19 """
20 @ivar handler: handler for main-loop integration
21 @type handler: L{handler.Handler}
22 """
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
32 @property
33 def stores(self):
34 if not self._stores:
35 self._stores = zerostore.Stores()
36 return self._stores
38 @property
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
46 @property
47 def fetcher(self):
48 if not self._fetcher:
49 from zeroinstall.injector import fetch
50 self._fetcher = fetch.Fetcher(self.handler)
51 return self._fetcher
53 @property
54 def handler(self):
55 if not self._handler:
56 from zeroinstall.injector import handler
57 if os.isatty(1):
58 self._handler = handler.ConsoleHandler()
59 else:
60 self._handler = handler.Handler()
61 return self._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')
86 if path:
87 info("Loading configuration from %s", path)
88 try:
89 parser.read(path)
90 except Exception, ex:
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
99 return config