Skip to content

Commit

Permalink
chore: Shift from pkg_resources to importlib.resources
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBhatti committed Sep 11, 2024
1 parent 2389cdb commit bb8b95e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 9 additions & 3 deletions freetextresponse/mixins/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
Note: We should resume test coverage for all lines in this file once
split into its own library.
"""
import pkg_resources
try:
from xblock.utils.resources import ResourceLoader
except ModuleNotFoundError:
# For backward compatibility with releases older than Quince.
from xblockutils.resources import ResourceLoader

from xblock.core import XBlock
from web_fragments.fragment import Fragment


loader = ResourceLoader(__name__)


class XBlockFragmentBuilderMixin(object):
"""
Create a default XBlock fragment builder
Expand Down Expand Up @@ -81,8 +88,7 @@ def build_fragment(
fragment.add_css_url(url)
else:
item = '../public/' + item
data = pkg_resources.resource_string(__name__, item)
data = data.decode('utf8')
data = loader.load_unicode(item)
fragment.add_css(data)
for item in js:
item = 'public/' + item
Expand Down
8 changes: 5 additions & 3 deletions freetextresponse/mixins/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Mixin workbench behavior into XBlocks
"""
from glob import glob
import pkg_resources
import importlib.resources
import importlib


def _read_file(file_path):
Expand Down Expand Up @@ -65,7 +66,8 @@ def workbench_scenarios(cls):
"""
module = cls.__module__
module = module.split('.', maxsplit=1)[0]
directory = pkg_resources.resource_filename(module, 'scenarios')
files = _find_files(directory)
module_ref = importlib.import_module(module)
files = importlib.resources.files(module_ref).joinpath('scenarios')
files = _find_files(files)
scenarios = _read_files(files)
return scenarios

0 comments on commit bb8b95e

Please sign in to comment.