Skip to content

Commit a030d9d

Browse files
committed
added save and load method to project and controls
1 parent 9f73677 commit a030d9d

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 (
@@ -220,3 +221,28 @@ def delete_IPC(self):
220221
with contextlib.suppress(FileNotFoundError):
221222
os.remove(self._IPCFilePath)
222223
return None
224+
225+
def save(self, path: str | Path, filename: str = "controls"):
226+
"""Save a controls object to a JSON file.
227+
228+
Parameters
229+
----------
230+
path : str or Path
231+
The directory in which the controls object will be written.
232+
233+
"""
234+
file = Path(path, f"{filename.removesuffix('.json')}.json")
235+
file.write_text(self.model_dump_json())
236+
237+
@classmethod
238+
def load(cls, path: str | Path) -> "Controls":
239+
"""Load a controls object from file.
240+
241+
Parameters
242+
----------
243+
path : str or Path
244+
The path to the controls object file.
245+
246+
"""
247+
file = Path(path)
248+
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

@@ -834,6 +835,31 @@ def classlist_script(name, classlist):
834835
+ "\n)"
835836
)
836837

838+
def save(self, path: str | Path, filename: str = "project"):
839+
"""Save a project to a JSON file.
840+
841+
Parameters
842+
----------
843+
path : str or Path
844+
The directory in which the project will be written.
845+
846+
"""
847+
file = Path(path, f"{filename.removesuffix('.json')}.json")
848+
file.write_text(project_to_json(self))
849+
850+
@classmethod
851+
def load(cls, path: str | Path) -> "Project":
852+
"""Load a project from file.
853+
854+
Parameters
855+
----------
856+
path : str or Path
857+
The path to the project file.
858+
859+
"""
860+
file = Path(path)
861+
return project_from_json(file.read_text())
862+
837863
def _classlist_wrapper(self, class_list: ClassList, func: Callable):
838864
"""Defines the function used to wrap around ClassList routines to force revalidation.
839865

0 commit comments

Comments
 (0)