Nocover on the __main__ test call and some cleaning of read/write

This commit is contained in:
Maximilien Rigaut 2022-02-10 02:17:50 +01:00
parent f8b16af96c
commit 1c9f94dcaa
1 changed files with 10 additions and 30 deletions

View File

@ -39,25 +39,15 @@ class PreParser:
self.list_of_paths = list_of_paths + [self.relative_path_resolver] self.list_of_paths = list_of_paths + [self.relative_path_resolver]
self.global_dict_of_imports = dict_of_imports self.global_dict_of_imports = dict_of_imports
self.local_dict_of_imports = {} # Dictionary of all local imports made to avoid duplicate file opening ? self.local_dict_of_imports = {} # Dictionary of all local imports made to avoid duplicate file opening ?
self.file = None
self.saved_import_list = None self.saved_import_list = None
def open(self):
"""
Opens the file and returns a file object.
:return: the file object
"""
self.file = open(self.relative_path_resolver(self.name), 'r')
return self.file
def readlines(self): def readlines(self):
""" """
Reads the file and returns a list of lines. Reads the file and returns a list of lines.
:return: a list of lines :return: a list of lines
""" """
if self.file is None: with open(self.relative_path_resolver(self.name), 'r') as f:
return self.open().readlines() return f.readlines()
return self.file.readlines()
def make_import_list(self): def make_import_list(self):
""" """
@ -115,28 +105,17 @@ class PreParser:
# todo: test import in sub-folders # todo: test import in sub-folders
# todo: test same imports on multiple lines # todo: test same imports on multiple lines
def close(self):
"""
Closes the file.
:return: None
"""
if self.file:
self.file.close()
self.file = None
def __del__(self):
"""
Destructor.
"""
self.close()
def __repr__(self): def __repr__(self):
""" """
Returns a string representation of the PreParser object. Returns a string representation of the PreParser object.
:return: a string representation of the PreParser object :return: a string representation of the PreParser object
""" """
return "PreParser(path={}, file={}, list_of_paths={}, dict_of_imports={})".format( return "PreParser(path={}, name={}, base_path={}, " \
self.path, self.file, self.list_of_paths, self.global_dict_of_imports) "relative_path_resolver={}, ist_of_paths={}, " \
"global_dict_of_imports={}, local_dict_of_imports={}" \
")".format(self.path, self.name, self.base_path,
self.relative_path_resolver, self.list_of_paths,
self.global_dict_of_imports, self.local_dict_of_imports)
def __str__(self): def __str__(self):
""" """
@ -146,7 +125,8 @@ class PreParser:
return self.__repr__() return self.__repr__()
if __name__ == "__main__": # This part is only used for testing
if __name__ == "__main__": # pragma: no cover
site_path = "../../../example_userfiles/index.bpr" site_path = "../../../example_userfiles/index.bpr"
__env = environment.Environment() __env = environment.Environment()
michel = PreParser(site_path, __env) michel = PreParser(site_path, __env)