From 75a3a25e91df16cfb3408304adbae43aad59b619 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 21 Aug 2017 15:10:57 +0100 Subject: [PATCH 1/2] Add modelFieldsOnTemplates action --- AnkiConnect.py | 37 +++++++++++++++++++++++++++++++++++++ README.md | 29 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/AnkiConnect.py b/AnkiConnect.py index 8dececb..391e6ce 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -21,6 +21,7 @@ import hashlib import inspect import json import os.path +import re import select import socket import sys @@ -527,6 +528,37 @@ class AnkiBridge: return [field['name'] for field in model['flds']] + def modelFieldsOnTemplates(self, modelName): + model = self.collection().models.byName(modelName) + + if model is not None: + templates = {} + for template in model['tmpls']: + fields = [] + + for side in ['qfmt', 'afmt']: + fieldsForSide = [] + + # based on _fieldsOnTemplate from aqt/clayout.py + matches = re.findall('{{[^#/}]+?}}', template[side]) + for match in matches: + # remove braces and modifiers + match = re.sub(r'[{}]', '', match) + match = match.split(":")[-1] + + # for the answer side, ignore fields present on the question side + the FrontSide field + if match == 'FrontSide' or side == 'afmt' and match in fields[0]: + continue + fieldsForSide.append(match) + + + fields.append(fieldsForSide) + + templates[template['name']] = fields + + return templates + + def getDeckConfig(self, deck): if not deck in self.deckNames(): return False @@ -857,6 +889,11 @@ class AnkiConnect: return self.anki.modelFieldNames(modelName) + @webApi + def modelFieldsOnTemplates(self, modelName): + return self.anki.modelFieldsOnTemplates(modelName) + + @webApi def getDeckConfig(self, deck): return self.anki.getDeckConfig(deck) diff --git a/README.md b/README.md index 1f35bcc..8d2c1fc 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,35 @@ Below is a list of currently supported actions. Requests with invalid actions or ] ``` +* **modelFieldsOnTemplates** + + Returns an object indicating the fields on the question and answer side of each card template for the given model + name. The question side is given first in each array. + + *Sample request*: + ``` + { + "action": "modelFieldsOnTemplates", + "params": { + "modelName": "Basic (and reversed card)" + } + } + ``` + + *Sample response*: + ``` + { + "Card 1": [ + ["Front"], + ["Back"] + ], + "Card 2": [ + ["Back"], + ["Front"] + ] + } + ``` + * **getDeckConfig** Gets the config group object for the given deck. From 2e09db9dab2242f69c1979fdd5f19e9c651ba869 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 21 Aug 2017 17:15:11 +0100 Subject: [PATCH 2/2] Add modelNamesAndIds action --- AnkiConnect.py | 17 +++++++++++++++++ README.md | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/AnkiConnect.py b/AnkiConnect.py index 391e6ce..036e935 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -512,6 +512,18 @@ class AnkiBridge: return collection.models.allNames() + def modelNamesAndIds(self): + models = {} + + modelNames = self.modelNames() + for model in modelNames: + mid = self.collection().models.byName(model)['id'] + mid = int(mid) # sometimes Anki stores the ID as a string + models[model] = mid + + return models + + def modelNameFromId(self, modelId): collection = self.collection() if collection is not None: @@ -884,6 +896,11 @@ class AnkiConnect: return self.anki.modelNames() + @webApi + def modelNamesAndIds(self): + return self.anki.modelNamesAndIds() + + @webApi def modelFieldNames(self, modelName): return self.anki.modelFieldNames(modelName) diff --git a/README.md b/README.md index 8d2c1fc..05320b5 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,27 @@ Below is a list of currently supported actions. Requests with invalid actions or ] ``` +* **modelNamesAndIds** + + Gets the complete list of model names and their corresponding IDs for the current user. + + *Sample request*: + ``` + { + "action": "modelNamesAndIds" + } + ``` + + *Sample response*: + ``` + { + "Basic": 1483883011648 + "Basic (and reversed card)": 1483883011644 + "Basic (optional reversed card)": 1483883011631 + "Cloze": 1483883011630 + } + ``` + * **modelFieldNames** Gets the complete list of field names for the provided model name.