From 4fb5b778f5a37b808baf60ef9caf7e0b4fce6ac0 Mon Sep 17 00:00:00 2001 From: Silvan Sievers Date: Sat, 30 Dec 2023 15:28:27 +0100 Subject: [PATCH] Code review: run parsers and functions on empty string if file does not exist. --- lab/parser.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lab/parser.py b/lab/parser.py index 0e1de8cbd..cc3956bb6 100644 --- a/lab/parser.py +++ b/lab/parser.py @@ -171,28 +171,27 @@ def parse(self, run_dir, props): content_cache = {} - def get_content(path, file_required=False): + def get_content(path): if path not in content_cache: try: content_cache[path] = path.read_text() except FileNotFoundError: - if file_required: - logging.error( - f'File "{path}", required by some pattern(s), is missing.' - ) - content_cache[path] = None + content_cache[path] = "" return content_cache[path] for filename, file_parser in self.file_parsers.items(): # If filename is absolute, path is set to filename. path = run_dir / filename - required_pattern = any(pattern.required for pattern in file_parser.patterns) - content = get_content(path, file_required=required_pattern) - if content: - file_parser.search_patterns(str(path), content, props) + content = get_content(path) + if not content and any( + pattern.required for pattern in file_parser.patterns + ): + logging.error( + f'File "{path}", required by some pattern(s), is missing.' + ) + file_parser.search_patterns(str(path), content, props) for function in self.functions: path = run_dir / function.filename content = get_content(path) - if content: - function.function(content, props) + function.function(content, props)