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

Source Code for Module generic_fns.results

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program 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 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Module for reading/writing/displaying the results in a data pipe.""" 
 24   
 25  # Python module imports. 
 26  from os.path import dirname 
 27  from re import search 
 28  import sys 
 29   
 30  # relax module imports. 
 31  from data import Relax_data_store; ds = Relax_data_store() 
 32  from generic_fns import pipes 
 33  from relax_errors import RelaxError, RelaxFileEmptyError 
 34  from relax_io import extract_data, get_file_path, open_read_file, open_write_file, strip 
 35  from specific_fns.setup import get_specific_fn 
 36   
 37   
38 -def determine_format(file):
39 """Determine the format of the results file. 40 41 @keyword file: The file object representing the results file. 42 @type file: file object 43 @return: The results file format. This can be 'xml' or 'columnar'. 44 @rtype: str or None 45 """ 46 47 # Header line. 48 header = file.readline() 49 header = header[:-1] # Strip the trailing newline. 50 if hasattr(header, 'decode'): # Python 3 byte type conversion. 51 header = header.decode() 52 53 # Be nice and go back to the start of the file. 54 file.seek(0) 55 56 # XML. 57 if search(r"<\?xml", header): 58 return 'xml' 59 60 # Columnar. 61 if header.split()[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', dir=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=dir) 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", dir=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 dir == 'pipe_name': 128 dir = pipes.cdp_name() 129 130 # Open the file for writing. 131 results_file = open_write_file(file_name=file, dir=dir, 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