Skip to content

Commit

Permalink
replacing all pkg_resources with importlib
Browse files Browse the repository at this point in the history
  • Loading branch information
fondbcn committed Jan 26, 2024
1 parent 6c0ac74 commit 548cad7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions nikola/nikola.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions nikola/plugins/basic_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ,
Expand Down
4 changes: 2 additions & 2 deletions nikola/plugins/command/auto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 5 additions & 4 deletions nikola/plugins/command/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io
import json
import os
import sys
import shutil
import textwrap
import unidecode
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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))

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

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

0 comments on commit 548cad7

Please sign in to comment.