diff --git a/xblock/core.py b/xblock/core.py index f2f06ac85..37cab9d21 100644 --- a/xblock/core.py +++ b/xblock/core.py @@ -161,8 +161,12 @@ def open_local_resource(cls, uri): @classmethod def _open_resource(cls, uri): - return importlib.resources.files(inspect.getmodule(cls).__package__).joinpath( - os.path.join(cls.resources_dir, uri) + return importlib.resources.files( + inspect.getmodule(cls).__package__ + ).joinpath( + cls.resources_dir + ).joinpath( + uri ).open('rb') @classmethod diff --git a/xblock/plugin.py b/xblock/plugin.py index bc10c3347..6b5888d0c 100644 --- a/xblock/plugin.py +++ b/xblock/plugin.py @@ -100,11 +100,7 @@ def select(identifier, all_entry_points): if select is None: select = default_select - all_entry_points = [ - entry_point - for entry_point in importlib.metadata.entry_points(group=cls.entry_point) - if entry_point.name == identifier - ] + all_entry_points = importlib.metadata.entry_points(group=cls.entry_point, name=identifier) for extra_identifier, extra_entry_point in iter(cls.extra_entry_points): if identifier == extra_identifier: all_entry_points.append(extra_entry_point) diff --git a/xblock/utils/resources.py b/xblock/utils/resources.py index b870e2bd2..778007cd1 100644 --- a/xblock/utils/resources.py +++ b/xblock/utils/resources.py @@ -22,7 +22,8 @@ def load_unicode(self, resource_path): Gets the content of a resource """ package_name = importlib.import_module(self.module_name).__package__ - return importlib.resources.files(package_name).joinpath(resource_path.lstrip('/')).read_text() + # Strip leading slash to avoid importlib exception with absolute paths + return importlib.resources.files(package_name).joinpath(resource_path.lstrip('/')).read_text(encoding="utf-8") def render_django_template(self, template_path, context=None, i18n_service=None): """ @@ -56,7 +57,9 @@ def render_mako_template(self, template_path, context=None): ) context = context or {} template_str = self.load_unicode(template_path) - directory = os.path.dirname(os.path.realpath(sys.modules[self.module_name].__file__)) + + package_name = importlib.import_module(self.module_name).__package__ + directory = str(importlib.resources.files(package_name)) lookup = MakoTemplateLookup(directories=[directory]) template = MakoTemplate(template_str, lookup=lookup) return template.render(**context)