From d143a50358510120c8ee89eb0399c6f2b93d33ec Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 22 May 2012 13:39:23 +0900 Subject: [PATCH] platform-specific proxy pickup --- aqt/profiles.py | 58 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/aqt/profiles.py b/aqt/profiles.py index a31ae2403..27f0f8c4f 100644 --- a/aqt/profiles.py +++ b/aqt/profiles.py @@ -7,7 +7,7 @@ # - Saves in sqlite rather than a flat file so the config can't be corrupted from aqt.qt import * -import os, sys, time, random, cPickle, shutil, locale, re, atexit +import os, sys, time, random, cPickle, shutil, locale, re, atexit, urllib from anki.db import DB from anki.utils import isMac, isWin, intTime, checksum from anki.lang import langs @@ -139,20 +139,7 @@ computer.""")) if name != "_global": self.name = name self.profile = prof - # export proxy settings - if prof['proxyHost'] == "off": - # force off; override environment - anki.sync.HTTP_PROXY = None - elif prof['proxyHost']: - url = prof['proxyHost'] - if url.lower().startswith("https"): - method = "https" - else: - method = "http" - anki.sync.HTTP_PROXY = ProxyInfo.from_url(url, method) - else: - # use environment - anki.sync.HTTP_PROXY = ProxyInfo.from_environment() + self.setupProxy() return True def save(self): @@ -298,3 +285,44 @@ create table if not exists profiles sql = "update profiles set data = ? where name = ?" self.db.execute(sql, cPickle.dumps(self.meta), "_global") self.db.commit() + + # Proxy handling + ###################################################################### + + def setupProxy(self): + prof = self.profile + # export proxy settings + if prof['proxyHost'] == "off": + # force off; override environment + anki.sync.HTTP_PROXY = None + elif prof['proxyHost']: + url = prof['proxyHost'] + anki.sync.HTTP_PROXY = ProxyInfo.from_url( + url, self.proxyMethod(url)) + else: + # set in env? + p = ProxyInfo.from_environment() + if not p: + # platform-specific fetch + url = None + if isWin: + r = urllib.getproxies_registry() + if 'https' in r: + url = r['https'] + elif 'http' in r: + url = r['http'] + elif isMac: + r = urllib.getproxies_macosx_sysconf() + if 'https' in r: + url = r['https'] + elif 'http' in r: + url = r['http'] + if url: + p = ProxyInfo.from_url(url, self.proxyMethod(url)) + anki.sync.HTTP_PROXY = p + + def proxyMethod(self, url): + if url.lower().startswith("https"): + return "https" + else: + return "http"