Skip to content

Commit

Permalink
Moving change_dir_back decorator into utils
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentraymond-ua committed Apr 19, 2024
1 parent 003a650 commit ceedf79
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions skema/utils/change_dir_back.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ceedf79

Please sign in to comment.