Skip to content
This repository was archived by the owner on Nov 4, 2025. It is now read-only.

Commit b896b66

Browse files
authored
Merge pull request #396 from mikkkee/master
2 parents 9f7dcca + 5a15369 commit b896b66

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ corresponding to when the API was available for use.
16191619
16201620
#### `guiImportFile`
16211621
1622-
* 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.
1622+
* 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. Only supported for Anki 2.1.52+.
16231623
16241624
<details>
16251625
<summary><i>Sample request:</i></summary>

plugin/__init__.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
from anki.importing import AnkiPackageImporter
4242
from anki.notes import Note
4343
from anki.errors import NotFoundError
44-
from aqt.import_export.importing import import_file, prompt_for_file_then_import
4544
from aqt.qt import Qt, QTimer, QMessageBox, QCheckBox
4645

4746
from .web import format_exception_reply, format_success_reply
@@ -1858,20 +1857,36 @@ def guiImportFile(self, path=None):
18581857
"""
18591858
Open Import File (Ctrl+Shift+I) dialog with provided file path.
18601859
If no path is given, the user will be prompted to select a file.
1860+
Only supported from Anki version >=2.1.52
18611861
18621862
path: string
18631863
import file path, note on Windows you must use forward slashes.
18641864
"""
1865+
if anki_version >= (2, 1, 52):
1866+
from aqt.import_export.importing import import_file, prompt_for_file_then_import
1867+
else:
1868+
raise Exception('guiImportFile is only supported from Anki version >=2.1.52')
1869+
1870+
if hasattr(Qt, 'WindowStaysOnTopHint'):
1871+
# Qt5
1872+
WindowOnTopFlag = Qt.WindowStaysOnTopHint
1873+
elif hasattr(Qt, 'WindowType') and hasattr(Qt.WindowType, 'WindowStaysOnTopHint'):
1874+
# Qt6
1875+
WindowOnTopFlag = Qt.WindowType.WindowStaysOnTopHint
1876+
else:
1877+
# Unsupported, don't try to bring window to top
1878+
WindowOnTopFlag = None
18651879

18661880
# Bring window to top for user to review import settings.
1867-
try:
1868-
# [Step 1/2] set always on top flag, show window (it stays on top for now)
1869-
self.window().setWindowFlags(self.window().windowFlags() | Qt.WindowStaysOnTopHint)
1870-
self.window().show()
1871-
finally:
1872-
# [Step 2/2] clear always on top flag, show window (it doesn't stay on top anymore)
1873-
self.window().setWindowFlags(self.window().windowFlags() & ~Qt.WindowStaysOnTopHint)
1874-
self.window().show()
1881+
if WindowOnTopFlag is not None:
1882+
try:
1883+
# [Step 1/2] set always on top flag, show window (it stays on top for now)
1884+
self.window().setWindowFlags(self.window().windowFlags() | WindowOnTopFlag)
1885+
self.window().show()
1886+
finally:
1887+
# [Step 2/2] clear always on top flag, show window (it doesn't stay on top anymore)
1888+
self.window().setWindowFlags(self.window().windowFlags() & ~WindowOnTopFlag)
1889+
self.window().show()
18751890

18761891
if path is None:
18771892
prompt_for_file_then_import(self.window())

tests/test_graphical.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
2+
from unittest import mock
23

3-
from conftest import ac, wait_until, \
4+
from conftest import ac, anki_version, wait_until, \
45
close_all_dialogs_and_wait_for_them_to_run_closing_callbacks, \
56
get_dialog_instance
67

@@ -29,7 +30,10 @@ def test_guiDeckOverview(setup):
2930

3031

3132
def test_guiImportFile(setup):
32-
ac.guiImportFile()
33+
if anki_version >= (2, 1, 52):
34+
with mock.patch('aqt.import_export.importing.prompt_for_file_then_import') as mock_prompt_for_file_then_import:
35+
mock_prompt_for_file_then_import.return_value = True
36+
ac.guiImportFile()
3337

3438

3539
class TestAddCards:

0 commit comments

Comments
 (0)