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

Source Code for Module prompt.state

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003, 2004 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  import sys 
 24   
 25  import help 
 26   
 27   
28 -class State:
29 - def __init__(self, relax):
30 # Help. 31 self.__relax_help__ = \ 32 """Class for saving or loading the program state.""" 33 34 # Add the generic help string. 35 self.__relax_help__ = self.__relax_help__ + "\n" + help.relax_class_help 36 37 # Place relax in the class namespace. 38 self.__relax__ = relax
39 40
41 - def load(self, file=None, dir=None):
42 """Function for loading a saved program state. 43 44 Keyword Arguments 45 ~~~~~~~~~~~~~~~~~ 46 47 file: The file name, which must be a string, of a saved program state. 48 49 dir: Directory which the file is found in. 50 51 52 Description 53 ~~~~~~~~~~~ 54 55 This function is able to handle uncompressed, bzip2 compressed files, or gzip compressed 56 files automatically. The full file name including extension can be supplied, however, if 57 the file cannot be found, this function will search for the file name with '.bz2' appended 58 followed by the file name with '.gz' appended. 59 60 61 Examples 62 ~~~~~~~~ 63 64 The following commands will load the state saved in the file 'save'. 65 66 relax> state.load('save') 67 relax> state.load(file='save') 68 69 70 The following commands will load the state saved in the bzip2 compressed file 'save.bz2'. 71 72 relax> state.load('save') 73 relax> state.load(file='save') 74 relax> state.load('save.bz2') 75 relax> state.load(file='save.bz2') 76 """ 77 78 # Function intro text. 79 if self.__relax__.interpreter.intro: 80 text = sys.ps3 + "state.load(" 81 text = text + "file=" + `file` 82 text = text + ", dir=" + `dir` + ")" 83 print text 84 85 # File name. 86 if type(file) != str: 87 raise RelaxStrError, ('file name', file) 88 89 # Directory. 90 if dir != None and type(dir) != str: 91 raise RelaxNoneStrError, ('directory', dir) 92 93 # Execute the functional code. 94 self.__relax__.generic.state.load(file=file, dir=dir)
95 96
97 - def save(self, file=None, dir=None, force=0, compress_type=1):
98 """Function for saving the program state. 99 100 Keyword Arguments 101 ~~~~~~~~~~~~~~~~~ 102 103 file: The file name, which must be a string, to save the current program state in. 104 105 dir: The directory to place the file in. 106 107 force: A flag which if set to 1 will cause the file to be overwritten. 108 109 110 Description 111 ~~~~~~~~~~~ 112 113 The default behaviour of this function is to compress the file using bzip2 compression. If 114 the extension '.bz2' is not included in the file name, it will be added. The compression 115 can, however, be changed to either no compression or gzip compression. This is controlled 116 by the compress_type argument which can be set to: 117 0 - No compression (no file extension). 118 1 - bzip2 compression ('.bz2' file extension). 119 2 - gzip compression ('.gz' file extension). 120 121 122 Examples 123 ~~~~~~~~ 124 125 The following commands will save the current program state into the file 'save': 126 127 relax> state.save('save', compress_type=0) 128 relax> state.save(file='save', compress_type=0) 129 130 131 The following commands will save the current program state into the bzip2 compressed file 132 'save.bz2': 133 134 relax> state.save('save') 135 relax> state.save(file='save') 136 relax> state.save('save.bz2') 137 relax> state.save(file='save.bz2') 138 139 140 If the file 'save' already exists, the following commands will save the current program 141 state by overwriting the file. 142 143 relax> state.save('save', 1) 144 relax> state.save(file='save', force=1) 145 """ 146 147 # Function intro text. 148 if self.__relax__.interpreter.intro: 149 text = sys.ps3 + "state.save(" 150 text = text + "file=" + `file` 151 text = text + ", dir=" + `dir` 152 text = text + ", force=" + `force` 153 text = text + ", compress_type=" + `compress_type` + ")" 154 print text 155 156 # File name. 157 if type(file) != str: 158 raise RelaxStrError, ('file name', file) 159 160 # Directory. 161 if dir != None and type(dir) != str: 162 raise RelaxNoneStrError, ('directory', dir) 163 164 # The force flag. 165 if type(force) != int or (force != 0 and force != 1): 166 raise RelaxBinError, ('force flag', force) 167 168 # Compression type. 169 if type(compress_type) != int: 170 raise RelaxIntError, ('compression type', compress_type) 171 172 # Execute the functional code. 173 self.__relax__.generic.state.save(file=file, dir=dir, force=force, compress_type=compress_type)
174