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

Source Code for Module prompt.state

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2005, 2007, 2009-2010 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 containing the 'state' user function class.""" 
 25  __docformat__ = 'plaintext' 
 26   
 27  # relax module imports. 
 28  from base_class import User_fn_class 
 29  import arg_check 
 30  from generic_fns.state import load_state, save_state 
 31   
 32   
33 -class State(User_fn_class):
34 """Class for saving or loading the program state.""" 35
36 - def load(self, state=None, dir=None, force=False):
37 """Function for loading a saved program state. 38 39 Keyword Arguments 40 ~~~~~~~~~~~~~~~~~ 41 42 state: The file name, which can be a string or a file descriptor object, of a saved program 43 state. 44 45 dir: The name of the directory in which the file is found. 46 47 force: A boolean flag which if True will cause the current program state to be overwritten. 48 49 50 Description 51 ~~~~~~~~~~~ 52 53 This function is able to handle uncompressed, bzip2 compressed files, or gzip compressed 54 files automatically. The full file name including extension can be supplied, however, if 55 the file cannot be found, this function will search for the file name with '.bz2' appended 56 followed by the file name with '.gz' appended. 57 58 Both the XML and pickled saved state formats are supported and automatically determined. 59 For more advanced users, file descriptor objects are also supported. If the force flag is 60 set to True, then the relax data store will be reset prior to the loading of the saved 61 state. 62 63 64 Examples 65 ~~~~~~~~ 66 67 The following commands will load the state saved in the file 'save'. 68 69 relax> state.load('save') 70 relax> state.load(state='save') 71 72 73 Use one of the following commands to load the state saved in the bzip2 compressed file 74 'save.bz2': 75 76 relax> state.load('save') 77 relax> state.load(state='save') 78 relax> state.load('save.bz2') 79 relax> state.load(state='save.bz2', force=True) 80 """ 81 82 # Function intro text. 83 if self._exec_info.intro: 84 text = self._exec_info.ps3 + "state.load(" 85 text = text + "state=" + repr(state) 86 text = text + ", dir=" + repr(dir) 87 text = text + ", force=" + repr(force) + ")" 88 print(text) 89 90 # The argument checks. 91 arg_check.is_str_or_inst(state, 'file name') 92 arg_check.is_str(dir, 'directory name', can_be_none=True) 93 arg_check.is_bool(force, 'force flag') 94 95 # Execute the functional code. 96 load_state(state=state, dir=dir, force=force)
97 98
99 - def save(self, state=None, dir=None, compress_type=1, force=False, pickle=False):
100 """Function for saving the program state. 101 102 Keyword Arguments 103 ~~~~~~~~~~~~~~~~~ 104 105 state: The file name, which can be a string or a file descriptor object, to save the 106 current program state in. 107 108 dir: The name of the directory in which to place the file. 109 110 compress_type: The type of compression to use when creating the file. 111 112 force: A boolean flag which if set to True will cause the file to be overwritten. 113 114 pickle: A flag which if true will cause the state file to be a pickled object rather than 115 the default XML format. 116 117 118 Description 119 ~~~~~~~~~~~ 120 121 This user function will place the program state - the relax data store - into a file for 122 later reloading or reference. The default format is an XML formatted file, but this can be 123 changed to a Python pickled object through the pickle flag. Note, the pickle format is not 124 human readable and often is not compatible with newer relax versions. 125 126 The default behaviour of this function is to compress the file using bzip2 compression. If 127 the extension '.bz2' is not included in the file name, it will be added. The compression 128 can, however, be changed to either no compression or gzip compression. This is controlled 129 by the compress_type argument which can be set to 130 131 0: No compression (no file extension). 132 1: bzip2 compression ('.bz2' file extension). 133 2: gzip compression ('.gz' file extension). 134 135 136 Examples 137 ~~~~~~~~ 138 139 The following commands will save the current program state, uncompressed, into the file 'save': 140 141 relax> state.save('save', compress_type=0) 142 relax> state.save(state='save', compress_type=0) 143 144 145 The following commands will save the current program state into the bzip2 compressed file 146 'save.bz2': 147 148 relax> state.save('save') 149 relax> state.save(state='save') 150 relax> state.save('save.bz2') 151 relax> state.save(state='save.bz2') 152 153 154 If the file 'save' already exists, the following commands will save the current program 155 state by overwriting the file. 156 157 relax> state.save('save', force=True) 158 relax> state.save(state='save', force=True) 159 """ 160 161 # Function intro text. 162 if self._exec_info.intro: 163 text = self._exec_info.ps3 + "state.save(" 164 text = text + "state=" + repr(state) 165 text = text + ", dir=" + repr(dir) 166 text = text + ", compress_type=" + repr(compress_type) 167 text = text + ", force=" + repr(force) 168 text = text + ", pickle=" + repr(pickle) + ")" 169 print(text) 170 171 # The argument checks. 172 arg_check.is_str_or_inst(state, 'file name') 173 arg_check.is_str(dir, 'directory name', can_be_none=True) 174 arg_check.is_int(compress_type, 'compression type') 175 arg_check.is_bool(force, 'force flag') 176 arg_check.is_bool(pickle, 'pickle flag') 177 178 # Execute the functional code. 179 save_state(state=state, dir=dir, compress_type=compress_type, force=force, pickle=pickle)
180