From 141ff224cc51b27c0063d4fc03046b2e102fdb73 Mon Sep 17 00:00:00 2001 From: Muhammad Tayayb Tahir Qureshi Date: Mon, 16 Sep 2024 20:40:33 +0500 Subject: [PATCH] chore: migrate to resourceloader --- submit_and_compare/mixins/scenario.py | 61 +++------------------------ 1 file changed, 6 insertions(+), 55 deletions(-) diff --git a/submit_and_compare/mixins/scenario.py b/submit_and_compare/mixins/scenario.py index b3bc50b..83edc39 100644 --- a/submit_and_compare/mixins/scenario.py +++ b/submit_and_compare/mixins/scenario.py @@ -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: @@ -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") \ No newline at end of file