-
Notifications
You must be signed in to change notification settings - Fork 83
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
Add parse_retrieved_files option for the PP plugin. #1029
Merged
sphuber
merged 12 commits into
main
from
fix/945-if-requested-do-not-produce-output-data
Nov 20, 2024
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0a40d5a
Add parse_retrieved_files option for the PP plugin.
yakutovicha e5c8200
Fix wrong identation.
yakutovicha 2725431
Merge branch 'main' into fix/945-if-requested-do-not-produce-output-data
yakutovicha ef05535
Fix checking the value of the parse_retrieved_files attribure.
yakutovicha 14c098d
renamings
yakutovicha 5a80c57
Merge branch 'main' into fix/945-if-requested-do-not-produce-output-data
yakutovicha 103a8ec
Keep `keep_plot_file` option for the backward compatibility.
yakutovicha b0bfb2b
Fix tests
yakutovicha 4dee338
Test parse_data_files input.
yakutovicha e519337
Add parser test.
yakutovicha a405a7c
Fix more tests.
yakutovicha 8ab0508
Merge branch 'main' into fix/945-if-requested-do-not-produce-output-data
sphuber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,35 +117,35 @@ def get_key_from_filename(filename): | |
matches = re.search(pattern, filename) | ||
return matches.group(1) | ||
|
||
for filename in filenames: | ||
# Directly parse the retrieved files after reading them to memory (`data_raw`). The raw data | ||
# of each file is released from memory after parsing, to improve memory usage. | ||
if filename.endswith(filename_suffix): | ||
# Read the file to memory | ||
try: | ||
with file_opener(filename) as handle: | ||
data_raw = handle.read() | ||
except OSError: | ||
return self.exit_codes.ERROR_OUTPUT_DATAFILE_READ.format(filename=filename) | ||
# Parse the file | ||
try: | ||
key = get_key_from_filename(filename) | ||
data_parsed.append((key, parsers[iflag](data_raw, self.units_dict[parsed_data['plot_num']]))) | ||
del data_raw | ||
except Exception as exception: # pylint: disable=broad-except | ||
return self.exit_codes.ERROR_OUTPUT_DATAFILE_PARSE.format(filename=filename, exception=exception) | ||
|
||
# If we don't have any parsed files, we exit. Note that this will not catch the case where there should be more | ||
# than one file, but the engine did not retrieve all of them. Since often we anyway don't know how many files | ||
# should be retrieved there really is no way to check this explicitly. | ||
if not data_parsed: | ||
return self.exit_codes.ERROR_OUTPUT_DATAFILE_MISSING.format(filename=filename_prefix) | ||
|
||
# Create output nodes | ||
if len(data_parsed) == 1: | ||
self.out('output_data', data_parsed[0][1]) | ||
else: | ||
self.out('output_data_multiple', dict(data_parsed)) | ||
if self.node.base.attributes.get('parse_retrieved_files', True): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of moving this entire block in a conditional, we can simply exit early. All the code starting from line 67 is only necessary to parse the additional output data. So I propose we simply add
after line 65. |
||
for filename in filenames: | ||
# Directly parse the retrieved files after reading them to memory (`data_raw`). The raw data | ||
# of each file is released from memory after parsing, to improve memory usage. | ||
if filename.endswith(filename_suffix): | ||
# Read the file to memory | ||
try: | ||
with file_opener(filename) as handle: | ||
data_raw = handle.read() | ||
except OSError: | ||
return self.exit_codes.ERROR_OUTPUT_DATAFILE_READ.format(filename=filename) | ||
# Parse the file | ||
try: | ||
key = get_key_from_filename(filename) | ||
data_parsed.append((key, parsers[iflag](data_raw, self.units_dict[parsed_data['plot_num']]))) | ||
del data_raw | ||
except Exception as exception: # pylint: disable=broad-except | ||
return self.exit_codes.ERROR_OUTPUT_DATAFILE_PARSE.format(filename=filename, exception=exception) | ||
|
||
# If we don't have any parsed files, we exit. Note that this will not catch the case where there should be more | ||
# than one file, but the engine did not retrieve all of them. Since often we anyway don't know how many files | ||
# should be retrieved there really is no way to check this explicitly. | ||
if not data_parsed: | ||
return self.exit_codes.ERROR_OUTPUT_DATAFILE_MISSING.format(filename=filename_prefix) | ||
|
||
if len(data_parsed) == 1: | ||
self.out('output_data', data_parsed[0][1]) | ||
else: | ||
self.out('output_data_multiple', dict(data_parsed)) | ||
|
||
return self.exit(logs=logs) | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a docstring as to what this does