mirror of
https://github.com/ankitects/anki.git
synced 2025-11-09 14:17:13 -05:00
Allow to run App&GUI without entering the main event loop
This commit is contained in:
parent
bd414595de
commit
b863d7972c
2 changed files with 52 additions and 4 deletions
|
|
@ -233,11 +233,14 @@ def run():
|
||||||
"Please notify support of this error:\n\n"+
|
"Please notify support of this error:\n\n"+
|
||||||
traceback.format_exc())
|
traceback.format_exc())
|
||||||
|
|
||||||
def _run():
|
def _run(argv=None, exec=True):
|
||||||
global mw
|
global mw
|
||||||
|
|
||||||
|
if argv is None:
|
||||||
|
argv = sys.argv
|
||||||
|
|
||||||
# parse args
|
# parse args
|
||||||
opts, args = parseArgs(sys.argv)
|
opts, args = parseArgs(argv)
|
||||||
opts.base = opts.base or ""
|
opts.base = opts.base or ""
|
||||||
opts.profile = opts.profile or ""
|
opts.profile = opts.profile or ""
|
||||||
|
|
||||||
|
|
@ -250,7 +253,7 @@ def _run():
|
||||||
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
|
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
|
||||||
|
|
||||||
# create the app
|
# create the app
|
||||||
app = AnkiApp(sys.argv)
|
app = AnkiApp(argv)
|
||||||
QCoreApplication.setApplicationName("Anki")
|
QCoreApplication.setApplicationName("Anki")
|
||||||
if app.secondInstance():
|
if app.secondInstance():
|
||||||
# we've signaled the primary instance, so we should close
|
# we've signaled the primary instance, so we should close
|
||||||
|
|
@ -285,4 +288,7 @@ environment points to a valid, writable folder.""")
|
||||||
# load the main window
|
# load the main window
|
||||||
import aqt.main
|
import aqt.main
|
||||||
mw = aqt.main.AnkiQt(app, pm, args)
|
mw = aqt.main.AnkiQt(app, pm, args)
|
||||||
app.exec_()
|
if exec:
|
||||||
|
app.exec()
|
||||||
|
else:
|
||||||
|
return app
|
||||||
|
|
|
||||||
42
tests/test_run.py
Normal file
42
tests/test_run.py
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
# coding: utf-8
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
import aqt
|
||||||
|
from aqt import _run
|
||||||
|
from aqt.profiles import ProfileManager
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def temporaryUser(name="__Temporary Test User__"):
|
||||||
|
|
||||||
|
pm = ProfileManager(base="")
|
||||||
|
|
||||||
|
if name in pm.profiles():
|
||||||
|
raise Exception(f"Could not create a temporary user with name {name}")
|
||||||
|
|
||||||
|
pm.create(name)
|
||||||
|
pm.name = name
|
||||||
|
|
||||||
|
yield name
|
||||||
|
|
||||||
|
pm.remove(name)
|
||||||
|
|
||||||
|
def test_run():
|
||||||
|
|
||||||
|
# we need a new user for the test
|
||||||
|
with temporaryUser() as name:
|
||||||
|
app = _run(argv=["anki", "-p", name], exec=False)
|
||||||
|
assert app
|
||||||
|
|
||||||
|
aqt.mw.cleanupAndExit()
|
||||||
|
|
||||||
|
# clean up what was spoiled
|
||||||
|
aqt.mw = None
|
||||||
|
|
||||||
|
# remove hooks added during app initialization
|
||||||
|
from anki import hooks
|
||||||
|
hooks._hooks = {}
|
||||||
|
|
||||||
|
# test_nextIvl will fail on some systems if the locales are not restored
|
||||||
|
import locale
|
||||||
|
locale.setlocale(locale.LC_ALL, locale.getdefaultlocale())
|
||||||
Loading…
Reference in a new issue