From 4836abf9267ccf18b806633d070ae9eaeebd6b97 Mon Sep 17 00:00:00 2001 From: Andreas Pedersen Date: Fri, 27 Sep 2024 12:20:56 +0200 Subject: [PATCH] added load --- src/easyreflectometry/project.py | 12 ++++++++++++ tests/test_project.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/easyreflectometry/project.py b/src/easyreflectometry/project.py index c5dd8880..56f8692b 100644 --- a/src/easyreflectometry/project.py +++ b/src/easyreflectometry/project.py @@ -3,6 +3,7 @@ import os from pathlib import Path from typing import List +from typing import Optional from easyscience.fitting import AvailableMinimizers @@ -120,6 +121,17 @@ def save_project_json(self): except Exception as exception: print(exception) + def load_project_json(self, path: Optional[Path] = None): + if path is None: + path = self.path_project_json + + if path.exists(): + with open(path, 'r') as file: + project_dict = json.load(file) + self._extract_project_dict(project_dict) + else: + print(f'ERROR: File {path} does not exist') + def _construct_project_dict(self, include_materials_not_in_model=False): project_dict = {} project_dict['info'] = self._info diff --git a/tests/test_project.py b/tests/test_project.py index 79e98332..44ef8b52 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -318,3 +318,20 @@ def test_save_project(self): # Expect assert project_path.exists() + + def test_load_project(self): + # When + project = Project() + project._models.append(Model()) + project._info['name'] = 'Test_Project' + project.save_project_json() + + global_object.map._clear() + new_project = Project() + + # Then + new_project.load_project_json(new_project._current_path / 'Test_Project' / 'project.json') + + # Expect + assert len(new_project._models) == 1 + assert new_project._info['name'] == 'Test_Project'