Merge branch 'fix-72' into 'main'

port: Show error when Gtk typelib is missing

Closes #72

See merge request jwestman/blueprint-compiler!54
This commit is contained in:
James Westman 2022-10-09 15:31:21 +00:00
commit e128b49069
4 changed files with 38 additions and 28 deletions

View file

@ -80,14 +80,16 @@ class CompileError(PrintableError):
self.hint("Are your dependencies up to date?")
def pretty_print(self, filename, code, stream=sys.stdout):
stream.write(f"""{self.color}{Colors.BOLD}{self.category}: {self.message}{Colors.CLEAR}\n""")
if code is not None and self.start is not None:
line_num, col_num = utils.idx_to_pos(self.start + 1, code)
line = code.splitlines(True)[line_num]
# Display 1-based line numbers
line_num += 1
stream.write(f"""{self.color}{Colors.BOLD}{self.category}: {self.message}{Colors.CLEAR}
at {filename} line {line_num} column {col_num}:
stream.write(f"""at {filename} line {line_num} column {col_num}:
{Colors.FAINT}{line_num :>4} |{Colors.CLEAR}{line.rstrip()}\n {Colors.FAINT}|{" "*(col_num-1)}^{Colors.CLEAR}\n""")
for hint in self.hints:
@ -115,6 +117,15 @@ class UnexpectedTokenError(CompileError):
super().__init__("Unexpected tokens", start, end)
class GtkTypelibMissingError(CompileError):
def __init__(self, **kwargs):
super().__init__(
"Could not find GTK 4 introspection files. Is gobject-introspection installed?",
fatal=True,
**kwargs
)
@dataclass
class CodeAction:
title: str

View file

@ -25,7 +25,7 @@ import gi # type: ignore
gi.require_version("GIRepository", "2.0")
from gi.repository import GIRepository # type: ignore
from .errors import CompileError, CompilerBugError
from .errors import CompileError, CompilerBugError, GtkTypelibMissingError
from . import typelib, xml_reader
_namespace_cache = {}
@ -49,9 +49,14 @@ def get_namespace(namespace, version):
break
if filename not in _namespace_cache:
hints = ["search path: " + os.pathsep.join(search_paths)]
if namespace == "Gtk":
raise GtkTypelibMissingError(hints=hints)
else:
raise CompileError(
f"Namespace {namespace}-{version} could not be found",
hints=["search path: " + os.pathsep.join(search_paths)],
hints=hints,
)
return _namespace_cache[filename]

View file

@ -23,7 +23,7 @@ import difflib
import os
from . import decompiler, tokenizer, parser
from .errors import MultipleErrors, PrintableError
from .errors import MultipleErrors, PrintableError, GtkTypelibMissingError
from .utils import Colors
@ -156,11 +156,15 @@ def step2():
def step3():
print(f"{Colors.BOLD}STEP 3: Convert UI files{Colors.CLEAR}")
try:
files = [
(file, change_suffix(file), decompile_file(file, change_suffix(file)))
for file in listdir_recursive(".")
if file.endswith(".ui")
]
except GtkTypelibMissingError as e:
e.pretty_print(None, None)
return ([], [])
success = 0
for in_file, out_file, result in files:

View file

@ -40,17 +40,7 @@ class GtkDirective(AstNode):
err.hint("Expected 'using Gtk 4.0;'")
raise err
try:
gir.get_namespace("Gtk", version)
except CompileError as e:
raise CompileError(
"Could not find GTK 4 introspection files. Is gobject-introspection installed?",
fatal=True,
# preserve the hints from the original error, because it contains
# useful debugging information
hints=e.hints,
)
@property
def gir_namespace(self):