Merge pull request #160 from snoyes/importPackage

Import package
This commit is contained in:
Alex Yatskov 2020-05-01 12:43:43 -07:00 committed by GitHub
commit 8c248d93ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 0 deletions

View File

@ -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**

View File

@ -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

View File

@ -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()