From 3d732524fde6d3dcea80456b6df841d02f5e708a Mon Sep 17 00:00:00 2001 From: fondbcn Date: Wed, 24 Jan 2024 12:05:09 +0100 Subject: [PATCH 01/12] replacing pkg_resources with importlib --- nikola/nikola.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/nikola/nikola.py b/nikola/nikola.py index 62f9279069..226d4e2bd3 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -46,7 +46,7 @@ import lxml.html import natsort import PyRSS2Gen as rss -from pkg_resources import resource_filename +from importlib import resources from blinker import signal from . import DEBUG, SHOW_TRACEBACKS, filters, utils, hierarchy_utils, shortcodes @@ -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 = [ - resource_filename('nikola', 'plugins'), + 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,9 +1694,7 @@ def _register_templated_shortcodes(self): """ self.register_shortcode('template', self._template_shortcode_handler) - builtin_sc_dir = resource_filename( - 'nikola', - os.path.join('data', 'shortcodes', utils.get_template_engine(self.THEMES))) + builtin_sc_dir = 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): From 6f5cb46b2ea04fd91ff503226835bd1ed49f3b98 Mon Sep 17 00:00:00 2001 From: fondbcn Date: Fri, 26 Jan 2024 10:53:14 +0100 Subject: [PATCH 02/12] replacing all pkg_resources with importlib --- nikola/nikola.py | 4 ++-- nikola/plugins/basic_import.py | 5 +++-- nikola/plugins/command/auto/__init__.py | 4 ++-- nikola/plugins/command/init.py | 9 +++++---- nikola/plugins/command/rst2html/__init__.py | 7 ++++--- nikola/plugins/command/theme.py | 4 ++-- nikola/utils.py | 5 ++--- 7 files changed, 20 insertions(+), 18 deletions(-) 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 f6defb62c1..0a066f2441 100644 --- a/nikola/plugins/command/auto/__init__.py +++ b/nikola/plugins/command/auto/__init__.py @@ -40,7 +40,7 @@ from pathlib import Path 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 @@ -221,7 +221,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)) From 2f9a06f8219d3e5ea0a7537da58ef6cf3e68a107 Mon Sep 17 00:00:00 2001 From: fondbcn Date: Sat, 27 Jan 2024 09:43:14 +0100 Subject: [PATCH 03/12] resolving deprecated functions with utils.py --- nikola/nikola.py | 5 ++--- nikola/plugins/basic_import.py | 4 +--- nikola/plugins/command/auto/__init__.py | 5 ++--- nikola/plugins/command/init.py | 10 ++++------ nikola/plugins/command/rst2html/__init__.py | 7 +++---- nikola/plugins/command/theme.py | 3 +-- nikola/utils.py | 10 +++++++++- 7 files changed, 22 insertions(+), 22 deletions(-) diff --git a/nikola/nikola.py b/nikola/nikola.py index c419448fdc..48f3071524 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -46,7 +46,6 @@ import lxml.html import natsort import PyRSS2Gen as rss -from importlib import resources 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 = [ - str(resources.path('nikola', 'plugins')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath('plugins')), + utils.pkg_resources('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 = 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)))) + builtin_sc_dir = utils.pkg_resources('nikola', 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 b0b62b5b2f..1499578b19 100644 --- a/nikola/plugins/basic_import.py +++ b/nikola/plugins/basic_import.py @@ -30,12 +30,10 @@ import csv import datetime import os -import sys from urllib.parse import urlparse from lxml import etree, html from mako.template import Template -from importlib import resources from nikola import utils @@ -99,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 = str(resources.path('nikola', 'conf.py.in')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath('conf.py.in')) + filename = utils.pkg_resources('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 0a066f2441..6cd0dc0806 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 -from importlib import 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 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(str(resources.path('nikola', '')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath(''))) + watched.add(pkg_resources('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 33a3dffc5f..3c5a16503e 100644 --- a/nikola/plugins/command/init.py +++ b/nikola/plugins/command/init.py @@ -30,7 +30,6 @@ import io import json import os -import sys import shutil import textwrap import unidecode @@ -39,12 +38,11 @@ import dateutil.tz import dateutil.zoneinfo from mako.template import Template -from importlib import resources 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 from nikola.packages.tzlocal import get_localzone @@ -274,13 +272,13 @@ class CommandInit(Command): @classmethod def copy_sample_site(cls, target): """Copy sample site data to target directory.""" - 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'))) + src = pkg_resources('nikola', os.path.join('data', 'samplesite')) shutil.copytree(src, target) @staticmethod def create_configuration(target): """Create configuration file.""" - template_path = str(resources.path('nikola', 'conf.py.in')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath('conf.py.in')) + template_path = pkg_resources('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: @@ -289,7 +287,7 @@ def create_configuration(target): @staticmethod def create_configuration_to_string(): """Return configuration file as a string.""" - template_path = str(resources.path('nikola', 'conf.py.in')) if sys.version_info.minor == 8 else str(resources.files('nikola').joinpath('conf.py.in')) + template_path = pkg_resources('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 d9ceae3df3..ce4c474788 100644 --- a/nikola/plugins/command/rst2html/__init__.py +++ b/nikola/plugins/command/rst2html/__init__.py @@ -28,11 +28,10 @@ import io -import sys import lxml.html -from importlib import resources from mako.template import Template from nikola.plugin_categories import Command +from nikola.utils import pkg_resources class CommandRst2Html(Command): @@ -54,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 = 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')) + rstcss_path = pkg_resources('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 = 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_path = pkg_resources('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 b24b23ac39..f6bcbee8f9 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 importlib import resources 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 + [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')))] + themes_dirs = self.site.themes_dirs + [utils.pkg_resources('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 9bc3f5ef4e..19a4f49d88 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -576,6 +576,14 @@ def __repr__(self): sort_keys=True)) +def pkg_resources(package, resource): + """Return the resource based on the python version""" + if sys.version_info.minor <= 8: + return resources.path(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. @@ -585,7 +593,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 = 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))) + dir_name = pkg_resources('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)) From 5aa00d885de8778638123e505845e8c9374c8bbd Mon Sep 17 00:00:00 2001 From: fondbcn Date: Sat, 27 Jan 2024 20:59:37 +0100 Subject: [PATCH 04/12] applying str to paths --- nikola/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nikola/utils.py b/nikola/utils.py index 19a4f49d88..e8efba290b 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -577,9 +577,9 @@ def __repr__(self): def pkg_resources(package, resource): - """Return the resource based on the python version""" + """Return the resource based on the python version.""" if sys.version_info.minor <= 8: - return resources.path(package, resource) + return str(resources.path(package, resource)) else: return str(resources.files(package).joinpath(resource)) From c107acddb11b0a945a3435232e6ebb524906df7e Mon Sep 17 00:00:00 2001 From: fondbcn Date: Tue, 30 Jan 2024 13:25:43 +0100 Subject: [PATCH 05/12] solution of pkg resources for 3.8 and newer --- nikola/data/__init__.py | 27 ++++++++++++++++++++++++ nikola/data/samplesite/files/__init__.py | 3 +++ nikola/data/shortcodes/__init__.py | 27 ++++++++++++++++++++++++ nikola/data/themes/__init__.py | 27 ++++++++++++++++++++++++ nikola/nikola.py | 2 +- nikola/plugins/command/init.py | 2 +- nikola/plugins/command/theme.py | 2 +- nikola/plugins/shortcode/__init__.py | 27 ++++++++++++++++++++++++ nikola/utils.py | 5 +++-- 9 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 nikola/data/__init__.py create mode 100644 nikola/data/samplesite/files/__init__.py create mode 100644 nikola/data/shortcodes/__init__.py create mode 100644 nikola/data/themes/__init__.py create mode 100644 nikola/plugins/shortcode/__init__.py diff --git a/nikola/data/__init__.py b/nikola/data/__init__.py new file mode 100644 index 0000000000..ab7b4dc9b7 --- /dev/null +++ b/nikola/data/__init__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +# Copyright © 2012-2024 Roberto Alsina and others. + +# Permission is hereby granted, free of charge, to any +# person obtaining a copy of this software and associated +# documentation files (the "Software"), to deal in the +# Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the +# Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice +# shall be included in all copies or substantial portions of +# the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +"""Nikola -- a modular, fast, simple, static website generator.""" diff --git a/nikola/data/samplesite/files/__init__.py b/nikola/data/samplesite/files/__init__.py new file mode 100644 index 0000000000..70c8c0dc50 --- /dev/null +++ b/nikola/data/samplesite/files/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +"""Plugins for Nikola.""" diff --git a/nikola/data/shortcodes/__init__.py b/nikola/data/shortcodes/__init__.py new file mode 100644 index 0000000000..ab7b4dc9b7 --- /dev/null +++ b/nikola/data/shortcodes/__init__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +# Copyright © 2012-2024 Roberto Alsina and others. + +# Permission is hereby granted, free of charge, to any +# person obtaining a copy of this software and associated +# documentation files (the "Software"), to deal in the +# Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the +# Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice +# shall be included in all copies or substantial portions of +# the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +"""Nikola -- a modular, fast, simple, static website generator.""" diff --git a/nikola/data/themes/__init__.py b/nikola/data/themes/__init__.py new file mode 100644 index 0000000000..ab7b4dc9b7 --- /dev/null +++ b/nikola/data/themes/__init__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +# Copyright © 2012-2024 Roberto Alsina and others. + +# Permission is hereby granted, free of charge, to any +# person obtaining a copy of this software and associated +# documentation files (the "Software"), to deal in the +# Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the +# Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice +# shall be included in all copies or substantial portions of +# the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +"""Nikola -- a modular, fast, simple, static website generator.""" diff --git a/nikola/nikola.py b/nikola/nikola.py index 48f3071524..cfb9a921a6 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -1693,7 +1693,7 @@ def _register_templated_shortcodes(self): """ self.register_shortcode('template', self._template_shortcode_handler) - builtin_sc_dir = utils.pkg_resources('nikola', os.path.join('data', 'shortcodes', utils.get_template_engine(self.THEMES))) + builtin_sc_dir = utils.pkg_resources('nikola.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/command/init.py b/nikola/plugins/command/init.py index 3c5a16503e..eebfcd8bf2 100644 --- a/nikola/plugins/command/init.py +++ b/nikola/plugins/command/init.py @@ -272,7 +272,7 @@ class CommandInit(Command): @classmethod def copy_sample_site(cls, target): """Copy sample site data to target directory.""" - src = pkg_resources('nikola', os.path.join('data', 'samplesite')) + src = pkg_resources('nikola.data', 'samplesite') shutil.copytree(src, target) @staticmethod diff --git a/nikola/plugins/command/theme.py b/nikola/plugins/command/theme.py index f6bcbee8f9..a8d3c9612b 100644 --- a/nikola/plugins/command/theme.py +++ b/nikola/plugins/command/theme.py @@ -286,7 +286,7 @@ def list_installed(self): print("Installed Themes:") print("-----------------") themes = [] - themes_dirs = self.site.themes_dirs + [utils.pkg_resources('nikola', os.path.join('data', 'themes'))] + themes_dirs = self.site.themes_dirs + [utils.pkg_resources('nikola.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/plugins/shortcode/__init__.py b/nikola/plugins/shortcode/__init__.py new file mode 100644 index 0000000000..ab7b4dc9b7 --- /dev/null +++ b/nikola/plugins/shortcode/__init__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +# Copyright © 2012-2024 Roberto Alsina and others. + +# Permission is hereby granted, free of charge, to any +# person obtaining a copy of this software and associated +# documentation files (the "Software"), to deal in the +# Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the +# Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice +# shall be included in all copies or substantial portions of +# the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +"""Nikola -- a modular, fast, simple, static website generator.""" diff --git a/nikola/utils.py b/nikola/utils.py index e8efba290b..2e0d86f20c 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -579,7 +579,8 @@ def __repr__(self): def pkg_resources(package, resource): """Return the resource based on the python version.""" if sys.version_info.minor <= 8: - return str(resources.path(package, resource)) + with resources.path(package, resource) as path: + return str(path) else: return str(resources.files(package).joinpath(resource)) @@ -593,7 +594,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 = pkg_resources('nikola', os.path.join('data', 'themes', theme)) + dir_name = pkg_resources('nikola.data.themes', theme) if os.path.isdir(dir_name): return dir_name raise Exception("Can't find theme '{0}'".format(theme)) From 470c89f268fa628577470e3112ebee4f50366c3d Mon Sep 17 00:00:00 2001 From: Abdelwakil Benouis <111845235+fondbcn@users.noreply.github.com> Date: Sat, 27 Jan 2024 21:08:37 +0100 Subject: [PATCH 06/12] Update nikola/utils.py Co-authored-by: Felix Fontein --- nikola/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nikola/utils.py b/nikola/utils.py index 2e0d86f20c..3d6b9edadd 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -576,7 +576,7 @@ def __repr__(self): sort_keys=True)) -def pkg_resources(package, resource): +def pkg_resources(package: str, resource: str) -> str: """Return the resource based on the python version.""" if sys.version_info.minor <= 8: with resources.path(package, resource) as path: From 3a96327bcb1a19538286a7bd54200750f14b8e05 Mon Sep 17 00:00:00 2001 From: fondbcn Date: Sun, 28 Jan 2024 20:30:37 +0100 Subject: [PATCH 07/12] removing type hint --- nikola/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nikola/utils.py b/nikola/utils.py index 3d6b9edadd..2e0d86f20c 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -576,7 +576,7 @@ def __repr__(self): sort_keys=True)) -def pkg_resources(package: str, resource: str) -> str: +def pkg_resources(package, resource): """Return the resource based on the python version.""" if sys.version_info.minor <= 8: with resources.path(package, resource) as path: From 74346d3c26ac64a6735c3173e09baa00fc7a905a Mon Sep 17 00:00:00 2001 From: fondbcn Date: Wed, 31 Jan 2024 10:26:30 +0100 Subject: [PATCH 08/12] alternative solution for pkg --- nikola/data/__init__.py | 27 --------------------------- nikola/data/shortcodes/__init__.py | 27 --------------------------- nikola/data/themes/__init__.py | 27 --------------------------- nikola/nikola.py | 2 +- nikola/plugins/command/init.py | 2 +- nikola/plugins/command/theme.py | 2 +- nikola/plugins/shortcode/__init__.py | 27 --------------------------- nikola/utils.py | 12 ++++++++---- 8 files changed, 11 insertions(+), 115 deletions(-) delete mode 100644 nikola/data/__init__.py delete mode 100644 nikola/data/shortcodes/__init__.py delete mode 100644 nikola/data/themes/__init__.py delete mode 100644 nikola/plugins/shortcode/__init__.py diff --git a/nikola/data/__init__.py b/nikola/data/__init__.py deleted file mode 100644 index ab7b4dc9b7..0000000000 --- a/nikola/data/__init__.py +++ /dev/null @@ -1,27 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright © 2012-2024 Roberto Alsina and others. - -# Permission is hereby granted, free of charge, to any -# person obtaining a copy of this software and associated -# documentation files (the "Software"), to deal in the -# Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the -# Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice -# shall be included in all copies or substantial portions of -# the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY -# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE -# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS -# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR -# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -"""Nikola -- a modular, fast, simple, static website generator.""" diff --git a/nikola/data/shortcodes/__init__.py b/nikola/data/shortcodes/__init__.py deleted file mode 100644 index ab7b4dc9b7..0000000000 --- a/nikola/data/shortcodes/__init__.py +++ /dev/null @@ -1,27 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright © 2012-2024 Roberto Alsina and others. - -# Permission is hereby granted, free of charge, to any -# person obtaining a copy of this software and associated -# documentation files (the "Software"), to deal in the -# Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the -# Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice -# shall be included in all copies or substantial portions of -# the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY -# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE -# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS -# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR -# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -"""Nikola -- a modular, fast, simple, static website generator.""" diff --git a/nikola/data/themes/__init__.py b/nikola/data/themes/__init__.py deleted file mode 100644 index ab7b4dc9b7..0000000000 --- a/nikola/data/themes/__init__.py +++ /dev/null @@ -1,27 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright © 2012-2024 Roberto Alsina and others. - -# Permission is hereby granted, free of charge, to any -# person obtaining a copy of this software and associated -# documentation files (the "Software"), to deal in the -# Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the -# Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice -# shall be included in all copies or substantial portions of -# the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY -# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE -# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS -# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR -# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -"""Nikola -- a modular, fast, simple, static website generator.""" diff --git a/nikola/nikola.py b/nikola/nikola.py index cfb9a921a6..8965dc1e33 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -1693,7 +1693,7 @@ def _register_templated_shortcodes(self): """ self.register_shortcode('template', self._template_shortcode_handler) - builtin_sc_dir = utils.pkg_resources('nikola.data.shortcodes', utils.get_template_engine(self.THEMES)) + builtin_sc_dir = utils.pkg_resources('nikola', 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/command/init.py b/nikola/plugins/command/init.py index eebfcd8bf2..3c5a16503e 100644 --- a/nikola/plugins/command/init.py +++ b/nikola/plugins/command/init.py @@ -272,7 +272,7 @@ class CommandInit(Command): @classmethod def copy_sample_site(cls, target): """Copy sample site data to target directory.""" - src = pkg_resources('nikola.data', 'samplesite') + src = pkg_resources('nikola', os.path.join('data', 'samplesite')) shutil.copytree(src, target) @staticmethod diff --git a/nikola/plugins/command/theme.py b/nikola/plugins/command/theme.py index a8d3c9612b..f6bcbee8f9 100644 --- a/nikola/plugins/command/theme.py +++ b/nikola/plugins/command/theme.py @@ -286,7 +286,7 @@ def list_installed(self): print("Installed Themes:") print("-----------------") themes = [] - themes_dirs = self.site.themes_dirs + [utils.pkg_resources('nikola.data', 'themes')] + themes_dirs = self.site.themes_dirs + [utils.pkg_resources('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/plugins/shortcode/__init__.py b/nikola/plugins/shortcode/__init__.py deleted file mode 100644 index ab7b4dc9b7..0000000000 --- a/nikola/plugins/shortcode/__init__.py +++ /dev/null @@ -1,27 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright © 2012-2024 Roberto Alsina and others. - -# Permission is hereby granted, free of charge, to any -# person obtaining a copy of this software and associated -# documentation files (the "Software"), to deal in the -# Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the -# Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice -# shall be included in all copies or substantial portions of -# the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY -# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE -# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS -# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR -# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -"""Nikola -- a modular, fast, simple, static website generator.""" diff --git a/nikola/utils.py b/nikola/utils.py index 2e0d86f20c..50f552630e 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 resources, reload as _reload +from importlib import reload as _reload from unicodedata import normalize as unicodenormalize from urllib.parse import quote as urlquote from urllib.parse import unquote as urlunquote @@ -70,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: @@ -579,8 +584,7 @@ def __repr__(self): def pkg_resources(package, resource): """Return the resource based on the python version.""" if sys.version_info.minor <= 8: - with resources.path(package, resource) as path: - return str(path) + return resource_filename(package, resource) else: return str(resources.files(package).joinpath(resource)) @@ -594,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 = pkg_resources('nikola.data.themes', theme) + dir_name = pkg_resources('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)) From 71c8a69d487bc4e58b9d96cfeaa0b642ee684c35 Mon Sep 17 00:00:00 2001 From: Abdelwakil Benouis <111845235+fondbcn@users.noreply.github.com> Date: Thu, 1 Feb 2024 09:06:05 +0100 Subject: [PATCH 09/12] Update nikola/utils.py Co-authored-by: Chris Warrick --- nikola/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nikola/utils.py b/nikola/utils.py index 50f552630e..788c26eb79 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -582,7 +582,7 @@ def __repr__(self): def pkg_resources(package, resource): - """Return the resource based on the python version.""" + """Return path to a resource from the package with the given name.""" if sys.version_info.minor <= 8: return resource_filename(package, resource) else: From 4afe5b8c25364aab21d35267116ed20f5047e55a Mon Sep 17 00:00:00 2001 From: Abdelwakil Benouis <111845235+fondbcn@users.noreply.github.com> Date: Thu, 1 Feb 2024 09:06:35 +0100 Subject: [PATCH 10/12] Update nikola/nikola.py Co-authored-by: Chris Warrick --- nikola/nikola.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nikola/nikola.py b/nikola/nikola.py index 8965dc1e33..1590e694a2 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -1693,7 +1693,9 @@ def _register_templated_shortcodes(self): """ self.register_shortcode('template', self._template_shortcode_handler) - builtin_sc_dir = utils.pkg_resources('nikola', os.path.join('data','shortcodes',utils.get_template_engine(self.THEMES))) + builtin_sc_dir = utils.pkg_resources( + 'nikola', + 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): From 100e958de7f28d87d6ed3de627544be90a6d3f3f Mon Sep 17 00:00:00 2001 From: fondbcn Date: Thu, 1 Feb 2024 09:13:50 +0100 Subject: [PATCH 11/12] final touches --- nikola/data/samplesite/files/__init__.py | 3 --- nikola/nikola.py | 4 ++-- nikola/plugins/basic_import.py | 2 +- nikola/plugins/command/auto/__init__.py | 4 ++-- nikola/plugins/command/init.py | 8 ++++---- nikola/plugins/command/rst2html/__init__.py | 6 +++--- nikola/plugins/command/theme.py | 2 +- nikola/utils.py | 2 +- 8 files changed, 14 insertions(+), 17 deletions(-) delete mode 100644 nikola/data/samplesite/files/__init__.py diff --git a/nikola/data/samplesite/files/__init__.py b/nikola/data/samplesite/files/__init__.py deleted file mode 100644 index 70c8c0dc50..0000000000 --- a/nikola/data/samplesite/files/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -# -*- coding: utf-8 -*- - -"""Plugins for Nikola.""" diff --git a/nikola/nikola.py b/nikola/nikola.py index 1590e694a2..124b030054 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -1030,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 = [ - utils.pkg_resources('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] @@ -1693,7 +1693,7 @@ def _register_templated_shortcodes(self): """ self.register_shortcode('template', self._template_shortcode_handler) - builtin_sc_dir = utils.pkg_resources( + 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 1499578b19..4f693960a2 100644 --- a/nikola/plugins/basic_import.py +++ b/nikola/plugins/basic_import.py @@ -97,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 = utils.pkg_resources('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 6cd0dc0806..aecbe4114d 100644 --- a/nikola/plugins/command/auto/__init__.py +++ b/nikola/plugins/command/auto/__init__.py @@ -42,7 +42,7 @@ import blinker from nikola.plugin_categories import Command -from nikola.utils import dns_sd, req_missing, get_theme_path, makedirs, pkg_resources +from nikola.utils import dns_sd, req_missing, get_theme_path, makedirs, pkg_resources_path try: import aiohttp @@ -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('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 3c5a16503e..df68e0e4f1 100644 --- a/nikola/plugins/command/init.py +++ b/nikola/plugins/command/init.py @@ -42,7 +42,7 @@ 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, pkg_resources +from nikola.utils import ask, ask_yesno, get_logger, makedirs, load_messages, pkg_resources_path from nikola.packages.tzlocal import get_localzone @@ -272,13 +272,13 @@ class CommandInit(Command): @classmethod def copy_sample_site(cls, target): """Copy sample site data to target directory.""" - src = pkg_resources('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 = pkg_resources('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: @@ -287,7 +287,7 @@ def create_configuration(target): @staticmethod def create_configuration_to_string(): """Return configuration file as a string.""" - template_path = pkg_resources('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 ce4c474788..dd8298adb8 100644 --- a/nikola/plugins/command/rst2html/__init__.py +++ b/nikola/plugins/command/rst2html/__init__.py @@ -31,7 +31,7 @@ import lxml.html from mako.template import Template from nikola.plugin_categories import Command -from nikola.utils import pkg_resources +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 = pkg_resources('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 = pkg_resources('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 f6bcbee8f9..388001fc20 100644 --- a/nikola/plugins/command/theme.py +++ b/nikola/plugins/command/theme.py @@ -286,7 +286,7 @@ def list_installed(self): print("Installed Themes:") print("-----------------") themes = [] - themes_dirs = self.site.themes_dirs + [utils.pkg_resources('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 788c26eb79..eb43ca7bf9 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -581,7 +581,7 @@ def __repr__(self): sort_keys=True)) -def pkg_resources(package, resource): +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) From a2af6d5e5ed3e81617c197908bcedc8bd583658c Mon Sep 17 00:00:00 2001 From: Abdelwakil Benouis <111845235+fondbcn@users.noreply.github.com> Date: Thu, 1 Feb 2024 22:32:30 +0100 Subject: [PATCH 12/12] Update utils.py --- nikola/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nikola/utils.py b/nikola/utils.py index eb43ca7bf9..71c2119fd9 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -598,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 = pkg_resources('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))