From 63b3109800b8676706a1acdf04ddd5f70fc2e715 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Tue, 7 Jun 2022 06:11:52 +0200 Subject: [PATCH] Added a little more debug and info. Corrected a mistake on the finalize_pile --- src/bootstraparse/modules/context_mngr.py | 18 ++++++++++--- src/bootstraparse/modules/error_mngr.py | 10 ++++--- src/bootstraparse/modules/export.py | 2 +- src/bootstraparse/modules/sitecreator.py | 5 +++- src/bootstraparse/modules/syntax.py | 2 ++ tests/modules/test_context_mngr.py | 32 +++++------------------ tests/modules/test_error_mngr.py | 4 +-- 7 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/bootstraparse/modules/context_mngr.py b/src/bootstraparse/modules/context_mngr.py index 5554708..6e0a2f4 100644 --- a/src/bootstraparse/modules/context_mngr.py +++ b/src/bootstraparse/modules/context_mngr.py @@ -202,7 +202,7 @@ class ReContextContainer(BaseContainer): children = "" def get_content(self, exm, arbitrary_list=None): - child_start, child_end = exm(export.ExportRequest(self.type, self.children)) # noqa: F841 + child_start, child_end = exm(export.ExportRequest(self.type, self.children)) # noqa F821 output = "\n" for element in self.content: if isinstance(element, syntax.Linebreak): @@ -340,7 +340,7 @@ class ContextManager: """ Class in charge of piling all the parsed elements and then encapsulating them inside one another. """ - def __init__(self, parsed_list): + def __init__(self, parsed_list, name=None, ident=0): """ Takes a list of parsed tokens. Parameters @@ -351,6 +351,8 @@ class ContextManager: self.output = [] self.parsed_list = parsed_list self.pile = [] + self.name = name + self.ident = ident self.matched_elements = {} self.dict_lookahead = { "list:ulist": ["list:ulist"], @@ -442,6 +444,8 @@ class ContextManager: while index < len(self.parsed_list): token = self.parsed_list[index] token.line_number = line_number + token.file_name = self.name + token.ident = self.ident self.pile.append(token) try: # Linebreaks @@ -500,8 +504,14 @@ class ContextManager: if isinstance(p, BaseContainer): final_pile.append(p) else: # Cleanup of non-matched elements + if isinstance(p, syntax.SemanticType): + line = p.line_number + name = p.file_name + else: + line = "Undefined" + name = "Undefined" log_exception( - TypeError(f"Expected BaseContainer, found {type(p)} instead."), + TypeError(f"Encountered a non-container element in the pile during the final pass: {p}, at line {line} in file {name}."), level="CRITICAL" ) self.pile = final_pile @@ -536,7 +546,7 @@ class ContextManager: return range_to_encapsulate, line_skipped def recontext(self, token): - token.content = ContextManager(token.content)() + token.content = ContextManager(token.content, name=self.name)() def get_last_container_in_pile(self, index): """ diff --git a/src/bootstraparse/modules/error_mngr.py b/src/bootstraparse/modules/error_mngr.py index d0b69f4..1fd5a12 100644 --- a/src/bootstraparse/modules/error_mngr.py +++ b/src/bootstraparse/modules/error_mngr.py @@ -158,10 +158,12 @@ class MismatchedContainerError(BootstraparseError): if token: self.line = token.line_number self.label = token.label + self.name = token.file_name else: self.line = None self.label = None - super().__init__(f"Could not process {self.label} at line {self.line}.") + self.name = None + super().__init__(f"Could not process {self.label} at line {self.line} in file {self.name}.") class LonelyOptionalError(BootstraparseError): @@ -173,12 +175,14 @@ class LonelyOptionalError(BootstraparseError): if token: self.line = token.line_number self.label = token.label + self.name = token.file_name else: self.line = None self.label = None + self.name = None if last_in_pile: super().__init__(f"Could not match token {token} at line {self.line} with " - f"last element in pile {last_in_pile} at line {last_in_pile.line_number} (not a container).") + f"last element in pile {last_in_pile} at line {last_in_pile.line_number} in file {self.name}(not a container).") else: - super().__init__(f"Could not match token {token} at line {self.line} " + super().__init__(f"Could not match token {token} at line {self.line} in file {self.name}" f"as there was nothing in the pile.") diff --git a/src/bootstraparse/modules/export.py b/src/bootstraparse/modules/export.py index 8334db9..049990f 100644 --- a/src/bootstraparse/modules/export.py +++ b/src/bootstraparse/modules/export.py @@ -247,7 +247,7 @@ if __name__ == '__main__': # pragma: no cover test = parser.parse_line(io_string) __config = config.ConfigLoader(pathresolver.b_path("configs/")) __templates = config.ConfigLoader(pathresolver.b_path("templates/")) - exm = ExportManager(None, None, __config, __templates) + exm = ExportManager(__config, __templates) cxm = context_mngr.ContextManager(test) cxc = ContextConverter(cxm(), exm) rich.inspect(cxc) diff --git a/src/bootstraparse/modules/sitecreator.py b/src/bootstraparse/modules/sitecreator.py index 6f1e4de..59780e8 100644 --- a/src/bootstraparse/modules/sitecreator.py +++ b/src/bootstraparse/modules/sitecreator.py @@ -1,5 +1,8 @@ # Module sequencing the successive actions necessary for website building import os + +import rich + from bootstraparse.modules import pathresolver, sitecrawler, environment, config, export, parser, context_mngr @@ -48,7 +51,7 @@ def create_crawler(origin, destination, _env): def preparse_parse(preparser): io = preparser.do_replacements() parsed_list = parser.parse_line(io) - output = context_mngr.ContextManager(parsed_list)() + output = context_mngr.ContextManager(parsed_list, name=preparser.name)() return output diff --git a/src/bootstraparse/modules/syntax.py b/src/bootstraparse/modules/syntax.py index 5d7f619..6669cf2 100644 --- a/src/bootstraparse/modules/syntax.py +++ b/src/bootstraparse/modules/syntax.py @@ -30,6 +30,8 @@ class SemanticType: def __init__(self, content): self.content = content + self.line_number = "Undefined" + self.file_name = "Undefined" self.label_container = self.label def to_markup(self): diff --git a/tests/modules/test_context_mngr.py b/tests/modules/test_context_mngr.py index c61ab1c..ef5ed00 100644 --- a/tests/modules/test_context_mngr.py +++ b/tests/modules/test_context_mngr.py @@ -571,31 +571,6 @@ def test_context_mngr(): assert base != base2 base.debug_map() -# -# def test_base_container_with_optionals(): -# base = context_mngr.BaseContainer() -# assert base is not None -# -# assert base.class_name() == 'BaseContainerWithOptionals' -# assert base.debug_map() is None -# -# base.add('test') -# assert base[0] == 'test' -# assert len(base) == 1 -# -# assert base.fetch_html_insert() == [] -# -# class OI: -# label = 'optional:insert' -# -# class CI: -# label = 'optional:class' -# -# i, c = OI(), CI() -# base.optionals = [i, c] -# assert base.fetch_html_insert() == [i] -# assert base.fetch_class_insert() == [c] - def test_print(): base = context_mngr.BaseContainer() @@ -681,3 +656,10 @@ def test_get_last_container_in_pile(base_cm): base_cm.pile[0] = None with pytest.raises(error_mngr.LonelyOptionalError): base_cm.get_last_container_in_pile(3) + + +def test_finalize_pile(base_cm): + base_cm() + base_cm.pile[0] = {} + with pytest.raises(TypeError): + base_cm.finalize_pile() diff --git a/tests/modules/test_error_mngr.py b/tests/modules/test_error_mngr.py index fb57347..f8f25f3 100644 --- a/tests/modules/test_error_mngr.py +++ b/tests/modules/test_error_mngr.py @@ -13,7 +13,7 @@ all_logging_tests = [(i, j, k) for i in [None, "test.log"] all_non_blocking_exception_tests = [ (ParsingError("test error", 10, 10, "testfile.bpr")), (ParsingError()), - (MismatchedContainerError(namedtuple("Test", ["label", "line_number"])("test", 10),)), + (MismatchedContainerError(namedtuple("Test", ["label", "line_number", "file_name"])("test", 10, "test.bpr"),)), ] all_blocking_exception_tests = [ @@ -110,7 +110,7 @@ def test_weird_exception_cases(): raise(error_mngr.LonelyOptionalError(None, None)) from typing import NamedTuple - tk = NamedTuple("falsetoken", [("line_number", int), ("label", str)])(1, "test") + tk = NamedTuple("falsetoken", [("line_number", int), ("label", str), ("file_name", str)])(1, "test", "test.bpr") with pytest.raises(error_mngr.LonelyOptionalError): raise(error_mngr.LonelyOptionalError(tk, None))