Package pipe_control :: Module state
[hide private]
[frames] | no frames]

Source Code for Module pipe_control.state

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2013 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Module for reading and writing the relax program state.""" 
 24   
 25  # relax module imports. 
 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   
33 -def load_pickle(file):
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 # Unpickle the data class. 41 state = load(file) 42 43 # Close the file. 44 file.close() 45 46 # Black list of objects (all dict objects, non-modifiable objects, data store specific methods, and other special objects). 47 black_list = dir(dict) + ['__weakref__', '__dict__', '__module__', '__reset__', '_back_compat_hook', 'add', 'from_xml', 'is_empty', 'to_xml'] 48 49 # Loop over the objects in the saved state, and dump them into the relax data store. 50 for name in dir(state): 51 # Skip blacklisted objects. 52 if name in black_list: 53 continue 54 55 # Get the object. 56 obj = getattr(state, name) 57 58 # Place ALL objects into the singleton! 59 setattr(ds, name, obj) 60 61 # Loop over the keys of the dictionary. 62 for key in list(state.keys()): 63 # Shift the PipeContainer. 64 ds[key] = state[key] 65 66 # Delete the state object. 67 del state 68 69 # Success. 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 # Open the file for reading. 87 file = open_read_file(file_name=state, dir=dir, verbosity=verbosity) 88 89 # Reset. 90 if force: 91 reset() 92 93 # Make sure that the data store is empty. 94 if not ds.is_empty(): 95 raise RelaxError("The relax data store is not empty.") 96 97 # Restore from the XML. 98 ds.from_xml(file) 99 100 # Signal a change in the current data pipe. 101 status.observers.pipe_alteration.notify() 102 103 # Signal the state loading 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 # Open the file for writing. 125 file = open_write_file(file_name=state, dir=dir, verbosity=verbosity, force=force, compress_type=compress_type) 126 127 # Save as XML. 128 ds.to_xml(file) 129 130 # Close the file. 131 file.close()
132