From ceedf791cb728fb6aab94e1f57dd8fdcadcfe165 Mon Sep 17 00:00:00 2001 From: Vincent Raymond Date: Fri, 19 Apr 2024 10:00:20 -0400 Subject: [PATCH] Moving change_dir_back decorator into utils --- .../model_coverage_report/model_coverage_report.py | 11 ++--------- skema/utils/change_dir_back.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 skema/utils/change_dir_back.py diff --git a/skema/program_analysis/model_coverage_report/model_coverage_report.py b/skema/program_analysis/model_coverage_report/model_coverage_report.py index a0f92f0b6c7..578830e2711 100644 --- a/skema/program_analysis/model_coverage_report/model_coverage_report.py +++ b/skema/program_analysis/model_coverage_report/model_coverage_report.py @@ -33,6 +33,7 @@ from skema.rest.utils import fn_preprocessor from skema.rest.workflows import code_snippets_to_pn_amr, llm_assisted_codebase_to_pn_amr from skema.utils.fold import del_nulls, dictionary_to_gromet_json +from skema.utils.change_dir_back import change_dir_back from skema.skema_py.server import System # Constants for file paths @@ -60,16 +61,8 @@ def get_overall_status(status_list: List[Enum]) -> Enum: def valid_path(path: str) -> bool: return "include_" not in path -def change_dir_back(func): - """Decorator to ensure the working directory is changed back after function call.""" - def wrapper(*args, **kwargs): - try: - return func(*args, **kwargs) - finally: - os.chdir(THIS_PATH) - return wrapper -@change_dir_back +@change_dir_back(THIS_PATH) def generate_data_product(output_dir: str, model_name: str, file_name: str, data_product_function: Callable, args=(), kwargs=None) -> Tuple[str, Any, Status]: output_path = Path(output_dir) / "data" / data_product_function.__name__ / model_name / file_name output_path.parent.mkdir(parents=True, exist_ok=True) diff --git a/skema/utils/change_dir_back.py b/skema/utils/change_dir_back.py new file mode 100644 index 00000000000..f48f3fa41f2 --- /dev/null +++ b/skema/utils/change_dir_back.py @@ -0,0 +1,12 @@ +import os +def change_dir_back(this_path): + """Decorator to ensure the working directory is changed back after function call.""" + def outer_decorator(func): + def inner_decorator(*args, **kwargs): + try: + return func(*args, **kwargs) + finally: + os.chdir(this_path) + + return inner_decorator + return outer_decorator \ No newline at end of file