Skip to content

Commit

Permalink
Revise handling of missing files in parser (aibasel#123)
Browse files Browse the repository at this point in the history
Let parsers print an error if the file for a required pattern is missing. Call parser functions with empty string for missing files.
---------
Co-authored-by: Jendrik Seipp <[email protected]>
  • Loading branch information
Silvan Sievers authored Dec 30, 2023
1 parent baf0158 commit 7c50227
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Lab
^^^
* Allow passing properties files to fetchers directly (Jendrik Seipp).
* Let fetch and report steps log only the total number of unexplained errors instead of printing all of them to stderr (Jendrik Seipp).
* Let parsers print an error if the file for a required pattern is missing. Call parser functions with empty string for missing files (Silvan Sievers).

Downward Lab
^^^^^^^^^^^^
Expand Down
12 changes: 7 additions & 5 deletions lab/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,21 @@ def get_content(path):
try:
content_cache[path] = path.read_text()
except FileNotFoundError:
logging.info(f'File "{path}" is missing and thus not parsed.')
content_cache[path] = None
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
content = get_content(path)
if content:
if content is None:
if any(pattern.required for pattern in file_parser.patterns):
logging.error(f'Required file "{path}" is missing.')
else:
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)
# Call function with empty string if file is missing.
content = get_content(path) or ""
function.function(content, props)

0 comments on commit 7c50227

Please sign in to comment.