From 5ec2d7e33e5a29a73ce2aafcf84f2065b059d86b Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 17 Aug 2017 13:25:06 +0100 Subject: [PATCH 1/7] Add config group actions + Move the multi action to a more sensible position --- AnkiConnect.py | 94 +++++++++++++++++++++++++--- README.md | 166 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 239 insertions(+), 21 deletions(-) diff --git a/AnkiConnect.py b/AnkiConnect.py index d31859c..d16ddd4 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -474,6 +474,13 @@ class AnkiBridge: return self.collection().sched + def multi(self, actions): + response = [] + for item in actions: + response.append(AnkiConnect.handler(ac, item)) + return response + + def media(self): collection = self.collection() if collection is not None: @@ -502,11 +509,59 @@ class AnkiBridge: return [field['name'] for field in model['flds']] - def multi(self, actions): - response = [] - for item in actions: - response.append(AnkiConnect.handler(ac, item)) - return response + def confForDeck(self, deck): + if not deck in self.deckNames(): + return False + + id = self.collection().decks.id(deck) + return self.collection().decks.confForDid(id) + + + def saveConf(self, conf): + id = str(conf['id']) + if not id in self.collection().decks.dconf: + return False + + mod = anki.utils.intTime() + usn = self.collection().usn() + + conf['mod'] = mod + conf['usn'] = usn + + self.collection().decks.dconf[id] = conf + self.collection().decks.changed = True + return True + + + def changeConf(self, decks, confId): + for deck in decks: + if not deck in self.deckNames(): + return False + + if not str(confId) in self.collection().decks.dconf: + return False + + for deck in decks: + did = str(self.collection().decks.id(deck)) + aqt.mw.col.decks.decks[did]['conf'] = confId + + return True + + + def addConf(self, name, cloneFrom=1): + if not str(cloneFrom) in self.collection().decks.dconf: + return False + + cloneFrom = self.collection().decks.getConf(cloneFrom) + return self.collection().decks.confId(name, cloneFrom) + + + def remConf(self, id): + if id == 1 or not str(id) in self.collection().decks.dconf: + return False + + self.collection().decks.remConf(id) + return True def deckNames(self): @@ -759,6 +814,11 @@ class AnkiConnect: return handler(**params) + @webApi + def multi(self, actions): + return self.anki.multi(actions) + + @webApi def deckNames(self): return self.anki.deckNames() @@ -780,8 +840,28 @@ class AnkiConnect: @webApi - def multi(self, actions): - return self.anki.multi(actions) + def confForDeck(self, deck): + return self.anki.confForDeck(deck) + + + @webApi + def saveConf(self, conf): + return self.anki.saveConf(conf) + + + @webApi + def changeConf(self, decks, confId): + return self.anki.changeConf(decks, confId) + + + @webApi + def addConf(self, name, cloneFrom=1): + return self.anki.addConf(name, cloneFrom) + + + @webApi + def remConf(self, id): + return self.anki.remConf(id) @webApi diff --git a/README.md b/README.md index d2493c3..f36c6ed 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,35 @@ Below is a list of currently supported actions. Requests with invalid actions or ``` 4 ``` + +* **multi** + + Performs multiple actions in one request, returning an array with the response of each action (in the given order). + + *Sample request*: + ``` + { + "action": "multi", + "params": { + "actions": [ + {"action": "deckNames"}, + { + "action": "browse", + "params": {"query": "deck:current"} + } + ] + } + } + ``` + + *Sample response*: + ``` + [ + ["Default"], + [1494723142483, 1494703460437, 1494703479525] + ] + ``` + * **deckNames** Gets the complete list of deck names for the current user. @@ -180,32 +209,141 @@ Below is a list of currently supported actions. Requests with invalid actions or ] ``` -* **multi** +* **confForDeck** - Performs multiple actions in one request, returning an array with the response of each action (in the given order). + Gets the config group object for the given deck. *Sample request*: ``` { - "action": "multi", + "action": "confForDeck", "params": { - "actions": [ - {"action": "deckNames"}, - { - "action": "browse", - "params": {"query": "deck:current"} - } - ] + "deck": "Default" } } ``` *Sample response*: ``` - [ - ["Default"], - [1494723142483, 1494703460437, 1494703479525] - ] + { + "lapse": { + "leechFails": 8, + "delays": [10], + "minInt": 1, + "leechAction": 0, + "mult": 0 + }, + "dyn": false, + "autoplay": true, + "mod": 1502970872, + "id": 1, + "maxTaken": 60, + "new": { + "bury": true, + "order": 1, + "initialFactor": 2500, + "perDay": 20, + "delays": [1, 10], + "separate": true, + "ints": [1, 4, 7] + }, + "name": "Default", + "rev": { + "bury": true, + "ivlFct": 1, + "ease4": 1.3, + "maxIvl": 36500, + "perDay": 100, + "minSpace": 1, + "fuzz": 0.05 + }, + "timer": 0, + "replayq": true, + "usn": -1 + } + ``` + +* **saveConf** + + Saves the given config group, returning `true` on success or `false` if the ID of the config group is invalid (i.e. + it does not exist). + + *Sample request*: + ``` + { + "action": "saveConf", + "params": { + "conf": (config group object) + } + } + ``` + + *Sample response*: + ``` + true + ``` + +* **changeConf** + + Changes the configuration group for the given decks to the one with the given ID. Returns `true` on success or + `false` if the given configuration group or any of the given decks do not exist. + + *Sample request*: + ``` + { + "action": "changeConf", + "params": { + "decks": ["Default"], + "confId": 1 + } + } + ``` + + *Sample response*: + ``` + true + ``` + +* **addConf** + + Creates a new config group with the given name, cloning from the group with the given ID, or from the default group + if this is unspecified. Returns the ID of the new config group, or `false` if the specified group to clone from does + not exist. + + *Sample request*: + ``` + { + "action": "addConf", + "params": { + "name": "Copy of Default", + "cloneFrom": 1 + } + } + ``` + + *Sample response*: + ``` + 1502972374573 + ``` + +* **remConf** + + Removes the config group with the given ID, returning `true` if successful, or `false` if attempting to remove + either the default config group (ID = 1) or a config group that does not exist. + + *Sample request*: + ``` + { + "action": "remConf", + "params": { + "id": 1502972374573 + } + } + ``` + + *Sample response*: + ``` + true ``` * **addNote** From ca6d513470458c2130a9182f266629298454c9c4 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Fri, 18 Aug 2017 20:37:46 +0100 Subject: [PATCH 2/7] Change id variables to did / configId --- AnkiConnect.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/AnkiConnect.py b/AnkiConnect.py index d16ddd4..b2f13bb 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -513,13 +513,13 @@ class AnkiBridge: if not deck in self.deckNames(): return False - id = self.collection().decks.id(deck) - return self.collection().decks.confForDid(id) + did = self.collection().decks.id(deck) + return self.collection().decks.confForDid(did) def saveConf(self, conf): - id = str(conf['id']) - if not id in self.collection().decks.dconf: + confId = str(conf['id']) + if not confId in self.collection().decks.dconf: return False mod = anki.utils.intTime() @@ -528,7 +528,7 @@ class AnkiBridge: conf['mod'] = mod conf['usn'] = usn - self.collection().decks.dconf[id] = conf + self.collection().decks.dconf[confId] = conf self.collection().decks.changed = True return True @@ -556,11 +556,11 @@ class AnkiBridge: return self.collection().decks.confId(name, cloneFrom) - def remConf(self, id): - if id == 1 or not str(id) in self.collection().decks.dconf: + def remConf(self, configId): + if configId == 1 or not str(configId) in self.collection().decks.dconf: return False - self.collection().decks.remConf(id) + self.collection().decks.remConf(configId) return True @@ -575,8 +575,8 @@ class AnkiBridge: deckNames = self.deckNames() for deck in deckNames: - id = self.collection().decks.id(deck) - decks[deck] = id + did = self.collection().decks.id(deck) + decks[deck] = did return decks @@ -637,8 +637,8 @@ class AnkiBridge: def deleteDecks(self, decks, cardsToo=False): self.startEditing() for deck in decks: - id = self.collection().decks.id(deck) - self.collection().decks.rem(id, cardsToo) + did = self.collection().decks.id(deck) + self.collection().decks.rem(did, cardsToo) self.stopEditing() @@ -860,8 +860,8 @@ class AnkiConnect: @webApi - def remConf(self, id): - return self.anki.remConf(id) + def remConf(self, configId): + return self.anki.remConf(configId) @webApi From e2c9eaaa3bd80c7c5df5a8ee5d071b8f0c9ddba0 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Fri, 18 Aug 2017 20:54:32 +0100 Subject: [PATCH 3/7] Rename actions --- AnkiConnect.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/AnkiConnect.py b/AnkiConnect.py index b2f13bb..d00fa23 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -509,7 +509,7 @@ class AnkiBridge: return [field['name'] for field in model['flds']] - def confForDeck(self, deck): + def getDeckConfig(self, deck): if not deck in self.deckNames(): return False @@ -517,7 +517,7 @@ class AnkiBridge: return self.collection().decks.confForDid(did) - def saveConf(self, conf): + def saveDeckConfig(self, conf): confId = str(conf['id']) if not confId in self.collection().decks.dconf: return False @@ -533,7 +533,7 @@ class AnkiBridge: return True - def changeConf(self, decks, confId): + def setDeckConfigId(self, decks, confId): for deck in decks: if not deck in self.deckNames(): return False @@ -548,7 +548,7 @@ class AnkiBridge: return True - def addConf(self, name, cloneFrom=1): + def cloneDeckConfigId(self, name, cloneFrom=1): if not str(cloneFrom) in self.collection().decks.dconf: return False @@ -556,7 +556,7 @@ class AnkiBridge: return self.collection().decks.confId(name, cloneFrom) - def remConf(self, configId): + def removeDeckConfigId(self, configId): if configId == 1 or not str(configId) in self.collection().decks.dconf: return False @@ -840,28 +840,28 @@ class AnkiConnect: @webApi - def confForDeck(self, deck): - return self.anki.confForDeck(deck) + def getDeckConfig(self, deck): + return self.anki.getDeckConfig(deck) @webApi - def saveConf(self, conf): - return self.anki.saveConf(conf) + def saveDeckConfig(self, conf): + return self.anki.saveDeckConfig(conf) @webApi - def changeConf(self, decks, confId): - return self.anki.changeConf(decks, confId) + def setDeckConfigId(self, decks, confId): + return self.anki.setDeckConfigId(decks, confId) @webApi - def addConf(self, name, cloneFrom=1): - return self.anki.addConf(name, cloneFrom) + def cloneDeckConfigId(self, name, cloneFrom=1): + return self.anki.cloneDeckConfigId(name, cloneFrom) @webApi - def remConf(self, configId): - return self.anki.remConf(configId) + def removeDeckConfigId(self, configId): + return self.anki.removeDeckConfigId(configId) @webApi From f980758207aed6bbf614fdc9518a0aabf65b9a65 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Fri, 18 Aug 2017 20:57:19 +0100 Subject: [PATCH 4/7] Rename actions in README --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f36c6ed..9447453 100644 --- a/README.md +++ b/README.md @@ -209,14 +209,14 @@ Below is a list of currently supported actions. Requests with invalid actions or ] ``` -* **confForDeck** +* **getDeckConfig** Gets the config group object for the given deck. *Sample request*: ``` { - "action": "confForDeck", + "action": "getDeckConfig", "params": { "deck": "Default" } @@ -263,7 +263,7 @@ Below is a list of currently supported actions. Requests with invalid actions or } ``` -* **saveConf** +* **saveDeckConfig** Saves the given config group, returning `true` on success or `false` if the ID of the config group is invalid (i.e. it does not exist). @@ -271,7 +271,7 @@ Below is a list of currently supported actions. Requests with invalid actions or *Sample request*: ``` { - "action": "saveConf", + "action": "saveDeckConfig", "params": { "conf": (config group object) } @@ -283,7 +283,7 @@ Below is a list of currently supported actions. Requests with invalid actions or true ``` -* **changeConf** +* **setDeckConfigId** Changes the configuration group for the given decks to the one with the given ID. Returns `true` on success or `false` if the given configuration group or any of the given decks do not exist. @@ -291,7 +291,7 @@ Below is a list of currently supported actions. Requests with invalid actions or *Sample request*: ``` { - "action": "changeConf", + "action": "setDeckConfigId", "params": { "decks": ["Default"], "confId": 1 @@ -304,7 +304,7 @@ Below is a list of currently supported actions. Requests with invalid actions or true ``` -* **addConf** +* **cloneDeckConfigId** Creates a new config group with the given name, cloning from the group with the given ID, or from the default group if this is unspecified. Returns the ID of the new config group, or `false` if the specified group to clone from does @@ -313,7 +313,7 @@ Below is a list of currently supported actions. Requests with invalid actions or *Sample request*: ``` { - "action": "addConf", + "action": "cloneDeckConfigId", "params": { "name": "Copy of Default", "cloneFrom": 1 @@ -326,7 +326,7 @@ Below is a list of currently supported actions. Requests with invalid actions or 1502972374573 ``` -* **remConf** +* **removeDeckConfigId** Removes the config group with the given ID, returning `true` if successful, or `false` if attempting to remove either the default config group (ID = 1) or a config group that does not exist. @@ -334,7 +334,7 @@ Below is a list of currently supported actions. Requests with invalid actions or *Sample request*: ``` { - "action": "remConf", + "action": "removeDeckConfigId", "params": { "id": 1502972374573 } From 933c7de6b977db6434a303c707895b64a3b601a2 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Fri, 18 Aug 2017 21:09:28 +0100 Subject: [PATCH 5/7] confId => configId --- AnkiConnect.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/AnkiConnect.py b/AnkiConnect.py index d00fa23..ef01195 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -518,8 +518,8 @@ class AnkiBridge: def saveDeckConfig(self, conf): - confId = str(conf['id']) - if not confId in self.collection().decks.dconf: + configId = str(conf['id']) + if not configId in self.collection().decks.dconf: return False mod = anki.utils.intTime() @@ -528,22 +528,22 @@ class AnkiBridge: conf['mod'] = mod conf['usn'] = usn - self.collection().decks.dconf[confId] = conf + self.collection().decks.dconf[configId] = conf self.collection().decks.changed = True return True - def setDeckConfigId(self, decks, confId): + def setDeckConfigId(self, decks, configId): for deck in decks: if not deck in self.deckNames(): return False - if not str(confId) in self.collection().decks.dconf: + if not str(configId) in self.collection().decks.dconf: return False for deck in decks: did = str(self.collection().decks.id(deck)) - aqt.mw.col.decks.decks[did]['conf'] = confId + aqt.mw.col.decks.decks[did]['conf'] = configId return True @@ -850,8 +850,8 @@ class AnkiConnect: @webApi - def setDeckConfigId(self, decks, confId): - return self.anki.setDeckConfigId(decks, confId) + def setDeckConfigId(self, decks, configId): + return self.anki.setDeckConfigId(decks, configId) @webApi From 33908a62973839ffadeae0ec3f472929770ff339 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Fri, 18 Aug 2017 21:10:51 +0100 Subject: [PATCH 6/7] Update README (id => configId) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9447453..02ef413 100644 --- a/README.md +++ b/README.md @@ -294,7 +294,7 @@ Below is a list of currently supported actions. Requests with invalid actions or "action": "setDeckConfigId", "params": { "decks": ["Default"], - "confId": 1 + "configId": 1 } } ``` @@ -336,7 +336,7 @@ Below is a list of currently supported actions. Requests with invalid actions or { "action": "removeDeckConfigId", "params": { - "id": 1502972374573 + "configId": 1502972374573 } } ``` From b6ab724dd718cda0d6e7122090cda30d2233ddec Mon Sep 17 00:00:00 2001 From: David Bailey Date: Fri, 18 Aug 2017 21:16:50 +0100 Subject: [PATCH 7/7] conf => config in saveDeckConfig --- AnkiConnect.py | 14 +++++++------- README.md | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/AnkiConnect.py b/AnkiConnect.py index ef01195..1d6d93e 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -517,18 +517,18 @@ class AnkiBridge: return self.collection().decks.confForDid(did) - def saveDeckConfig(self, conf): - configId = str(conf['id']) + def saveDeckConfig(self, config): + configId = str(config['id']) if not configId in self.collection().decks.dconf: return False mod = anki.utils.intTime() usn = self.collection().usn() - conf['mod'] = mod - conf['usn'] = usn + config['mod'] = mod + config['usn'] = usn - self.collection().decks.dconf[configId] = conf + self.collection().decks.dconf[configId] = config self.collection().decks.changed = True return True @@ -845,8 +845,8 @@ class AnkiConnect: @webApi - def saveDeckConfig(self, conf): - return self.anki.saveDeckConfig(conf) + def saveDeckConfig(self, config): + return self.anki.saveDeckConfig(config) @webApi diff --git a/README.md b/README.md index 02ef413..1f35bcc 100644 --- a/README.md +++ b/README.md @@ -273,7 +273,7 @@ Below is a list of currently supported actions. Requests with invalid actions or { "action": "saveDeckConfig", "params": { - "conf": (config group object) + "config": (config group object) } } ```