def test_download(): if not TEST_REMOTE: return f = FullSyncer(ts.client.col, "abc", ts.server.con) assertException(Exception, f.download) f.hkey = TEST_HKEY f.download()
def _fullSync(self): # tell the calling thread we need a decision on sync direction, and # wait for a reply self.fullSyncChoice = False self.localIsEmpty = self.col.isEmpty() self.fireEvent("fullSync") while not self.fullSyncChoice: time.sleep(0.1) f = self.fullSyncChoice if f == "cancel": return self.client = FullSyncer(self.col, self.hkey, self.server.client, hostNum=self.hostNum) try: if f == "upload": if not self.client.upload(): self.fireEvent("upbad") else: ret = self.client.download() if ret == "downloadClobber": self.fireEvent(ret) return except Exception as e: if "sync cancelled" in str(e): return raise # reopen db and move on to media sync self.col.reopen() self._syncMedia()
def test_remoteSync(): if not TEST_REMOTE: return # not yet associated, so will require a full sync assert ts.client.sync() == "fullSync" # upload f = FullSyncer(ts.client.col, TEST_HKEY, ts.server.con) assert f.upload() ts.client.col.reopen() # should report no changes assert ts.client.sync() == "noChanges" # bump local col ts.client.col.setMod() ts.client.col.save() assert ts.client.sync() == "success" # again, no changes assert ts.client.sync() == "noChanges"
def sync_server(self): server = RemoteServer(self.profile['syncKey'], self.profile['hostNum']) client = Syncer(self.col, server) res = client.sync() if res in ['success', 'noChanges']: pass elif res == 'fullSync': print('FULL SYNC') client = FullSyncer(self.col, self.profile['syncKey'], server.client, self.profile['hostNum']) client.download() self.col.reopen()
def sync_collection(username, password, full_sync="upload"): from anki.sync import Syncer, RemoteServer, FullSyncer, MediaSyncer, RemoteMediaServer collection = open_or_create_collection(username) server = RemoteServer(None) app.logger.info("u: %s,pass: %s" % (username, password)) hkey = server.hostKey(username, password) syncer = Syncer(collection, server) ret = syncer.sync() app.logger.info("syncer return: %s" % ret) if (ret == "fullSync"): # app.logger.info("trying to do fullSync - upload - Not tested") client = FullSyncer(collection, hkey, server.client) if full_sync == "download": client.download() else: client.upload() if ret not in ("noChanges", "fullSync", "success"): collection.close() return False mediaserver = RemoteMediaServer(collection, hkey, server.client) mediaclient = MediaSyncer(collection, mediaserver) mediaret = mediaclient.sync() app.logger.info("mediasync returned: %s" % mediaret) collection.save() collection.close() return True
def run(self): # init this first so an early crash doesn't cause an error # in the main thread self.syncMsg = "" self.uname = "" try: self.col = Collection(self.path) except: self.fireEvent("corrupt") return self.server = RemoteServer(self.hkey, hostNum=self.hostNum) self.client = Syncer(self.col, self.server) self.sentTotal = 0 self.recvTotal = 0 def syncEvent(type): self.fireEvent("sync", type) def syncMsg(msg): self.fireEvent("syncMsg", msg) def http_progress(upload: int, download: int) -> None: if not self._abort: self.sentTotal += upload self.recvTotal += download self.progress_event.emit(self.sentTotal, self.recvTotal) # type: ignore elif self._abort == 1: self._abort = 2 raise Exception("sync cancelled") self.server.client.progress_hook = http_progress hooks.sync_stage_did_change.append(syncEvent) hooks.sync_progress_did_change.append(syncMsg) # run sync and catch any errors try: self._sync() except: err = traceback.format_exc() self.fireEvent("error", err) finally: # don't bump mod time unless we explicitly save self.col.close(save=False, downgrade=False) hooks.sync_stage_did_change.remove(syncEvent) hooks.sync_progress_did_change.remove(syncMsg)
def _fullSync(self): # tell the calling thread we need a decision on sync direction, and # wait for a reply self.fullSyncChoice = False self.fireEvent("fullSync") while not self.fullSyncChoice: time.sleep(0.1) f = self.fullSyncChoice if f == "cancel": return self.client = FullSyncer(self.col, self.hkey, self.server.con) if f == "upload": self.client.upload() else: self.client.download() # reopen db and move on to media sync self.col.reopen() self._syncMedia()
def test_remoteSync(): if not TEST_REMOTE: return # not yet associated, so will require a full sync assert ts.client.sync() == "fullSync" # upload f = FullSyncer(ts.client.col, TEST_HKEY, ts.server.con) f.upload() ts.client.col.reopen() # should report no changes assert ts.client.sync() == "noChanges" # bump local col ts.client.col.setMod() ts.client.col.save() assert ts.client.sync() == "success" # again, no changes assert ts.client.sync() == "noChanges" # downloading the remote col should give us the same mod lmod = ts.client.col.mod f = FullSyncer(ts.client.col, TEST_HKEY, ts.server.con) f.download() d = aopen(ts.client.col.path) assert d.mod == lmod
def _fullSync(self): # if the local deck is empty, assume user is trying to download if self.col.isEmpty(): f = "download" else: # tell the calling thread we need a decision on sync direction, and # wait for a reply self.fullSyncChoice = False self.fireEvent("fullSync") while not self.fullSyncChoice: time.sleep(0.1) f = self.fullSyncChoice if f == "cancel": return self.client = FullSyncer(self.col, self.hkey, self.server.con) if f == "upload": if not self.client.upload(): self.fireEvent("upbad") else: self.client.download() # reopen db and move on to media sync self.col.reopen() self._syncMedia()
def _fullSync(self, server): # if the local deck is empty, assume user is trying to download if self.collection.isEmpty(): f = "download" else: # tell the calling thread we need a decision on sync direction, and # wait for a reply f = "cancel" print "decision needed" print f if f == "cancel": return client = FullSyncer(self.collection, self.pm.profile['syncKey'], server.con) if f == "upload": client.upload() else: client.download()
class SyncThread(QThread): _event = pyqtSignal(str, str) progress_event = pyqtSignal(int, int) def __init__(self, path, hkey, auth=None, hostNum=None): QThread.__init__(self) self.path = path self.hkey = hkey self.auth = auth self.hostNum = hostNum self._abort = 0 # 1=flagged, 2=aborting def flagAbort(self): self._abort = 1 def run(self): # init this first so an early crash doesn't cause an error # in the main thread self.syncMsg = "" self.uname = "" try: self.col = Collection(self.path) except: self.fireEvent("corrupt") return self.server = RemoteServer(self.hkey, hostNum=self.hostNum) self.client = Syncer(self.col, self.server) self.sentTotal = 0 self.recvTotal = 0 def syncEvent(type): self.fireEvent("sync", type) def syncMsg(msg): self.fireEvent("syncMsg", msg) def http_progress(upload: int, download: int) -> None: if not self._abort: self.sentTotal += upload self.recvTotal += download self.progress_event.emit(self.sentTotal, self.recvTotal) # type: ignore elif self._abort == 1: self._abort = 2 raise Exception("sync cancelled") self.server.client.progress_hook = http_progress hooks.sync_stage_did_change.append(syncEvent) hooks.sync_progress_did_change.append(syncMsg) # run sync and catch any errors try: self._sync() except: err = traceback.format_exc() self.fireEvent("error", err) finally: # don't bump mod time unless we explicitly save self.col.close(save=False, downgrade=False) hooks.sync_stage_did_change.remove(syncEvent) hooks.sync_progress_did_change.remove(syncMsg) def _abortingSync(self): try: return self.client.sync() except Exception as e: if "sync cancelled" in str(e): self.server.abort() raise else: raise def _sync(self): if self.auth: # need to authenticate and obtain host key self.hkey = self.server.hostKey(*self.auth) if not self.hkey: # provided details were invalid return self.fireEvent("badAuth") else: # write new details and tell calling thread to save self.fireEvent("newKey", self.hkey) # run sync and check state try: ret = self._abortingSync() except Exception as e: log = traceback.format_exc() err = repr(str(e)) if ("Unable to find the server" in err or "Errno 2" in err or "getaddrinfo" in err): self.fireEvent("offline") elif "sync cancelled" in err: pass else: self.fireEvent("error", log) return if ret == "badAuth": return self.fireEvent("badAuth") elif ret == "clockOff": return self.fireEvent("clockOff") elif ret == "basicCheckFailed" or ret == "sanityCheckFailed": return self.fireEvent("checkFailed") # full sync? if ret == "fullSync": return self._fullSync() # save and note success state if ret == "noChanges": self.fireEvent("noChanges") elif ret == "success": self.fireEvent("success") elif ret == "serverAbort": self.syncMsg = self.client.syncMsg return else: self.fireEvent("error", "Unknown sync return code.") self.syncMsg = self.client.syncMsg self.uname = self.client.uname self.hostNum = self.client.hostNum def _fullSync(self): # tell the calling thread we need a decision on sync direction, and # wait for a reply self.fullSyncChoice = False self.localIsEmpty = self.col.isEmpty() self.fireEvent("fullSync") while not self.fullSyncChoice: time.sleep(0.1) f = self.fullSyncChoice if f == "cancel": return self.client = FullSyncer(self.col, self.hkey, self.server.client, hostNum=self.hostNum) try: if f == "upload": if not self.client.upload(): self.fireEvent("upbad") else: ret = self.client.download() if ret == "downloadClobber": self.fireEvent(ret) return except Exception as e: if "sync cancelled" in str(e): return raise def fireEvent(self, cmd, arg=""): self._event.emit(cmd, arg)