From e999a44a47e07e5c22207e2be72bb0d64e943c72 Mon Sep 17 00:00:00 2001 From: mikkkee Date: Sat, 17 Jun 2023 17:45:39 +0800 Subject: [PATCH 1/3] Add a GUI action to bring up import file dialog with provided file path --- plugin/__init__.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/plugin/__init__.py b/plugin/__init__.py index 5ca8514..d225c3d 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -41,6 +41,7 @@ from anki.exporting import AnkiPackageExporter from anki.importing import AnkiPackageImporter from anki.notes import Note from anki.errors import NotFoundError +from aqt.import_export.importing import import_file, prompt_for_file_then_import from aqt.qt import Qt, QTimer, QMessageBox, QCheckBox from .web import format_exception_reply, format_success_reply @@ -1852,6 +1853,32 @@ class AnkiConnect: return False + @util.api() + def guiImportFile(self, path=None): + """ + Open Import File (Ctrl+Shift+I) dialog with provided file path. + If no path is given, the user will be prompted to select a file. + + path: string + import file path, note on Windows you must use forward slashes. + """ + + # Bring window to top for user to review import settings. + try: + # [Step 1/2] set always on top flag, show window (it stays on top for now) + self.window().setWindowFlags(self.window().windowFlags() | Qt.WindowStaysOnTopHint) + self.window().show() + finally: + # [Step 2/2] clear always on top flag, show window (it doesn't stay on top anymore) + self.window().setWindowFlags(self.window().windowFlags() & ~Qt.WindowStaysOnTopHint) + self.window().show() + + if path is None: + prompt_for_file_then_import(self.window()) + else: + import_file(self.window(), path) + + @util.api() def guiExitAnki(self): timer = QTimer() From cd66410ff3290dd7aec11ea75bfca40cbc02c720 Mon Sep 17 00:00:00 2001 From: mikkkee Date: Sat, 17 Jun 2023 17:52:14 +0800 Subject: [PATCH 2/3] Update documentation for new method guiImportFile --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index e7f2eb4..b6d97e0 100644 --- a/README.md +++ b/README.md @@ -1617,6 +1617,35 @@ corresponding to when the API was available for use. ``` +#### `guiImportFile` + +* Invokes the *Import... (Ctrl+Shift+I)* dialog with an optional file path. Brings up the dialog for user to review the import. Supports all file types that Anki supports. Brings open file dialog if no path is provided. Forward slashes must be used in the path on Windows. + +
+ Sample request: + + ```json + { + "action": "guiImportFile", + "version": 6, + "params": { + "path": "C:/Users/Desktop/cards.txt" + } + } + ``` +
+ +
+ Sample result: + + ```json + { + "result": null, + "error": null + } + ``` +
+ #### `guiExitAnki` * Schedules a request to gracefully close Anki. This operation is asynchronous, so it will return immediately and From 9e214e90e59f4b5230ac61d47853da5ec9e261aa Mon Sep 17 00:00:00 2001 From: mikkkee Date: Wed, 5 Jul 2023 22:17:30 +0800 Subject: [PATCH 3/3] Add a test for guiImportFile --- tests/test_graphical.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_graphical.py b/tests/test_graphical.py index 6311bb4..ebbedc1 100755 --- a/tests/test_graphical.py +++ b/tests/test_graphical.py @@ -28,6 +28,10 @@ def test_guiDeckOverview(setup): assert ac.guiDeckOverview(name="test_deck") is True +def test_guiImportFile(setup): + ac.guiImportFile() + + class TestAddCards: note = { "deckName": "test_deck",