Skip to content

Commit

Permalink
fix: Address PR request changes
Browse files Browse the repository at this point in the history
  • Loading branch information
farhan committed Jul 19, 2024
1 parent 41c5a14 commit 44698d0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
8 changes: 6 additions & 2 deletions xblock/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check warning on line 164 in xblock/core.py

View check run for this annotation

Codecov / codecov/patch

xblock/core.py#L164

Added line #L164 was not covered by tests
inspect.getmodule(cls).__package__
).joinpath(
cls.resources_dir
).joinpath(
uri
).open('rb')

@classmethod
Expand Down
6 changes: 1 addition & 5 deletions xblock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions xblock/utils/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 44698d0

Please sign in to comment.