platform-specific proxy pickup

This commit is contained in:
Damien Elmes 2012-05-22 13:39:23 +09:00
parent 1eaa6b9c1a
commit d143a50358

View file

@ -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"