diff --git a/.gitignore b/.gitignore index 41060c93..ab0dc67d 100644 --- a/.gitignore +++ b/.gitignore @@ -172,5 +172,4 @@ cython_debug/ # ADF agent.log* -precompute !java/lib/src/main/java/adf_core_python/core/agent/precompute diff --git a/src/adf_core_python/core/agent/precompute/__init__.py b/src/adf_core_python/core/agent/precompute/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/adf_core_python/core/agent/precompute/precompute_data.py b/src/adf_core_python/core/agent/precompute/precompute_data.py new file mode 100644 index 00000000..3dc33733 --- /dev/null +++ b/src/adf_core_python/core/agent/precompute/precompute_data.py @@ -0,0 +1,75 @@ +import json +import os + +ENCODE = "utf-8" + + +class PrecomputeData: + def __init__(self, dir_path: str) -> None: + """ + Initialize the PrecomputeData object. + + Parameters + ---------- + dir_path : str + The directory path to save the precompute data. + + Raises + ------ + Exception + """ + self._dir_path = dir_path + + def read_json_data(self, module_name: str) -> dict: + """ + Read the precompute data from the file. + + Returns + ------- + dict + The precompute data. + + Raises + ------ + Exception + """ + + with open(f"{self._dir_path}/{module_name}.json", "r", encoding=ENCODE) as file: + return json.load(file) + + def write_json_data(self, data: dict, module_name: str) -> None: + """ + Write the precompute data to the file. + + Parameters + ---------- + data : dict + The data to write. + + Raises + ------ + Exception + """ + if not os.path.exists(self._dir_path): + os.makedirs(self._dir_path) + + with open(f"{self._dir_path}/{module_name}.json", "w", encoding=ENCODE) as file: + json.dump(data, file, indent=4) + + def remove_precompute_data(self) -> None: + """ + Remove the precompute data file. + """ + if os.path.exists(self._dir_path): + os.remove(self._dir_path) + + def is_available(self) -> bool: + """ + Check if the precompute data is available. + + Returns + ------- + bool + True if the precompute data is available, False otherwise. + """ + return os.path.exists(self._dir_path)