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

Source Code for Module prompt.state

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2005 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 118 0: No compression (no file extension). 119 1: bzip2 compression ('.bz2' file extension). 120 2: gzip compression ('.gz' file extension). 121 122 123 Examples 124 ~~~~~~~~ 125 126 The following commands will save the current program state into the file 'save': 127 128 relax> state.save('save', compress_type=0) 129 relax> state.save(file='save', compress_type=0) 130 131 132 The following commands will save the current program state into the bzip2 compressed file 133 'save.bz2': 134 135 relax> state.save('save') 136 relax> state.save(file='save') 137 relax> state.save('save.bz2') 138 relax> state.save(file='save.bz2') 139 140 141 If the file 'save' already exists, the following commands will save the current program 142 state by overwriting the file. 143 144 relax> state.save('save', 1) 145 relax> state.save(file='save', force=1) 146 """ 147 148 # Function intro text. 149 if self.__relax__.interpreter.intro: 150 text = sys.ps3 + "state.save(" 151 text = text + "file=" + `file` 152 text = text + ", dir=" + `dir` 153 text = text + ", force=" + `force` 154 text = text + ", compress_type=" + `compress_type` + ")" 155 print text 156 157 # File name. 158 if type(file) != str: 159 raise RelaxStrError, ('file name', file) 160 161 # Directory. 162 if dir != None and type(dir) != str: 163 raise RelaxNoneStrError, ('directory', dir) 164 165 # The force flag. 166 if type(force) != int or (force != 0 and force != 1): 167 raise RelaxBinError, ('force flag', force) 168 169 # Compression type. 170 if type(compress_type) != int: 171 raise RelaxIntError, ('compression type', compress_type) 172 173 # Execute the functional code. 174 self.__relax__.generic.state.save(file=file, dir=dir, force=force, compress_type=compress_type)
175