Skip to content

Commit

Permalink
fix: remove pkg_resources for python 3.12 compatibility
Browse files Browse the repository at this point in the history
pkg_resources is available in python 3.12 only if setuptools is explicitly installed, which is not always the case.
  • Loading branch information
CodeWithEmad committed Oct 7, 2024
1 parent 7ab687c commit c0701a3
Showing 1 changed file with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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):
Expand All @@ -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

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

0 comments on commit c0701a3

Please sign in to comment.