Skip to content

Commit

Permalink
chore: loading scenarios using loader
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBhatti committed Sep 27, 2024
1 parent ae7c827 commit 13d20b2
Showing 1 changed file with 6 additions and 56 deletions.
62 changes: 6 additions & 56 deletions freetextresponse/mixins/scenario.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,13 @@
"""
Mixin workbench behavior into XBlocks
"""
from glob import glob
import importlib.resources
import importlib
try:
from xblock.utils.resources import ResourceLoader
except ModuleNotFoundError:
from xblockutils.resources import ResourceLoader


def _read_file(file_path):
"""
Read in a file's contents
"""
with open(file_path) 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


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 = f"{directory}/*.xml"

files = glob(pattern)
return files
loader = ResourceLoader(__name__)


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

0 comments on commit 13d20b2

Please sign in to comment.