Compare commits

..

No commits in common. "master" and "23.10.29.0" have entirely different histories.

7 changed files with 396 additions and 892 deletions

89
.github/workflows/tests.yml vendored Normal file
View File

@ -0,0 +1,89 @@
name: Tests
on: [push, pull_request, workflow_dispatch]
jobs:
run-tests:
name: ${{ matrix.name }}
runs-on: ubuntu-latest
strategy:
matrix:
include:
- name: Anki 2.1.45
python: 3.8
environment: py38-anki2.1.45
- name: Anki 2.1.46
python: 3.8
environment: py38-anki2.1.46
- name: Anki 2.1.47
python: 3.8
environment: py38-anki2.1.47
- name: Anki 2.1.48
python: 3.8
environment: py38-anki2.1.48
- name: Anki 2.1.49
python: 3.8
environment: py38-anki2.1.49
- name: Anki 2.1.50 (Qt5)
python: 3.9
environment: py39-anki2.1.50-qt5
- name: Anki 2.1.50 (Qt6)
python: 3.9
environment: py39-anki2.1.50-qt6
- name: Anki 2.1.51 (Qt5)
python: 3.9
environment: py39-anki2.1.51-qt5
- name: Anki 2.1.51 (Qt6)
python: 3.9
environment: py39-anki2.1.51-qt6
- name: Anki 2.1.52 (Qt5)
python: 3.9
environment: py39-anki2.1.52-qt5
- name: Anki 2.1.52 (Qt6)
python: 3.9
environment: py39-anki2.1.52-qt6
- name: Anki 2.1.53 (Qt5)
python: 3.9
environment: py39-anki2.1.53-qt5
- name: Anki 2.1.53 (Qt6)
python: 3.9
environment: py39-anki2.1.53-qt6
- name: Anki 2.1.54 (Qt5)
python: 3.9
environment: py39-anki2.1.54-qt5
- name: Anki 2.1.54 (Qt6)
python: 3.9
environment: py39-anki2.1.54-qt6
- name: Anki 2.1.55 (Qt5)
python: 3.9
environment: py39-anki2.1.55-qt5
- name: Anki 2.1.55 (Qt6)
python: 3.9
environment: py39-anki2.1.55-qt6
- name: Anki 2.1.56 (Qt5)
python: 3.9
environment: py39-anki2.1.56-qt5
- name: Anki 2.1.56 (Qt6)
python: 3.9
environment: py39-anki2.1.56-qt6
fail-fast: false
steps:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y pyqt5-dev-tools xvfb jq
- name: Setup Python ${{ matrix.python }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
- name: Install tox
run: pip install 'tox==3.28.0'
- name: Checkout repository
uses: actions/checkout@v3
- name: Run tests
run: tox -vvve ${{ matrix.environment }} -- --forked --verbose

View File

@ -1 +0,0 @@
See https://foosoft.net/projects/contributing/ for details.

691
README.md
View File

@ -920,7 +920,7 @@ Search parameters are passed to Anki, check the docs for more information: https
#### `deleteDecks`
* Deletes decks with the given names.
* Deletes decks with the given names.
The argument `cardsToo` *must* be specified and set to `true`.
<details>
@ -1226,10 +1226,6 @@ Search parameters are passed to Anki, check the docs for more information: https
* Invokes the *Card Browser* dialog and searches for a given query. Returns an array of identifiers of the cards that
were found. Query syntax is [documented here](https://docs.ankiweb.net/searching.html).
Optionally, the `reorderCards` property can be provided to reorder the cards shown in the *Card Browser*.
This is an array including the `order` and `columnId` objects. `order` can be either `ascending` or `descending` while `columnId` can be one of several column identifiers (as documented in the [Anki source code](https://github.com/ankitects/anki/blob/main/rslib/src/browser_table.rs)).
The specified column needs to be visible in the *Card Browser*.
<details>
<summary><i>Sample request:</i></summary>
@ -1238,11 +1234,7 @@ Search parameters are passed to Anki, check the docs for more information: https
"action": "guiBrowse",
"version": 6,
"params": {
"query": "deck:current",
"reorderCards": {
"order": "descending",
"columnId": "noteCrt"
}
"query": "deck:current"
}
}
```
@ -1259,36 +1251,6 @@ Search parameters are passed to Anki, check the docs for more information: https
```
</details>
#### `guiSelectNote`
* Finds the open instance of the *Card Browser* dialog and selects a note given a note identifier.
Returns `true` if the *Card Browser* is open, `false` otherwise.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "guiSelectNote",
"version": 6,
"params": {
"note": 1494723142483
}
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": true,
"error": null
}
```
</details>
#### `guiSelectedNotes`
* Finds the open instance of the *Card Browser* dialog and returns an array of identifiers of the notes that are
@ -1988,7 +1950,7 @@ Search parameters are passed to Anki, check the docs for more information: https
</details>
<details>
<summary><i>Sample results:</i></summary>
<summary><i>Samples results:</i></summary>
```json
{
@ -2057,11 +2019,11 @@ Search parameters are passed to Anki, check the docs for more information: https
```json
{
"action": "apiReflect",
"version": 6,
"params": {
"scopes": ["actions", "invalidType"],
"actions": ["apiReflect", "invalidMethod"]
}
},
"version": 6
}
```
</details>
@ -2132,33 +2094,6 @@ Search parameters are passed to Anki, check the docs for more information: https
```
</details>
#### `getActiveProfile`
* Retrieve the active profile.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "getActiveProfile",
"version": 6
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": "User 1",
"error": null
}
```
</details>
#### `loadProfile`
* Selects the profile specified in request.
@ -2169,10 +2104,10 @@ Search parameters are passed to Anki, check the docs for more information: https
```json
{
"action": "loadProfile",
"version": 6,
"params": {
"name": "user1"
}
},
"version": 6
}
```
</details>
@ -2388,381 +2323,6 @@ Search parameters are passed to Anki, check the docs for more information: https
```
</details>
#### `findModelsById`
* Gets a list of models for the provided model IDs from the current user.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "findModelsById",
"version": 6,
"params": {
"modelIds": [1704387367119, 1704387398570]
}
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": [
{
"id": 1704387367119,
"name": "Basic",
"type": 0,
"mod": 1704387367,
"usn": -1,
"sortf": 0,
"did": null,
"tmpls": [
{
"name": "Card 1",
"ord": 0,
"qfmt": "{{Front}}",
"afmt": "{{FrontSide}}\n\n<hr id=answer>\n\n{{Back}}",
"bqfmt": "",
"bafmt": "",
"did": null,
"bfont": "",
"bsize": 0,
"id": 9176047152973362695
}
],
"flds": [
{
"name": "Front",
"ord": 0,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": 2453723143453745216,
"tag": null,
"preventDeletion": false
},
{
"name": "Back",
"ord": 1,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": -4853200230425436781,
"tag": null,
"preventDeletion": false
}
],
"css": ".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}\n",
"latexPre": "\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage[utf8]{inputenc}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n",
"latexPost": "\\end{document}",
"latexsvg": false,
"req": [
[
0,
"any",
[
0
]
]
],
"originalStockKind": 1
},
{
"id": 1704387398570,
"name": "Basic (and reversed card)",
"type": 0,
"mod": 1704387398,
"usn": -1,
"sortf": 0,
"did": null,
"tmpls": [
{
"name": "Card 1",
"ord": 0,
"qfmt": "{{Front}}",
"afmt": "{{FrontSide}}\n\n<hr id=answer>\n\n{{Back}}",
"bqfmt": "",
"bafmt": "",
"did": null,
"bfont": "",
"bsize": 0,
"id": 1689886528158874152
},
{
"name": "Card 2",
"ord": 1,
"qfmt": "{{Back}}",
"afmt": "{{FrontSide}}\n\n<hr id=answer>\n\n{{Front}}",
"bqfmt": "",
"bafmt": "",
"did": null,
"bfont": "",
"bsize": 0,
"id": -7839609225644824587
}
],
"flds": [
{
"name": "Front",
"ord": 0,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": -7787837672455357996,
"tag": null,
"preventDeletion": false
},
{
"name": "Back",
"ord": 1,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": 6364828289839985081,
"tag": null,
"preventDeletion": false
}
],
"css": ".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}\n",
"latexPre": "\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage[utf8]{inputenc}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n",
"latexPost": "\\end{document}",
"latexsvg": false,
"req": [
[
0,
"any",
[
0
]
],
[
1,
"any",
[
1
]
]
],
"originalStockKind": 1
}
],
"error": null
}
```
</details>
#### `findModelsByName`
* Gets a list of models for the provided model names from the current user.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "findModelsByName",
"version": 6,
"params": {
"modelNames": ["Basic", "Basic (and reversed card)"]
}
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": [
{
"id": 1704387367119,
"name": "Basic",
"type": 0,
"mod": 1704387367,
"usn": -1,
"sortf": 0,
"did": null,
"tmpls": [
{
"name": "Card 1",
"ord": 0,
"qfmt": "{{Front}}",
"afmt": "{{FrontSide}}\n\n<hr id=answer>\n\n{{Back}}",
"bqfmt": "",
"bafmt": "",
"did": null,
"bfont": "",
"bsize": 0,
"id": 9176047152973362695
}
],
"flds": [
{
"name": "Front",
"ord": 0,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": 2453723143453745216,
"tag": null,
"preventDeletion": false
},
{
"name": "Back",
"ord": 1,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": -4853200230425436781,
"tag": null,
"preventDeletion": false
}
],
"css": ".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}\n",
"latexPre": "\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage[utf8]{inputenc}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n",
"latexPost": "\\end{document}",
"latexsvg": false,
"req": [
[
0,
"any",
[
0
]
]
],
"originalStockKind": 1
},
{
"id": 1704387398570,
"name": "Basic (and reversed card)",
"type": 0,
"mod": 1704387398,
"usn": -1,
"sortf": 0,
"did": null,
"tmpls": [
{
"name": "Card 1",
"ord": 0,
"qfmt": "{{Front}}",
"afmt": "{{FrontSide}}\n\n<hr id=answer>\n\n{{Back}}",
"bqfmt": "",
"bafmt": "",
"did": null,
"bfont": "",
"bsize": 0,
"id": 1689886528158874152
},
{
"name": "Card 2",
"ord": 1,
"qfmt": "{{Back}}",
"afmt": "{{FrontSide}}\n\n<hr id=answer>\n\n{{Front}}",
"bqfmt": "",
"bafmt": "",
"did": null,
"bfont": "",
"bsize": 0,
"id": -7839609225644824587
}
],
"flds": [
{
"name": "Front",
"ord": 0,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": -7787837672455357996,
"tag": null,
"preventDeletion": false
},
{
"name": "Back",
"ord": 1,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": 6364828289839985081,
"tag": null,
"preventDeletion": false
}
],
"css": ".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}\n",
"latexPre": "\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage[utf8]{inputenc}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n",
"latexPost": "\\end{document}",
"latexsvg": false,
"req": [
[
0,
"any",
[
0
]
],
[
1,
"any",
[
1
]
]
],
"originalStockKind": 1
}
],
"error": null
}
```
</details>
#### `modelFieldNames`
* Gets the complete list of field names for the provided model name.
@ -2895,13 +2455,13 @@ Search parameters are passed to Anki, check the docs for more information: https
#### `createModel`
* Creates a new model to be used in Anki. User must provide the `modelName`, `inOrderFields` and `cardTemplates` to be
used in the model. There are optional fields `css` and `isCloze`. If not specified, `css` will use the default Anki css and `isCloze` will be equal to `false`. If `isCloze` is `true` then model will be created as Cloze.
used in the model. There are optional fields `css` and `isCloze`. If not specified, `css` will use the default Anki css and `isCloze` will be equal to `False`. If `isCloze` is `True` then model will be created as Cloze.
Optionally the `Name` field can be provided for each entry of `cardTemplates`. By default the
card names will be `Card 1`, `Card 2`, and so on.
<details>
<summary><i>Sample request:</i></summary>
<summary><i>Sample request</i></summary>
```json
{
@ -2925,7 +2485,7 @@ Search parameters are passed to Anki, check the docs for more information: https
</details>
<details>
<summary><i>Sample result:</i></summary>
<summary><i>Sample result</i></summary>
```json
{
@ -3015,7 +2575,7 @@ Search parameters are passed to Anki, check the docs for more information: https
</details>
<details>
<summary><i>Sample result:</i></summary>
<summary><i>Sample result</i></summary>
```json
{
@ -3053,7 +2613,7 @@ Search parameters are passed to Anki, check the docs for more information: https
</details>
<details>
<summary><i>Sample result:</i></summary>
<summary><i>Sample result</i></summary>
```json
{
@ -3213,7 +2773,7 @@ Search parameters are passed to Anki, check the docs for more information: https
```json
{
"action": "modelTemplateReposition",
"action": "modelTemplateRemove",
"version": 6,
"params": {
"modelName": "Basic",
@ -3466,7 +3026,7 @@ Search parameters are passed to Anki, check the docs for more information: https
```json
{
"action": "modelFieldSetFontSize",
"action": "modelFieldSetFont",
"version": 6,
"params": {
"modelName": "Basic",
@ -3619,36 +3179,55 @@ Search parameters are passed to Anki, check the docs for more information: https
#### `addNotes`
* Creates multiple notes using the given deck and model, with the provided field values and tags. Returns an array of
identifiers of the created notes. In the event of any errors, all errors are gathered and returned.
* Please see the documentation for `addNote` for an explanation of objects in the `notes` array.
identifiers of the created notes (notes that could not be created will have a `null` identifier). Please see the
documentation for `addNote` for an explanation of objects in the `notes` array.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action":"addNotes",
"version":6,
"params":{
"notes":[
{
"deckName":"College::PluginDev",
"modelName":"non_existent_model",
"fields":{
"Front":"front",
"Back":"bak"
"action": "addNotes",
"version": 6,
"params": {
"notes": [
{
"deckName": "Default",
"modelName": "Basic",
"fields": {
"Front": "front content",
"Back": "back content"
},
"tags": [
"yomichan"
],
"audio": [{
"url": "https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=猫&kana=ねこ",
"filename": "yomichan_ねこ_猫.mp3",
"skipHash": "7e2c2f954ef6051373ba916f000168dc",
"fields": [
"Front"
]
}],
"video": [{
"url": "https://cdn.videvo.net/videvo_files/video/free/2015-06/small_watermarked/Contador_Glam_preview.mp4",
"filename": "countdown.mp4",
"skipHash": "4117e8aab0d37534d9c8eac362388bbe",
"fields": [
"Back"
]
}],
"picture": [{
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/A_black_cat_named_Tilly.jpg/220px-A_black_cat_named_Tilly.jpg",
"filename": "black_cat.jpg",
"skipHash": "8d6e4646dfae812bf39651b59d7429ce",
"fields": [
"Back"
]
}]
}
},
{
"deckName":"College::PluginDev",
"modelName":"Basic",
"fields":{
"Front":"front",
"Back":"bak"
}
}
]
}
]
}
}
```
</details>
@ -3658,8 +3237,8 @@ Search parameters are passed to Anki, check the docs for more information: https
```json
{
"result":null,
"error":"['model was not found: non_existent_model']"
"result": [1496198395707, null],
"error": null
}
```
</details>
@ -3706,70 +3285,6 @@ Search parameters are passed to Anki, check the docs for more information: https
```
</details>
#### `canAddNotesWithErrorDetail`
* Accepts an array of objects which define parameters for candidate notes (see `addNote`) and returns an array of
objects with fields `canAdd` and `error`.
* `canAdd` indicates whether or not the parameters at the corresponding index could be used to create a new note.
* `error` contains an explanation of why a note cannot be added.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "canAddNotesWithErrorDetail",
"version": 6,
"params": {
"notes": [
{
"deckName": "Default",
"modelName": "Basic",
"fields": {
"Front": "front content",
"Back": "back content"
},
"tags": [
"yomichan"
]
},
{
"deckName": "Default",
"modelName": "Basic",
"fields": {
"Front": "front content 2",
"Back": "back content 2"
},
"tags": [
"yomichan"
]
}
]
}
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": [
{
"canAdd": false,
"error": "cannot create note because it is a duplicate"
},
{
"canAdd": true
}
],
"error": null
}
```
</details>
#### `updateNoteFields`
* Modify the fields of an existing note. You can also include audio, video, or picture files which will be added to the note with an
@ -3857,45 +3372,6 @@ Search parameters are passed to Anki, check the docs for more information: https
}
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": null,
"error": null
}
```
</details>
#### `updateNoteModel`
* Update the model, fields, and tags of an existing note.
This allows you to change the note's model, update its fields with new content, and set new tags.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "updateNoteModel",
"version": 6,
"params": {
"note": {
"id": 1514547547030,
"modelName": "NewModel",
"fields": {
"NewField1": "new field 1",
"NewField2": "new field 2",
"NewField3": "new field 3"
},
"tags": ["new", "updated", "tags"]
}
}
}
```
</details>
@ -3928,6 +3404,7 @@ Search parameters are passed to Anki, check the docs for more information: https
}
}
```
</details>
<details>
@ -3939,6 +3416,7 @@ Search parameters are passed to Anki, check the docs for more information: https
"error": null
}
```
</details>
#### `getNoteTags`
@ -3957,6 +3435,7 @@ Search parameters are passed to Anki, check the docs for more information: https
}
}
```
</details>
<details>
@ -3968,6 +3447,7 @@ Search parameters are passed to Anki, check the docs for more information: https
"error": null
}
```
</details>
#### `addTags`
@ -4174,8 +3654,8 @@ Search parameters are passed to Anki, check the docs for more information: https
#### `notesInfo`
* Returns a list of objects containing for each note ID the note fields, tags, note type, modification time,the cards belonging to
the note and the profile where the note was created.
* Returns a list of objects containing for each note ID the note fields, tags, note type and the cards belonging to
the note.
<details>
<summary><i>Sample request:</i></summary>
@ -4199,49 +3679,12 @@ Search parameters are passed to Anki, check the docs for more information: https
"result": [
{
"noteId":1502298033753,
"profile": "User_1",
"modelName": "Basic",
"tags":["tag","another_tag"],
"fields": {
"Front": {"value": "front content", "order": 0},
"Back": {"value": "back content", "order": 1}
},
"mod": 1718377864,
"cards": [1498938915662]
}
],
"error": null
}
```
</details>
s
#### `notesModTime`
* Returns a list of objects containings for each note ID the modification time.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "notesModTime",
"version": 6,
"params": {
"notes": [1502298033753]
}
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": [
{
"noteId": 1498938915662,
"mod": 1629454092
}
}
],
"error": null
@ -4387,8 +3830,8 @@ s
```json
{
"result": "<center> lots of HTML here </center>",
"error": null
"error": null,
"result": "<center> lots of HTML here </center>"
}
```
</details>

View File

@ -15,11 +15,10 @@
import aqt
required_anki_version = (23, 10, 0)
anki_version = tuple(int(segment) for segment in aqt.appVersion.split("."))
if anki_version < required_anki_version:
raise Exception(f"Minimum Anki version supported: {required_anki_version[0]}.{required_anki_version[1]}.{required_anki_version[2]}")
if anki_version < (2, 1, 45):
raise Exception("Minimum Anki version supported: 2.1.45")
import base64
import glob
@ -42,7 +41,6 @@ from anki.exporting import AnkiPackageExporter
from anki.importing import AnkiPackageImporter
from anki.notes import Note
from anki.errors import NotFoundError
from anki.scheduler.base import ScheduleCardsAsNew
from aqt.qt import Qt, QTimer, QMessageBox, QCheckBox
from .web import format_exception_reply, format_success_reply
@ -192,14 +190,14 @@ class AnkiConnect:
def getModel(self, modelName):
model = self.collection().models.by_name(modelName)
model = self.collection().models.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
return model
def getField(self, model, fieldName):
fieldMap = self.collection().models.field_map(model)
fieldMap = self.collection().models.fieldMap(model)
if fieldName not in fieldMap:
raise Exception('field was not found in {}: {}'.format(model['name'], fieldName))
return fieldMap[fieldName][1]
@ -216,19 +214,24 @@ class AnkiConnect:
self.window().requireReset()
def stopEditing(self):
if self.collection() is not None:
self.window().maybeReset()
def createNote(self, note):
collection = self.collection()
model = collection.models.by_name(note['modelName'])
model = collection.models.byName(note['modelName'])
if model is None:
raise Exception('model was not found: {}'.format(note['modelName']))
deck = collection.decks.by_name(note['deckName'])
deck = collection.decks.byName(note['deckName'])
if deck is None:
raise Exception('deck was not found: {}'.format(note['deckName']))
ankiNote = anki.notes.Note(collection, model)
ankiNote.note_type()['did'] = deck['id']
ankiNote.model()['did'] = deck['id']
if 'tags' in note:
ankiNote.tags = note['tags']
@ -311,14 +314,14 @@ class AnkiConnect:
val = note.fields[0]
if not val.strip():
return 1
csum = anki.utils.field_checksum(val)
csum = anki.utils.fieldChecksum(val)
# Create dictionary of deck ids
dids = None
if duplicateScope == 'deck':
did = deck['id']
if duplicateScopeDeckName is not None:
deck2 = collection.decks.by_name(duplicateScopeDeckName)
deck2 = collection.decks.byName(duplicateScopeDeckName)
if deck2 is None:
# Invalid deck, so cannot be duplicate
return 0
@ -359,13 +362,13 @@ class AnkiConnect:
def getCard(self, card_id: int) -> Card:
try:
return self.collection().get_card(card_id)
return self.collection().getCard(card_id)
except NotFoundError:
self.raiseNotFoundError('Card was not found: {}'.format(card_id))
def getNote(self, note_id: int) -> Note:
try:
return self.collection().get_note(note_id)
return self.collection().getNote(note_id)
except NotFoundError:
self.raiseNotFoundError('Note was not found: {}'.format(note_id))
@ -418,20 +421,14 @@ class AnkiConnect:
msg.setText('"{}" requests permission to use Anki through AnkiConnect. Do you want to give it access?'.format(origin))
msg.setInformativeText("By granting permission, you'll allow the website to modify your collection on your behalf, including the execution of destructive actions such as deck deletion.")
msg.setWindowIcon(self.window().windowIcon())
msg.setIcon(QMessageBox.Icon.Question)
msg.setStandardButtons(QMessageBox.StandardButton.Yes|QMessageBox.StandardButton.No)
msg.setDefaultButton(QMessageBox.StandardButton.No)
msg.setIcon(QMessageBox.Question)
msg.setStandardButtons(QMessageBox.Yes|QMessageBox.No)
msg.setDefaultButton(QMessageBox.No)
msg.setCheckBox(QCheckBox(text='Ignore further requests from "{}"'.format(origin), parent=msg))
if hasattr(Qt, 'WindowStaysOnTopHint'):
# Qt5
WindowOnTopFlag = Qt.WindowStaysOnTopHint
elif hasattr(Qt, 'WindowType') and hasattr(Qt.WindowType, 'WindowStaysOnTopHint'):
# Qt6
WindowOnTopFlag = Qt.WindowType.WindowStaysOnTopHint
msg.setWindowFlags(WindowOnTopFlag)
pressedButton = msg.exec()
msg.setWindowFlags(Qt.WindowStaysOnTopHint)
pressedButton = msg.exec_()
if pressedButton == QMessageBox.StandardButton.Yes:
if pressedButton == QMessageBox.Yes:
config = aqt.mw.addonManager.getConfig(__name__)
config["webCorsOriginList"] = util.setting('webCorsOriginList')
config["webCorsOriginList"].append(origin)
@ -443,7 +440,7 @@ class AnkiConnect:
}
# if the origin isn't an empty string, the user clicks "No", and the ignore box is checked
elif origin and pressedButton == QMessageBox.StandardButton.No and msg.checkBox().isChecked():
elif origin and pressedButton == QMessageBox.No and msg.checkBox().isChecked():
config = aqt.mw.addonManager.getConfig(__name__)
config["ignoreOriginList"] = util.setting('ignoreOriginList')
config["ignoreOriginList"].append(origin)
@ -457,10 +454,7 @@ class AnkiConnect:
@util.api()
def getProfiles(self):
return self.window().pm.profiles()
@util.api()
def getActiveProfile(self):
return self.window().pm.name
@util.api()
def loadProfile(self, name):
@ -492,15 +486,7 @@ class AnkiConnect:
@util.api()
def sync(self):
mw = self.window()
auth = mw.pm.sync_auth()
if not auth:
raise Exception("sync: auth not configured")
out = mw.col.sync_collection(auth, mw.pm.media_syncing_enabled())
accepted_sync_statuses = [out.NO_CHANGES, out.NORMAL_SYNC]
if out.required not in accepted_sync_statuses:
raise Exception(f"Sync status {out.required} not one of {accepted_sync_statuses} - see SyncCollectionResponse.ChangesRequired for list of sync statuses: https://github.com/ankitects/anki/blob/e41c4573d789afe8b020fab5d9d1eede50c3fa3d/proto/anki/sync.proto#L57-L65")
mw.onSync()
self.window().onSync()
@util.api()
@ -531,7 +517,7 @@ class AnkiConnect:
@util.api()
def deckNames(self):
return [x.name for x in self.decks().all_names_and_ids()]
return self.decks().allNames()
@util.api()
@ -559,8 +545,13 @@ class AnkiConnect:
@util.api()
def createDeck(self, deck):
self.startEditing()
return self.decks().id(deck)
try:
self.startEditing()
did = self.decks().id(deck)
finally:
self.stopEditing()
return did
@util.api()
@ -568,7 +559,7 @@ class AnkiConnect:
self.startEditing()
did = self.collection().decks.id(deck)
mod = anki.utils.int_time()
mod = anki.utils.intTime()
usn = self.collection().usn()
# normal cards
@ -578,6 +569,7 @@ class AnkiConnect:
# then move into new deck
self.collection().db.execute('update cards set usn=?, mod=?, did=? where id in ' + scids, usn, mod, did)
self.stopEditing()
@util.api()
@ -591,11 +583,14 @@ class AnkiConnect:
# this is dangerous, so let's raise our own exception
raise Exception("Since Anki 2.1.28 it's not possible "
"to delete decks without deleting cards as well")
self.startEditing()
decks = filter(lambda d: d in self.deckNames(), decks)
for deck in decks:
did = self.decks().id(deck)
self.decks().remove([did])
try:
self.startEditing()
decks = filter(lambda d: d in self.deckNames(), decks)
for deck in decks:
did = self.decks().id(deck)
self.decks().rem(did, cardsToo=cardsToo)
finally:
self.stopEditing()
@util.api()
@ -605,7 +600,7 @@ class AnkiConnect:
collection = self.collection()
did = collection.decks.id(deck)
return collection.decks.config_dict_for_deck_id(did)
return collection.decks.confForDid(did)
@util.api()
@ -613,13 +608,13 @@ class AnkiConnect:
collection = self.collection()
config['id'] = str(config['id'])
config['mod'] = anki.utils.int_time()
config['mod'] = anki.utils.intTime()
config['usn'] = collection.usn()
if int(config['id']) not in [c['id'] for c in collection.decks.all_config()]:
return False
try:
collection.decks.save(config)
collection.decks.update_config(config)
collection.decks.updateConf(config)
except:
return False
return True
@ -653,8 +648,8 @@ class AnkiConnect:
if configId not in [c['id'] for c in collection.decks.all_config()]:
return False
config = collection.decks.get_config(configId)
return collection.decks.add_config_returning_id(name, config)
config = collection.decks.getConf(configId)
return collection.decks.confId(name, config)
@util.api()
@ -663,7 +658,7 @@ class AnkiConnect:
if int(configId) not in [c['id'] for c in collection.decks.all_config()]:
return False
collection.decks.remove_config(configId)
collection.decks.remConf(configId)
return True
@util.api()
@ -727,7 +722,10 @@ class AnkiConnect:
@util.api()
def deleteMediaFile(self, filename):
self.media().trash_files([filename])
try:
self.media().syncDelete(filename)
except AttributeError:
self.media().trash_files([filename])
@util.api()
def getMediaDirPath(self):
@ -742,6 +740,8 @@ class AnkiConnect:
nCardsAdded = collection.addNote(ankiNote)
if nCardsAdded < 1:
raise Exception('The field values you have provided would make an empty question on all cards.')
collection.autosave()
self.stopEditing()
return ankiNote.id
@ -799,17 +799,6 @@ class AnkiConnect:
except:
return False
@util.api()
def canAddNoteWithErrorDetail(self, note):
try:
return {
'canAdd': bool(self.createNote(note))
}
except Exception as e:
return {
'canAdd': False,
'error': str(e)
}
@util.api()
def updateNoteFields(self, note):
@ -820,9 +809,19 @@ class AnkiConnect:
if name in ankiNote:
ankiNote[name] = value
self.addMediaFromNote(ankiNote, note)
audioObjectOrList = note.get('audio')
self.addMedia(ankiNote, audioObjectOrList, util.MediaType.Audio)
self.collection().update_note(ankiNote, skip_undo_entry=True);
videoObjectOrList = note.get('video')
self.addMedia(ankiNote, videoObjectOrList, util.MediaType.Video)
pictureObjectOrList = note.get('picture')
self.addMedia(ankiNote, pictureObjectOrList, util.MediaType.Picture)
ankiNote.flush()
self.collection().autosave()
self.stopEditing()
@util.api()
@ -837,57 +836,6 @@ class AnkiConnect:
if not updated:
raise Exception('Must provide a "fields" or "tags" property.')
@util.api()
def updateNoteModel(self, note):
"""
Update the model and fields of a given note.
:param note: A dictionary containing note details, including 'id', 'modelName', 'fields', and 'tags'.
"""
# Extract and validate the note ID
note_id = note.get('id')
if not note_id:
raise ValueError("Note ID is required")
# Extract and validate the new model name
new_model_name = note.get('modelName')
if not new_model_name:
raise ValueError("Model name is required")
# Extract and validate the new fields
new_fields = note.get('fields')
if not new_fields or not isinstance(new_fields, dict):
raise ValueError("Fields must be provided as a dictionary")
# Extract the new tags
new_tags = note.get('tags', [])
# Get the current note from the collection
anki_note = self.getNote(note_id)
# Get the new model from the collection
collection = self.collection()
new_model = collection.models.by_name(new_model_name)
if not new_model:
raise ValueError(f"Model '{new_model_name}' not found")
# Update the note's model
anki_note.mid = new_model['id']
anki_note._fmap = collection.models.field_map(new_model)
anki_note.fields = [''] * len(new_model['flds'])
# Update the fields with new values
for name, value in new_fields.items():
for anki_name in anki_note.keys():
if name.lower() == anki_name.lower():
anki_note[anki_name] = value
break
# Update the tags
anki_note.tags = new_tags
# Update note to ensure changes are saved
collection.update_note(anki_note, skip_undo_entry=True);
@util.api()
def updateNoteTags(self, note, tags):
@ -911,6 +859,7 @@ class AnkiConnect:
def addTags(self, notes, tags, add=True):
self.startEditing()
self.collection().tags.bulkAdd(notes, tags, add)
self.stopEditing()
@util.api()
@ -938,10 +887,10 @@ class AnkiConnect:
except NotFoundError:
continue
if note.has_tag(tag_to_replace):
note.remove_tag(tag_to_replace)
note.add_tag(replace_with_tag)
self.collection().update_note(note, skip_undo_entry=True);
if note.hasTag(tag_to_replace):
note.delTag(tag_to_replace)
note.addTag(replace_with_tag)
note.flush()
self.window().requireReset()
self.window().progress.finish()
@ -955,10 +904,10 @@ class AnkiConnect:
collection = self.collection()
for nid in collection.db.list('select id from notes'):
note = self.getNote(nid)
if note.has_tag(tag_to_replace):
note.remove_tag(tag_to_replace)
note.add_tag(replace_with_tag)
self.collection().update_note(note, skip_undo_entry=True);
if note.hasTag(tag_to_replace):
note.delTag(tag_to_replace)
note.addTag(replace_with_tag)
note.flush()
self.window().requireReset()
self.window().progress.finish()
@ -977,7 +926,7 @@ class AnkiConnect:
couldSetEaseFactors.append(True)
ankiCard.factor = easeFactors[i]
self.collection().update_card(ankiCard, skip_undo_entry=True)
ankiCard.flush()
return couldSetEaseFactors
@ -1007,7 +956,7 @@ class AnkiConnect:
ankiCard = self.getCard(card)
for i, key in enumerate(keys):
setattr(ankiCard, key, newValues[i])
self.collection().update_card(ankiCard, skip_undo_entry=True)
ankiCard.flush()
result.append(True)
except Exception as e:
result.append([False, str(e)])
@ -1044,6 +993,7 @@ class AnkiConnect:
scheduler.suspendCards(cards)
else:
scheduler.unsuspendCards(cards)
self.stopEditing()
return True
@ -1105,7 +1055,7 @@ class AnkiConnect:
@util.api()
def modelNames(self):
return [n.name for n in self.collection().models.all_names_and_ids()]
return self.collection().models.allNames()
@util.api()
@ -1115,7 +1065,7 @@ class AnkiConnect:
raise Exception('Must provide at least one field for inOrderFields')
if len(cardTemplates) == 0:
raise Exception('Must provide at least one card for cardTemplates')
if modelName in [n.name for n in self.collection().models.all_names_and_ids()]:
if modelName in self.collection().models.allNames():
raise Exception('Model name already exists')
collection = self.collection()
@ -1128,7 +1078,7 @@ class AnkiConnect:
# Create fields and add them to Note
for field in inOrderFields:
fm = mm.new_field(field)
fm = mm.newField(field)
mm.addField(m, fm)
# Add shared css to model if exists. Use default otherwise
@ -1142,7 +1092,7 @@ class AnkiConnect:
if 'Name' in card:
cardName = card['Name']
t = mm.new_template(cardName)
t = mm.newTemplate(cardName)
cardCount += 1
t['qfmt'] = card['Front']
t['afmt'] = card['Back']
@ -1156,33 +1106,11 @@ class AnkiConnect:
def modelNamesAndIds(self):
models = {}
for model in self.modelNames():
models[model] = int(self.collection().models.by_name(model)['id'])
models[model] = int(self.collection().models.byName(model)['id'])
return models
@util.api()
def findModelsById(self, modelIds):
models = []
for id in modelIds:
model = self.collection().models.get(id)
if model is None:
raise Exception("model was not found: {}".format(id))
else:
models.append(model)
return models
@util.api()
def findModelsByName(self, modelNames):
models = []
for name in modelNames:
model = self.collection().models.by_name(name)
if model is None:
raise Exception("model was not found: {}".format(name))
else:
models.append(model)
return models
@util.api()
def modelNameFromId(self, modelId):
model = self.collection().models.get(modelId)
@ -1194,7 +1122,7 @@ class AnkiConnect:
@util.api()
def modelFieldNames(self, modelName):
model = self.collection().models.by_name(modelName)
model = self.collection().models.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
else:
@ -1203,7 +1131,7 @@ class AnkiConnect:
@util.api()
def modelFieldDescriptions(self, modelName):
model = self.collection().models.by_name(modelName)
model = self.collection().models.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
else:
@ -1231,7 +1159,7 @@ class AnkiConnect:
@util.api()
def modelFieldsOnTemplates(self, modelName):
model = self.collection().models.by_name(modelName)
model = self.collection().models.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
@ -1262,7 +1190,7 @@ class AnkiConnect:
@util.api()
def modelTemplates(self, modelName):
model = self.collection().models.by_name(modelName)
model = self.collection().models.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
@ -1275,7 +1203,7 @@ class AnkiConnect:
@util.api()
def modelStyling(self, modelName):
model = self.collection().models.by_name(modelName)
model = self.collection().models.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
@ -1285,7 +1213,7 @@ class AnkiConnect:
@util.api()
def updateModelTemplates(self, model):
models = self.collection().models
ankiModel = models.by_name(model['name'])
ankiModel = models.byName(model['name'])
if ankiModel is None:
raise Exception('model was not found: {}'.format(model['name']))
@ -1307,7 +1235,7 @@ class AnkiConnect:
@util.api()
def updateModelStyling(self, model):
models = self.collection().models
ankiModel = models.by_name(model['name'])
ankiModel = models.byName(model['name'])
if ankiModel is None:
raise Exception('model was not found: {}'.format(model['name']))
@ -1321,13 +1249,13 @@ class AnkiConnect:
if not modelName:
ankiModel = self.collection().models.allNames()
else:
model = self.collection().models.by_name(modelName)
model = self.collection().models.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
ankiModel = [modelName]
updatedModels = 0
for model in ankiModel:
model = self.collection().models.by_name(model)
model = self.collection().models.byName(model)
checkForText = False
if css and findText in model['css']:
checkForText = True
@ -1416,7 +1344,7 @@ class AnkiConnect:
model = self.getModel(modelName)
field = self.getField(model, fieldName)
mm.reposition_field(model, field, index)
mm.repositionField(model, field, index)
self.save_model(mm, model)
@ -1427,16 +1355,16 @@ class AnkiConnect:
model = self.getModel(modelName)
# only adds the field if it doesn't already exist
fieldMap = mm.field_map(model)
fieldMap = mm.fieldMap(model)
if fieldName not in fieldMap:
field = mm.new_field(fieldName)
field = mm.newField(fieldName)
mm.addField(model, field)
# repositions, even if the field already exists
if index is not None:
fieldMap = mm.field_map(model)
fieldMap = mm.fieldMap(model)
newField = fieldMap[fieldName][1]
mm.reposition_field(model, newField, index)
mm.repositionField(model, newField, index)
self.save_model(mm, model)
@ -1447,7 +1375,7 @@ class AnkiConnect:
model = self.getModel(modelName)
field = self.getField(model, fieldName)
mm.remove_field(model, field)
mm.removeField(model, field)
self.save_model(mm, model)
@ -1510,7 +1438,7 @@ class AnkiConnect:
if query is None:
return []
return list(map(int, self.collection().find_notes(query)))
return list(map(int, self.collection().findNotes(query)))
@util.api()
@ -1518,7 +1446,7 @@ class AnkiConnect:
if query is None:
return []
return list(map(int, self.collection().find_cards(query)))
return list(map(int, self.collection().findCards(query)))
@util.api()
@ -1527,15 +1455,13 @@ class AnkiConnect:
for cid in cards:
try:
card = self.getCard(cid)
model = card.note_type()
model = card.model()
note = card.note()
fields = {}
for info in model['flds']:
order = info['ord']
name = info['name']
fields[name] = {'value': note.fields[order], 'order': order}
states = self.collection()._backend.get_scheduling_states(card.id)
nextReviews = self.collection()._backend.describe_next_states(states)
result.append({
'cardId': card.id,
@ -1559,8 +1485,6 @@ class AnkiConnect:
'lapses': card.lapses,
'left': card.left,
'mod': card.mod,
'nextReviews': list(nextReviews),
'flags': card.flags,
})
except NotFoundError:
# Anki will give a NotFoundError if the card ID does not exist.
@ -1589,23 +1513,21 @@ class AnkiConnect:
result.append({})
return result
@util.api()
def forgetCards(self, cards):
self.startEditing()
request = ScheduleCardsAsNew(
card_ids=cards,
log=True,
restore_position=True,
reset_counts=False,
context=None,
)
self.collection()._backend.schedule_cards_as_new(request)
scids = anki.utils.ids2str(cards)
self.collection().db.execute('update cards set type=0, queue=0, left=0, ivl=0, due=0, odue=0, factor=0 where id in ' + scids)
self.stopEditing()
@util.api()
def relearnCards(self, cards):
self.startEditing()
scids = anki.utils.ids2str(cards)
self.collection().db.execute('update cards set type=3, queue=1 where id in ' + scids)
self.stopEditing()
@util.api()
@ -1678,7 +1600,7 @@ class AnkiConnect:
for nid in notes:
try:
note = self.getNote(nid)
model = note.note_type()
model = note.model()
fields = {}
for info in model['flds']:
@ -1688,11 +1610,9 @@ class AnkiConnect:
result.append({
'noteId': note.id,
'profile': self.window().pm.name,
'tags' : note.tags,
'fields': fields,
'modelName': model['name'],
'mod': note.mod,
'cards': self.collection().db.list('select id from cards where nid = ? order by ord', note.id)
})
except NotFoundError:
@ -1704,34 +1624,20 @@ class AnkiConnect:
return result
@util.api()
def notesModTime(self, notes):
result = []
for nid in notes:
try:
note = self.getNote(nid)
result.append({
'noteId': note.id,
'mod': note.mod
})
except NotFoundError:
# Anki will give a NotFoundError if the note ID does not exist.
# Best behavior is probably to add an 'empty card' to the
# returned result, so that the items of the input and return
# lists correspond.
result.append({})
return result
@util.api()
def deleteNotes(self, notes):
self.collection().remove_notes(notes)
try:
self.collection().remNotes(notes)
finally:
self.stopEditing()
@util.api()
def removeEmptyNotes(self):
for model in self.collection().models.all():
if self.collection().models.use_count(model) == 0:
self.collection().models.remove(model["id"])
if self.collection().models.useCount(model) == 0:
self.collection().models.rem(model)
self.window().requireReset()
@ -1741,7 +1647,7 @@ class AnkiConnect:
@util.api()
def guiBrowse(self, query=None, reorderCards=None):
def guiBrowse(self, query=None):
browser = aqt.dialogs.open('Browser', self.window())
browser.activateWindow()
@ -1752,23 +1658,6 @@ class AnkiConnect:
else:
browser.onSearchActivated()
if reorderCards is not None:
if not isinstance(reorderCards, dict):
raise Exception('reorderCards should be a dict: {}'.format(reorderCards))
if not ('columnId' in reorderCards and 'order' in reorderCards):
raise Exception('Must provide a "columnId" and a "order" property"')
cardOrder = reorderCards['order']
if cardOrder not in ('ascending', 'descending'):
raise Exception('invalid card order: {}'.format(reorderCards['order']))
cardOrder = Qt.SortOrder.DescendingOrder if cardOrder == 'descending' else Qt.SortOrder.AscendingOrder
columnId = browser.table._model.active_column_index(reorderCards['columnId'])
if columnId == None:
raise Exception('invalid columnId: {}'.format(reorderCards['columnId']))
browser.table._on_sort_column_changed(columnId, cardOrder)
return self.findCards(query)
@ -1776,14 +1665,6 @@ class AnkiConnect:
def guiEditNote(self, note):
Edit.open_dialog_and_show_note_with_id(note)
@util.api()
def guiSelectNote(self, note):
(creator, instance) = aqt.dialogs._dialogs['Browser']
if instance is None:
return False
instance.table.clear_selection()
instance.table.select_single_card(note)
return True
@util.api()
def guiSelectedNotes(self):
@ -1797,18 +1678,18 @@ class AnkiConnect:
if note is not None:
collection = self.collection()
deck = collection.decks.by_name(note['deckName'])
deck = collection.decks.byName(note['deckName'])
if deck is None:
raise Exception('deck was not found: {}'.format(note['deckName']))
collection.decks.select(deck['id'])
savedMid = deck.pop('mid', None)
model = collection.models.by_name(note['modelName'])
model = collection.models.byName(note['modelName'])
if model is None:
raise Exception('model was not found: {}'.format(note['modelName']))
collection.models.set_current(model)
collection.models.setCurrent(model)
collection.models.update(model)
ankiNote = anki.notes.Note(collection, model)
@ -1867,7 +1748,7 @@ class AnkiConnect:
reviewer = self.reviewer()
card = reviewer.card
model = card.note_type()
model = card.model()
note = card.note()
fields = {}
@ -1948,7 +1829,7 @@ class AnkiConnect:
def guiDeckOverview(self, name):
collection = self.collection()
if collection is not None:
deck = collection.decks.by_name(name)
deck = collection.decks.byName(name)
if deck is not None:
collection.decks.select(deck['id'])
self.window().onOverview()
@ -2029,19 +1910,11 @@ class AnkiConnect:
@util.api()
def addNotes(self, notes):
results = []
errs = []
for note in notes:
try:
results.append(self.addNote(note))
except Exception as e:
# I specifically chose to continue, so we gather all the errors of all notes (ie not break)
errs.append(str(e))
if errs:
# Roll back the changes so on error nothing happens
self.deleteNotes(results)
raise Exception(str(errs))
except:
results.append(None)
return results
@ -2054,20 +1927,12 @@ class AnkiConnect:
return results
@util.api()
def canAddNotesWithErrorDetail(self, notes):
results = []
for note in notes:
results.append(self.canAddNoteWithErrorDetail(note))
return results
@util.api()
def exportPackage(self, deck, path, includeSched=False):
collection = self.collection()
if collection is not None:
deck = collection.decks.by_name(deck)
deck = collection.decks.byName(deck)
if deck is not None:
exporter = AnkiPackageExporter(collection)
exporter.did = deck['id']
@ -2087,8 +1952,10 @@ class AnkiConnect:
importer = AnkiPackageImporter(collection, path)
importer.run()
except:
self.stopEditing()
raise
else:
self.stopEditing()
return True
return False

View File

@ -2,8 +2,8 @@ import aqt
import aqt.editor
import aqt.browser.previewer
from aqt import gui_hooks
from aqt.qt import Qt, QKeySequence, QShortcut, QCloseEvent, QMainWindow
from aqt.utils import restoreGeom, saveGeom, tooltip
from aqt.qt import QDialog, Qt, QKeySequence, QShortcut
from aqt.utils import disable_help_button, restoreGeom, saveGeom, tooltip
from anki.errors import NotFoundError
from anki.consts import QUEUE_TYPE_SUSPENDED
from anki.utils import ids2str
@ -187,13 +187,15 @@ class Edit(aqt.editcurrent.EditCurrent):
# upon a request to open the dialog via `aqt.dialogs.open()`,
# the manager will call either the constructor or the `reopen` method
def __init__(self, note):
QMainWindow.__init__(self, None, Qt.WindowType.Window)
QDialog.__init__(self, None, Qt.WindowType.Window)
aqt.mw.garbage_collect_on_dialog_finish(self)
self.form = aqt.forms.editcurrent.Ui_Dialog()
self.form.setupUi(self)
self.setWindowTitle("Edit")
self.setMinimumWidth(250)
self.setMinimumHeight(400)
restoreGeom(self, self.dialog_geometry_tag)
disable_help_button(self)
self.form.buttonBox.setVisible(False) # hides the Close button bar
self.setup_editor_buttons()
@ -213,16 +215,14 @@ class Edit(aqt.editcurrent.EditCurrent):
self.show_note(note)
self.bring_to_foreground()
def cleanup(self):
def cleanup_and_close(self):
gui_hooks.editor_did_load_note.remove(self.editor_did_load_note)
gui_hooks.operation_did_execute.remove(self.on_operation_did_execute)
self.editor.cleanup()
saveGeom(self, self.dialog_geometry_tag)
aqt.dialogs.markClosed(self.dialog_registry_tag)
def closeEvent(self, evt: QCloseEvent) -> None:
self.editor.call_after_note_saved(self.cleanup)
QDialog.reject(self)
# This method (mostly) solves (at least on my Windows 10 machine) three issues
# with window activation. Without this not even too hacky a fix,
@ -283,7 +283,7 @@ class Edit(aqt.editcurrent.EditCurrent):
try:
self.note = history.get_last_note()
except IndexError:
self.cleanup()
self.cleanup_and_close()
return
self.show_note(self.note)

20
tox-install-command Normal file
View File

@ -0,0 +1,20 @@
#!/bin/bash
set -eux
trap '[[ -v SERVER_PID ]] && pkill -P $SERVER_PID' EXIT
print_first_group() { perl -snle 'm/$re/; print $1; exit 0' -- -re="$1"; }
envname="$1"
toxworkdir="$2"
packages=("${@:3}")
version=$(print_first_group 'anki([\d\.a-z]+)' <<< "$envname")
upload_time=$(curl https://pypi.org/pypi/anki/json \
| jq --arg v "$version" -r '.releases[$v][0].upload_time_iso_8601')
cutoff_time=$(date --utc -d "$upload_time +1 hour" '+%Y-%m-%dT%H:%M:%S')
coproc SERVER { "$toxworkdir"/.tox/bin/python -um pypi_timemachine "$cutoff_time"; }
index_url=$(print_first_group '(http\S+)' <&"${SERVER[0]}")
python -m pip install --index-url "$index_url" "anki==$version" "$AQT==$version"
python -m pip install "${packages[@]}"

86
tox.ini Normal file
View File

@ -0,0 +1,86 @@
# For testing, you will need:
# * PyQt5 dev tools
# * tox
# * X virtual framebuffer--to test without GUI
#
# Install these by running:
# $ sudo apt install pyqt5-dev-tools xvfb
# $ python3 -m pip install --user --upgrade tox
#
# Then, to run tests against multiple anki versions:
# $ tox
#
# To run tests slightly less safely, but faster:
# $ tox -- --no-tear-down-profile-after-each-test
#
# To run tests more safely, but *much* slower:
# $ tox -- --forked
# Test tool cheat sheet:
# * Test several environments in parallel:
# $ tox -p auto
#
# * To activate one of the test environments:
# $ source .tox/py38-anki49/bin/activate
#
# * Stop on first failure:
# $ xvfb-run python -m pytest -x
#
# * See stdout/stderr (doesn't work with --forked!):
# $ xvfb-run python -m pytest -s
#
# * Run some specific tests:
# $ xvfb-run python -m pytest -k "test_cards.py or test_guiBrowse"
#
# * To run with visible GUI in WSL2
# (Make sure to disable access control in your X server, such as VcXsrv):
# $ DISPLAY=$(ip route list default | awk '{print $3}'):0 python -m pytest
#
# * Environmental variables of interest:
# LIBGL_ALWAYS_INDIRECT=1
# QTWEBENGINE_CHROMIUM_FLAGS="--disable-gpu"
# QT_DEBUG_PLUGINS=1
# ANKIDEV=1
# Note: pypi packages anki and aqt do not pin their dependencies.
# To tests against historically accurate dependencies, we use a “time machine”
# that prevents pip from using packages that were uploaded after a specified date.
[tox]
minversion = 3.24
skipsdist = true
skip_install = true
requires =
pypi-timemachine
envlist =
py38-anki2.1.{45,46,47,48,49}
py39-anki2.1.{50,51,52,53,54,55,56}-qt{5,6}
[testenv:.tox]
install_command =
python -m pip install {packages}
[testenv]
install_command =
bash tox-install-command {envname} {toxworkdir} {packages}
commands =
env HOME={envtmpdir}/home xvfb-run python -m pytest {posargs}
setenv =
DISABLE_QT5_COMPAT=1
QTWEBENGINE_CHROMIUM_FLAGS=--no-sandbox
!qt{5,6}: AQT=aqt
qt5: AQT=aqt[qt5]
qt6: AQT=aqt[qt6]
allowlist_externals =
bash
env
xvfb-run
deps =
pytest==7.1.1
pytest-forked==1.4.0
pytest-anki @ git+https://github.com/oakkitten/pytest-anki.git@a0d27aa5