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

replacing pkg_resources with importlib #3749

Merged
merged 12 commits into from
Feb 1, 2024
5 changes: 2 additions & 3 deletions nikola/nikola.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)))

Expand Down
3 changes: 1 addition & 2 deletions nikola/plugins/basic_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

from lxml import etree, html
from mako.template import Template
from pkg_resources import resource_filename

from nikola import utils

Expand Down Expand Up @@ -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 ,
Expand Down
5 changes: 2 additions & 3 deletions nikola/plugins/command/auto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 4 additions & 5 deletions nikola/plugins/command/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand All @@ -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))

Expand Down
6 changes: 3 additions & 3 deletions nikola/plugins/command/rst2html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions nikola/plugins/command/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down
16 changes: 14 additions & 2 deletions nikola/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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.

Expand All @@ -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))
Expand Down
Loading