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
=== modified file 'indicator_weather/helpers.py'
--- indicator_weather/helpers.py 2011-04-22 19:09:19 +0000
+++ indicator_weather/helpers.py 2011-05-02 20:13:28 +0000
@@ -2,16 +2,16 @@
2### BEGIN LICENSE2### BEGIN LICENSE
3# Copyright (C) 2010 Sebastian MacDonald Sebas310@gmail.com3# Copyright (C) 2010 Sebastian MacDonald Sebas310@gmail.com
4# Copyright (C) 2010 Mehdi Rejraji mehd36@gmail.com4# Copyright (C) 2010 Mehdi Rejraji mehd36@gmail.com
5# This program is free software: you can redistribute it and/or modify it 5# This program is free software: you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 3, as published 6# under the terms of the GNU General Public License version 3, as published
7# by the Free Software Foundation.7# by the Free Software Foundation.
8# 8#
9# This program is distributed in the hope that it will be useful, but 9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranties of 10# WITHOUT ANY WARRANTY; without even the implied warranties of
11# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 11# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12# PURPOSE. See the GNU General Public License for more details.12# PURPOSE. See the GNU General Public License for more details.
13# 13#
14# You should have received a copy of the GNU General Public License along 14# You should have received a copy of the GNU General Public License along
15# with this program. If not, see <http://www.gnu.org/licenses/>.15# with this program. If not, see <http://www.gnu.org/licenses/>.
16### END LICENSE16### END LICENSE
1717
@@ -29,8 +29,10 @@
29 # this has to be called only once, otherwise we get segfaults29 # this has to be called only once, otherwise we get segfaults
30 DCONF_SCHEMAS = Gio.Settings.list_schemas()30 DCONF_SCHEMAS = Gio.Settings.list_schemas()
31except ImportError:31except ImportError:
32 pass32 DCONF_SCHEMAS = []
3333
34import gconf
35import traceback
34import os36import os
35import gtk37import gtk
36import urllib238import urllib2
@@ -45,9 +47,9 @@
45gettext.textdomain('indicator-weather')47gettext.textdomain('indicator-weather')
4648
47def get_builder(builder_file_name):49def get_builder(builder_file_name):
48 """Return a fully-instantiated gtk.Builder instance from specified ui 50 """Return a fully-instantiated gtk.Builder instance from specified ui
49 file51 file
50 52
51 :param builder_file_name: The name of the builder file, without extension.53 :param builder_file_name: The name of the builder file, without extension.
52 Assumed to be in the 'ui' directory under the data path.54 Assumed to be in the 'ui' directory under the data path.
53 """55 """
@@ -85,94 +87,121 @@
8587
86class ProxyMonitor:88class ProxyMonitor:
87 """ Class to monitor proxy settings """89 """ Class to monitor proxy settings """
88 90
89 @staticmethod91 @staticmethod
90 def monitor_proxy(log):92 def monitor_proxy(log):
91 ProxyMonitor.log = log93 ProxyMonitor.log = log
92 # try dconf
93 try:94 try:
94 if "org.gnome.system.proxy.http" in DCONF_SCHEMAS:95 # disable dconf settings for now
96 # because they do not seem to be in effect
97 if False and "org.gnome.system.proxy.http" in DCONF_SCHEMAS:
98 # load dconf settings
95 proxy_settings = Gio.Settings.new("org.gnome.system.proxy.http")99 proxy_settings = Gio.Settings.new("org.gnome.system.proxy.http")
96 ProxyMonitor.dconf_proxy_changed(proxy_settings)100 ProxyMonitor.dconf_proxy_changed(proxy_settings)
97 proxy_settings.connect("changed", ProxyMonitor.dconf_proxy_changed)101 proxy_settings.connect("changed", ProxyMonitor.dconf_proxy_changed)
98 else:102 else:
99 # make except block work103 # load gconf settings
100 raise Exception;104 client = gconf.client_get_default()
101 except:105 client.add_dir("/system/http_proxy", gconf.CLIENT_PRELOAD_ONELEVEL)
102 # try gconf106 ProxyMonitor.gconf_proxy_changed(client)
103 import gconf107 client.notify_add("/system/http_proxy", ProxyMonitor.gconf_proxy_changed)
104 client = gconf.client_get_default()108
105 client.add_dir("/system/http_proxy", gconf.CLIENT_PRELOAD_ONELEVEL)109 except Exception, e:
106 ProxyMonitor.gconf_proxy_changed(client)110 log.error("ProxyMonitor: %s" % e)
107 client.notify_add("/system/http_proxy", ProxyMonitor.gconf_proxy_changed)111 log.debug(traceback.format_exc(e))
108112
109 @staticmethod113 @staticmethod
110 def dconf_proxy_changed(settings):114 def dconf_proxy_changed(settings, changed_key=None):
111 """115 """
112 Loads dconf hhtp proxy settings116 Loads dconf hhtp proxy settings
113 """117 """
114 ProxyMonitor.log.debug("ProxyMonitor: loading dconf settings")118 try:
115 proxy_info = {}119 ProxyMonitor.log.debug("ProxyMonitor: loading dconf settings")
116 # Taken from http://forum.compiz.org/viewtopic.php?t=9480120 proxy_info = {}
117 if settings.get_boolean("enabled"):121 # Taken from http://forum.compiz.org/viewtopic.php?t=9480
118 proxy_info['host'] = settings.get_string("host")122 if settings.get_boolean("enabled"):
119 proxy_info['port'] = settings.get_int("port")123 proxy_info['host'] = settings.get_string("host")
120 if settings.get_boolean("use-authentication"):124 proxy_info['port'] = settings.get_int("port")
121 proxy_info['user'] = settings.get_string("authentication-user")125 if settings.get_boolean("use-authentication"):
122 proxy_info['pass'] = settings.get_string("authentication-password")126 proxy_info['user'] = settings.get_string("authentication-user")
123127 proxy_info['pass'] = settings.get_string("authentication-password")
124 ProxyMonitor.install_proxy_handler(proxy_info)128
129 ProxyMonitor.install_proxy_handler(proxy_info)
130
131 except Exception, e:
132 ProxyMonitor.log.error("ProxyMonitor: %s" % e)
133 ProxyMonitor.log.debug(traceback.format_exc(e))
125134
126 @staticmethod135 @staticmethod
127 def gconf_proxy_changed(client, cnxn_id=None, entry=None, data=None):136 def gconf_proxy_changed(client, cnxn_id=None, entry=None, data=None):
128 """137 """
129 Loads gconf hhtp proxy settings138 Loads gconf hhtp proxy settings
130 """139 """
131 ProxyMonitor.log.debug("ProxyMonitor: loading gconf settings")140 try:
132 proxy_info = {}141 ProxyMonitor.log.debug("ProxyMonitor: loading gconf settings")
133 # Taken from http://forum.compiz.org/viewtopic.php?t=9480142 proxy_info = {}
134 if client.get_bool("/system/http_proxy/use_http_proxy"):143 # Taken from http://forum.compiz.org/viewtopic.php?t=9480
135 proxy_info['host'] = client.get_string("/system/http_proxy/host")144 if client.get_bool("/system/http_proxy/use_http_proxy"):
136 proxy_info['port'] = client.get_int("/system/http_proxy/port")145 proxy_info['host'] = client.get_string("/system/http_proxy/host")
137 if client.get_bool("/system/http_proxy/use_authentication"):146 proxy_info['port'] = client.get_int("/system/http_proxy/port")
138 proxy_info['user'] = client.get_string("/system/http_proxy/authentication_user")147 if client.get_bool("/system/http_proxy/use_authentication"):
139 proxy_info['pass'] = client.get_string("/system/http_proxy/authentication_password")148 proxy_info['user'] = client.get_string("/system/http_proxy/authentication_user")
140149 proxy_info['pass'] = client.get_string("/system/http_proxy/authentication_password")
141 ProxyMonitor.install_proxy_handler(proxy_info)150
151 ProxyMonitor.install_proxy_handler(proxy_info)
152
153 except Exception, e:
154 ProxyMonitor.log.error("ProxyMonitor: %s" % e)
155 ProxyMonitor.log.debug(traceback.format_exc(e))
142156
143 @staticmethod157 @staticmethod
144 def install_proxy_handler(proxy_info):158 def install_proxy_handler(proxy_info):
145 """159 """
146 Installs http proxy support in urllib2160 Installs http proxy support in urllib2
147 """161 """
162 # validate data
163 if 'host' in proxy_info:
164 if proxy_info['host'] is not None:
165 proxy_info['host'] = proxy_info['host'].strip()
166 if not proxy_info['host']:
167 ProxyMonitor.log.error("ProxyMonitor: empty proxy host!")
168 proxy_info.pop('host')
169 proxy_info.pop('port')
170 elif not proxy_info['port']:
171 ProxyMonitor.log.error("ProxyMonitor: invalid proxy port!")
172 proxy_info.pop('host')
173 proxy_info.pop('port')
174
175 if 'host' in proxy_info and 'user' in proxy_info:
176 if proxy_info['user'] is not None:
177 proxy_info['user'] = proxy_info['user'].strip()
178 if proxy_info['pass'] is not None:
179 proxy_info['pass'] = proxy_info['pass'].strip()
180 else:
181 proxy_info['pass'] = ""
182 if not proxy_info['user']:
183 ProxyMonitor.log.error("ProxyMonitor: empty proxy user name!")
184 proxy_info.pop('user')
185 proxy_info.pop('pass')
186 proxy_info.pop('host')
187
188 # create proxy handler
148 if 'host' not in proxy_info:189 if 'host' not in proxy_info:
149 ProxyMonitor.log.debug("ProxyMonitor: using direct connection")190 ProxyMonitor.log.debug("ProxyMonitor: using direct connection")
150 proxy_support = urllib2.ProxyHandler({})191 proxy_support = urllib2.ProxyHandler({})
151 192
152 elif 'user' not in proxy_info:193 elif 'user' not in proxy_info:
153 ProxyMonitor.log.debug("ProxyMonitor: using simple proxy")194 ProxyMonitor.log.debug("ProxyMonitor: using simple proxy: " + \
154 if proxy_info['host'] is None:195 "%(host)s:%(port)d" % proxy_info)
155 ProxyMonitor.log.error("ProxyMonitor: invalid proxy host")
156 return
157 if proxy_info['port'] == 0:
158 ProxyMonitor.log.error("ProxyMonitor: invalid proxy port")
159 return
160 proxy_support = urllib2.ProxyHandler({196 proxy_support = urllib2.ProxyHandler({
161 'http': "http://%(host)s:%(port)d" % proxy_info})197 'http': "http://%(host)s:%(port)d" % proxy_info})
162
163 else:198 else:
164 ProxyMonitor.log.debug("ProxyMonitor: using proxy with auth")199 ProxyMonitor.log.debug("ProxyMonitor: using proxy with auth: " + \
165 if proxy_info['user'] is None:200 "%(user)s@%(host)s:%(port)d" % proxy_info)
166 ProxyMonitor.log.error("ProxyMonitor: invalid proxy user")
167 return
168 if proxy_info['pass'] is None:
169 ProxyMonitor.log.error("ProxyMonitor: invalid proxy pass")
170 return
171 proxy_support = urllib2.ProxyHandler({201 proxy_support = urllib2.ProxyHandler({
172 'http': "http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info})202 'http': "http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info})
173 203
174 #disable this logging as it might log the password204 # install new urllib2 opener
175 #ProxyMonitor.log.debug("Proxy info: %s" % proxy_info)
176 opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)205 opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
177 urllib2.install_opener(opener)206 urllib2.install_opener(opener)
178207
@@ -197,19 +226,22 @@
197 @staticmethod226 @staticmethod
198 def monitor_indicator_datetime(log):227 def monitor_indicator_datetime(log):
199 TimeFormatter.log = log228 TimeFormatter.log = log
200 for schema in TimeFormatter.SCHEMAS:229 try:
201 try:230 for schema in TimeFormatter.SCHEMAS:
202 if schema in DCONF_SCHEMAS:231 if schema in DCONF_SCHEMAS:
203 log.debug("TimeFormatter: loading indicator-datetime settings: %s" % schema)232 log.debug("TimeFormatter: loading indicator-datetime settings: %s" % schema)
204 TimeFormatter.settings = Gio.Settings.new(schema)233 TimeFormatter.settings = Gio.Settings.new(schema)
205 TimeFormatter.calc_format(TimeFormatter.settings)234 TimeFormatter.calc_format(TimeFormatter.settings)
206 TimeFormatter.settings.connect("changed", TimeFormatter.calc_format)235 TimeFormatter.settings.connect("changed", TimeFormatter.calc_format)
207 break236 break
208 else:237 # this else belongs to for loop
209 raise Exception;238 else:
210 except:
211 log.debug("TimeFormatter: indicator-datetime settings not found")239 log.debug("TimeFormatter: indicator-datetime settings not found")
212240
241 except Exception, e:
242 log.error("TimeFormatter: %s" % e)
243 log.debug(traceback.format_exc(e))
244
213 @staticmethod245 @staticmethod
214 def format_time(t):246 def format_time(t):
215 """ do the format """247 """ do the format """

Subscribers

People subscribed via source and target branches