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