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

Remove use of importlib_resources throughout the codebase #707

Merged
merged 2 commits into from
Jun 10, 2024

Conversation

robinwhittleton
Copy link
Member

We can replace this with importlib.resources instead, now that we’re baselined on a high enough level of Python.

I’ve included a small commit that doesn’t have any functional change, but tidies up a piece of inconsistent linting code I noticed while doing this.

I’ll leave it up to you to do the other couple of setup.py changes, unless you would like me to add a commit. They’re:

  1. Remove the importlib_resources requirement as it’s no longer used in the codebase.
  2. Bump the minimum Python version to 3.10. I don’t think there’s any value in going to 3.9: we’re not using it on the server for a start. Fedora 34 was the last version to ship with 3.9 and that went EOL over two years ago. On Mac people will just install whatever version necessary.

We can replace this with importlib.resources instead, now that we’re baselined on a high enough level of Python.
Doesn’t break anything as it stands as it overrides the earlier core_css_path_name, but we might as well match the process for the rest of the checks.
@@ -182,7 +182,7 @@ def compare_versions(plain_output: bool) -> int:
for filename in natsorted(list(files_with_differences)):
html += f"\t\t<section>\n\t\t\t<h1>{filename.name}</h1>\n\t\t\t<img src=\"{filename.name}-original.png\">\n\t\t\t<img src=\"{filename.name}-new.png\">\n\t\t</section>\n"

with importlib_resources.open_text("se.data.templates", "diff-template.html", encoding="utf-8") as file:
with importlib.resources.files("se.data.templates").joinpath("diff-template.html").open("r", encoding="utf-8") as file:
Copy link
Member

Choose a reason for hiding this comment

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

Can this accept a Path object instead of a string? If so then we should do that.

Copy link
Member Author

Choose a reason for hiding this comment

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

I’ll investigate!

Copy link
Member Author

Choose a reason for hiding this comment

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

So I think the answer’s no (thought this isn’t my forte): the files part creates an “Anchor” from the package, and joinpath then joins that with the given string to create a Path that can be opened.

If you think this is a bit wordy then we could cut it down with from importlib.resources import files, as_file and use them directly. Might need to import as though, files on its own is a bit generic.

Copy link
Member

Choose a reason for hiding this comment

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

The docs say an anchor can be a string, so we should create a path using the Path object and then just cast it as a string when passed to .files(). So something like this should work (though I haven't tested):

with importlib.resources.files(str(Path("se.data.templates") / "diff-template.html")).open("r", encoding="utf-8") as file:

The reason to try to do it this way is consistency, as the rest of the codebase uses Path and slashes, and not string concat or joinpath.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tested that in create_draft.py with the titlepage.svg generator, and it doesn’t work, ended up with:

ModuleNotFoundError: No module named 'se.data.templates/titlepage'

I think the problem is that these aren’t real Paths, they’re views into the package. Only when you get to the end is a Path produced for the rest of the system to interact with.

@acabal acabal merged commit ef8332b into master Jun 10, 2024
2 checks passed
@acabal
Copy link
Member

acabal commented Jun 10, 2024

OK, I guess it doesn't work like I thought it did. Anyway this looks good, thanks!

@robinwhittleton robinwhittleton deleted the remove_importlib_resources branch June 11, 2024 07:17
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