Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2017 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source
# & Institut Laue - Langevin
# SPDX - License - Identifier: GPL - 3.0 +
# This file is part of the mantidqt package
#
from json import dumps
from os import makedirs
from os.path import isdir
from mantidqt.project import workspacesaver
from mantid import logger
ENCODED_FILE_NAME = "mantidsave.project"
class ProjectSaver(object):
def save_project(self, directory, workspace_to_save=None, interfaces_to_save=None):
"""
:param directory: String;
:param workspace_to_save: List; of Strings that will have workspace names in it, if None will save all
:param interfaces_to_save: List; of Strings that will have interface tags in it, if None will save all
:return:
"""
# Check if the directory doesn't exist
if directory is None:
logger.warning("Can not save to empty directory")
return
# Check this isn't saving a blank project file
if workspace_to_save is None and interfaces_to_save is None:
logger.warning("Can not save an empty project")
return
# Save workspaces to that location
workspace_saver = workspacesaver.WorkspaceSaver(directory=directory)
workspace_saver.save_workspaces(workspaces_to_save=workspace_to_save)
# Get interface details in dicts
dictionaries_to_save = {}
# Pass dicts to Project Writer
writer = ProjectWriter(dictionaries_to_save, directory, workspace_saver.get_output_list())
writer.write_out()
# Static private method to create JSON from objects and return a string
def _create_out_string_json(dictionary):
"""
:param dictionary:
:return:
"""
return dumps(dictionary)
class ProjectWriter(object):
def __init__(self, dicts, save_location, workspace_names):
"""
:param dicts:
:param save_location:
:param workspace_names:
"""
self.dicts_to_save = dicts
self.workspace_names = workspace_names
self.directory = save_location
def write_out(self):
"""
"""
# Get the JSON string versions
workspace_interface_dict = {"workspaces": self.workspace_names, "interfaces": self.dicts_to_save}
json_string = _create_out_string_json(workspace_interface_dict)
# Open file and save the string to it alongside the workspace_names
file_name = self.directory + '/' + ENCODED_FILE_NAME
if not isdir(self.directory):
makedirs(self.directory)
f = open(file_name, 'w+')
f.write(json_string)