diff --git a/nikola/nikola.py b/nikola/nikola.py index 226d4e2bd3..c419448fdc 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -1031,7 +1031,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 = [ - resources.files('nikola').joinpath('plugins'), + str(resources.path('nikola', 'plugins')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath('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 +1694,7 @@ def _register_templated_shortcodes(self): """ self.register_shortcode('template', self._template_shortcode_handler) - builtin_sc_dir = resources.files('nikola').joinpath(os.path.join('data', 'shortcodes', utils.get_template_engine(self.THEMES))) + builtin_sc_dir = str(resources.path('nikola')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath(os.path.join('data', 'shortcodes', utils.get_template_engine(self.THEMES)))) for sc_dir in [builtin_sc_dir, 'shortcodes']: if not os.path.isdir(sc_dir): diff --git a/nikola/plugins/basic_import.py b/nikola/plugins/basic_import.py index ad53572511..b0b62b5b2f 100644 --- a/nikola/plugins/basic_import.py +++ b/nikola/plugins/basic_import.py @@ -30,11 +30,12 @@ import csv import datetime import os +import sys from urllib.parse import urlparse from lxml import etree, html from mako.template import Template -from pkg_resources import resource_filename +from importlib import resources from nikola import utils @@ -98,7 +99,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 = str(resources.path('nikola', 'conf.py.in')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath('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 9668b784fc..5c75bd88bb 100644 --- a/nikola/plugins/command/auto/__init__.py +++ b/nikola/plugins/command/auto/__init__.py @@ -39,7 +39,7 @@ import webbrowser import blinker -import pkg_resources +from importlib import resources from nikola.plugin_categories import Command from nikola.utils import dns_sd, req_missing, get_theme_path, makedirs @@ -220,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(str(resources.path('nikola', '')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath(''))) 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..33a3dffc5f 100644 --- a/nikola/plugins/command/init.py +++ b/nikola/plugins/command/init.py @@ -30,6 +30,7 @@ import io import json import os +import sys import shutil import textwrap import unidecode @@ -38,7 +39,7 @@ import dateutil.tz import dateutil.zoneinfo from mako.template import Template -from pkg_resources import resource_filename +from importlib import resources import nikola from nikola.nikola import DEFAULT_INDEX_READ_MORE_LINK, DEFAULT_FEED_READ_MORE_LINK, LEGAL_VALUES @@ -273,13 +274,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 = str(resources.path('nikola', os.path.join('data', 'samplesite'))) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath(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 = str(resources.path('nikola', 'conf.py.in')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath('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 +289,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 = str(resources.path('nikola', 'conf.py.in')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath('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..d9ceae3df3 100644 --- a/nikola/plugins/command/rst2html/__init__.py +++ b/nikola/plugins/command/rst2html/__init__.py @@ -28,8 +28,9 @@ import io +import sys import lxml.html -from pkg_resources import resource_filename +from importlib import resources from mako.template import Template from nikola.plugin_categories import Command @@ -53,11 +54,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 = str(resources.path('nikola', 'data/themes/base/assets/css/rst_base.css')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath('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 = str(resources.path('nikola', 'plugins/command/rst2html/rst2html.tmpl')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath('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..b24b23ac39 100644 --- a/nikola/plugins/command/theme.py +++ b/nikola/plugins/command/theme.py @@ -38,7 +38,7 @@ import pygments from pygments.lexers import PythonLexer from pygments.formatters import TerminalFormatter -from pkg_resources import resource_filename +from importlib import resources from nikola.plugin_categories import Command from nikola import utils @@ -287,7 +287,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 + [str(resources.path('nikola', os.path.join('data', 'themes'))) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath(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..9bc3f5ef4e 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -44,7 +44,7 @@ from collections import defaultdict, OrderedDict from collections.abc import Callable, Iterable from html import unescape as html_unescape -from importlib import reload as _reload +from importlib import resources, reload as _reload from unicodedata import normalize as unicodenormalize from urllib.parse import quote as urlquote from urllib.parse import unquote as urlunquote @@ -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 @@ -586,7 +585,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 = str(resources.path('nikola', os.path.join('data', 'themes', theme))) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath(os.path.join('data', 'themes', theme))) if os.path.isdir(dir_name): return dir_name raise Exception("Can't find theme '{0}'".format(theme))