From 4d8241b86f3dcddba4d96fdb9dad54cff5f05d1b Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Thu, 10 Feb 2022 19:49:03 +0100 Subject: [PATCH 1/4] Fixed a typo on the preparser, adjusted the tests and added a minimal tree_view --- src/bootstraparse/modules/preparser.py | 25 ++++++++++++++++++------- tests/modules/test_preparser.py | 6 +++++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/bootstraparse/modules/preparser.py b/src/bootstraparse/modules/preparser.py index cc61971..ca461d6 100644 --- a/src/bootstraparse/modules/preparser.py +++ b/src/bootstraparse/modules/preparser.py @@ -7,6 +7,7 @@ from io import StringIO from bootstraparse.modules import pathresolver as pr from bootstraparse.modules import environment import rich +from rich.tree import Tree # list of regexps _rgx_import_file = regex.compile(r'::( ?\< ?(?P[\w\-._/]+) ?\>[ \s]*)+') @@ -41,6 +42,7 @@ class PreParser: self.local_dict_of_imports = {} # Dictionary of all local imports made to avoid duplicate file opening ? self.is_global_dict_of_imports_initialized = False self.saved_import_list = None + self.tree_view = None def readlines(self): """ @@ -120,13 +122,13 @@ class PreParser: import_file = self.global_dict_of_imports[import_path].export_with_imports() temp_file.writelines(import_file.readlines()) old_import_line = import_line + temp_file.write("\n") + if source_line_count != 0: + source_line_count += 1 temp_file.writelines(source_lines[source_line_count:]) temp_file.seek(0) return temp_file - # todo: test import in sub-folders - # todo: test same imports on multiple lines - def __repr__(self): """ Returns a string representation of the PreParser object. @@ -169,12 +171,17 @@ class PreParser: """ return not self.__eq__(other) - def rich_tree(self): + def rich_tree(self, force=False): """ Returns a rich representation of the PreParser object. :return: a rich representation of the PreParser object """ - pass + if self.tree_view or force: + return self.tree_view + self.tree_view = Tree(self.name) + for path, _ in self.parse_import_list(): + self.tree_view.add(self.global_dict_of_imports[path].rich_tree()) + return self.tree_view # This part is only used for testing @@ -185,5 +192,9 @@ if __name__ == "__main__": # pragma: no cover michel.parse_import_list() # michel.make_import_list() michel.export_with_imports() - with open('../../../example_userfiles/output/show_me_what_you_got.txt', 'w+') as file: - file.writelines(michel.export_with_imports().readlines()) \ No newline at end of file + path = '../../../example_userfiles/output/show_me_what_you_got.txt' + os.makedirs(os.path.dirname(path), exist_ok=True) + with open(path, 'w+') as file: + file.writelines(michel.export_with_imports().readlines()) + + rich.print(michel.rich_tree()) diff --git a/tests/modules/test_preparser.py b/tests/modules/test_preparser.py index 3f94729..d807e8a 100644 --- a/tests/modules/test_preparser.py +++ b/tests/modules/test_preparser.py @@ -196,6 +196,7 @@ def test_preparser_content(filename, content): pp.make_import_list() assert pp.export_with_imports() == content # nothing is implementes yet + # todo: test import in sub-folders # @pytest.mark.skip("Not implemented") @@ -222,6 +223,9 @@ def test_rich_tree(): Test the rich_tree function """ test_file = temp_name("test_rich_tree.bpr") - make_new_file(test_file, "Test line 1\n::< test_rich_tree.bpr >\nTest line 3") + test_file_2 = temp_name("test_rich_tree_2.bpr") + make_new_file(test_file, "Test line 1\n::< test_rich_tree_2.bpr >\nTest line 3") + make_new_file(test_file_2, "Test line 1\nTest line 3") pp = preparser.PreParser(test_file, env) + pp.make_import_list() pp.rich_tree() From 336928524d10c9e55f3bbc2cfd9d042328ea31b0 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Thu, 10 Feb 2022 20:11:23 +0100 Subject: [PATCH 2/4] A couple parameters on the Tree --- src/bootstraparse/modules/preparser.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/bootstraparse/modules/preparser.py b/src/bootstraparse/modules/preparser.py index ca461d6..40c373d 100644 --- a/src/bootstraparse/modules/preparser.py +++ b/src/bootstraparse/modules/preparser.py @@ -18,11 +18,11 @@ class PreParser: """ Takes a path and environment, executes all pre-parsing methods on the specified file. """ - def __init__(self, path, __env, list_of_paths=None, dict_of_imports=None): + def __init__(self, file_path, __env, list_of_paths=None, dict_of_imports=None): """ Initializes the PreParser object. Takes the following parameters: - :param path: the path of the file to be parsed + :param file_path: the path of the file to be parsed :param __env: the environment object :param list_of_paths: the list of files that have been imported in this branch of the import tree :param dict_of_imports: Dictionary of all imports made to avoid duplicate file opening / pre-parsing @@ -33,10 +33,10 @@ class PreParser: dict_of_imports = {} self.__env = __env - self.path = path - self.name = os.path.basename(path) - self.base_path = os.path.dirname(path) - self.relative_path_resolver = pr.PathResolver(path) + self.path = file_path + self.name = os.path.basename(file_path) + self.base_path = os.path.dirname(file_path) + self.relative_path_resolver = pr.PathResolver(file_path) self.list_of_paths = list_of_paths + [self.relative_path_resolver(self.name)] self.global_dict_of_imports = dict_of_imports self.local_dict_of_imports = {} # Dictionary of all local imports made to avoid duplicate file opening ? @@ -171,16 +171,16 @@ class PreParser: """ return not self.__eq__(other) - def rich_tree(self, force=False): + def rich_tree(self, prefix="", suffix="", force=True): """ Returns a rich representation of the PreParser object. :return: a rich representation of the PreParser object """ - if self.tree_view or force: + if self.tree_view and not force: return self.tree_view - self.tree_view = Tree(self.name) - for path, _ in self.parse_import_list(): - self.tree_view.add(self.global_dict_of_imports[path].rich_tree()) + self.tree_view = Tree(prefix+self.name+suffix) + for p, l in self.parse_import_list(): + self.tree_view.add(self.global_dict_of_imports[p].rich_tree(suffix=" (Line:{})".format(l), force=True)) return self.tree_view From d05192f68e8965c031266d0de598bd61bf530f27 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Thu, 10 Feb 2022 20:11:48 +0100 Subject: [PATCH 3/4] Pushed the coverage to 100% --- tests/modules/test_preparser.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/modules/test_preparser.py b/tests/modules/test_preparser.py index d807e8a..ce7f6ee 100644 --- a/tests/modules/test_preparser.py +++ b/tests/modules/test_preparser.py @@ -229,3 +229,4 @@ def test_rich_tree(): pp = preparser.PreParser(test_file, env) pp.make_import_list() pp.rich_tree() + pp.rich_tree(force=False) From 12008831aaf503d04b5954469f131ac75cd2d68c Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Fri, 11 Feb 2022 00:47:42 +0100 Subject: [PATCH 4/4] Added the lamest easiest and most straightforwart solution to our problem. Also finished tests. --- example_userfiles/_imported.bpr | 2 +- example_userfiles/whatever2.bpr | 2 +- src/bootstraparse/modules/preparser.py | 9 +-------- tests/modules/test_preparser.py | 23 +++++++++++++---------- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/example_userfiles/_imported.bpr b/example_userfiles/_imported.bpr index b0cb723..326f55b 100644 --- a/example_userfiles/_imported.bpr +++ b/example_userfiles/_imported.bpr @@ -1,3 +1,3 @@ ~~header This is header text -header~~ \ No newline at end of file +header~~ diff --git a/example_userfiles/whatever2.bpr b/example_userfiles/whatever2.bpr index e37c8d3..2676ec5 100644 --- a/example_userfiles/whatever2.bpr +++ b/example_userfiles/whatever2.bpr @@ -1,2 +1,2 @@ This is whatever2.bpr -hello world \ No newline at end of file +hello world diff --git a/src/bootstraparse/modules/preparser.py b/src/bootstraparse/modules/preparser.py index 40c373d..138b4e7 100644 --- a/src/bootstraparse/modules/preparser.py +++ b/src/bootstraparse/modules/preparser.py @@ -108,23 +108,16 @@ class PreParser: """ self.make_import_list() - temp_file = StringIO() source_line_count = 0 - old_import_line = 0 import_list = self.parse_import_list() source_lines = self.readlines() for import_path, import_line in import_list: - if import_line != old_import_line: - source_line_count += 1 + source_lines[import_line] = "" temp_file.writelines(source_lines[source_line_count:import_line]) source_line_count = import_line import_file = self.global_dict_of_imports[import_path].export_with_imports() temp_file.writelines(import_file.readlines()) - old_import_line = import_line - temp_file.write("\n") - if source_line_count != 0: - source_line_count += 1 temp_file.writelines(source_lines[source_line_count:]) temp_file.seek(0) return temp_file diff --git a/tests/modules/test_preparser.py b/tests/modules/test_preparser.py index ce7f6ee..32e9b9e 100644 --- a/tests/modules/test_preparser.py +++ b/tests/modules/test_preparser.py @@ -32,8 +32,9 @@ Test content line 3 ::< pages/page2.bpr > Test content line 5 ::< pages/page1.bpr > -Test content line 7""" -content_page1 = """Test page1\nTest page1-2\nTest page1-3""" +Test content line 7 +""" +content_page1 = """Test page1\nTest page1-2\nTest page1-3\n""" content_page2 = """Test page2\n::< page1.bpr >""" content_page3 = """Test page3\n::< page3.bpr >""" content_index_import_list = [(temp_name("pages/page1.bpr"), 1), @@ -54,9 +55,12 @@ Test page1-2 Test page1-3 Test content line 5 Test page1 -Test content line 7""" -final_content_page1 = """Test page1\nTest page1-2\nTest page1-3""" -final_content_page2 = """Test page2\nTest page1""" +Test page1-2 +Test page1-3 +Test content line 7 +""" +final_content_page1 = """Test page1\nTest page1-2\nTest page1-3\n""" +final_content_page2 = """Test page2\nTest page1\nTest page1-2\nTest page1-3\n""" website_tree = { "index.bpr": content_index, @@ -180,11 +184,10 @@ def test_make_import_list(filename, content, capsys): assert pp.global_dict_of_imports.keys() == set([f for f, _ in pp.saved_import_list]) -@pytest.mark.xfail(reason="Not implemented", raises=Exception) @pytest.mark.parametrize("filename, content", [ - ("index.bpr", content_index), - ("pages/page1.bpr", content_page1), - ("pages/page2.bpr", content_page2), + ("index.bpr", final_content_index), + ("pages/page1.bpr", final_content_page1), + ("pages/page2.bpr", final_content_page2), ]) def test_preparser_content(filename, content): """ @@ -195,7 +198,7 @@ def test_preparser_content(filename, content): pp = preparser.PreParser(testfile, env) pp.make_import_list() - assert pp.export_with_imports() == content # nothing is implementes yet + assert pp.export_with_imports().read() == content # nothing is implementes yet # todo: test import in sub-folders