Beispiel #1
0
    def sync(self):
        """Sync collection to AnkiWeb"""
        if self.pm is None:
            return

        hkey = self.pm.sync_key()
        hostNum = self.pm.sync_shard()
        if not hkey:
            click.echo('No sync auth registered in profile')
            return

        # Initialize servers and sync clients
        server = RemoteServer(hkey, hostNum=hostNum)
        main_client = Syncer(self.col, server)

        # Perform main sync
        try:
            click.echo('Syncing deck ... ', nl=False)
            ret = main_client.sync()
        except Exception as e:
            if 'sync cancelled' in str(e):
                server.abort()
            click.secho('Error during sync!', fg='red')
            click.echo(e)
            raise click.Abort()

        # Parse return value
        if ret == "noChanges":
            click.echo('done (no changes)!')
        elif ret == "success":
            click.echo('done!')
        elif ret == "serverAbort":
            click.echo('aborted!')
            return
        elif ret == "fullSync":
            click.echo('aborted!')
            click.secho('Full sync required!', fg='red')
            return
        else:
            click.echo('failed!')
            click.echo(f'Message: {ret}')
            return

        # Perform media sync
        try:
            debug_output = 'media=debug' in os.environ.get('RUST_LOG', '')

            with cd(self.col.media.dir()):
                if debug_output:
                    click.echo('Syncing media:')
                else:
                    click.echo('Syncing media ... ', nl=False)
                self.col.backend.sync_media(
                    hkey, f"https://sync{hostNum}.ankiweb.net/msync/")
                if not debug_output:
                    click.echo('done!')
        except Exception as e:
            if "sync cancelled" in str(e):
                return
            raise
Beispiel #2
0
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
Beispiel #3
0
def dl():
    """return a path to a downloaded version of the collection."""
    hkey = mw.pm.profile["syncKey"]
    hostNum = mw.pm.profile.get("hostNum")
    server = RemoteServer(hkey, hostNum)
    dl = Downloader(mw.col, hkey, server.client, hostNum=hostNum)
    return dl.download()
Beispiel #4
0
    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)
Beispiel #5
0
    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, log=True)
        except:
            self.fireEvent("corrupt")
            return
        self.server = RemoteServer(self.hkey)
        self.client = Syncer(self.col, self.server)
        self.sentTotal = 0
        self.recvTotal = 0
        # throttle updates; qt doesn't handle lots of posted events well
        self.byteUpdate = time.time()

        def syncEvent(type):
            self.fireEvent("sync", type)

        def syncMsg(msg):
            self.fireEvent("syncMsg", msg)

        def canPost():
            if (time.time() - self.byteUpdate) > 0.1:
                self.byteUpdate = time.time()
                return True

        def sendEvent(bytes):
            self.sentTotal += bytes
            if canPost():
                self.fireEvent("send", str(self.sentTotal))

        def recvEvent(bytes):
            self.recvTotal += bytes
            if canPost():
                self.fireEvent("recv", str(self.recvTotal))

        addHook("sync", syncEvent)
        addHook("syncMsg", syncMsg)
        addHook("httpSend", sendEvent)
        addHook("httpRecv", recvEvent)
        # 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)
            remHook("sync", syncEvent)
            remHook("syncMsg", syncMsg)
            remHook("httpSend", sendEvent)
            remHook("httpRecv", recvEvent)
Beispiel #6
0
    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, log=True)
        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 sendEvent(bytes):
            if not self._abort:
                self.sentTotal += bytes
                self.fireEvent("send", str(self.sentTotal))
            elif self._abort == 1:
                self._abort = 2
                raise Exception("sync cancelled")

        def recvEvent(bytes):
            if not self._abort:
                self.recvTotal += bytes
                self.fireEvent("recv", str(self.recvTotal))
            elif self._abort == 1:
                self._abort = 2
                raise Exception("sync cancelled")

        addHook("sync", syncEvent)
        addHook("syncMsg", syncMsg)
        addHook("httpSend", sendEvent)
        addHook("httpRecv", recvEvent)
        # 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)
            remHook("sync", syncEvent)
            remHook("syncMsg", syncMsg)
            remHook("httpSend", sendEvent)
            remHook("httpRecv", recvEvent)
Beispiel #7
0
 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()
Beispiel #8
0
def _sync_anki(col_path, anki_hkey):
    try:
        col = Collection(col_path)

        server = RemoteServer(anki_hkey)
        client = FullSyncer(col, anki_hkey, server.client)
        client.download()
        col = Collection(col_path) # reload collection

        media_server = RemoteMediaServer(col, anki_hkey, server.client)
        media_client = MediaSyncer(col, media_server)
        media_client.sync()
        col.close(save=True)
    except:
        return traceback.format_exc()
Beispiel #9
0
 def sync(self):
     server= RemoteServer(self.pm.profile['syncKey'])
     client = Syncer(self.collection, server)
     ret = None
     try:
         print client.sync()
     except Exception, e:
         log = traceback.format_exc()
         try:
             err = unicode(e[0], "utf8", "ignore")
         except:
             # number, exception with no args, etc
             err = ""
         if "Unable to find the server" in err:
             print "offline"
             return
         else:
             if not isinstance(log, unicode):
                 err = unicode(log, "utf8", "replace")
                 print "error", log
         return
Beispiel #10
0
 def run(self):
     self.col = Collection(self.path)
     self.server = RemoteServer(self.hkey)
     self.client = Syncer(self.col, self.server)
     self.sentTotal = 0
     self.recvTotal = 0
     # throttle updates; qt doesn't handle lots of posted events well
     self.byteUpdate = time.time()
     def syncEvent(type):
         self.fireEvent("sync", type)
     def canPost():
         if (time.time() - self.byteUpdate) > 0.1:
             self.byteUpdate = time.time()
             return True
     def sendEvent(bytes):
         self.sentTotal += bytes
         if canPost():
             self.fireEvent("send", self.sentTotal)
     def recvEvent(bytes):
         self.recvTotal += bytes
         if canPost():
             self.fireEvent("recv", self.recvTotal)
     addHook("sync", syncEvent)
     addHook("httpSend", sendEvent)
     addHook("httpRecv", recvEvent)
     # run sync and catch any errors
     try:
         self._sync()
     except:
         err = traceback.format_exc()
         print err
         self.fireEvent("error", err)
     finally:
         # don't bump mod time unless we explicitly save
         self.col.close(save=False)
         remHook("sync", syncEvent)
         remHook("httpSend", sendEvent)
         remHook("httpRecv", recvEvent)
Beispiel #11
0
def setup_remote():
    setup_basic()
    # mark deck1 as changed
    ts.deck1.save()
    ts.server = RemoteServer(TEST_HKEY)
    ts.client.server = ts.server
Beispiel #12
0
def setup_remoteMedia():
    setup_basic()
    con = httpCon()
    ts.server = RemoteMediaServer(TEST_HKEY, con)
    ts.server2 = RemoteServer(TEST_HKEY)
    ts.client = MediaSyncer(ts.deck1, ts.server)
Beispiel #13
0
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)
Beispiel #14
0
    def sync(self):
        """Sync collection to AnkiWeb"""
        if self.pm is None:
            return

        import click

        if not self.pm.profile['syncKey']:
            click.echo('No sync auth registered in profile')
            return

        from anki.sync import (Syncer, MediaSyncer, RemoteServer,
                               RemoteMediaServer)

        # Initialize servers and sync clients
        hkey = self.pm.profile['syncKey']
        hostNum = self.pm.profile.get('hostNum')
        server = RemoteServer(hkey, hostNum=hostNum)
        main_client = Syncer(self.col, server)
        media_client = MediaSyncer(
            self.col,
            RemoteMediaServer(self.col, hkey, server.client, hostNum=hostNum))

        # Perform main sync
        try:
            click.echo('Syncing deck ... ', nl=False)
            ret = main_client.sync()
        except Exception as e:
            if 'sync cancelled' in str(e):
                server.abort()
            click.secho('Error during sync!', fg='red')
            click.echo(e)
            raise click.Abort()

        # Parse return value
        if ret == "noChanges":
            click.echo('done (no changes)!')
        elif ret == "success":
            click.echo('done!')
        elif ret == "serverAbort":
            click.echo('aborted!')
            return
        elif ret == "fullSync":
            click.echo('aborted!')
            click.secho('Full sync required!', fg='red')
            return
        else:
            click.echo('failed!')
            click.echo(f'Message: {ret}')
            return

        # Perform media sync
        try:
            click.echo('Syncing media ... ', nl=False)
            save_cwd = os.getcwd()
            os.chdir(self.col.media.dir())
            ret = media_client.sync()
            os.chdir(save_cwd)
        except Exception as e:
            if "sync cancelled" in str(e):
                return
            raise

        if ret == "noChanges":
            click.echo('done (no changes)!')
        elif ret in ("sanityCheckFailed", "corruptMediaDB"):
            click.echo('failed!')
        else:
            click.echo('done!')
Beispiel #15
0
def _get_hkey(anki_username, anki_password):
    try:
        return RemoteServer(None).hostKey(anki_username, anki_password)
    except:
        logging.info('anki auth with: %s %s didn\'t work' % (anki_username, anki_password))
        return None