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. We fix that by replacing all usage of pkg_resources with importlib_resources.
  • Loading branch information
CodeWithEmad committed Aug 1, 2024
1 parent 9e4ac98 commit b8f761c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ django-statici18n
edx-i18n-tools
Mako
XBlock
importlib-resources>=6.1.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""TO-DO: Write a description of what this XBlock is."""

import pkg_resources
import importlib_resources as resources
from django.utils import translation
from web_fragments.fragment import Fragment
from xblock.core import XBlock
Expand All @@ -24,7 +24,7 @@ class {{cookiecutter.class_name}}(XBlock):

def resource_string(self, path):
"""Handy helper for getting resources from our kit."""
data = pkg_resources.resource_string(__name__, path)
data = resources.files(__package__).joinpath(path).read_bytes()
return data.decode("utf8")

# TO-DO: change this view to display your data your own way.
Expand Down Expand Up @@ -93,10 +93,11 @@ def _get_statici18n_js_url():
text_js = 'public/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)):
try:
resources.files(__package__).joinpath(text_js.format(locale_code=code)).read_bytes()
return text_js.format(locale_code=code)
except FileNotFoundError:
continue
return None

@staticmethod
Expand Down

0 comments on commit b8f761c

Please sign in to comment.