Support apkg export (#143)

Support apkg export
This commit is contained in:
Markus Reil 2020-03-17 12:29:49 +07:00 committed by GitHub
parent 955b749aac
commit 9ba5c08c96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.pyc *.pyc
AnkiConnect.zip AnkiConnect.zip
meta.json meta.json
.idea/

View File

@ -20,6 +20,7 @@ with the latest stable (2.1.x) releases of Anki; older versions (2.0.x and below
* [Cards](https://foosoft.net/projects/anki-connect/#cards) * [Cards](https://foosoft.net/projects/anki-connect/#cards)
* [Media](https://foosoft.net/projects/anki-connect/#media) * [Media](https://foosoft.net/projects/anki-connect/#media)
* [Graphical](https://foosoft.net/projects/anki-connect/#graphical) * [Graphical](https://foosoft.net/projects/anki-connect/#graphical)
* [Export](https://foosoft.net/projects/anki-connect/#export)
## Installation ## ## Installation ##
@ -1866,3 +1867,32 @@ guarantee that your application continues to function properly in the future.
"error": null "error": null
} }
``` ```
### Export ###
* **exportPackage**
Exports a given deck in .apkg format. Returns `true` if successful or `false` otherwise.
The optional property `includeSched` (default is `false`) can be specified to include the
cards' scheduling data.
*Sample request*:
```json
{
"action": "exportPackage",
"version": 6,
"params": {
"deck": "Default",
"path": "/data/Deck.apkg",
"includeSched": true
}
}
```
*Sample result*:
```json
{
"result": true,
"error": null
}
```

View File

@ -29,8 +29,11 @@ from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QMessageBox from PyQt5.QtWidgets import QMessageBox
import anki import anki
import anki.exporting
import anki.lang import anki.lang
import anki.storage
import aqt import aqt
from anki.exporting import AnkiPackageExporter
from . import web, util from . import web, util
@ -1198,6 +1201,27 @@ class AnkiConnect:
return results return results
@util.api()
def exportPackage(self, deck, path, includeSched=False):
results = []
results.append(self.createPackage(deck, path, includeSched))
return results
def createPackage(self, deck, path, includeSched):
collection = self.collection()
if collection is not None:
deck = collection.decks.byName(deck)
if deck is not None:
exporter = AnkiPackageExporter(collection)
exporter.did = deck["id"]
exporter.includeSched = includeSched
exporter.exportInto(path)
return True
return False
# #
# Entry # Entry
# #

21
tests/test_export.py Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env python
import os
import tempfile
import unittest
import util
class TestExport(unittest.TestCase):
def runTest(self):
fd, newname = tempfile.mkstemp(prefix="testexport", suffix=".apkg")
os.close(fd)
os.unlink(newname)
success = util.invoke('exportPackage', deck='Default', path=newname)
self.assertTrue(success)
self.assertTrue(os.path.exists(newname))
if __name__ == '__main__':
unittest.main()