From 717044dcad1268a96405a05e8fe0beb0e0a40546 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 16 Jun 2009 03:59:54 +0900 Subject: [PATCH] add progress handler back to full sync upload --- anki/sync.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/anki/sync.py b/anki/sync.py index 4a1bb7dc4..106913d6a 100644 --- a/anki/sync.py +++ b/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 -# amounts of memory +# amounts of memory, and track progress. + +sendProgressHook = None def incrementalSend(self, strOrFile): if self.sock is None: @@ -64,8 +66,13 @@ def incrementalSend(self, strOrFile): isinstance(strOrFile, unicode)): self.sock.sendall(strOrFile) else: + cnt = 0 while 1: + time.sleep(0.1) + if sendProgressHook: + sendProgressHook(cnt) data = strOrFile.read(65536) + cnt += len(data) if not data: break self.sock.sendall(data) @@ -76,6 +83,9 @@ def incrementalSend(self, strOrFile): httplib.HTTPConnection.send = incrementalSend +def fullSyncProgressHook(cnt): + runHook("fullSyncProgress", "fromLocal", cnt) + ########################################################################## 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]) def fullSyncFromLocal(self, fields, path): + global sendProgressHook try: # write into a temporary file, since POST needs content-length 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) try: + sendProgressHook = fullSyncProgressHook assert urllib2.urlopen(req).read() == "OK" finally: + sendProgressHook = None tmp.close() os.close(fd) finally: