Skip to content

Commit

Permalink
chore: Replace pkg_resources lib with xblockutils
Browse files Browse the repository at this point in the history
  • Loading branch information
ttqureshi committed Sep 30, 2024
1 parent a33db7f commit ce5680a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
9 changes: 7 additions & 2 deletions openassessment/xblock/load_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import logging
from urllib.parse import urlparse

from pkg_resources import resource_string
from django.conf import settings
try:
from xblock.utils.resources import ResourceLoader
except ModuleNotFoundError:
from xblockutils.resources import ResourceLoader

logger = logging.getLogger(__name__) # pylint: disable=invalid-name

Expand All @@ -31,6 +34,8 @@ class LoadStatic:
_base_url = ''
_is_loaded = False

resource_loader = ResourceLoader(__name__)

@staticmethod
def reload_manifest():
"""
Expand All @@ -45,7 +50,7 @@ def reload_manifest():
logger.error('LMS_ROOT_URL is undefined')

try:
json_data = resource_string(__name__, 'static/dist/manifest.json').decode("utf8")
json_data = LoadStatic.resource_loader.load_unicode('static/dist/manifest.json')
LoadStatic._manifest = json.loads(json_data)
base_url_override = LoadStatic._manifest.get('base_url', None)
LoadStatic._is_loaded = True
Expand Down
9 changes: 6 additions & 3 deletions openassessment/xblock/openassessmentblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import logging
import re

import pkg_resources
import pytz
try:
from xblock.utils.resources import ResourceLoader
except ModuleNotFoundError:
from xblockutils.resources import ResourceLoader

from django.conf import settings
from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -72,12 +75,12 @@
from openassessment.xblock.apis.ora_data_accessor import ORADataAccessor

logger = logging.getLogger(__name__) # pylint: disable=invalid-name
resource_loader = ResourceLoader(__name__)


def load(path):
"""Handy helper for getting resources from our kit."""
data = pkg_resources.resource_string(__name__, path)
return data.decode("utf8")
return resource_loader.load_unicode(path)


@XBlock.needs("i18n")
Expand Down
24 changes: 12 additions & 12 deletions openassessment/xblock/test/test_load_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,24 @@ def test_get_url_absolute_base_url(self):
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
absolute_base_url, loaded_key_url))

@patch('pkg_resources.resource_string')
def test_get_url_file_not_found(self, resource_string):
@patch('xblock.utils.resources.ResourceLoader.load_unicode')
def test_get_url_file_not_found(self, load_unicode):
key_url = 'some_url.js'
resource_string.side_effect = IOError()
load_unicode.side_effect = IOError()
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
self.default_base_url, 'some_url.js'))

@override_settings(LMS_ROOT_URL='localhost/')
@patch('pkg_resources.resource_string')
def test_get_url_file_not_found_with_root_url(self, resource_string):
@patch('xblock.utils.resources.ResourceLoader.load_unicode')
def test_get_url_file_not_found_with_root_url(self, load_unicode):
key_url = 'some_url.js'
resource_string.side_effect = IOError()
load_unicode.side_effect = IOError()
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
'localhost/', self.default_base_url, 'some_url.js'))

@patch('pkg_resources.resource_string')
def test_get_url_with_manifest(self, resource_string):
resource_string.return_value = None
@patch('xblock.utils.resources.ResourceLoader.load_unicode')
def test_get_url_with_manifest(self, load_unicode):
load_unicode.return_value = None
key_url = 'some_url.js'
with patch('json.loads') as jsondata:
jsondata.return_value = {
Expand All @@ -92,9 +92,9 @@ def test_get_url_with_manifest(self, resource_string):
self.default_base_url, 'some_url.hashchunk.js'))

@override_settings(LMS_ROOT_URL='localhost/')
@patch('pkg_resources.resource_string')
def test_get_url_with_manifest_and_root_url(self, resource_string):
resource_string.return_value = None
@patch('xblock.utils.resources.ResourceLoader.load_unicode')
def test_get_url_with_manifest_and_root_url(self, load_unicode):
load_unicode.return_value = None
key_url = 'some_url.js'
with patch('json.loads') as jsondata:
jsondata.return_value = {
Expand Down

0 comments on commit ce5680a

Please sign in to comment.