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

Source Code for Module generic_fns.state

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2011 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax 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 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax 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 relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Module for reading and writing the relax program state.""" 
 25   
 26  # Python module imports. 
 27  from cPickle import dump, load 
 28  from re import search 
 29   
 30  # relax module imports. 
 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   
38 -def determine_format(file):
39 """Determine the format of the state file. 40 41 @keyword file: The file object representing the state file. 42 @type file: file object 43 @return: The state format. This can be 'xml' or 'pickle'. 44 @rtype: str or None 45 """ 46 47 # 1st line. 48 header = file.readline() 49 header = header[:-1] # Strip the trailing newline. 50 51 # Be nice and go back to the start of the file. 52 file.seek(0) 53 54 # XML. 55 if search("<\?xml", header): 56 return 'xml' 57 58 # Pickle. 59 elif search("ccopy_reg", header): 60 return 'pickle'
61 62
63 -def load_pickle(file):
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 # Unpickle the data class. 71 state = load(file) 72 73 # Close the file. 74 file.close() 75 76 # Black list of objects (all dict objects, non-modifiable objects, data store specific methods, and other special objects). 77 black_list = dir(dict) + ['__weakref__', '__dict__', '__module__', '__reset__', 'add', 'from_xml', 'is_empty', 'to_xml'] 78 79 # Loop over the objects in the saved state, and dump them into the relax data store. 80 for name in dir(state): 81 # Skip blacklisted objects. 82 if name in black_list: 83 continue 84 85 # Get the object. 86 obj = getattr(state, name) 87 88 # Place ALL objects into the singleton! 89 setattr(ds, name, obj) 90 91 # Loop over the keys of the dictionary. 92 for key in list(state.keys()): 93 # Shift the PipeContainer. 94 ds[key] = state[key] 95 96 # Delete the state object. 97 del state 98 99 # Success. 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 # Open the file for reading. 117 file = open_read_file(file_name=state, dir=dir, verbosity=verbosity) 118 119 # Determine the format of the file. 120 format = determine_format(file) 121 122 # Reset. 123 if force: 124 reset() 125 126 # Make sure that the data store is empty. 127 if not ds.is_empty(): 128 raise RelaxError("The relax data store is not empty.") 129 130 # XML state. 131 if format == 'xml': 132 ds.from_xml(file) 133 134 # Pickled state. 135 elif format == 'pickle': 136 load_pickle(file) 137 138 # Bad state file. 139 else: 140 raise RelaxError("The saved state " + repr(state) + " is not compatible with this version of relax.") 141 142 # Signal a change in the current data pipe. 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 # Open the file for writing. 164 file = open_write_file(file_name=state, dir=dir, verbosity=verbosity, force=force, compress_type=compress_type) 165 166 # Pickle the data class and write it to file 167 if pickle: 168 dump(ds, file, 1) 169 170 # Otherwise save as XML. 171 else: 172 ds.to_xml(file) 173 174 # Close the file. 175 file.close()
176