Skip to content

Commit

Permalink
alternative solution for pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
fondbcn committed Jan 31, 2024
1 parent 6431f31 commit 49186a5
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 115 deletions.
27 changes: 0 additions & 27 deletions nikola/data/__init__.py

This file was deleted.

27 changes: 0 additions & 27 deletions nikola/data/shortcodes/__init__.py

This file was deleted.

27 changes: 0 additions & 27 deletions nikola/data/themes/__init__.py

This file was deleted.

2 changes: 1 addition & 1 deletion nikola/nikola.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion nikola/plugins/command/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nikola/plugins/command/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
27 changes: 0 additions & 27 deletions nikola/plugins/shortcode/__init__.py

This file was deleted.

12 changes: 8 additions & 4 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 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
Expand All @@ -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:
Expand Down Expand Up @@ -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))

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

0 comments on commit 49186a5

Please sign in to comment.