Package generic_fns :: Module script
[hide private]
[frames] | no frames]

Source Code for Module generic_fns.script

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2003-2012 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  # Module docstring. 
24  """Functions for executing relax scripts.""" 
25   
26  # Python module imports. 
27  from os import F_OK, access 
28   
29  # relax module imports. 
30  import prompt.interpreter 
31  from relax_errors import RelaxError 
32  from relax_io import get_file_path 
33  from status import Status; status = Status() 
34   
35   
36 -def script(file=None, dir=None):
37 """Function for executing a script file. 38 39 @keyword file: The name of the script to execute. 40 @type file: str 41 @keyword dir: The name of the directory in which the script is located. 42 @type dir: str or None 43 """ 44 45 # File argument. 46 if file == None: 47 raise RelaxNoneError('file') 48 elif not isinstance(file, str): 49 raise RelaxStrError('file', file) 50 51 # The full path. 52 file_path = get_file_path(file, dir) 53 54 # Test if the script file exists. 55 if not access(file_path, F_OK): 56 raise RelaxError("The script file '%s' does not exist." % file_path) 57 58 # Turn on the function intro flag. 59 orig_intro_state = status.uf_intro 60 status.uf_intro = True 61 62 # Load the interpreter. 63 interpreter = prompt.interpreter.Interpreter(show_script=False, quit=False, raise_relax_error=True) 64 interpreter.populate_self() 65 interpreter.on(verbose=False) 66 67 # Execute the script. 68 prompt.interpreter.run_script(local=interpreter._locals, script_file=file_path) 69 70 # Return the function intro flag to the original value. 71 status.uf_intro = orig_intro_state
72