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

Source Code for Module prompt.interpreter

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