Skip to content

Commit

Permalink
chore: migrate to resourceloader
Browse files Browse the repository at this point in the history
  • Loading branch information
ttqureshi committed Sep 16, 2024
1 parent 92c89db commit 141ff22
Showing 1 changed file with 6 additions and 55 deletions.
61 changes: 6 additions & 55 deletions submit_and_compare/mixins/scenario.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,14 @@
"""
Mixin workbench behavior into XBlocks
"""
from glob import glob
import importlib
import importlib.resources


def _read_file(file_path):
"""
Read in a file's contents
"""
with open(file_path, encoding="utf-8") as file_input:
file_contents = file_input.read()
return file_contents


def _parse_title(file_path):
"""
Parse a title from a file name
"""
title = file_path
title = title.split('/')[-1]
title = '.'.join(title.split('.')[:-1])
title = ' '.join(title.split('-'))
title = ' '.join([
word.capitalize()
for word in title.split(' ')
])
return title
try:
from xblock.utils.resources import ResourceLoader
except ModuleNotFoundError:
from xblockutils.resources import ResourceLoader


def _read_files(files):
"""
Read the contents of a list of files
"""
file_contents = [
(
_parse_title(file_path),
_read_file(file_path),
)
for file_path in files
]
return file_contents


def _find_files(directory):
"""
Find XML files in the directory
"""
pattern = "{directory}/*.xml".format(
directory=directory,
)
files = glob(pattern)
return files
loader = ResourceLoader(__name__)


class XBlockWorkbenchMixin:
Expand All @@ -65,9 +21,4 @@ def workbench_scenarios(cls):
"""
Gather scenarios to be displayed in the workbench
"""
module = cls.__module__
module = module.split('.', maxsplit=1)[0]
directory = importlib.resources.files(module) / 'scenarios'
files = _find_files(directory)
scenarios = _read_files(files)
return scenarios
return loader.load_scenarios_from_path("../scenarios")

0 comments on commit 141ff22

Please sign in to comment.