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

Eliminate dependence on pyyaml-include by adapting Butler loader #89

Merged
merged 11 commits into from
Apr 23, 2024

Conversation

JoanneBogart
Copy link
Collaborator

Adapted the Butler loader, eliminating its use of lsst.resources so there are no dependencies on LSST Science Pipelines.
Moved yaml parsing code into config_utils.py

… it is an absolute path. 2) Otherwise it is interpreted as relative to current file (not necessarily the same as top-level file)
Copy link
Collaborator

@jchiang87 jchiang87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be some test code that runs on example yaml files. Those files will also demonstrate more clearly how the include directives can be used.

return result

else:
print("Error:: unrecognised node type in !include statement",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can a logger be used instead of a print statement here and below so that the output level can be controlled?

stream : text io stream
The stream to parse.

This code was adapted from the LSST Science Pipelines Butler.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be useful to give a more complete reference to the LSST code, e.g., a url pointing to the daf_butler code.

else:
actual_path = os.path.join(self._current_dir, filepath)
print("Opening YAML file via !include: %s", actual_path)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formatting works for a logger message, but a print statement needs to have

print("Opening YAML file via !include: %s" % actual_path)

"""
def __init__(self, filestream):
super().__init__(filestream)
self._root = os.path.dirname(filestream.name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are self._root and self._current_dir both needed? The base class doesn't seem to have a _root attribute, so both of these don't seem to be needed.

Copy link
Collaborator Author

@JoanneBogart JoanneBogart Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That may be left over from earlier behavior, where all include paths were evaluated relative to initial file. I changed that to be relative to file with the reference. Will look into it.
Indeed, it's not used anywhere.

Comment on lines 78 to 83
old_current = self._current_dir
self._current_dir = actual_path
# Read all the data from the resource
with open(actual_path) as f:
content = yaml.load(f, YamlIncludeLoader)
self._current_dir = old_current
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to do any special handling of self._current_dir here since it's set per-instance of YamlIncludeLoader. So this code can just be

# Read all the data from the resource
with open(actual_path) as f:
    content = yaml.load(f, YamlIncludeLoader)

The other lines don't have any net effect, otherwise it would have been necessary to set self._current_dir = os.path.dirname(actual_path) on line 79.

Copy link
Collaborator

@jchiang87 jchiang87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine, though it would be good to delete the unneeded commented out code.

@JoanneBogart JoanneBogart merged commit 7a748c7 into main Apr 23, 2024
1 check passed
@JoanneBogart JoanneBogart deleted the u/jrbogart/yaml-include-class branch April 23, 2024 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants