3 Module for Straw's configuration settings.
5 __copyright__
= "Copyright (c) 2002-2005 Free Software Foundation, Inc."
6 __license__
= """ GNU General Public License
8 This program is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA. """
21 from Constants
import *
22 from error
import log
, logtb
, logparam
32 GCONF_STRAW_ROOT
= "/apps/straw"
34 # straw's home directory
36 home
= os
.getenv('HOME')
38 home
= os
.path
.expanduser('~')
39 return os
.path
.join(home
, '.straw')
41 def ensure_directory(strawdir
):
42 if os
.path
.exists(strawdir
):
43 if not os
.path
.isdir(strawdir
):
49 class ConfigGConfPersistence
:
50 SAVERS
= { int: "int",
56 def __init__(self
, client
):
59 def save_option(self
, option
, value
):
60 getattr(self
.client
, 'set_' + self
.SAVERS
[type(value
)])(
61 GCONF_STRAW_ROOT
+ option
, value
)
63 def load_option(self
, key
, option_type
):
64 return getattr(self
.client
, 'get_' + self
.SAVERS
[option_type
])(
65 GCONF_STRAW_ROOT
+ key
)
67 class ConfigPersistence
:
68 def __init__(self
, *backends
):
69 self
.backends
= backends
71 def save_option(self
, option
, value
):
72 for b
in self
.backends
:
74 b
[0].save_option(option
, value
)
76 def load_option(self
, key
, option_type
):
77 for b
in self
.backends
:
79 return b
[0].load_option(key
, option_type
)
81 class ProxyConfig(object):
90 class GconfProxyConfig(ProxyConfig
):
91 """Encapsulate proxy use and location information (host, port, ip),
92 gconf reading and name lookup logic"""
94 GCONF_HTTP_PROXY
= "/system/http_proxy"
95 GCONF_HTTP_PROXY_USE
= GCONF_HTTP_PROXY
+ "/use_http_proxy"
96 GCONF_HTTP_PROXY_HOST
= GCONF_HTTP_PROXY
+ "/host"
97 GCONF_HTTP_PROXY_PORT
= GCONF_HTTP_PROXY
+ "/port"
98 GCONF_HTTP_PROXY_AUTHENTICATION
= GCONF_HTTP_PROXY
+ "/use_authentication"
99 GCONF_HTTP_PROXY_USER
= GCONF_HTTP_PROXY
+ "/authentication_user"
100 GCONF_HTTP_PROXY_PASSWORD
= GCONF_HTTP_PROXY
+ "/authentication_password"
103 ProxyConfig
.__init
__(self
)
104 client
= gconf
.client_get_default()
105 self
.active
= client
.dir_exists(self
.GCONF_HTTP_PROXY
)
110 client
.add_dir(self
.GCONF_HTTP_PROXY
, gconf
.CLIENT_PRELOAD_RECURSIVE
)
111 client
.notify_add(self
.GCONF_HTTP_PROXY_USE
, self
.proxy_mode_changed
)
112 client
.notify_add(self
.GCONF_HTTP_PROXY_HOST
, self
.proxy_host_changed
)
113 client
.notify_add(self
.GCONF_HTTP_PROXY_PORT
, self
.proxy_port_changed
)
114 client
.notify_add(self
.GCONF_HTTP_PROXY_AUTHENTICATION
, self
.proxy_auth_changed
)
115 client
.notify_add(self
.GCONF_HTTP_PROXY_USER
, self
.proxy_auth_username_changed
)
116 client
.notify_add(self
.GCONF_HTTP_PROXY_PASSWORD
, self
.proxy_auth_password_changed
)
118 self
.active
= client
.get_bool(self
.GCONF_HTTP_PROXY_USE
)
119 self
.host
= client
.get_string(self
.GCONF_HTTP_PROXY_HOST
)
120 self
.port
= client
.get_int(self
.GCONF_HTTP_PROXY_PORT
)
121 self
.auth
= client
.get_bool(self
.GCONF_HTTP_PROXY_AUTHENTICATION
)
123 self
.username
= client
.get_string(self
.GCONF_HTTP_PROXY_USER
)
124 self
.password
= client
.get_string(self
.GCONF_HTTP_PROXY_PASSWORD
)
126 def proxy_mode_changed(self
, client
, notify_id
, entry
, *args
):
127 self
.active
= entry
.value
.get_bool()
129 def proxy_host_changed(self
, client
, notify_id
, entry
, *args
):
130 value
= entry
.value
.get_string()
136 def proxy_port_changed(self
, client
, notify_id
, entry
, *args
):
137 value
= entry
.value
.get_int()
143 def proxy_auth_changed(self
, client
, notify_id
, entry
, *args
):
144 value
= entry
.value
.get_bool()
148 def proxy_auth_username_changed(self
, client
, notify_id
, entry
, *args
):
149 value
= entry
.value
.get_string()
150 self
.username
= value
152 def proxy_auth_password_changed(self
, client
, notify_id
, entry
, *args
):
153 value
= entry
.value
.get_string()
154 self
.password
= value
156 class EnvironmentProxyConfig(ProxyConfig
):
157 """Encapsulate proxy use and location information, environment reading"""
160 ProxyConfig
.__init
__(self
)
165 proxies
= urllib
.getproxies()
169 for key
, value
in proxies
.iteritems():
170 proxy
= proxies
.get(key
)
171 user
, authority
= urllib
.splituser(proxy
)
173 self
.host
, self
.port
= urllib
.splitport(authority
)
176 self
.username
, self
.password
= urllib
.splitpasswd(user
)
180 class Config(gobject
.GObject
):
182 'item-order-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ()),
183 'offline-mode-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ()),
184 'refresh-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ()),
185 'item-stored-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ())
188 def __init__(self
, persistence
, defaults
):
189 gobject
.GObject
.__init
__(self
)
191 self
._defaults
= defaults
192 self
._options
= defaults
193 self
.persistence
= persistence
195 self
.initialize_proxy()
197 self
._apply
_defaults
()
199 def _apply_defaults(self
):
202 def initialize_proxy(self
):
203 # EnvironmentProxy has precedence
204 self
.proxy
= EnvironmentProxyConfig()
206 if not self
.proxy
.active
:
207 # .. defaults to GConfProxyConfig so we can listen for network
208 # setting changes (e.g, changes in network preferences)
209 self
.proxy
= GconfProxyConfig()
211 def initialize_options(self
):
212 for key
in self
._options
.keys():
213 self
._options
[key
] = self
.persistence
.load_option(key
, type(self
._defaults
[key
]))
216 if self
._options
.has_key(key
) and self
._options
[key
] != None:
217 return self
._options
[key
]
219 return self
._defaults
[key
]
221 def set(self
, key
, value
):
222 if self
._options
.has_key(key
) and self
._options
[key
] != value
:
223 self
.persistence
.save_option(key
, value
)
224 self
._options
[key
] = value
226 def create_gconf_persistence():
227 client
= gconf
.client_get_default()
228 client
.add_dir(GCONF_STRAW_ROOT
, gconf
.CLIENT_PRELOAD_ONELEVEL
)
229 return ConfigGConfPersistence(client
)
231 def create_instance():
232 defaults
= config_options_defaults
234 cp
= ConfigPersistence((create_gconf_persistence(), defaults
.keys()))
235 config
= Config(cp
, defaults
)
237 config
.initialize_proxy()
238 config
.initialize_options()
242 config_instance
= None
245 global config_instance
247 if not config_instance
:
248 config_instance
= create_instance()
250 return config_instance
253 config
= _get_instance()
254 return config
.get(key
)
257 config
= _get_instance()
258 return config
.set(key
, value
)
262 config
= _get_instance()