From 454114971f2648167573b9a43be4f169fffe908f Mon Sep 17 00:00:00 2001 From: Abdelwakil Benouis <111845235+fondbcn@users.noreply.github.com> Date: Thu, 1 Feb 2024 23:17:39 +0100 Subject: [PATCH] replacing pkg_resources with importlib (#3749) Fixes #3743 --- nikola/nikola.py | 5 ++--- nikola/plugins/basic_import.py | 3 +-- nikola/plugins/command/auto/__init__.py | 5 ++--- nikola/plugins/command/init.py | 9 ++++----- nikola/plugins/command/rst2html/__init__.py | 6 +++--- nikola/plugins/command/theme.py | 3 +-- nikola/utils.py | 16 ++++++++++++++-- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/nikola/nikola.py b/nikola/nikola.py index 62f9279069..124b030054 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -46,7 +46,6 @@ import lxml.html import natsort import PyRSS2Gen as rss -from pkg_resources import resource_filename from blinker import signal from . import DEBUG, SHOW_TRACEBACKS, filters, utils, hierarchy_utils, shortcodes @@ -1031,7 +1030,7 @@ def init_plugins(self, commands_only=False, load_all=False): extra_plugins_dirs = self.config['EXTRA_PLUGINS_DIRS'] self._loading_commands_only = commands_only self._plugin_places = [ - resource_filename('nikola', 'plugins'), + utils.pkg_resources_path('nikola', 'plugins'), os.path.expanduser(os.path.join('~', '.nikola', 'plugins')), os.path.join(os.getcwd(), 'plugins'), ] + [path for path in extra_plugins_dirs if path] @@ -1694,7 +1693,7 @@ def _register_templated_shortcodes(self): """ self.register_shortcode('template', self._template_shortcode_handler) - builtin_sc_dir = resource_filename( + builtin_sc_dir = utils.pkg_resources_path( 'nikola', os.path.join('data', 'shortcodes', utils.get_template_engine(self.THEMES))) diff --git a/nikola/plugins/basic_import.py b/nikola/plugins/basic_import.py index ad53572511..4f693960a2 100644 --- a/nikola/plugins/basic_import.py +++ b/nikola/plugins/basic_import.py @@ -34,7 +34,6 @@ from lxml import etree, html from mako.template import Template -from pkg_resources import resource_filename from nikola import utils @@ -98,7 +97,7 @@ def generate_base_site(self): utils.LOGGER.warning('The folder {0} already exists - assuming that this is a ' 'already existing Nikola site.'.format(self.output_folder)) - filename = resource_filename('nikola', 'conf.py.in') + filename = utils.pkg_resources_path('nikola', 'conf.py.in') # The 'strict_undefined=True' will give the missing symbol name if any, # (ex: NameError: 'THEME' is not defined ) # for other errors from mako/runtime.py, you can add format_extensions=True , diff --git a/nikola/plugins/command/auto/__init__.py b/nikola/plugins/command/auto/__init__.py index f6defb62c1..aecbe4114d 100644 --- a/nikola/plugins/command/auto/__init__.py +++ b/nikola/plugins/command/auto/__init__.py @@ -40,10 +40,9 @@ from pathlib import Path import blinker -import pkg_resources from nikola.plugin_categories import Command -from nikola.utils import dns_sd, req_missing, get_theme_path, makedirs +from nikola.utils import dns_sd, req_missing, get_theme_path, makedirs, pkg_resources_path try: import aiohttp @@ -221,7 +220,7 @@ def _execute(self, options, args): watched.add(item) watched |= self.site.registered_auto_watched_folders # Nikola itself (useful for developers) - watched.add(pkg_resources.resource_filename('nikola', '')) + watched.add(pkg_resources_path('nikola', '')) out_folder = self.site.config['OUTPUT_FOLDER'] if not os.path.exists(out_folder): diff --git a/nikola/plugins/command/init.py b/nikola/plugins/command/init.py index cf22a4467d..df68e0e4f1 100644 --- a/nikola/plugins/command/init.py +++ b/nikola/plugins/command/init.py @@ -38,12 +38,11 @@ import dateutil.tz import dateutil.zoneinfo from mako.template import Template -from pkg_resources import resource_filename import nikola from nikola.nikola import DEFAULT_INDEX_READ_MORE_LINK, DEFAULT_FEED_READ_MORE_LINK, LEGAL_VALUES from nikola.plugin_categories import Command -from nikola.utils import ask, ask_yesno, get_logger, makedirs, load_messages +from nikola.utils import ask, ask_yesno, get_logger, makedirs, load_messages, pkg_resources_path from nikola.packages.tzlocal import get_localzone @@ -273,13 +272,13 @@ class CommandInit(Command): @classmethod def copy_sample_site(cls, target): """Copy sample site data to target directory.""" - src = resource_filename('nikola', os.path.join('data', 'samplesite')) + src = pkg_resources_path('nikola', os.path.join('data', 'samplesite')) shutil.copytree(src, target) @staticmethod def create_configuration(target): """Create configuration file.""" - template_path = resource_filename('nikola', 'conf.py.in') + template_path = pkg_resources_path('nikola', 'conf.py.in') conf_template = Template(filename=template_path) conf_path = os.path.join(target, 'conf.py') with io.open(conf_path, 'w+', encoding='utf8') as fd: @@ -288,7 +287,7 @@ def create_configuration(target): @staticmethod def create_configuration_to_string(): """Return configuration file as a string.""" - template_path = resource_filename('nikola', 'conf.py.in') + template_path = pkg_resources_path('nikola', 'conf.py.in') conf_template = Template(filename=template_path) return conf_template.render(**prepare_config(SAMPLE_CONF)) diff --git a/nikola/plugins/command/rst2html/__init__.py b/nikola/plugins/command/rst2html/__init__.py index 4fdd36f65b..dd8298adb8 100644 --- a/nikola/plugins/command/rst2html/__init__.py +++ b/nikola/plugins/command/rst2html/__init__.py @@ -29,9 +29,9 @@ import io import lxml.html -from pkg_resources import resource_filename from mako.template import Template from nikola.plugin_categories import Command +from nikola.utils import pkg_resources_path class CommandRst2Html(Command): @@ -53,11 +53,11 @@ def _execute(self, options, args): data = in_file.read() output, error_level, deps, shortcode_deps = compiler.compile_string(data, source, True) - rstcss_path = resource_filename('nikola', 'data/themes/base/assets/css/rst_base.css') + rstcss_path = pkg_resources_path('nikola', 'data/themes/base/assets/css/rst_base.css') with io.open(rstcss_path, "r", encoding="utf-8-sig") as fh: rstcss = fh.read() - template_path = resource_filename('nikola', 'plugins/command/rst2html/rst2html.tmpl') + template_path = pkg_resources_path('nikola', 'plugins/command/rst2html/rst2html.tmpl') template = Template(filename=template_path) template_output = template.render(rstcss=rstcss, output=output) parser = lxml.html.HTMLParser(remove_blank_text=True) diff --git a/nikola/plugins/command/theme.py b/nikola/plugins/command/theme.py index f7608b547c..388001fc20 100644 --- a/nikola/plugins/command/theme.py +++ b/nikola/plugins/command/theme.py @@ -38,7 +38,6 @@ import pygments from pygments.lexers import PythonLexer from pygments.formatters import TerminalFormatter -from pkg_resources import resource_filename from nikola.plugin_categories import Command from nikola import utils @@ -287,7 +286,7 @@ def list_installed(self): print("Installed Themes:") print("-----------------") themes = [] - themes_dirs = self.site.themes_dirs + [resource_filename('nikola', os.path.join('data', 'themes'))] + themes_dirs = self.site.themes_dirs + [utils.pkg_resources_path('nikola', os.path.join('data', 'themes'))] for tdir in themes_dirs: if os.path.isdir(tdir): themes += [(i, os.path.join(tdir, i)) for i in os.listdir(tdir)] diff --git a/nikola/utils.py b/nikola/utils.py index 20e35529ce..71c2119fd9 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -60,7 +60,6 @@ from blinker import signal from doit import tools from doit.cmdparse import CmdParse -from pkg_resources import resource_filename from nikola.packages.pygments_better_html import BetterHtmlFormatter from typing import List from unidecode import unidecode @@ -71,6 +70,11 @@ from .hierarchy_utils import TreeNode, clone_treenode, flatten_tree_structure, sort_classifications from .hierarchy_utils import join_hierarchical_category_path, parse_escaped_hierarchical_category_name +if sys.version_info.minor <= 8: + from pkg_resources import resource_filename +else: + from importlib import resources + try: import toml except ImportError: @@ -577,6 +581,14 @@ def __repr__(self): sort_keys=True)) +def pkg_resources_path(package, resource): + """Return path to a resource from the package with the given name.""" + if sys.version_info.minor <= 8: + return resource_filename(package, resource) + else: + return str(resources.files(package).joinpath(resource)) + + def get_theme_path_real(theme, themes_dirs): """Return the path where the given theme's files are located. @@ -586,7 +598,7 @@ def get_theme_path_real(theme, themes_dirs): dir_name = os.path.join(themes_dir, theme) if os.path.isdir(dir_name): return dir_name - dir_name = resource_filename('nikola', os.path.join('data', 'themes', theme)) + dir_name = pkg_resources_path('nikola', os.path.join('data', 'themes', theme)) if os.path.isdir(dir_name): return dir_name raise Exception("Can't find theme '{0}'".format(theme))