Fixed a typo on the preparser, adjusted the tests and added a minimal tree_view

This commit is contained in:
Maximilien Rigaut 2022-02-10 19:49:03 +01:00
parent 19e782d897
commit 4d8241b86f
2 changed files with 23 additions and 8 deletions

View File

@ -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<file_name>[\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())
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())

View File

@ -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()