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

Source Code for Module generic_fns.state

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2012 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  # Python module imports. 
 26  from re import search 
 27   
 28  # relax module imports. 
 29  from data import Relax_data_store; ds = Relax_data_store() 
 30  from generic_fns.reset import reset 
 31  from relax_errors import RelaxError 
 32  from relax_io import open_read_file, open_write_file 
 33  from status import Status; status = Status() 
 34   
 35   
36 -def load_pickle(file):
37 """Load the program state from the pickled file. 38 39 @param file: The file object containing the relax state. 40 @type file: file object 41 """ 42 43 # Unpickle the data class. 44 state = load(file) 45 46 # Close the file. 47 file.close() 48 49 # Black list of objects (all dict objects, non-modifiable objects, data store specific methods, and other special objects). 50 black_list = dir(dict) + ['__weakref__', '__dict__', '__module__', '__reset__', '_back_compat_hook', 'add', 'from_xml', 'is_empty', 'to_xml'] 51 52 # Loop over the objects in the saved state, and dump them into the relax data store. 53 for name in dir(state): 54 # Skip blacklisted objects. 55 if name in black_list: 56 continue 57 58 # Get the object. 59 obj = getattr(state, name) 60 61 # Place ALL objects into the singleton! 62 setattr(ds, name, obj) 63 64 # Loop over the keys of the dictionary. 65 for key in list(state.keys()): 66 # Shift the PipeContainer. 67 ds[key] = state[key] 68 69 # Delete the state object. 70 del state 71 72 # Success. 73 return True
74 75
76 -def load_state(state=None, dir=None, verbosity=1, force=False):
77 """Function for loading a saved program state. 78 79 @keyword state: The saved state file. 80 @type state: str 81 @keyword dir: The path of the state file. 82 @type dir: str 83 @keyword verbosity: The verbosity level. 84 @type verbosity: int 85 @keyword force: If True, the relax data store will be reset prior to state loading. 86 @type force: bool 87 """ 88 89 # Open the file for reading. 90 file = open_read_file(file_name=state, dir=dir, verbosity=verbosity) 91 92 # Reset. 93 if force: 94 reset() 95 96 # Make sure that the data store is empty. 97 if not ds.is_empty(): 98 raise RelaxError("The relax data store is not empty.") 99 100 # Restore from the XML. 101 ds.from_xml(file) 102 103 # Signal a change in the current data pipe. 104 status.observers.pipe_alteration.notify() 105 106 # Signal the state loading 107 status.observers.state_load.notify()
108 109
110 -def save_state(state=None, dir=None, compress_type=1, verbosity=1, force=False):
111 """Function for saving the program state. 112 113 @keyword state: The saved state file. 114 @type state: str 115 @keyword dir: The path of the state file. 116 @type dir: str 117 @keyword verbosity: The verbosity level. 118 @type verbosity: int 119 @keyword force: Boolean argument which if True causes the file to be overwritten if it 120 already exists. 121 @type force: bool 122 @keyword compress_type: The compression type. The integer values correspond to the compression 123 type: 0, no compression; 1, Bzip2 compression; 2, Gzip compression. 124 @type compress_type: int 125 """ 126 127 # Open the file for writing. 128 file = open_write_file(file_name=state, dir=dir, verbosity=verbosity, force=force, compress_type=compress_type) 129 130 # Save as XML. 131 ds.to_xml(file) 132 133 # Close the file. 134 file.close()
135