1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module for reading and writing the relax program state."""
25
26
27 from cPickle import dump, load
28 from re import search
29
30
31 from data import Relax_data_store; ds = Relax_data_store()
32 from generic_fns.reset import reset
33 from relax_errors import RelaxError
34 from relax_io import open_read_file, open_write_file
35 from status import Status; status = Status()
36
37
61
62
64 """Load the program state from the pickled file.
65
66 @param file: The file object containing the relax state.
67 @type file: file object
68 """
69
70
71 state = load(file)
72
73
74 file.close()
75
76
77 black_list = dir(dict) + ['__weakref__', '__dict__', '__module__', '__reset__', 'add', 'from_xml', 'is_empty', 'to_xml']
78
79
80 for name in dir(state):
81
82 if name in black_list:
83 continue
84
85
86 obj = getattr(state, name)
87
88
89 setattr(ds, name, obj)
90
91
92 for key in list(state.keys()):
93
94 ds[key] = state[key]
95
96
97 del state
98
99
100 return True
101
102
103 -def load_state(state=None, dir=None, verbosity=1, force=False):
104 """Function for loading a saved program state.
105
106 @keyword state: The saved state file.
107 @type state: str
108 @keyword dir: The path of the state file.
109 @type dir: str
110 @keyword verbosity: The verbosity level.
111 @type verbosity: int
112 @keyword force: If True, the relax data store will be reset prior to state loading.
113 @type force: bool
114 """
115
116
117 file = open_read_file(file_name=state, dir=dir, verbosity=verbosity)
118
119
120 format = determine_format(file)
121
122
123 if force:
124 reset()
125
126
127 if not ds.is_empty():
128 raise RelaxError("The relax data store is not empty.")
129
130
131 if format == 'xml':
132 ds.from_xml(file)
133
134
135 elif format == 'pickle':
136 load_pickle(file)
137
138
139 else:
140 raise RelaxError("The saved state " + repr(state) + " is not compatible with this version of relax.")
141
142
143 status.observers.pipe_alteration.notify()
144
145
146 -def save_state(state=None, dir=None, compress_type=1, verbosity=1, force=False, pickle=False):
147 """Function for saving the program state.
148
149 @keyword state: The saved state file.
150 @type state: str
151 @keyword dir: The path of the state file.
152 @type dir: str
153 @keyword verbosity: The verbosity level.
154 @type verbosity: int
155 @keyword force: Boolean argument which if True causes the file to be overwritten if it
156 already exists.
157 @type force: bool
158 @keyword compress_type: The compression type. The integer values correspond to the compression
159 type: 0, no compression; 1, Bzip2 compression; 2, Gzip compression.
160 @type compress_type: int
161 """
162
163
164 file = open_write_file(file_name=state, dir=dir, verbosity=verbosity, force=force, compress_type=compress_type)
165
166
167 if pickle:
168 dump(ds, file, 1)
169
170
171 else:
172 ds.to_xml(file)
173
174
175 file.close()
176