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

Source Code for Module generic_fns.results

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2004, 2007-2009 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  """Module for reading/writing/displaying the results in a data pipe.""" 
 25   
 26  # Python module imports. 
 27  from os.path import dirname 
 28  from re import search 
 29  from string import split 
 30  import sys 
 31   
 32  # relax module imports. 
 33  from data import Relax_data_store; ds = Relax_data_store() 
 34  from generic_fns import pipes 
 35  from relax_errors import RelaxError, RelaxFileEmptyError 
 36  from relax_io import extract_data, get_file_path, open_read_file, open_write_file, strip 
 37  from specific_fns.setup import get_specific_fn 
 38   
 39   
40 -def determine_format(file):
41 """Determine the format of the results file. 42 43 @keyword file: The file object representing the results file. 44 @type file: file object 45 @return: The results file format. This can be 'xml' or 'columnar'. 46 @rtype: str or None 47 """ 48 49 # Header line. 50 header = file.readline() 51 header = header[:-1] # Strip the trailing newline. 52 53 # Be nice and go back to the start of the file. 54 file.seek(0) 55 56 # XML. 57 if search("<\?xml", header): 58 return 'xml' 59 60 # Columnar. 61 if split(header)[0:3] == ['Num', 'Name', 'Selected']: 62 return 'columnar'
63 64
65 -def display():
66 """Displaying the results/contents of the current data pipe.""" 67 68 # Test if the current data pipe exists. 69 pipes.test() 70 71 # Write the results. 72 ds.to_xml(sys.stdout)
73 74
75 -def read(file='results', directory=None):
76 """Function for reading the data out of a file.""" 77 78 # Test if the current data pipe exists. 79 pipes.test() 80 81 # Make sure that the data pipe is empty. 82 if not cdp.is_empty(): 83 raise RelaxError("The current data pipe is not empty.") 84 85 # Get the full file path, for later use. 86 file_path = get_file_path(file_name=file, dir=directory) 87 88 # Open the file. 89 file = open_read_file(file_name=file_path) 90 91 # Determine the format of the file. 92 format = determine_format(file) 93 94 # XML results. 95 if format == 'xml': 96 ds.from_xml(file, dir=dirname(file_path), pipe_to=pipes.cdp_name()) 97 98 # Columnar results. 99 elif format == 'columnar': 100 read_function = get_specific_fn('read_columnar_results', pipes.get_type(), raise_error=False) 101 102 # Extract the data from the file. 103 file_data = extract_data(file=file) 104 105 # Strip data. 106 file_data = strip(file_data) 107 108 # Do nothing if the file does not exist. 109 if not file_data: 110 raise RelaxFileEmptyError 111 112 # Read the results. 113 read_function(file_data) 114 115 # Unknown results file. 116 else: 117 raise RelaxError("The format of the results file " + repr(file_path) + " cannot be determined.")
118 119
120 -def write(file="results", directory=None, force=False, compress_type=1, verbosity=1):
121 """Create the results file.""" 122 123 # Test if the current data pipe exists. 124 pipes.test() 125 126 # The special data pipe name directory. 127 if directory == 'pipe_name': 128 directory = pipes.cdp_name() 129 130 # Open the file for writing. 131 results_file = open_write_file(file_name=file, dir=directory, force=force, compress_type=compress_type, verbosity=verbosity) 132 133 # Write the results. 134 ds.to_xml(results_file, pipes=pipes.cdp_name()) 135 136 # Close the results file. 137 results_file.close()
138