Python: Add support for snake case imports (#6229)

When using slint.loader.app_window.XXX look for "app_window.slint" followed by "app-window.slint".

Fixes #6216
This commit is contained in:
Simon Hausmann 2024-09-20 10:25:14 +02:00 committed by GitHub
parent 34fe6b1537
commit 844590cac8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 13 additions and 5 deletions

View file

@ -129,7 +129,8 @@ options:
Any attribute lookup in `slint.loader` is searched for in `sys.path`. If a directory with the name exists, it is returned as a loader object, and subsequent
attribute lookups follow the same logic. If the name matches a file with the `.slint` extension, it is automatically loaded with `load_file` and the
[namespace](https://docs.python.org/3/library/types.html#types.SimpleNamespace) is returned, which contains classes for each exported component that
inherits `Window`.
inherits `Window`. If the file name contains a dash, like `app-window.slint`, an attribute lookup for `app_window` will
first try to locate `app_window.slint` and then fall back to `app-window.slint`.
### Accessing Properties

View file

@ -275,6 +275,13 @@ class SlintAutoLoader:
setattr(self, name, type_namespace)
return type_namespace
dir_candidate = os.path.join(path, name.replace('_', '-'))
file_candidate = dir_candidate + ".slint"
if os.path.isfile(file_candidate):
type_namespace = load_file(file_candidate)
setattr(self, name, type_namespace)
return type_namespace
return None

View file

@ -8,7 +8,7 @@ import os
def test_callback_decorators(caplog):
module = load_file(os.path.join(os.path.dirname(
__spec__.origin), "test_load_file.slint"), quiet=False)
__spec__.origin), "test-load-file.slint"), quiet=False)
class SubClass(module.App):
@slint.callback()

View file

@ -8,7 +8,7 @@ import os
def test_load_file(caplog):
module = load_file(os.path.join(os.path.dirname(
__spec__.origin), "test_load_file.slint"), quiet=False)
__spec__.origin), "test-load-file.slint"), quiet=False)
assert "The property 'color' has been deprecated. Please use 'background' instead" in caplog.text
@ -42,7 +42,7 @@ def test_load_file_fail():
def test_load_file_wrapper():
module = load_file(os.path.join(os.path.dirname(
__spec__.origin), "test_load_file.slint"), quiet=False)
__spec__.origin), "test-load-file.slint"), quiet=False)
instance = module.App()
@ -66,7 +66,7 @@ def test_load_file_wrapper():
def test_constructor_kwargs():
module = load_file(os.path.join(os.path.dirname(
__spec__.origin), "test_load_file.slint"), quiet=False)
__spec__.origin), "test-load-file.slint"), quiet=False)
def early_say_hello(arg):
return "early:" + arg