Python: rework the load_file API, part 2

Take arguments for configuring style, etc., throw an exception on errors, and log warning diagnostics.
This commit is contained in:
Simon Hausmann 2024-03-06 11:24:54 +01:00 committed by Simon Hausmann
parent 280f314eeb
commit 765c773b90
3 changed files with 47 additions and 5 deletions

View file

@ -3,12 +3,41 @@
from . import slint as native
import types
import logging
def load_file(path):
class CompileError(Exception):
def __init__(self, message, diagnostics):
self.message = message
self.diagnostics = diagnostics
def load_file(path, quiet=False, style=None, include_paths=None, library_paths=None, translation_domain=None):
compiler = native.ComponentCompiler()
if style is not None:
compiler.style = style
if include_paths is not None:
compiler.include_paths = include_paths
if library_paths is not None:
compiler.library_paths = library_paths
if translation_domain is not None:
compiler.translation_domain = translation_domain
compdef = compiler.build_from_path(path)
diagnostics = compiler.diagnostics
if diagnostics:
if not quiet:
for diag in diagnostics:
if diag.level == native.DiagnosticLevel.Warning:
logging.warning(diag)
errors = [diag for diag in diagnostics if diag.level ==
native.DiagnosticLevel.Error]
if errors:
raise CompileError(f"Could not compile {path}", diagnostics)
module = types.SimpleNamespace()
setattr(module, compdef.name, type("SlintMetaClass", (), {
"__compdef": compdef,