From b3397bc820c641b98679a7fbae512388fd970067 Mon Sep 17 00:00:00 2001 From: Robin Whittleton Date: Sun, 9 Jun 2024 21:30:10 +0200 Subject: [PATCH 1/2] Remove use of importlib_resources throughout the codebase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We can replace this with importlib.resources instead, now that we’re baselined on a high enough level of Python. --- mypy.ini | 2 +- pylintrc | 2 +- se/commands/compare_versions.py | 4 ++-- se/commands/create_draft.py | 8 ++++---- se/commands/split_file.py | 4 ++-- se/images.py | 4 ++-- se/se_epub_build.py | 18 +++++++++--------- se/se_epub_lint.py | 14 +++++++------- se/spelling.py | 4 ++-- 9 files changed, 30 insertions(+), 30 deletions(-) diff --git a/mypy.ini b/mypy.ini index 557157f3..4426c4a8 100644 --- a/mypy.ini +++ b/mypy.ini @@ -6,5 +6,5 @@ warn_unused_ignores = True [mypy-se.vendor.*] ignore_errors = True -[mypy-importlib_resources,pytest,regex,lxml.*,smartypants,hyphen.*,git,roman,tinycss2.*,cssselect,pyphen,titlecase,magic,ftfy,cairosvg,selenium.*,PIL,natsort,cssutils,psutil,chardet,rich.*] +[mypy-pytest,regex,lxml.*,smartypants,hyphen.*,git,roman,tinycss2.*,cssselect,pyphen,titlecase,magic,ftfy,cairosvg,selenium.*,PIL,natsort,cssutils,psutil,chardet,rich.*] ignore_missing_imports = True diff --git a/pylintrc b/pylintrc index 769c3557..6270aea0 100644 --- a/pylintrc +++ b/pylintrc @@ -6,7 +6,7 @@ ignore=vendor extension-pkg-whitelist=lxml,math,unicodedata [MESSAGES CONTROL] -disable=line-too-long,too-many-nested-blocks,too-many-branches,too-many-statements,too-many-boolean-expressions,too-many-lines,too-many-locals,broad-except,too-few-public-methods,too-many-arguments,too-many-instance-attributes,too-many-public-methods,duplicate-code,deprecated-method +disable=line-too-long,too-many-nested-blocks,too-many-branches,too-many-statements,too-many-boolean-expressions,too-many-lines,too-many-locals,broad-except,too-few-public-methods,too-many-arguments,too-many-instance-attributes,too-many-public-methods,duplicate-code [FORMAT] indent-string=\t diff --git a/se/commands/compare_versions.py b/se/commands/compare_versions.py index f1a15248..f53d3975 100644 --- a/se/commands/compare_versions.py +++ b/se/commands/compare_versions.py @@ -6,8 +6,8 @@ import shutil import tempfile from pathlib import Path +import importlib.resources -import importlib_resources import git from natsort import natsorted from PIL import Image, ImageChops @@ -182,7 +182,7 @@ def compare_versions(plain_output: bool) -> int: for filename in natsorted(list(files_with_differences)): html += f"\t\t
\n\t\t\t

{filename.name}

\n\t\t\t\n\t\t\t\n\t\t
\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: html = file.read().replace("", html.strip()) with open(output_directory / "diff.html", "w", encoding="utf-8") as file: diff --git a/se/commands/create_draft.py b/se/commands/create_draft.py index 8490c84b..432054b5 100644 --- a/se/commands/create_draft.py +++ b/se/commands/create_draft.py @@ -9,10 +9,10 @@ from argparse import Namespace from html import escape from pathlib import Path +import importlib.resources from typing import Optional, Tuple, Union, List, Dict import git -import importlib_resources import regex import requests from rich.console import Console @@ -190,7 +190,7 @@ def _generate_titlepage_svg(title: str, authors: List[str], contributors: dict, canvas_width = se.TITLEPAGE_WIDTH - (TITLEPAGE_HORIZONTAL_PADDING * 2) # Read our template SVG to get some values before we begin - with importlib_resources.open_text("se.data.templates", "titlepage.svg", encoding="utf-8") as file: + with importlib.resources.files("se.data.templates").joinpath("titlepage.svg").open("r", encoding="utf-8") as file: svg = file.read() # Remove the template text elements from the SVG source, we'll write out to it later @@ -286,7 +286,7 @@ def _generate_cover_svg(title: str, authors: List[str], title_string: str) -> st canvas_width = COVER_TITLE_BOX_WIDTH - (COVER_TITLE_BOX_PADDING * 2) # Read our template SVG to get some values before we begin - with importlib_resources.open_text("se.data.templates", "cover.svg", encoding="utf-8") as file: + with importlib.resources.files("se.data.templates").joinpath("cover.svg").open("r", encoding="utf-8") as file: svg = file.read() # Remove the template text elements from the SVG source, we'll write out to it later @@ -406,7 +406,7 @@ def _copy_template_file(filename: str, dest_path: Path) -> None: """ if dest_path.is_dir(): dest_path = dest_path / filename - with importlib_resources.path("se.data.templates", filename) as src_path: + with importlib.resources.as_file(importlib.resources.files("se.data.templates").joinpath(filename)) as src_path: shutil.copyfile(src_path, dest_path) def _add_name_abbr(contributor: str) -> str: diff --git a/se/commands/split_file.py b/se/commands/split_file.py index 9e58b632..7df07948 100644 --- a/se/commands/split_file.py +++ b/se/commands/split_file.py @@ -4,8 +4,8 @@ import argparse from pathlib import Path +import importlib.resources -import importlib_resources import regex import roman @@ -57,7 +57,7 @@ def split_file(plain_output: bool) -> int: se.print_error(f"Couldn’t open file: [path][link=file://{filename}]{filename}[/][/].", plain_output=plain_output) return se.InvalidFileException.code else: - with importlib_resources.open_text("se.data.templates", "chapter-template.xhtml", encoding="utf-8") as file: + with importlib.resources.files("se.data.templates").joinpath("chapter-template.xhtml").open("r", encoding="utf-8") as file: template_xhtml = file.read() # Try to guess the ebook language and update the template accordingly diff --git a/se/images.py b/se/images.py index 651f3a14..41ca9566 100644 --- a/se/images.py +++ b/se/images.py @@ -8,13 +8,13 @@ import tempfile import struct import urllib.parse +import importlib.resources from html import unescape from typing import List, Callable, Dict import regex from PIL import Image, ImageMath, PngImagePlugin, UnidentifiedImageError from PIL.Image import Image as Image_type # Separate import to satisfy type checking -import importlib_resources from lxml import etree import se @@ -313,7 +313,7 @@ def svg_text_to_paths(in_svg: Path, out_svg: Path, remove_style=True) -> None: name_list = {"league_spartan": ["league-spartan-bold.svg"], "sorts_mill_goudy": ["sorts-mill-goudy-italic.svg", "sorts-mill-goudy.svg"]} for font_family, font_names in name_list.items(): for font_name in font_names: - with importlib_resources.path(f"se.data.fonts.{font_family}", font_name) as font_path: + with importlib.resources.as_file(importlib.resources.files(f"se.data.fonts.{font_family}").joinpath(font_name)) as font_path: font_paths.append(font_path) fonts = [] for font_path in font_paths: diff --git a/se/se_epub_build.py b/se/se_epub_build.py index 95eb6b85..6ec74efa 100644 --- a/se/se_epub_build.py +++ b/se/se_epub_build.py @@ -15,8 +15,8 @@ from hashlib import sha1 from html import unescape from pathlib import Path +import importlib.resources from typing import Dict, Tuple, List, Optional -import importlib_resources from cairosvg import svg2png from PIL import Image, ImageOps @@ -179,7 +179,7 @@ def build(self, run_epubcheck: bool, check_only: bool, build_kobo: bool, build_k # Are we including proofreading CSS? if proof: with open(work_compatible_epub_dir / "epub" / "css" / "local.css", "a", encoding="utf-8") as local_css_file: - with importlib_resources.open_text("se.data.templates", "proofreading.css", encoding="utf-8") as proofreading_css_file: + with importlib.resources.files("se.data.templates").joinpath("proofreading.css").open("r", encoding="utf-8") as proofreading_css_file: local_css_file.write("\n" + proofreading_css_file.read()) # Update the release date in the metadata and colophon @@ -227,7 +227,7 @@ def build(self, run_epubcheck: bool, check_only: bool, build_kobo: bool, build_k compatibility_css_filename = "compatibility-white-label.css" with open(work_compatible_epub_dir / "epub" / "css" / "core.css", "a", encoding="utf-8") as core_css_file: - with importlib_resources.open_text("se.data.templates", compatibility_css_filename, encoding="utf-8") as compatibility_css_file: + with importlib.resources.files("se.data.templates").joinpath(compatibility_css_filename).open("r", encoding="utf-8") as compatibility_css_file: core_css_file.write("\n" + compatibility_css_file.read()) # Simplify CSS and tags @@ -430,7 +430,7 @@ def build(self, run_epubcheck: bool, check_only: bool, build_kobo: bool, build_k # Initialize the transform object, if we haven't yet if not mathml_transform: - with importlib_resources.path("se.data", "mathmlcontent2presentation.xsl") as mathml_xsl_filename: + with importlib.resources.as_file(importlib.resources.files("se.data").joinpath("mathmlcontent2presentation.xsl")) as mathml_xsl_filename: mathml_transform = etree.XSLT(etree.parse(str(mathml_xsl_filename))) # Transform the mathml and get a string representation @@ -1064,7 +1064,7 @@ def build(self, run_epubcheck: bool, check_only: bool, build_kobo: bool, build_k node.append(etree.fromstring("""""")) # Now use an XSLT transform to generate the NCX - with importlib_resources.path("se.data", "navdoc2ncx.xsl") as navdoc2ncx_xsl_filename: + with importlib.resources.as_file(importlib.resources.files("se.data").joinpath("navdoc2ncx.xsl")) as navdoc2ncx_xsl_filename: toc_tree = se.epub.convert_toc_to_ncx(work_compatible_epub_dir, toc_filename, navdoc2ncx_xsl_filename) # Convert the