From aab9346deb798d64fb06d58800a2eb40024a4cde Mon Sep 17 00:00:00 2001 From: Robert Irelan Date: Fri, 21 Jun 2024 17:46:58 -0700 Subject: [PATCH] sync: return error if full sync required or auth not set up I run Anki on a headless machine (using Xvfb on Linux), and have clients interact with it using Anki Connect. Currently, if a full sync is required, I have no way of knowing about it from any error that Anki Connect returns. It's easy to detect if a full sync would be required, so return an error in this case. --- plugin/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugin/__init__.py b/plugin/__init__.py index 031ba30..d996b36 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -488,7 +488,15 @@ class AnkiConnect: @util.api() def sync(self): - self.window().onSync() + mw = self.window() + auth = mw.pm.sync_auth() + if not auth: + raise Exception("sync: auth not configured") + out = mw.col.sync_collection(auth, mw.pm.media_syncing_enabled()) + accepted_sync_statuses = [out.NO_CHANGES, out.NORMAL_SYNC] + if out.required not in accepted_sync_statuses: + raise Exception(f"Sync status {out.required} not one of {accepted_sync_statuses} - see SyncCollectionResponse.ChangesRequired for list of sync statuses: https://github.com/ankitects/anki/blob/e41c4573d789afe8b020fab5d9d1eede50c3fa3d/proto/anki/sync.proto#L57-L65") + mw.onSync() @util.api()