Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow calling parser without reading/writing files #94

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions lab/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,17 @@ def add_function(self, function, file="run.log"):
"""
self.file_parsers[file].add_function(function)

def parse(self):
"""Search all patterns and apply all functions.

The found values are written to the run's ``properties`` file.

"""
run_dir = os.path.abspath(".")
prop_file = os.path.join(run_dir, "properties")
self.props = tools.Properties(filename=prop_file)
def fill_file_parsers_from_strings(self, file_contents):
"""Store the given contents in the file parsers."""
for filename, file_parser in list(self.file_parsers.items()):
if filename in file_contents:
file_parser.content = file_contents[filename]
else:
logging.info(f'File "{filename}" is missing and thus not parsed.')
del self.file_parsers[filename]

def fill_file_parsers_from_files(self):
"""Read file contents and store them in the file parsers."""
for filename, file_parser in list(self.file_parsers.items()):
# If filename is absolute it will not be changed here.
path = os.path.join(run_dir, filename)
Expand All @@ -209,10 +210,40 @@ def parse(self):
else:
logging.error(f'Failed to read "{path}": {err}')

def execute_file_parsers(self):
"""Search all patterns and apply all functions.

The found values are stored in self.props.
Requires filling the file_parsers first using
fill_file_parsers_from_strings or fill_file_parsers_from_files.

"""
for file_parser in self.file_parsers.values():
self.props.update(file_parser.search_patterns())

for file_parser in self.file_parsers.values():
file_parser.apply_functions(self.props)

def parse_from_strings(self, file_contents):
"""Search all patterns and apply all functions.

The found values are stored in self.props.

"""
self.fill_file_parsers_from_strings(file_contents)
self.execute_file_parsers()

def parse(self):
"""Search all patterns and apply all functions.

The found values are written to the run's ``properties`` file.

"""
run_dir = os.path.abspath(".")
prop_file = os.path.join(run_dir, "properties")
self.props = tools.Properties(filename=prop_file)

self.fill_file_parsers_from_files()
self.execute_file_parsers()

self.props.write()