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

Source Code for Module prompt.interpreter

  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 __builtin__ 
 24  from code import InteractiveConsole, softspace 
 25  from os import F_OK, access 
 26  import readline 
 27  import signal 
 28  import sys 
 29   
 30  # Python modules accessible on the command prompt. 
 31  from math import pi 
 32  import Numeric 
 33  import Scientific 
 34   
 35  # Auxiliary modules. 
 36  from help import _Helper, _Helper_python 
 37  from command import Ls, Lh, Ll, system 
 38  from tab_completion import Tab_completion 
 39   
 40  # User functions. 
 41  from angles import Angles 
 42  from dx import OpenDX 
 43  from eliminate import Eliminate 
 44  from fix import Fix 
 45  from gpl import GPL 
 46  from init_data import Init_data 
 47  from minimisation import Minimisation 
 48  from model_selection import Modsel 
 49  from nuclei import Nuclei 
 50  from pdb import PDB 
 51   
 52  # User classes. 
 53  from diffusion_tensor import Diffusion_tensor 
 54  from grace import Grace 
 55  from jw_mapping import Jw_mapping 
 56  from model_free import Model_free 
 57  from molmol import Molmol 
 58  from monte_carlo import Monte_carlo 
 59  from noe import Noe 
 60  from palmer import Palmer 
 61  from relax_data import Relax_data 
 62  from relax_fit import Relax_fit 
 63  from results import Results 
 64  from run import Run 
 65  from select import Select 
 66  from sequence import Sequence 
 67  from state import State 
 68  from thread import Threading 
 69  from unselect import Unselect 
 70  from value import Value 
 71  from vmd import Vmd 
 72   
 73   
74 -class Interpreter:
75 - def __init__(self, relax):
76 """The interpreter class.""" 77 78 # Place the program class structure under self.relax 79 self.relax = relax 80 81 # The prompts. 82 sys.ps1 = 'relax> ' 83 sys.ps2 = 'relax| ' 84 sys.ps3 = '\nrelax> ' 85 86 # The function intro flag. 87 self.intro = 0 88 89 # Python modules. 90 self._pi = pi 91 self._Numeric = Numeric 92 self._Scientific = Scientific 93 94 # Place the user functions into the namespace of the interpreter class. 95 self._Angles = Angles(relax) 96 self._Eliminate = Eliminate(relax) 97 self._Fix = Fix(relax) 98 self._GPL = GPL 99 self._Init_data = Init_data(relax) 100 self._Minimisation = Minimisation(relax) 101 self._Modsel = Modsel(relax) 102 self._Nuclei = Nuclei(relax) 103 self._OpenDX = OpenDX(relax) 104 self._PDB = PDB(relax) 105 self._system = system 106 107 # Place the user classes into the interpreter class namespace. 108 self._Diffusion_tensor = Diffusion_tensor(relax) 109 self._OpenDX = OpenDX(relax) 110 self._Grace = Grace(relax) 111 self._Jw_mapping = Jw_mapping(relax) 112 self._Model_free = Model_free(relax) 113 self._Molmol = Molmol(relax) 114 self._Monte_carlo = Monte_carlo(relax) 115 self._Noe = Noe(relax) 116 self._Palmer = Palmer(relax) 117 self._Relax_data = Relax_data(relax) 118 self._Relax_fit = Relax_fit(relax) 119 self._Results = Results(relax) 120 self._Run = Run(relax) 121 self._Select = Select(relax) 122 self._Sequence = Sequence(relax) 123 self._State = State(relax) 124 self._Threading = Threading(relax) 125 self._Unselect = Unselect(relax) 126 self._Value = Value(relax) 127 self._Vmd = Vmd(relax)
128 129
130 - def run(self):
131 """Run the python interpreter. 132 133 The namespace of this function is the namespace seen inside the interpreter. All user 134 accessible functions, classes, etc, should be placed in this namespace. 135 """ 136 137 # Python modules. 138 pi = self._pi 139 Numeric = self._Numeric 140 Scientific = self._Scientific 141 142 # Import the functions emulating system commands. 143 lh = Lh() 144 ll = Ll() 145 ls = Ls() 146 system = self._system 147 148 # Place functions in the local namespace. 149 gpl = GPL = self._GPL() 150 151 # Place the user functions in the local namespace. 152 angles = self._Angles.angles 153 calc = self._Minimisation.calc 154 eliminate = self._Eliminate.eliminate 155 fix = self._Fix.fix 156 grid_search = self._Minimisation.grid_search 157 init_data = self._Init_data.init 158 minimise = self._Minimisation.minimise 159 model_selection = self._Modsel.model_selection 160 nuclei = self._Nuclei.nuclei 161 pdb = self._PDB.pdb 162 163 # Place the user classes in the local namespace. 164 diffusion_tensor = self._Diffusion_tensor 165 dx = self._OpenDX 166 grace = self._Grace 167 jw_mapping = self._Jw_mapping 168 model_free = self._Model_free 169 molmol = self._Molmol 170 monte_carlo = self._Monte_carlo 171 noe = self._Noe 172 palmer = self._Palmer 173 relax_data = self._Relax_data 174 relax_fit = self._Relax_fit 175 results = self._Results 176 run = self._Run 177 select = self._Select 178 sequence = self._Sequence 179 state = self._State 180 thread = self._Threading 181 unselect = self._Unselect 182 vmd = self._Vmd 183 value = self._Value 184 185 # Builtin interpreter functions. 186 intro_off = self._off 187 intro_on = self._on 188 exit = bye = quit = q = _Exit() 189 script = self.script 190 191 # Modify the help system. 192 help_python = _Helper_python() 193 help = _Helper() 194 195 # The local namespace. 196 self.local = locals() 197 198 # Setup tab completion. 199 readline.set_completer(Tab_completion(name_space=self.local).finish) 200 readline.set_completer_delims(' \t\n`~!@#$%^&*()=+{}\\|;:",<>/?') 201 #readline.set_completer_delims(' \t\n`~!@#$%^&*()=+{}\\|;:\'",<>/?') 202 readline.parse_and_bind("tab: complete") 203 204 # Execute the script file if given. 205 if self.relax.script_file: 206 # Turn on the function intro flag. 207 self.intro = 1 208 209 # Run the script. 210 run_script(intro=self.relax.intro_string, local=self.local, script_file=self.relax.script_file, quit=1) 211 212 # Go to the prompt. 213 else: 214 prompt(intro=self.relax.intro_string, local=self.local)
215 216
217 - def _off(self):
218 """Function for turning the function introductions off.""" 219 220 self.intro = 0 221 print "Function intros have been disabled."
222 223
224 - def _on(self):
225 """Function for turning the function introductions on.""" 226 227 self.intro = 1 228 print "Function intros have been enabled."
229 230
231 - def script(self, file=None, quit=0):
232 """Function for executing a script file.""" 233 234 # File argument. 235 if file == None: 236 raise RelaxNoneError, 'file' 237 elif type(file) != str: 238 raise RelaxStrError, ('file', file) 239 240 # Test if the script file exists. 241 if not access(file, F_OK): 242 raise RelaxError, "The script file '" + file + "' does not exist." 243 244 # Quit argument. 245 if type(quit) != int or (quit != 0 and quit != 1): 246 raise RelaxBinError, ('quit', quit) 247 248 # Turn on the function intro flag. 249 self.intro = 1 250 251 # Execute the script. 252 run_script(local=self.local, script_file=file, quit=quit) 253 254 # Turn off the function intro flag. 255 self.intro = 0
256 257
258 -class _Exit:
259 - def __repr__(self):
260 """Exit the program.""" 261 262 print "Exiting the program." 263 sys.exit()
264 265
266 -def interact_prompt(self, intro, local):
267 """Replacement function for 'code.InteractiveConsole.interact'. 268 269 This will enter into the prompt. 270 """ 271 272 # Print the program introduction. 273 if intro: 274 self.write("%s\n" % intro) 275 276 # Ignore SIGINT. 277 signal.signal(2, 1) 278 279 # Prompt. 280 more = 0 281 while 1: 282 try: 283 if more: 284 prompt = sys.ps2 285 else: 286 prompt = sys.ps1 287 try: 288 line = self.raw_input(prompt) 289 except EOFError: 290 self.write("\n") 291 break 292 else: 293 more = self.push(line) 294 except KeyboardInterrupt: 295 self.write("\nKeyboardInterrupt\n") 296 self.resetbuffer() 297 more = 0
298 299
300 -def interact_script(self, intro, local, script_file, quit):
301 """Replacement function for 'code.InteractiveConsole.interact'. 302 303 This will execute the script file. 304 """ 305 306 # Print the program introduction. 307 if intro: 308 sys.stdout.write("%s\n" % intro) 309 310 # Turn the intro flag on so functions will print their intro strings. 311 local['self'].intro = 1 312 313 # Print the script. 314 try: 315 file = open(script_file, 'r') 316 except IOError, warning: 317 try: 318 raise RelaxError, "The script file '" + script_file + "' does not exist." 319 except AllRelaxErrors, instance: 320 sys.stdout.write(instance.__str__()) 321 sys.stdout.write("\n") 322 return 323 sys.stdout.write("script = " + `script_file` + "\n") 324 sys.stdout.write("----------------------------------------------------------------------------------------------------\n") 325 sys.stdout.write(file.read()) 326 sys.stdout.write("----------------------------------------------------------------------------------------------------\n") 327 file.close() 328 329 # Execute the script. 330 try: 331 execfile(script_file, local) 332 except KeyboardInterrupt: 333 sys.stderr.write("\nScript execution cancelled.\n") 334 except AllRelaxErrors, instance: 335 if Debug: 336 self.showtraceback() 337 else: 338 sys.stderr.write(instance.__str__()) 339 except: 340 raise 341 342 sys.stdout.write("\n") 343 344 # Quit. 345 if quit: 346 sys.exit()
347 348
349 -def prompt(intro=None, local=None):
350 """Python interpreter emulation. 351 352 This function replaces 'code.interact'. 353 """ 354 355 # Replace the 'InteractiveConsole.interact' and 'InteractiveConsole.runcode' functions. 356 InteractiveConsole.interact = interact_prompt 357 InteractiveConsole.runcode = runcode 358 359 # The console. 360 console = InteractiveConsole(local) 361 console.interact(intro, local)
362 363
364 -def run_script(intro=None, local=None, script_file=None, quit=1):
365 """Python interpreter emulation. 366 367 This function replaces 'code.interact'. 368 """ 369 370 # Replace the 'InteractiveConsole.interact' and 'InteractiveConsole.runcode' functions. 371 InteractiveConsole.interact = interact_script 372 InteractiveConsole.runcode = runcode 373 374 # The console. 375 console = InteractiveConsole(local) 376 console.interact(intro, local, script_file, quit)
377 378
379 -def runcode(self, code):
380 """Replacement code for code.InteractiveInterpreter.runcode""" 381 382 try: 383 exec code in self.locals 384 except SystemExit: 385 raise 386 except AllRelaxErrors, instance: 387 self.write(instance.__str__()) 388 self.write("\n") 389 except: 390 self.showtraceback() 391 else: 392 if softspace(sys.stdout, 0): 393 print
394