From 13d20b2a5d10bf9cb75d43db56e1a08fd6fe5abd Mon Sep 17 00:00:00 2001 From: Omer Bhatti Date: Fri, 27 Sep 2024 19:18:38 +0500 Subject: [PATCH] chore: loading scenarios using loader --- freetextresponse/mixins/scenario.py | 62 +++-------------------------- 1 file changed, 6 insertions(+), 56 deletions(-) diff --git a/freetextresponse/mixins/scenario.py b/freetextresponse/mixins/scenario.py index 76b8b4cd..59c28ef0 100644 --- a/freetextresponse/mixins/scenario.py +++ b/freetextresponse/mixins/scenario.py @@ -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): @@ -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")