1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module for reading and writing the relax program state."""
24
25
26 from data_store import Relax_data_store; ds = Relax_data_store()
27 from lib.errors import RelaxError
28 from lib.io import open_read_file, open_write_file
29 from pipe_control.reset import reset
30 from status import Status; status = Status()
31
32
34 """Load the program state from the pickled file.
35
36 @param file: The file object containing the relax state.
37 @type file: file object
38 """
39
40
41 state = load(file)
42
43
44 file.close()
45
46
47 black_list = dir(dict) + ['__weakref__', '__dict__', '__module__', '__reset__', '_back_compat_hook', 'add', 'from_xml', 'is_empty', 'to_xml']
48
49
50 for name in dir(state):
51
52 if name in black_list:
53 continue
54
55
56 obj = getattr(state, name)
57
58
59 setattr(ds, name, obj)
60
61
62 for key in list(state.keys()):
63
64 ds[key] = state[key]
65
66
67 del state
68
69
70 return True
71
72
73 -def load_state(state=None, dir=None, verbosity=1, force=False):
74 """Function for loading a saved program state.
75
76 @keyword state: The saved state file.
77 @type state: str
78 @keyword dir: The path of the state file.
79 @type dir: str
80 @keyword verbosity: The verbosity level.
81 @type verbosity: int
82 @keyword force: If True, the relax data store will be reset prior to state loading.
83 @type force: bool
84 """
85
86
87 file = open_read_file(file_name=state, dir=dir, verbosity=verbosity)
88
89
90 if force:
91 reset()
92
93
94 if not ds.is_empty():
95 raise RelaxError("The relax data store is not empty.")
96
97
98 ds.from_xml(file)
99
100
101 status.observers.pipe_alteration.notify()
102
103
104 status.observers.state_load.notify()
105
106
107 -def save_state(state=None, dir=None, compress_type=1, verbosity=1, force=False):
108 """Function for saving the program state.
109
110 @keyword state: The saved state file.
111 @type state: str
112 @keyword dir: The path of the state file.
113 @type dir: str
114 @keyword verbosity: The verbosity level.
115 @type verbosity: int
116 @keyword force: Boolean argument which if True causes the file to be overwritten if it
117 already exists.
118 @type force: bool
119 @keyword compress_type: The compression type. The integer values correspond to the compression
120 type: 0, no compression; 1, Bzip2 compression; 2, Gzip compression.
121 @type compress_type: int
122 """
123
124
125 file = open_write_file(file_name=state, dir=dir, verbosity=verbosity, force=force, compress_type=compress_type)
126
127
128 ds.to_xml(file)
129
130
131 file.close()
132