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",
55 CASTS
= { gconf
.VALUE_BOOL
: gconf
.Value
.get_bool
,
56 gconf
.VALUE_INT
: gconf
.Value
.get_int
,
57 gconf
.VALUE_FLOAT
: gconf
.Value
.get_float
,
58 gconf
.VALUE_STRING
: gconf
.Value
.get_string
}
60 def __init__(self
, client
):
63 def save_option(self
, option
, value
):
64 getattr(self
.client
, 'set_' + self
.SAVERS
[type(value
)])(
65 GCONF_STRAW_ROOT
+ option
, value
)
67 def load_option(self
, key
, option_type
):
68 gval
= self
.client
.get(GCONF_STRAW_ROOT
+ key
)
76 value
= getattr(gconf
.Value
, 'get_' + self
.SAVERS
[option_type
])(gval
)
78 print "FIXME! TypeError logging (option = %s): %s" % (key
, str(te
))
82 class ConfigPersistence
:
83 def __init__(self
, *backends
):
84 self
.backends
= backends
86 def save_option(self
, option
, value
):
87 for b
in self
.backends
:
89 b
[0].save_option(option
, value
)
91 def load_option(self
, key
, option_type
):
92 for b
in self
.backends
:
94 return b
[0].load_option(key
, option_type
)
96 class ProxyConfig(object):
105 class GconfProxyConfig(ProxyConfig
):
106 """Encapsulate proxy use and location information (host, port, ip),
107 gconf reading and name lookup logic"""
109 GCONF_HTTP_PROXY
= "/system/http_proxy"
110 GCONF_HTTP_PROXY_USE
= GCONF_HTTP_PROXY
+ "/use_http_proxy"
111 GCONF_HTTP_PROXY_HOST
= GCONF_HTTP_PROXY
+ "/host"
112 GCONF_HTTP_PROXY_PORT
= GCONF_HTTP_PROXY
+ "/port"
113 GCONF_HTTP_PROXY_AUTHENTICATION
= GCONF_HTTP_PROXY
+ "/use_authentication"
114 GCONF_HTTP_PROXY_USER
= GCONF_HTTP_PROXY
+ "/authentication_user"
115 GCONF_HTTP_PROXY_PASSWORD
= GCONF_HTTP_PROXY
+ "/authentication_password"
118 ProxyConfig
.__init
__(self
)
119 client
= gconf
.client_get_default()
120 self
.active
= client
.dir_exists(self
.GCONF_HTTP_PROXY
)
125 client
.add_dir(self
.GCONF_HTTP_PROXY
, gconf
.CLIENT_PRELOAD_RECURSIVE
)
126 client
.notify_add(self
.GCONF_HTTP_PROXY_USE
, self
.proxy_mode_changed
)
127 client
.notify_add(self
.GCONF_HTTP_PROXY_HOST
, self
.proxy_host_changed
)
128 client
.notify_add(self
.GCONF_HTTP_PROXY_PORT
, self
.proxy_port_changed
)
129 client
.notify_add(self
.GCONF_HTTP_PROXY_AUTHENTICATION
, self
.proxy_auth_changed
)
130 client
.notify_add(self
.GCONF_HTTP_PROXY_USER
, self
.proxy_auth_username_changed
)
131 client
.notify_add(self
.GCONF_HTTP_PROXY_PASSWORD
, self
.proxy_auth_password_changed
)
133 self
.active
= client
.get_bool(self
.GCONF_HTTP_PROXY_USE
)
134 self
.host
= client
.get_string(self
.GCONF_HTTP_PROXY_HOST
)
135 self
.port
= client
.get_int(self
.GCONF_HTTP_PROXY_PORT
)
136 self
.auth
= client
.get_bool(self
.GCONF_HTTP_PROXY_AUTHENTICATION
)
138 self
.username
= client
.get_string(self
.GCONF_HTTP_PROXY_USER
)
139 self
.password
= client
.get_string(self
.GCONF_HTTP_PROXY_PASSWORD
)
141 def proxy_mode_changed(self
, client
, notify_id
, entry
, *args
):
142 self
.active
= entry
.value
.get_bool()
144 def proxy_host_changed(self
, client
, notify_id
, entry
, *args
):
145 value
= entry
.value
.get_string()
151 def proxy_port_changed(self
, client
, notify_id
, entry
, *args
):
152 value
= entry
.value
.get_int()
158 def proxy_auth_changed(self
, client
, notify_id
, entry
, *args
):
159 value
= entry
.value
.get_bool()
163 def proxy_auth_username_changed(self
, client
, notify_id
, entry
, *args
):
164 value
= entry
.value
.get_string()
165 self
.username
= value
167 def proxy_auth_password_changed(self
, client
, notify_id
, entry
, *args
):
168 value
= entry
.value
.get_string()
169 self
.password
= value
171 class EnvironmentProxyConfig(ProxyConfig
):
172 """Encapsulate proxy use and location information, environment reading"""
175 ProxyConfig
.__init
__(self
)
180 proxies
= urllib
.getproxies()
184 for key
, value
in proxies
.iteritems():
185 proxy
= proxies
.get(key
)
186 user
, authority
= urllib
.splituser(proxy
)
188 self
.host
, self
.port
= urllib
.splitport(authority
)
191 self
.username
, self
.password
= urllib
.splitpasswd(user
)
195 class Config(gobject
.GObject
):
197 'item-order-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ()),
198 'offline-mode-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ()),
199 'refresh-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ()),
200 'item-stored-changed' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, ())
203 def __init__(self
, persistence
, defaults
):
204 gobject
.GObject
.__init
__(self
)
206 self
._defaults
= dict(defaults
)
207 self
._options
= dict(defaults
)
208 self
.persistence
= persistence
210 self
.initialize_options()
211 self
.initialize_proxy()
213 self
._apply
_defaults
()
215 def _apply_defaults(self
):
216 for name
, value
in self
._defaults
.iteritems():
217 if not name
in self
._options
or self
._options
[name
] == None:
218 self
._options
[name
] = value
220 def initialize_proxy(self
):
221 # EnvironmentProxy has precedence
222 self
.proxy
= EnvironmentProxyConfig()
224 if not self
.proxy
.active
:
225 # .. defaults to GConfProxyConfig so we can listen for network
226 # setting changes (e.g, changes in network preferences)
227 self
.proxy
= GconfProxyConfig()
229 def initialize_options(self
):
230 for key
in self
._options
.keys():
231 self
._options
[key
] = self
.persistence
.load_option(key
, type(self
._defaults
[key
]))
234 if key
in self
._options
and self
._options
[key
] != None:
235 return self
._options
[key
]
237 return self
._defaults
[key
]
239 def set(self
, key
, value
):
240 if self
._options
.has_key(key
):# and self._options[key] != value:
242 value
= type(self
._defaults
[key
])(value
)
244 self
.persistence
.save_option(key
, value
)
245 self
._options
[key
] = value
247 def create_gconf_persistence():
248 client
= gconf
.client_get_default()
249 client
.add_dir(GCONF_STRAW_ROOT
, gconf
.CLIENT_PRELOAD_ONELEVEL
)
250 return ConfigGConfPersistence(client
)
252 def create_instance():
253 defaults
= config_options_defaults
255 cp
= ConfigPersistence((create_gconf_persistence(), defaults
.keys()))
256 config
= Config(cp
, defaults
)
260 config_instance
= None
263 global config_instance
265 if not config_instance
:
266 config_instance
= create_instance()
268 return config_instance
271 config
= _get_instance()
272 return config
.get(key
)
275 config
= _get_instance()
276 return config
.set(key
, value
)
280 config
= _get_instance()