From c0701a31a7da0bbf3a983e7b0a110b06798ce505 Mon Sep 17 00:00:00 2001 From: Emad Rad Date: Fri, 2 Aug 2024 01:00:24 +0330 Subject: [PATCH] fix: remove pkg_resources for python 3.12 compatibility pkg_resources is available in python 3.12 only if setuptools is explicitly installed, which is not always the case. --- .../{{cookiecutter.package_name}}.py | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/cookiecutter-xblock/{{cookiecutter.repo_name}}/{{cookiecutter.package_name}}/{{cookiecutter.package_name}}.py b/cookiecutter-xblock/{{cookiecutter.repo_name}}/{{cookiecutter.package_name}}/{{cookiecutter.package_name}}.py index 97ba1bfc..67519560 100644 --- a/cookiecutter-xblock/{{cookiecutter.repo_name}}/{{cookiecutter.package_name}}/{{cookiecutter.package_name}}.py +++ b/cookiecutter-xblock/{{cookiecutter.repo_name}}/{{cookiecutter.package_name}}/{{cookiecutter.package_name}}.py @@ -1,11 +1,16 @@ """TO-DO: Write a description of what this XBlock is.""" -import pkg_resources +import os from django.utils import translation from web_fragments.fragment import Fragment from xblock.core import XBlock from xblock.fields import Integer, Scope -from xblock.utils.resources import ResourceLoader +try: + from xblock.utils.resources import ResourceLoader +except ModuleNotFoundError: + from xblockutils.resources import ResourceLoader + +resource_loader = ResourceLoader(__name__) class {{cookiecutter.class_name}}(XBlock): @@ -22,10 +27,12 @@ class {{cookiecutter.class_name}}(XBlock): help="A simple counter, to show something happening", ) - def resource_string(self, path): - """Handy helper for getting resources from our kit.""" - data = pkg_resources.resource_string(__name__, path) - return data.decode("utf8") + def get_resource_string(self, path): + """ + Retrieve string contents for the file path + """ + path = os.path.join('static', path) + return resource_loader.load_unicode(path) # TO-DO: change this view to display your data your own way. def student_view(self, context=None): @@ -34,16 +41,16 @@ def student_view(self, context=None): """ if context: pass # TO-DO: do something based on the context. - html = self.resource_string("static/html/{{cookiecutter.package_name}}.html") + html = self.resource_string("html/{{cookiecutter.package_name}}.html") frag = Fragment(html.format(self=self)) - frag.add_css(self.resource_string("static/css/{{cookiecutter.package_name}}.css")) + frag.add_css(self.resource_string("css/{{cookiecutter.package_name}}.css")) # Add i18n js statici18n_js_url = self._get_statici18n_js_url() if statici18n_js_url: frag.add_javascript_url(self.runtime.local_resource_url(self, statici18n_js_url)) - frag.add_javascript(self.resource_string("static/js/src/{{cookiecutter.package_name}}.js")) + frag.add_javascript(self.resource_string("js/src/{{cookiecutter.package_name}}.js")) frag.initialize_js('{{cookiecutter.class_name}}') return frag @@ -90,13 +97,14 @@ def _get_statici18n_js_url(): locale_code = translation.get_language() if locale_code is None: return None - text_js = 'public/js/translations/{locale_code}/text.js' + text_js = 'static/js/translations/{locale_code}/text.js' lang_code = locale_code.split('-')[0] for code in (locale_code, lang_code, 'en'): - loader = ResourceLoader(__name__) - if pkg_resources.resource_exists( - loader.module_name, text_js.format(locale_code=code)): - return text_js.format(locale_code=code) + import importlib.resources as resources + + text_js_path = text_js.format(locale_code=code) + if resources.is_resource(resource_loader.module_name, text_js_path): + return text_js_path return None @staticmethod