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

Support asdf:// URI scheme #854

Merged

Conversation

eslavich
Copy link
Contributor

@eslavich eslavich commented Aug 6, 2020

This PR allow asdf to properly resolve relative references against asdf:// URIs. The urllib.parse module maintains a list of URI schemes that should behave like http with regard to resolving relative paths, and needed asdf added to its list.

There's a second change in here that fixes a long-standing issue with resolving references in metaschemas. It's common for such schemas to define an id property, but the asdf library didn't know not to treat that property object as a URI. The fix is to check that id is a string before assuming it's a URI.

@eslavich eslavich added this to the 2.8.0 milestone Aug 6, 2020
Copy link
Contributor

@jdavies-st jdavies-st left a comment

Choose a reason for hiding this comment

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

Looks good. Just a couple questions for my own understanding.

"""
assert generic_io.resolve_uri(
'{}://somewhere.org/some-schema'.format(protocol), '#/definitions/foo'
) == '{}://somewhere.org/some-schema#/definitions/foo'.format(protocol)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use f-strings here and bump the minimum version of python to 3.6 for our 2.8 release?

I don't see a good reason to keep supporting python 3.5 when it is no longer supported by astropy and numpy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're asking the good questions. I'll start an email discussion.

Comment on lines +11 to +18
# We're importing our own copy of urllib.parse because
# we need to patch it to support asdf:// URIs, but it'd
# be irresponsible to do this for all users of a
# standard library.
urllib_parse_spec = importlib.util.find_spec('urllib.parse')
patched_urllib_parse = importlib.util.module_from_spec(urllib_parse_spec)
urllib_parse_spec.loader.exec_module(patched_urllib_parse)
del urllib_parse_spec
Copy link
Contributor

Choose a reason for hiding this comment

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

What the advantage of doing it this way rather than

import urllib.parse as patched_urllib_parse

It's still local to this module's namespace, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately while that aliases the module reference to a new name, it still points to the same module object. Import caches the module after loading it the first time so that other imports don't have to read and parse the module's code again.

# module1.py
import urllib.parse as module1_alias

# module2.py
import urllib.parse as module2_alias

# Fire up a console:
In [1]: import module1

In [2]: import module2

In [3]: module1.module1_alias is module2.module2_alias
Out[3]: True

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see. I didn't realize there was caching going on. This bypasses that. #themoreyouknow

@eslavich eslavich merged commit 30ada52 into asdf-format:master Aug 6, 2020
eslavich pushed a commit that referenced this pull request Aug 6, 2020
@eslavich eslavich deleted the eslavich-handle-asdf-uri-scheme branch August 6, 2020 16:33
# urllib.parse needs to know that it should treat asdf://
# URIs like http:// URIs for the purposes of joining
# a relative path to a base URI.
patched_urllib_parse.uses_relative.append('asdf')
Copy link
Contributor

Choose a reason for hiding this comment

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

Digging deep into the standard library. What a naughty boy. ;-) I had to go look at the library source code to see this explained.

Copy link
Contributor

@perrygreenfield perrygreenfield left a comment

Choose a reason for hiding this comment

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

LGTM

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.

3 participants