mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 15:02:21 -04:00
add progress handler back to full sync upload
This commit is contained in:
parent
88dab64bfc
commit
717044dcad
1 changed files with 14 additions and 1 deletions
15
anki/sync.py
15
anki/sync.py
|
@ -49,7 +49,9 @@ SYNC_HOST = "anki.ichi2.net"; SYNC_PORT = 80
|
||||||
|
|
||||||
##########################################################################
|
##########################################################################
|
||||||
# Monkey-patch httplib to incrementally send instead of chewing up large
|
# Monkey-patch httplib to incrementally send instead of chewing up large
|
||||||
# amounts of memory
|
# amounts of memory, and track progress.
|
||||||
|
|
||||||
|
sendProgressHook = None
|
||||||
|
|
||||||
def incrementalSend(self, strOrFile):
|
def incrementalSend(self, strOrFile):
|
||||||
if self.sock is None:
|
if self.sock is None:
|
||||||
|
@ -64,8 +66,13 @@ def incrementalSend(self, strOrFile):
|
||||||
isinstance(strOrFile, unicode)):
|
isinstance(strOrFile, unicode)):
|
||||||
self.sock.sendall(strOrFile)
|
self.sock.sendall(strOrFile)
|
||||||
else:
|
else:
|
||||||
|
cnt = 0
|
||||||
while 1:
|
while 1:
|
||||||
|
time.sleep(0.1)
|
||||||
|
if sendProgressHook:
|
||||||
|
sendProgressHook(cnt)
|
||||||
data = strOrFile.read(65536)
|
data = strOrFile.read(65536)
|
||||||
|
cnt += len(data)
|
||||||
if not data:
|
if not data:
|
||||||
break
|
break
|
||||||
self.sock.sendall(data)
|
self.sock.sendall(data)
|
||||||
|
@ -76,6 +83,9 @@ def incrementalSend(self, strOrFile):
|
||||||
|
|
||||||
httplib.HTTPConnection.send = incrementalSend
|
httplib.HTTPConnection.send = incrementalSend
|
||||||
|
|
||||||
|
def fullSyncProgressHook(cnt):
|
||||||
|
runHook("fullSyncProgress", "fromLocal", cnt)
|
||||||
|
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
class SyncTools(object):
|
class SyncTools(object):
|
||||||
|
@ -967,6 +977,7 @@ and cards.id in %s""" % ids2str([c[0] for c in cards])))
|
||||||
self.fullSyncFromServer(ret[1], ret[2])
|
self.fullSyncFromServer(ret[1], ret[2])
|
||||||
|
|
||||||
def fullSyncFromLocal(self, fields, path):
|
def fullSyncFromLocal(self, fields, path):
|
||||||
|
global sendProgressHook
|
||||||
try:
|
try:
|
||||||
# write into a temporary file, since POST needs content-length
|
# write into a temporary file, since POST needs content-length
|
||||||
src = open(path, "rb")
|
src = open(path, "rb")
|
||||||
|
@ -1006,8 +1017,10 @@ and cards.id in %s""" % ids2str([c[0] for c in cards])))
|
||||||
}
|
}
|
||||||
req = urllib2.Request(SYNC_URL + "fullup", tmp, headers)
|
req = urllib2.Request(SYNC_URL + "fullup", tmp, headers)
|
||||||
try:
|
try:
|
||||||
|
sendProgressHook = fullSyncProgressHook
|
||||||
assert urllib2.urlopen(req).read() == "OK"
|
assert urllib2.urlopen(req).read() == "OK"
|
||||||
finally:
|
finally:
|
||||||
|
sendProgressHook = None
|
||||||
tmp.close()
|
tmp.close()
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
finally:
|
finally:
|
||||||
|
|
Loading…
Reference in a new issue