-
Notifications
You must be signed in to change notification settings - Fork 57
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,27 @@ | |
import math | ||
import struct | ||
import types | ||
import importlib.util | ||
|
||
from urllib.parse import urljoin | ||
from urllib.request import pathname2url | ||
from urllib import parse as urlparse | ||
|
||
import numpy as np | ||
|
||
# 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 | ||
Comment on lines
+11
to
+18
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. What the advantage of doing it this way rather than
It's still local to this module's namespace, right? 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. 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 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. Ah, I see. I didn't realize there was caching going on. This bypasses that. #themoreyouknow |
||
|
||
# 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') | ||
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. Digging deep into the standard library. What a naughty boy. ;-) I had to go look at the library source code to see this explained. |
||
patched_urllib_parse.uses_netloc.append('asdf') | ||
|
||
|
||
__all__ = ['human_list', 'get_array_base', 'get_base_uri', 'filepath_to_url', | ||
'iter_subclasses', 'calculate_padding', 'resolve_name', 'NotSet', | ||
|
@@ -58,15 +72,15 @@ def get_base_uri(uri): | |
""" | ||
For a given URI, return the part without any fragment. | ||
""" | ||
parts = urlparse.urlparse(uri) | ||
return urlparse.urlunparse(list(parts[:5]) + ['']) | ||
parts = patched_urllib_parse.urlparse(uri) | ||
return patched_urllib_parse.urlunparse(list(parts[:5]) + ['']) | ||
|
||
|
||
def filepath_to_url(path): | ||
""" | ||
For a given local file path, return a file:// url. | ||
""" | ||
return urljoin('file:', pathname2url(path)) | ||
return patched_urllib_parse.urljoin('file:', pathname2url(path)) | ||
|
||
|
||
def iter_subclasses(cls): | ||
|
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.
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
andnumpy
.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.
You're asking the good questions. I'll start an email discussion.