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

Use importlib.resources to load files #5896

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions xarray/core/formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from collections import OrderedDict
from functools import lru_cache, partial
from html import escape

import pkg_resources
from importlib import resources

from .formatting import inline_variable_array_repr, short_data_repr
from .options import _get_boolean_with_default
Expand All @@ -15,7 +14,7 @@
def _load_static_files():
"""Lazily load the resource files into memory the first time they are needed"""
return [
pkg_resources.resource_string("xarray", fname).decode("utf8")
resources.files("xarray").joinpath(fname).read_bytes().decode("utf8")
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a reason why you don't use read_text? Otherwise I'd use this:

Suggested change
resources.files("xarray").joinpath(fname).read_bytes().decode("utf8")
importlib.resources.read_text("xarray", fname, encoding="utf-8")

which is probably the same as

Suggested change
resources.files("xarray").joinpath(fname).read_bytes().decode("utf8")
resources.files("xarray").joinpath(fname).read_text(encoding="utf-8")

Copy link
Contributor Author

@Illviljan Illviljan Oct 25, 2021

Choose a reason for hiding this comment

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

It was the recommended replacement method, https://importlib-resources.readthedocs.io/en/latest/migration.html#pkg-resources-resource-string

Because it crashes:

[
    resources.read_text("xarray", fname, encoding="utf-8")
    for fname in STATIC_FILES
]
(...)
ValueError: 'static/html/icons-svg-inline.html' must be only a file name

And isn't an exact replacement:

a = [
    pkg_resources.resource_string("xarray", fname).decode("utf8")
    for fname in STATIC_FILES
]

b = [
    resources.files("xarray").joinpath(fname).read_text(encoding="utf-8")  # 3.9 only
    for fname in STATIC_FILES
]

a == b # b is missing \r at least
Out[86]: False

Copy link
Collaborator

@keewis keewis Oct 25, 2021

Choose a reason for hiding this comment

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

right, for py3.9 the replacement is exact (at least for me) but maybe not on other python versions or platforms? I'm guessing read_text will transform newlines to \r\n on windows?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Might be the platform then, I used windows 10 and python 3.9.6.

Copy link
Collaborator

@keewis keewis Oct 25, 2021

Choose a reason for hiding this comment

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

and I'm on linux with py3.9.

Does a contain \r for you? If so, it might be the newline option to open...

for fname in STATIC_FILES
]

Expand Down