diff --git a/anki/__init__.py b/anki/__init__.py index e23a6fc97..80ae7723b 100644 --- a/anki/__init__.py +++ b/anki/__init__.py @@ -37,13 +37,15 @@ Save & close: col.close() """ -import sys +import sys, simplejson as _simplejson if sys.version_info[0] > 2: raise Exception("Anki should be run with python2.x.") elif sys.version_info[1] < 5: raise Exception("Anki requires Python 2.5+") -if sys.getfilesystemencoding().lower() in ("ascii", "ansi_x3.4-1968"): +elif sys.getfilesystemencoding().lower() in ("ascii", "ansi_x3.4-1968"): raise Exception("Anki requires a UTF-8 locale.") +elif _simplejson.__version__ < "1.7.3": + raise Exception("SimpleJSON must be 1.7.3 or later.") version = "1.99" from anki.storage import Collection diff --git a/anki/consts.py b/anki/consts.py index ee7223f80..d67dd2915 100644 --- a/anki/consts.py +++ b/anki/consts.py @@ -31,16 +31,11 @@ COUNT_REMAINING = 1 MEDIA_ADD = 0 MEDIA_REM = 1 -# syncing vars -SYNC_ZIP_SIZE = int(2.5*1024*1024) -MIME_BOUNDARY = "Anki-sync-boundary" -SYNC_URL = os.environ.get("SYNC_URL") or "https://ankiweb.net/sync/" -SYNC_VER = 0 -HTTP_CERTS = os.path.join(os.path.dirname(__file__), "ankiweb.certs") -HTTP_TIMEOUT = 30 - -# deck schema +# deck schema & syncing vars SCHEMA_VERSION = 1 +SYNC_ZIP_SIZE = int(2.5*1024*1024) +SYNC_URL = os.environ.get("SYNC_URL") or "https://beta.ankiweb.net/sync/" +SYNC_VER = 0 # Labels ########################################################################## diff --git a/anki/sync.py b/anki/sync.py index 8ab7db759..640eec359 100644 --- a/anki/sync.py +++ b/anki/sync.py @@ -7,22 +7,20 @@ from cStringIO import StringIO from datetime import date from anki.db import DB from anki.errors import * -from anki.utils import ids2str, checksum, intTime, httpCon +from anki.utils import ids2str, checksum, intTime from anki.consts import * from anki.lang import _ from hooks import runHook -if simplejson.__version__ < "1.7.3": - raise Exception("SimpleJSON must be 1.7.3 or later.") +# syncing vars +HTTP_CERTS = os.path.join(os.path.dirname(__file__), "ankiweb.certs") +HTTP_TIMEOUT = 30 -# - make sure /sync/download is compressed -# - status() should be using the hooks instead - -# todo: -# - ability to cancel -# - need to make sure syncing doesn't bump the col modified time if nothing was -# changed, since by default closing the col bumps the mod time -# - ensure the user doesn't add foreign chars to passsword +def httpCon(): + return httplib2.Http( + timeout=HTTP_TIMEOUT, ca_certs=HTTP_CERTS, + # python2 doesn't support SNI + disable_ssl_certificate_validation="beta" in SYNC_URL) # Incremental syncing ########################################################################## @@ -410,7 +408,8 @@ class HttpSyncer(object): def req(self, method, fobj=None, comp=6, badAuthRaises=True, hkey=True): - bdry = "--"+MIME_BOUNDARY + BOUNDARY="Anki-sync-boundary" + bdry = "--"+BOUNDARY buf = StringIO() # compression flag and session key as post vars vars = {} @@ -445,7 +444,7 @@ Content-Type: application/octet-stream\r\n\r\n""") size = buf.tell() # connection headers headers = { - 'Content-Type': 'multipart/form-data; boundary=%s' % MIME_BOUNDARY, + 'Content-Type': 'multipart/form-data; boundary=%s' % BOUNDARY, 'Content-Length': str(size), } body = buf.getvalue() diff --git a/anki/utils.py b/anki/utils.py index fd574c7ee..a0d5574c4 100644 --- a/anki/utils.py +++ b/anki/utils.py @@ -302,13 +302,3 @@ def call(argv, wait=True, **kwargs): isMac = sys.platform.startswith("darwin") isWin = sys.platform.startswith("win32") - -# OS helpers -############################################################################## - -def httpCon(): - disable = os.environ.get("SSL_NOVALIDATE") or False - return httplib2.Http( - timeout=HTTP_TIMEOUT, - disable_ssl_certificate_validation=disable, - ca_certs=HTTP_CERTS) diff --git a/tests/test_remote_sync.py b/tests/test_remote_sync.py index 36b5db5fe..b15f2ee3a 100644 --- a/tests/test_remote_sync.py +++ b/tests/test_remote_sync.py @@ -4,9 +4,9 @@ import nose, os, tempfile, shutil, time from tests.shared import assertException from anki.errors import * -from anki.utils import intTime, httpCon +from anki.utils import intTime from anki.sync import Syncer, FullSyncer, LocalServer, RemoteServer, \ - MediaSyncer, RemoteMediaServer + MediaSyncer, RemoteMediaServer, httpCon from anki.notes import Note from anki.cards import Card from tests.shared import getEmptyDeck