Skip to content

Commit e4a4a61

Browse files
committed
added save and load method to project and controls
1 parent 47093d4 commit e4a4a61

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

RATapi/controls.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import tempfile
44
import warnings
5+
from pathlib import Path
56

67
import prettytable
78
from pydantic import (
@@ -164,3 +165,28 @@ def delete_IPC(self):
164165
with contextlib.suppress(FileNotFoundError):
165166
os.remove(self._IPCFilePath)
166167
return None
168+
169+
def save(self, path: str | Path, filename: str = "controls"):
170+
"""Save a controls object to a JSON file.
171+
172+
Parameters
173+
----------
174+
path : str or Path
175+
The directory in which the controls object will be written.
176+
177+
"""
178+
file = Path(path, f"{filename.removesuffix('.json')}.json")
179+
file.write_text(self.model_dump_json())
180+
181+
@classmethod
182+
def load(cls, path: str | Path) -> 'Controls':
183+
"""Load a controls object from file.
184+
185+
Parameters
186+
----------
187+
path : str or Path
188+
The path to the controls object file.
189+
190+
"""
191+
file = Path(path)
192+
return cls.model_validate_json(file.read_text())

RATapi/project.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import RATapi.models
2525
from RATapi.classlist import ClassList
26+
from RATapi.utils.convert import project_from_json, project_to_json
2627
from RATapi.utils.custom_errors import custom_pydantic_validation_error
2728
from RATapi.utils.enums import Calculations, Geometries, LayerModels, Priors, TypeOptions
2829

@@ -809,6 +810,31 @@ def classlist_script(name, classlist):
809810
+ "\n)"
810811
)
811812

813+
def save(self, path: str | Path, filename: str = "project"):
814+
"""Save a project to a JSON file.
815+
816+
Parameters
817+
----------
818+
path : str or Path
819+
The directory in which the project will be written.
820+
821+
"""
822+
file = Path(path, f"{filename.removesuffix('.json')}.json")
823+
file.write_text(project_to_json(self))
824+
825+
@classmethod
826+
def load(cls, path: str | Path) -> 'Project':
827+
"""Load a project from file.
828+
829+
Parameters
830+
----------
831+
path : str or Path
832+
The path to the project file.
833+
834+
"""
835+
file = Path(path)
836+
return project_from_json(file.read_text())
837+
812838
def _classlist_wrapper(self, class_list: ClassList, func: Callable):
813839
"""Defines the function used to wrap around ClassList routines to force revalidation.
814840

0 commit comments

Comments
 (0)