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