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 ConfigOptions
import *
22 from error
import log
, logtb
, logparam
30 GCONF_STRAW_ROOT
= "/apps/straw"
32 # straw's home directory
34 home
= os
.getenv('HOME')
36 home
= os
.path
.expanduser('~')
37 return os
.path
.join(home
, '.straw')
39 def ensure_directory(strawdir
):
40 if os
.path
.exists(strawdir
):
41 if not os
.path
.isdir(strawdir
):
47 class ConfigGConfPersistence
:
48 SAVERS
= { int: "int",
54 def __init__(self
, client
):
57 def save_option(self
, option
, value
):
58 getattr(self
.client
, 'set_' + self
.SAVERS
[type(value
)])(
59 GCONF_STRAW_ROOT
+ option
, value
)
61 def load_option(self
, key
, option_type
):
62 return getattr(self
.client
, 'get_' + self
.SAVERS
[option_type
])(
63 GCONF_STRAW_ROOT
+ key
)
65 class ConfigPersistence
:
66 def __init__(self
, *backends
):
67 self
.backends
= backends
69 def save_option(self
, option
, value
):
70 for b
in self
.backends
:
72 b
[0].save_option(option
, value
)
74 def load_option(self
, key
, option_type
):
75 for b
in self
.backends
:
77 return b
[0].load_option(key
, option_type
)
79 class ProxyConfig(object):
90 doc
= "this will be true when one of the subclasses is used"
93 def fset(self
, active
):
95 return property(**locals())
99 doc
= "the host name where the proxy server lives"
102 def fset(self
, host
):
104 return property(**locals())
108 doc
= "the port of the proxy server to connect to"
111 def fset(self
, port
):
113 return property(**locals())
117 doc
= "true if we need to authenticate to the proxy server"
120 def fset(self
, isauth
):
122 return property(**locals())
126 doc
= "the username to use when authenticating to the proxy"
128 return self
._username
129 def fset(self
, username
):
130 self
._username
= username
131 return property(**locals())
135 doc
= "the password to use when authenticating to the proxy"
137 return self
._password
138 def fset(self
, password
):
139 self
._password
= password
140 return property(**locals())
143 class GconfProxyConfig(ProxyConfig
):
144 """Encapsulate proxy use and location information (host, port, ip),
145 gconf reading and name lookup logic"""
147 GCONF_HTTP_PROXY
= "/system/http_proxy"
148 GCONF_HTTP_PROXY_USE
= GCONF_HTTP_PROXY
+ "/use_http_proxy"
149 GCONF_HTTP_PROXY_HOST
= GCONF_HTTP_PROXY
+ "/host"
150 GCONF_HTTP_PROXY_PORT
= GCONF_HTTP_PROXY
+ "/port"
151 GCONF_HTTP_PROXY_AUTHENTICATION
= GCONF_HTTP_PROXY
+ "/use_authentication"
152 GCONF_HTTP_PROXY_USER
= GCONF_HTTP_PROXY
+ "/authentication_user"
153 GCONF_HTTP_PROXY_PASSWORD
= GCONF_HTTP_PROXY
+ "/authentication_password"
156 ProxyConfig
.__init
__(self
)
157 client
= gconf
.client_get_default()
158 self
.active
= client
.dir_exists(self
.GCONF_HTTP_PROXY
)
162 client
.add_dir(self
.GCONF_HTTP_PROXY
, gconf
.CLIENT_PRELOAD_RECURSIVE
)
163 client
.notify_add(self
.GCONF_HTTP_PROXY_USE
, self
.proxy_mode_changed
)
164 client
.notify_add(self
.GCONF_HTTP_PROXY_HOST
, self
.proxy_host_changed
)
165 client
.notify_add(self
.GCONF_HTTP_PROXY_PORT
, self
.proxy_port_changed
)
166 client
.notify_add(self
.GCONF_HTTP_PROXY_AUTHENTICATION
, self
.proxy_auth_changed
)
167 client
.notify_add(self
.GCONF_HTTP_PROXY_USER
, self
.proxy_auth_username_changed
)
168 client
.notify_add(self
.GCONF_HTTP_PROXY_PASSWORD
, self
.proxy_auth_password_changed
)
170 self
.active
= client
.get_bool(self
.GCONF_HTTP_PROXY_USE
)
171 self
.host
= client
.get_string(self
.GCONF_HTTP_PROXY_HOST
)
172 self
.port
= client
.get_int(self
.GCONF_HTTP_PROXY_PORT
)
173 self
.auth
= client
.get_bool(self
.GCONF_HTTP_PROXY_AUTHENTICATION
)
175 self
.username
= client
.get_string(self
.GCONF_HTTP_PROXY_USER
)
176 self
.password
= client
.get_string(self
.GCONF_HTTP_PROXY_PASSWORD
)
179 # here be gconf logic
180 def proxy_mode_changed(self
, client
, notify_id
, entry
, *args
):
181 self
.active
= entry
.value
.get_bool()
183 def proxy_host_changed(self
, client
, notify_id
, entry
, *args
):
184 value
= entry
.value
.get_string()
190 def proxy_port_changed(self
, client
, notify_id
, entry
, *args
):
191 value
= entry
.value
.get_int()
197 def proxy_auth_changed(self
, client
, notify_id
, entry
, *args
):
198 value
= entry
.value
.get_bool()
202 def proxy_auth_username_changed(self
, client
, notify_id
, entry
, *args
):
203 value
= entry
.value
.get_string()
204 self
.username
= value
206 def proxy_auth_password_changed(self
, client
, notify_id
, entry
, *args
):
207 value
= entry
.value
.get_string()
208 self
.password
= value
210 class EnvironmentProxyConfig(ProxyConfig
):
211 """Encapsulate proxy use and location information, environment reading"""
214 ProxyConfig
.__init
__(self
)
219 proxies
= urllib
.getproxies()
223 for key
, value
in proxies
.iteritems():
224 proxy
= proxies
.get(key
)
225 user
, authority
= urllib
.splituser(proxy
)
227 self
.host
, self
.port
= urllib
.splitport(authority
)
230 self
.username
, self
.password
= urllib
.splitpasswd(user
)
234 class Config(gobject
.GObject
):
236 'item-order-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ()),
237 'offline-mode-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ()),
238 'refresh-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ()),
239 'item-stored-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ())
242 def __init__(self
, persistence
, defaults
):
243 gobject
.GObject
.__init
__(self
)
245 self
._defaults
= defaults
246 self
._options
= defaults
247 self
.persistence
= persistence
249 self
.initialize_proxy()
251 self
._apply
_defaults
()
253 def _apply_defaults(self
):
256 def initialize_proxy(self
):
257 # EnvironmentProxy has precedence
258 self
._proxy
= EnvironmentProxyConfig()
259 if not self
._proxy
.active
:
260 # .. defaults to GConfProxyConfig so we can listen for network
261 # setting changes (e.g, changes in network preference)
262 self
._proxy
= GconfProxyConfig()
264 def initialize_options(self
):
265 for key
in self
._options
.keys():
266 self
._options
[key
] = self
.persistence
.load_option(key
, type(self
._defaults
[key
]))
269 if self
._options
.has_key(key
) and self
._options
[key
] != None:
270 return self
._options
[key
]
272 return self
._defaults
[key
]
274 def set(self
, key
, value
):
275 if self
._options
.has_key(key
) and self
._options
[key
] != value
:
276 self
.persistence
.save_option(key
, value
)
277 self
._options
[key
] = value
279 def create_gconf_persistence():
280 client
= gconf
.client_get_default()
281 client
.add_dir(GCONF_STRAW_ROOT
, gconf
.CLIENT_PRELOAD_ONELEVEL
)
282 return ConfigGConfPersistence(client
)
284 def create_instance():
285 defaults
= config_options_defaults
287 cp
= ConfigPersistence((create_gconf_persistence(), defaults
.keys()))
288 config
= Config(cp
, defaults
)
290 config
.initialize_proxy()
291 config
.initialize_options()
295 config_instance
= None
298 global config_instance
300 if not config_instance
:
301 config_instance
= create_instance()
303 return config_instance
306 config
= _get_instance()
307 return config
.get(key
)
310 config
= _get_instance()
311 return config
.set(key
, value
)