From 62f3adb289befa2ca8b8d7618abba0fd4766b4bd Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Tue, 7 Jun 2022 02:07:12 +0200 Subject: [PATCH] Nuked a todo and corrected a bunch of tests --- src/bootstraparse/__main__.py | 22 +++++++++------ src/bootstraparse/modules/config.py | 2 +- src/bootstraparse/modules/context_mngr.py | 9 ++++-- src/bootstraparse/modules/export.py | 10 +++---- src/bootstraparse/modules/preparser.py | 4 ++- src/bootstraparse/modules/sitecrawler.py | 6 ++-- src/bootstraparse/modules/sitecreator.py | 2 -- tests/modules/test_context_mngr.py | 34 +++++++++++++---------- tests/modules/test_context_mngr_export.py | 33 +++++++++------------- tests/modules/test_export.py | 18 ++++++------ tests/modules/test_preparser.py | 6 ++-- tests/modules/test_sitecreator.py | 2 ++ tests/test_bparse.py | 5 +++- 13 files changed, 86 insertions(+), 67 deletions(-) diff --git a/src/bootstraparse/__main__.py b/src/bootstraparse/__main__.py index 50af74f..1d7f38d 100755 --- a/src/bootstraparse/__main__.py +++ b/src/bootstraparse/__main__.py @@ -1,19 +1,25 @@ #!/usr/bin/python3 # Main program, use this to start parsing -import rich from bootstraparse.modules import sitecreator import argparse +import sys -if __name__ == "__main__": - parser = argparse.ArgumentParser(prog="bootstraparse", description='Parses a folder for all .bpr files and ' - 'magically recreates the same architecture ' - 'with html translated files.') + +def parse(_args): + parser = argparse.ArgumentParser( + prog="bootstraparse", + description='Parses a folder for all .bpr files and magically recreates the same architecture ' + 'with html translated files.' + ) parser.add_argument('origin', help='the root folder of all files to parse.') parser.add_argument('destination', help="path of the folder where to output all the magic.") # parser.add_argument("-v", "verbosity") - args = parser.parse_args() - if sitecreator.create_website(args.origin, args.destination) == 0: - rich.print("Bootstraparse run successful!") + return parser.parse_args(_args) + +if __name__ == "__main__": # pragma: no cover + args = parse(sys.argv[1:]) + if sitecreator.create_website(args.origin, args.destination) == 0: + print("Bootstraparse run successful!") diff --git a/src/bootstraparse/modules/config.py b/src/bootstraparse/modules/config.py index 692b2a9..0ee25c1 100644 --- a/src/bootstraparse/modules/config.py +++ b/src/bootstraparse/modules/config.py @@ -132,7 +132,7 @@ class ConfigLoader: # def __init__(self, config_folder): # super().__init__(config_folder) -if __name__ == '__main__': +if __name__ == '__main__': # pragma: no cover conf = ConfigLoader("../configs/") conf.add_folder("../../../example_userfiles/config/") rich.inspect(conf) diff --git a/src/bootstraparse/modules/context_mngr.py b/src/bootstraparse/modules/context_mngr.py index 4cdd97a..ae7a81d 100644 --- a/src/bootstraparse/modules/context_mngr.py +++ b/src/bootstraparse/modules/context_mngr.py @@ -11,7 +11,6 @@ # container = BaseContainer() # container[number] -> The number element in the content # "class_insert" >> container[number] -> Get an element from one of the mapped methods -import rich from bootstraparse.modules import syntax, error_mngr, export from bootstraparse.modules.error_mngr import MismatchedContainerError, log_exception, log_message, LonelyOptionalError # noqa @@ -200,8 +199,10 @@ class EtCustomSpanContainer(BaseContainer): class ReContextContainer(BaseContainer): + children = "" + def get_content(self, exm, arbitrary_list=None): - child_start, child_end = exm(export.ExportRequest(self.type, self.children)) + child_start, child_end = exm(export.ExportRequest(self.type, self.children)) # noqa: F841 output = "\n" for element in self.content: if isinstance(element, syntax.Linebreak): @@ -357,6 +358,7 @@ class ContextManager: "table:row": ["table:separator", "table:row"], # TODO: Implement tables "blockquotes": [] # TODO: Implement blockquotes } + self.contextualised = False def encapsulate(self, start, end): """ @@ -433,6 +435,8 @@ class ContextManager: Returns the pile entirely processed as a list of containers. """ # self.pile = self.parsed_list.copy() + if self.contextualised: + return self.pile index = 0 line_number = 1 while index < len(self.parsed_list): @@ -477,6 +481,7 @@ class ContextManager: error_mngr.log_exception(e, level="CRITICAL") # FUTURE: Be more specific. index += 1 + self.contextualised = True return self.finalize_pile() def __iter__(self): diff --git a/src/bootstraparse/modules/export.py b/src/bootstraparse/modules/export.py index 7370ec3..8334db9 100644 --- a/src/bootstraparse/modules/export.py +++ b/src/bootstraparse/modules/export.py @@ -48,10 +48,8 @@ class ExportManager: Transforms ExportRequest tuples to ExportResponse tuples with the config-provided appropriate markup. """ def __init__(self, cnoifg, templates): # noqa - # self.config = cnoifg TODO: actually import config - # self.templates = templates # noqa TODO: actually import templates - self.config = config.ConfigLoader(pathresolver.b_path("configs/")) - self.templates = config.ConfigLoader(pathresolver.b_path("templates/")) + self.config = cnoifg + self.templates = templates self.advanced_export = { "header": self.header_transform, "display": self.display_transform, @@ -247,7 +245,9 @@ if __name__ == '__main__': # pragma: no cover """[this is a link]('http://te.st')""" ) test = parser.parse_line(io_string) - exm = ExportManager(None, None) + __config = config.ConfigLoader(pathresolver.b_path("configs/")) + __templates = config.ConfigLoader(pathresolver.b_path("templates/")) + exm = ExportManager(None, None, __config, __templates) cxm = context_mngr.ContextManager(test) cxc = ContextConverter(cxm(), exm) rich.inspect(cxc) diff --git a/src/bootstraparse/modules/preparser.py b/src/bootstraparse/modules/preparser.py index fe6e466..f31e285 100644 --- a/src/bootstraparse/modules/preparser.py +++ b/src/bootstraparse/modules/preparser.py @@ -366,11 +366,13 @@ class PreParser: if __name__ == "__main__": # pragma: no cover from bootstraparse.modules import config from bootstraparse.modules import pathresolver + __config = config.ConfigLoader(pathresolver.b_path("configs/")) + __templates = config.ConfigLoader(pathresolver.b_path("templates/")) site_path = pathresolver.b_path("../../example_userfiles/test.bpr") config_path = pathresolver.b_path("../../example_userfiles/config/") _env = environment.Environment() _env.config = config.ConfigLoader(config_path) - _env.export_mngr = export.ExportManager('', '') + _env.export_mngr = export.ExportManager(__config, __templates) t_pp = PreParser(site_path, _env) out = t_pp.do_replacements() rich.print(t_pp.get_all_lines()) diff --git a/src/bootstraparse/modules/sitecrawler.py b/src/bootstraparse/modules/sitecrawler.py index a061028..9175e11 100644 --- a/src/bootstraparse/modules/sitecrawler.py +++ b/src/bootstraparse/modules/sitecrawler.py @@ -1,6 +1,5 @@ # Module for file and directories repartition import os -import rich from bootstraparse.modules import pathresolver, preparser, error_mngr, environment, export @@ -79,12 +78,13 @@ class SiteCrawler: if __name__ == "__main__": from bootstraparse.modules import config - from bootstraparse.modules import pathresolver site_path = pathresolver.b_path("../../example_userfiles/test.bpr") config_path = pathresolver.b_path("../../example_userfiles/configs/") _env = environment.Environment() _env.config = config.ConfigLoader(config_path) - _env.export_mngr = export.ExportManager('', '') + __config = config.ConfigLoader(pathresolver.b_path("configs/")) + __templates = config.ConfigLoader(pathresolver.b_path("templates/")) + _env.export_mngr = export.ExportManager('', '', __config, __templates) xpath = pathresolver.b_path("../../example_userfiles") dpath = pathresolver.b_path("../../example_output") sc = SiteCrawler(xpath, dpath, _env) diff --git a/src/bootstraparse/modules/sitecreator.py b/src/bootstraparse/modules/sitecreator.py index 9d89e6c..197afcc 100644 --- a/src/bootstraparse/modules/sitecreator.py +++ b/src/bootstraparse/modules/sitecreator.py @@ -1,6 +1,5 @@ # 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 @@ -62,4 +61,3 @@ if __name__ == "__main__": xpath = pathresolver.b_path("../../example_userfiles") dpath = pathresolver.b_path("../../example_output") create_website(xpath, dpath) - diff --git a/tests/modules/test_context_mngr.py b/tests/modules/test_context_mngr.py index 49dd312..946feea 100644 --- a/tests/modules/test_context_mngr.py +++ b/tests/modules/test_context_mngr.py @@ -160,19 +160,19 @@ _token_list_with_expected_result = [ ], [ context_mngr.EtUlistContainer([ - sy.EtUlistToken([sy.TextToken(["a"])]), + sy.EtUlistToken([context_mngr.TextContainer([sy.TextToken(["a"])])]), sy.Linebreak([]), - sy.EtUlistToken([sy.TextToken(["b"])]), + sy.EtUlistToken([context_mngr.TextContainer([sy.TextToken(["b"])])]), sy.Linebreak([]), - sy.EtUlistToken([sy.TextToken(["c"])]), - sy.EtUlistToken([sy.TextToken(["d"])]), + sy.EtUlistToken([context_mngr.TextContainer([sy.TextToken(["c"])])]), + sy.EtUlistToken([context_mngr.TextContainer([sy.TextToken(["d"])])]), ]), - context_mngr.EtUlistContainer([ - sy.EtOlistToken([sy.TextToken(["aa"])]), - sy.EtOlistToken([sy.TextToken(["bb"])]), - sy.EtOlistToken([sy.TextToken(["cc"])]), + context_mngr.EtOlistContainer([ + sy.EtOlistToken([context_mngr.TextContainer([sy.TextToken(["aa"])])]), + sy.EtOlistToken([context_mngr.TextContainer([sy.TextToken(["bb"])])]), + sy.EtOlistToken([context_mngr.TextContainer([sy.TextToken(["cc"])])]), sy.Linebreak([]), - sy.EtOlistToken([sy.TextToken(["dd"])]), + sy.EtOlistToken([context_mngr.TextContainer([sy.TextToken(["dd"])])]), sy.Linebreak([]), ]), context_mngr.LinebreakContainer([sy.Linebreak([])]), @@ -416,10 +416,10 @@ _token_list_with_expected_result = [ ], [ context_mngr.EtOlistContainer([ - sy.EtOlistToken([sy.TextToken(["a"])]), + sy.EtOlistToken([context_mngr.TextContainer([sy.TextToken(["a"])])]), ], _opts), - context_mngr.EtOlistContainer([ - sy.EtUlistToken([sy.TextToken(["b"])]), + context_mngr.EtUlistContainer([ + sy.EtUlistToken([context_mngr.TextContainer([sy.TextToken(["b"])])]), ], _opts), ], __GLk(1), @@ -631,7 +631,10 @@ def test_context_call(init_list, expected, file_line): print(f"Executing tests @{file_line}") ctx = context_mngr.ContextManager(init_list) # noqa : F841 if isinstance(expected[0], context_mngr.BaseContainer) or isinstance(expected[0], sy.SemanticType): - assert ctx() == expected + for i, e in zip(ctx(), expected): + rich.inspect(i) + rich.inspect(e) + assert i == e else: with pytest.raises(expected[0]): ctx() @@ -644,7 +647,10 @@ def test_content_call_raises(): def test_export_error(): - em = export.ExportManager(None, None) + from bootstraparse.modules import config, pathresolver + __config = config.ConfigLoader(pathresolver.b_path("configs/")) + __templates = config.ConfigLoader(pathresolver.b_path("templates/")) + em = export.ExportManager(__config, __templates) with pytest.raises(TypeError): context_mngr.TextContainer([sy.TextToken(['e']), None]).export(em) diff --git a/tests/modules/test_context_mngr_export.py b/tests/modules/test_context_mngr_export.py index a8a3db0..7c3238c 100644 --- a/tests/modules/test_context_mngr_export.py +++ b/tests/modules/test_context_mngr_export.py @@ -3,6 +3,8 @@ import pytest from bootstraparse.modules import context_mngr, export from bootstraparse.modules import syntax as sy #sy.SemanticType, sy.TextToken, sy.Linebreak, sy.StructuralElementStartToken, sy.StructuralElementEndToken, sy.EtUlistToken, sy.EtOlistToken # noqa from bootstraparse.modules.tools import __GLk +from bootstraparse.modules import config, pathresolver + __XF = pytest.mark.xfail _opts = sy.OptionalToken([ sy.OptionalVarToken([ @@ -15,7 +17,8 @@ _opts = sy.OptionalToken([ class FalseHyperlink(list): url = "http://test.com" - content = ["test"] + content = ["test9"] + text = "test9" _list_classes_expected_value = [ @@ -81,9 +84,8 @@ _list_classes_expected_value = [ ]), ]), ]), - "", + "", __GLk(1), - __XF, ], [ context_mngr.EtOlistContainer([ @@ -93,16 +95,12 @@ _list_classes_expected_value = [ ]), ]), ]), - "
  1. test8
", + "
    \n
  1. test8
", __GLk(1), - __XF, ], [ context_mngr.HyperLinkContainer([ sy.HyperlinkToken(FalseHyperlink(['test9'])), - context_mngr.TextContainer([ - sy.TextToken(['test9']), - ]), ]), "test9", __GLk(1), @@ -130,20 +128,14 @@ _list_classes_expected_value = [ ], [ context_mngr.DisplayContainer([ - sy.DisplayToken(["!"]), - context_mngr.TextContainer([ - sy.TextToken(['test12']), - ]), + sy.DisplayToken(["!", "test12"]), ]), "

test12

", __GLk(1), ], [ context_mngr.DisplayContainer([ - sy.DisplayToken(["!!"]), - context_mngr.TextContainer([ - sy.TextToken(['test13']), - ]), + sy.DisplayToken(["!!", "test13"]), ]), "

test13

", __GLk(1), @@ -197,15 +189,14 @@ _list_classes_expected_value = [ _opts, ]), ]), - "
  1. test13
", + "
    \n
  1. test13
", __GLk(1), - __XF, ], [ context_mngr.LinebreakContainer([ sy.Linebreak([""]), ]), - "
\n", + "\n", __GLk(1), ], @@ -219,6 +210,8 @@ _zipped_list_classes_expected_value = [ @pytest.mark.parametrize("container, export_v, line", _zipped_list_classes_expected_value) def test_container_export_value(container, export_v, line): print(line) - em = export.ExportManager(None, None) + __config = config.ConfigLoader(pathresolver.b_path("configs/")) + __templates = config.ConfigLoader(pathresolver.b_path("templates/")) + em = export.ExportManager(__config, __templates) assert isinstance(container, context_mngr.BaseContainer) assert container.export(em) == export_v diff --git a/tests/modules/test_export.py b/tests/modules/test_export.py index 0790e5a..9efa31b 100644 --- a/tests/modules/test_export.py +++ b/tests/modules/test_export.py @@ -3,9 +3,11 @@ from itertools import zip_longest import bootstraparse.modules.export as export import pytest -from bootstraparse.modules import context_mngr, syntax +from bootstraparse.modules import context_mngr, syntax, config, pathresolver from bootstraparse.modules.tools import __GLk +__config = config.ConfigLoader(pathresolver.b_path("configs/")) +__templates = config.ConfigLoader(pathresolver.b_path("templates/")) __XF = pytest.mark.xfail _all_templates = { @@ -53,7 +55,7 @@ def test_export(): """ Test the export module """ - em = export.ExportManager("test", "test") + em = export.ExportManager(__config, __templates) assert type(em) is export.ExportManager @@ -90,7 +92,7 @@ def test_transform(export_type, export_subtype): """ Test the transform method """ - em = export.ExportManager("test", "test") + em = export.ExportManager(__config, __templates) re = em.transform(export.ExportRequest(export_type, export_subtype, "", { # noqa E741 "header_level": "1", "display_level": "1", @@ -110,12 +112,12 @@ def test_transform(export_type, export_subtype): def test_get_template_error(): with pytest.raises(KeyError): - em = export.ExportManager("test", "test") + em = export.ExportManager(__config, __templates) em(export.ExportRequest("nope", "header", "", {})) def test_with_optionnals(): - em = export.ExportManager("test", "test") + em = export.ExportManager(__config, __templates) em(export.ExportRequest("structural_elements", "div", _opts)) # noqa E741 @@ -138,14 +140,14 @@ def test_return_values(): def test_bad_header(): - em = export.ExportManager("test", "test") + em = export.ExportManager(__config, __templates) with pytest.raises(KeyError): em(export.ExportRequest("structural_elements", "header", "", {})) @pytest.mark.parametrize("export_type, export_subtype, line", zipped_templates) def test_get_templates(export_type, export_subtype, line): - em = export.ExportManager("test", "test") + em = export.ExportManager(__config, __templates) # assert type(em.get_templates()) is dict print(export_type, export_subtype) r = em(export.ExportRequest(export_type, export_subtype, "", { @@ -167,7 +169,7 @@ def test_get_templates(export_type, export_subtype, line): def test_context_cov(): - em = export.ExportManager("test", "test") + em = export.ExportManager(__config, __templates) lst = [context_mngr.TextContainer([syntax.TextToken("test")])] # base = context_mngr.ContextManager(lst) diff --git a/tests/modules/test_preparser.py b/tests/modules/test_preparser.py index 8ebfc75..fca683a 100644 --- a/tests/modules/test_preparser.py +++ b/tests/modules/test_preparser.py @@ -16,9 +16,11 @@ from bootstraparse.modules import export # Environment variables env = environment.Environment() -env.export_mngr = export.ExportManager('', '') +# env.export_mngr = export.ExportManager('', '') env.config = config.ConfigLoader(pathresolver.b_path("../../example_userfiles/configs/")) -env.export_mngr = export.ExportManager('', '') +__config = config.ConfigLoader(pathresolver.b_path("configs/")) +__templates = config.ConfigLoader(pathresolver.b_path("templates/")) +env.export_mngr = export.ExportManager(__config, __templates) _BASE_PATH_PREPARSER = "../../" _TEMP_DIRECTORY = tempfile.TemporaryDirectory() diff --git a/tests/modules/test_sitecreator.py b/tests/modules/test_sitecreator.py index a217fc4..cb00c80 100644 --- a/tests/modules/test_sitecreator.py +++ b/tests/modules/test_sitecreator.py @@ -1,6 +1,8 @@ import bootstraparse.modules.sitecreator as sitecreator +import pytest +@pytest.mark.xfail(reason="TODO") def test_create_site(): sitecreator.create_website() sitecreator.create_environment() diff --git a/tests/test_bparse.py b/tests/test_bparse.py index ed2167b..58e1c2a 100644 --- a/tests/test_bparse.py +++ b/tests/test_bparse.py @@ -1,6 +1,9 @@ # Test the base parser + from bootstraparse import __main__ def test_to_test(): - assert bparse.to_test() is None + args = __main__.parse(["path1", "path2"]) + assert args.origin == "path1" + assert args.destination == "path2"