From 32bf6559ed519a458fe61af74ed26c6ff41e9d4b Mon Sep 17 00:00:00 2001 From: Scott Noyes Date: Fri, 1 May 2020 11:19:47 -0500 Subject: [PATCH 1/3] Add importPackage action --- plugin/__init__.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/plugin/__init__.py b/plugin/__init__.py index 4d1f933..bcac580 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -34,6 +34,7 @@ import anki.lang import anki.storage import aqt from anki.exporting import AnkiPackageExporter +from anki.importing import AnkiPackageImporter from . import web, util @@ -1215,6 +1216,22 @@ class AnkiConnect: return True return False + @util.api() + def importPackage(self, path): + collection = self.collection() + if collection is not None: + try: + self.startEditing() + importer = AnkiPackageImporter(collection, path) + importer.run() + except: + self.stopEditing() + raise + else: + self.stopEditing() + return True + + return False # # Entry From 6b6b8b689bf4b8d5eea3ff3f10a44f3878cc29ec Mon Sep 17 00:00:00 2001 From: Scott Noyes Date: Fri, 1 May 2020 11:21:55 -0500 Subject: [PATCH 2/3] Update README for importPackage action --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 7f1104a..cd35b14 100644 --- a/README.md +++ b/README.md @@ -323,6 +323,30 @@ guarantee that your application continues to function properly in the future. } ``` +* **importPackage** + + Imports a file in `.apkg` format into the collection. Returns `true` if successful or `false` otherwise. + Note that the file path is relative to Anki's collection.media folder, not to the client. + + *Sample request*: + ```json + { + "action": "importPackage", + "version": 6, + "params": { + "path": "/data/Deck.apkg", + } + } + ``` + + *Sample result*: + ```json + { + "result": true, + "error": null + } + ``` + #### Decks * **deckNames** From 204268b0312cb61b4ffa58a64015348050e5b517 Mon Sep 17 00:00:00 2001 From: Scott Noyes Date: Fri, 1 May 2020 13:55:30 -0500 Subject: [PATCH 3/3] Add unit test for importPackage --- tests/test_misc.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_misc.py b/tests/test_misc.py index 6d1297b..228508f 100755 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -34,6 +34,20 @@ class TestMisc(unittest.TestCase): self.assertTrue(result) self.assertTrue(os.path.exists(newname)) + # importPackage + deckName = 'importTest' + fd, newname = tempfile.mkstemp(prefix='testimport', suffix='.apkg') + os.close(fd) + os.unlink(newname) + util.invoke('createDeck', deck=deckName) + note = {'deckName': deckName, 'modelName': 'Basic', 'fields': {'Front': 'front1', 'Back': 'back1'}, 'tags': ''} + noteId = util.invoke('addNote', note=note) + util.invoke('exportPackage', deck=deckName, path=newname) + util.invoke('deleteDecks', decks=[deckName], cardsToo=True) + util.invoke('importPackage', path=newname) + deckNames = util.invoke('deckNames') + self.assertIn(deckName, deckNames) + if __name__ == '__main__': unittest.main()