Merge lp:~ph7/weather-indicator/http-proxy-fix into lp:weather-indicator

Proposed by Panagiotis Skintzos
Status: Merged
Merged at revision: 260
Proposed branch: lp:~ph7/weather-indicator/http-proxy-fix
Merge into: lp:weather-indicator
Diff against target: 256 lines (+102/-70)
1 file modified
indicator_weather/helpers.py (+102/-70)
To merge this branch: bzr merge lp:~ph7/weather-indicator/http-proxy-fix
Reviewer Review Type Date Requested Status
Vadim Rutkovsky Approve
Review via email: mp+59696@code.launchpad.net

Description of the change

Let's stick to gconf for proxy detection, until dconf settings are really in use by gnome-network-properties.

To post a comment you must log in.
Revision history for this message
Vadim Rutkovsky (roignac) wrote :

Thanks, approved

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'indicator_weather/helpers.py'
2--- indicator_weather/helpers.py 2011-04-22 19:09:19 +0000
3+++ indicator_weather/helpers.py 2011-05-02 20:13:28 +0000
4@@ -2,16 +2,16 @@
5 ### BEGIN LICENSE
6 # Copyright (C) 2010 Sebastian MacDonald Sebas310@gmail.com
7 # Copyright (C) 2010 Mehdi Rejraji mehd36@gmail.com
8-# This program is free software: you can redistribute it and/or modify it
9-# under the terms of the GNU General Public License version 3, as published
10+# This program is free software: you can redistribute it and/or modify it
11+# under the terms of the GNU General Public License version 3, as published
12 # by the Free Software Foundation.
13-#
14-# This program is distributed in the hope that it will be useful, but
15-# WITHOUT ANY WARRANTY; without even the implied warranties of
16-# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
17+#
18+# This program is distributed in the hope that it will be useful, but
19+# WITHOUT ANY WARRANTY; without even the implied warranties of
20+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
21 # PURPOSE. See the GNU General Public License for more details.
22-#
23-# You should have received a copy of the GNU General Public License along
24+#
25+# You should have received a copy of the GNU General Public License along
26 # with this program. If not, see <http://www.gnu.org/licenses/>.
27 ### END LICENSE
28
29@@ -29,8 +29,10 @@
30 # this has to be called only once, otherwise we get segfaults
31 DCONF_SCHEMAS = Gio.Settings.list_schemas()
32 except ImportError:
33- pass
34+ DCONF_SCHEMAS = []
35
36+import gconf
37+import traceback
38 import os
39 import gtk
40 import urllib2
41@@ -45,9 +47,9 @@
42 gettext.textdomain('indicator-weather')
43
44 def get_builder(builder_file_name):
45- """Return a fully-instantiated gtk.Builder instance from specified ui
46+ """Return a fully-instantiated gtk.Builder instance from specified ui
47 file
48-
49+
50 :param builder_file_name: The name of the builder file, without extension.
51 Assumed to be in the 'ui' directory under the data path.
52 """
53@@ -85,94 +87,121 @@
54
55 class ProxyMonitor:
56 """ Class to monitor proxy settings """
57-
58+
59 @staticmethod
60 def monitor_proxy(log):
61 ProxyMonitor.log = log
62- # try dconf
63 try:
64- if "org.gnome.system.proxy.http" in DCONF_SCHEMAS:
65+ # disable dconf settings for now
66+ # because they do not seem to be in effect
67+ if False and "org.gnome.system.proxy.http" in DCONF_SCHEMAS:
68+ # load dconf settings
69 proxy_settings = Gio.Settings.new("org.gnome.system.proxy.http")
70 ProxyMonitor.dconf_proxy_changed(proxy_settings)
71 proxy_settings.connect("changed", ProxyMonitor.dconf_proxy_changed)
72 else:
73- # make except block work
74- raise Exception;
75- except:
76- # try gconf
77- import gconf
78- client = gconf.client_get_default()
79- client.add_dir("/system/http_proxy", gconf.CLIENT_PRELOAD_ONELEVEL)
80- ProxyMonitor.gconf_proxy_changed(client)
81- client.notify_add("/system/http_proxy", ProxyMonitor.gconf_proxy_changed)
82+ # load gconf settings
83+ client = gconf.client_get_default()
84+ client.add_dir("/system/http_proxy", gconf.CLIENT_PRELOAD_ONELEVEL)
85+ ProxyMonitor.gconf_proxy_changed(client)
86+ client.notify_add("/system/http_proxy", ProxyMonitor.gconf_proxy_changed)
87+
88+ except Exception, e:
89+ log.error("ProxyMonitor: %s" % e)
90+ log.debug(traceback.format_exc(e))
91
92 @staticmethod
93- def dconf_proxy_changed(settings):
94+ def dconf_proxy_changed(settings, changed_key=None):
95 """
96 Loads dconf hhtp proxy settings
97 """
98- ProxyMonitor.log.debug("ProxyMonitor: loading dconf settings")
99- proxy_info = {}
100- # Taken from http://forum.compiz.org/viewtopic.php?t=9480
101- if settings.get_boolean("enabled"):
102- proxy_info['host'] = settings.get_string("host")
103- proxy_info['port'] = settings.get_int("port")
104- if settings.get_boolean("use-authentication"):
105- proxy_info['user'] = settings.get_string("authentication-user")
106- proxy_info['pass'] = settings.get_string("authentication-password")
107-
108- ProxyMonitor.install_proxy_handler(proxy_info)
109+ try:
110+ ProxyMonitor.log.debug("ProxyMonitor: loading dconf settings")
111+ proxy_info = {}
112+ # Taken from http://forum.compiz.org/viewtopic.php?t=9480
113+ if settings.get_boolean("enabled"):
114+ proxy_info['host'] = settings.get_string("host")
115+ proxy_info['port'] = settings.get_int("port")
116+ if settings.get_boolean("use-authentication"):
117+ proxy_info['user'] = settings.get_string("authentication-user")
118+ proxy_info['pass'] = settings.get_string("authentication-password")
119+
120+ ProxyMonitor.install_proxy_handler(proxy_info)
121+
122+ except Exception, e:
123+ ProxyMonitor.log.error("ProxyMonitor: %s" % e)
124+ ProxyMonitor.log.debug(traceback.format_exc(e))
125
126 @staticmethod
127 def gconf_proxy_changed(client, cnxn_id=None, entry=None, data=None):
128 """
129 Loads gconf hhtp proxy settings
130 """
131- ProxyMonitor.log.debug("ProxyMonitor: loading gconf settings")
132- proxy_info = {}
133- # Taken from http://forum.compiz.org/viewtopic.php?t=9480
134- if client.get_bool("/system/http_proxy/use_http_proxy"):
135- proxy_info['host'] = client.get_string("/system/http_proxy/host")
136- proxy_info['port'] = client.get_int("/system/http_proxy/port")
137- if client.get_bool("/system/http_proxy/use_authentication"):
138- proxy_info['user'] = client.get_string("/system/http_proxy/authentication_user")
139- proxy_info['pass'] = client.get_string("/system/http_proxy/authentication_password")
140-
141- ProxyMonitor.install_proxy_handler(proxy_info)
142+ try:
143+ ProxyMonitor.log.debug("ProxyMonitor: loading gconf settings")
144+ proxy_info = {}
145+ # Taken from http://forum.compiz.org/viewtopic.php?t=9480
146+ if client.get_bool("/system/http_proxy/use_http_proxy"):
147+ proxy_info['host'] = client.get_string("/system/http_proxy/host")
148+ proxy_info['port'] = client.get_int("/system/http_proxy/port")
149+ if client.get_bool("/system/http_proxy/use_authentication"):
150+ proxy_info['user'] = client.get_string("/system/http_proxy/authentication_user")
151+ proxy_info['pass'] = client.get_string("/system/http_proxy/authentication_password")
152+
153+ ProxyMonitor.install_proxy_handler(proxy_info)
154+
155+ except Exception, e:
156+ ProxyMonitor.log.error("ProxyMonitor: %s" % e)
157+ ProxyMonitor.log.debug(traceback.format_exc(e))
158
159 @staticmethod
160 def install_proxy_handler(proxy_info):
161 """
162 Installs http proxy support in urllib2
163 """
164+ # validate data
165+ if 'host' in proxy_info:
166+ if proxy_info['host'] is not None:
167+ proxy_info['host'] = proxy_info['host'].strip()
168+ if not proxy_info['host']:
169+ ProxyMonitor.log.error("ProxyMonitor: empty proxy host!")
170+ proxy_info.pop('host')
171+ proxy_info.pop('port')
172+ elif not proxy_info['port']:
173+ ProxyMonitor.log.error("ProxyMonitor: invalid proxy port!")
174+ proxy_info.pop('host')
175+ proxy_info.pop('port')
176+
177+ if 'host' in proxy_info and 'user' in proxy_info:
178+ if proxy_info['user'] is not None:
179+ proxy_info['user'] = proxy_info['user'].strip()
180+ if proxy_info['pass'] is not None:
181+ proxy_info['pass'] = proxy_info['pass'].strip()
182+ else:
183+ proxy_info['pass'] = ""
184+ if not proxy_info['user']:
185+ ProxyMonitor.log.error("ProxyMonitor: empty proxy user name!")
186+ proxy_info.pop('user')
187+ proxy_info.pop('pass')
188+ proxy_info.pop('host')
189+
190+ # create proxy handler
191 if 'host' not in proxy_info:
192 ProxyMonitor.log.debug("ProxyMonitor: using direct connection")
193 proxy_support = urllib2.ProxyHandler({})
194-
195+
196 elif 'user' not in proxy_info:
197- ProxyMonitor.log.debug("ProxyMonitor: using simple proxy")
198- if proxy_info['host'] is None:
199- ProxyMonitor.log.error("ProxyMonitor: invalid proxy host")
200- return
201- if proxy_info['port'] == 0:
202- ProxyMonitor.log.error("ProxyMonitor: invalid proxy port")
203- return
204+ ProxyMonitor.log.debug("ProxyMonitor: using simple proxy: " + \
205+ "%(host)s:%(port)d" % proxy_info)
206 proxy_support = urllib2.ProxyHandler({
207 'http': "http://%(host)s:%(port)d" % proxy_info})
208-
209 else:
210- ProxyMonitor.log.debug("ProxyMonitor: using proxy with auth")
211- if proxy_info['user'] is None:
212- ProxyMonitor.log.error("ProxyMonitor: invalid proxy user")
213- return
214- if proxy_info['pass'] is None:
215- ProxyMonitor.log.error("ProxyMonitor: invalid proxy pass")
216- return
217+ ProxyMonitor.log.debug("ProxyMonitor: using proxy with auth: " + \
218+ "%(user)s@%(host)s:%(port)d" % proxy_info)
219 proxy_support = urllib2.ProxyHandler({
220 'http': "http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info})
221-
222- #disable this logging as it might log the password
223- #ProxyMonitor.log.debug("Proxy info: %s" % proxy_info)
224+
225+ # install new urllib2 opener
226 opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
227 urllib2.install_opener(opener)
228
229@@ -197,19 +226,22 @@
230 @staticmethod
231 def monitor_indicator_datetime(log):
232 TimeFormatter.log = log
233- for schema in TimeFormatter.SCHEMAS:
234- try:
235+ try:
236+ for schema in TimeFormatter.SCHEMAS:
237 if schema in DCONF_SCHEMAS:
238 log.debug("TimeFormatter: loading indicator-datetime settings: %s" % schema)
239 TimeFormatter.settings = Gio.Settings.new(schema)
240 TimeFormatter.calc_format(TimeFormatter.settings)
241 TimeFormatter.settings.connect("changed", TimeFormatter.calc_format)
242 break
243- else:
244- raise Exception;
245- except:
246+ # this else belongs to for loop
247+ else:
248 log.debug("TimeFormatter: indicator-datetime settings not found")
249
250+ except Exception, e:
251+ log.error("TimeFormatter: %s" % e)
252+ log.debug(traceback.format_exc(e))
253+
254 @staticmethod
255 def format_time(t):
256 """ do the format """

Subscribers

People subscribed via source and target branches