asdf-format/asdf

Add ability to directly access schema file to paths

Opened this issue · 0 comments

In order to automatically generate static Python code based on schemas (for Roman) I ideally would like the ability to directly access the path to a schema using its URI. Currently, most access patterns result in the loaded bytes object containing the file's data rather than just the file path. Note that it is possible for me to work with the bytes, but it requires a lot of hacks deep into the code generator. Instead, if I provide a function that returns a file path given a URI then the generator can simply use its own read and parse functionality to read the schema.

Note that, I can do this for the Rad schemas to get say the aperture-1.0.0 schema using:

from asdf.config import get_config

schema_uri = "asdf://stsci.edu/datamodels/roman/schemas/aperture-1.0.0"

manager = get_config().resource_manager

schema_file_path = manager._mappings_by_uri[schema_uri]._delegate._uri_to_file[schema_uri]

This makes use of several private ASDF APIs. The reason I cannot use the public API is that the DirectoryResourceMapping.__getitem__ object used to register the Rad schemas with ASDF calls .read_bytes() on the path objects in the _uri_to_file dictionary.

Something like a .get_file_path_for_uri(uri) method on the resource manager would be idea for what I need. I may also require a .has_file_path_for_uri(uri) as well.