Python: Replace import magic with an auto-loader

As discussed on Reddit, the magic import logic is not very tool friendly and a little too magic perhaps. Instead, this patch introduces an automatic loader (`slint.loader`), which can traverse `sys.path` and lazily load `.slint` files by attribute lookup.

Closes #4856
This commit is contained in:
Simon Hausmann 2024-04-18 15:51:28 +02:00 committed by Simon Hausmann
parent 93307707cd
commit f86f4993fa
6 changed files with 48 additions and 52 deletions

View file

@ -3,25 +3,23 @@
import pytest
from slint import slint as native
from slint import loader
import sys
import os
def test_magic_import():
import test_load_file_slint as compiledmodule
instance = compiledmodule.App()
instance = loader.test_load_file.App()
del instance
def test_magic_import_path():
oldsyspath = sys.path
with pytest.raises(ModuleNotFoundError, match="No module named 'printerdemo_slint'"):
import printerdemo_slint
assert loader.printerdemo == None
try:
sys.path.append(os.path.join(os.path.dirname(__file__),
"..", "..", "..", "examples", "printerdemo", "ui"))
import printerdemo_slint
instance = printerdemo_slint.MainWindow()
"..", "..", ".."))
instance = loader.examples.printerdemo.ui.printerdemo.MainWindow()
del instance
finally:
sys.path = oldsyspath