Skip to content

Commit

Permalink
resolving deprecated functions with utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
fondbcn committed Jan 27, 2024
1 parent 548cad7 commit d7893c9
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
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 importlib import resources
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 = [
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]
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 1 addition & 3 deletions nikola/plugins/basic_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ,
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 @@ -39,10 +39,9 @@
import webbrowser

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


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

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

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 + [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)]
Expand Down
10 changes: 9 additions & 1 deletion nikola/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
Expand Down

0 comments on commit d7893c9

Please sign in to comment.