Package Proxy :: Module Popsdat
[hide private]
[frames] | no frames]

Source Code for Module Proxy.Popsdat

  1  # Copyright (C) 2005 Joint Institute for VLBI in Europe 
  2  # 
  3  # This program is free software; you can redistribute it and/or modify 
  4  # it under the terms of the GNU General Public License as published by 
  5  # the Free Software Foundation; either version 2 of the License, or 
  6  # (at your option) any later version. 
  7  # 
  8  # This program is distributed in the hope that it will be useful, 
  9  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 11  # GNU General Public License for more details. 
 12   
 13  # You should have received a copy of the GNU General Public License 
 14  # along with this program; if not, write to the Free Software 
 15  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 16   
 17  """ 
 18   
 19  This module provides dictionaries of default values and string lengths 
 20  for AIPS adverbs generated from the POPSDAT.HLP help file.  These 
 21  dictionaries are pickled to avoid parsing POPSDAT.HLP again and again. 
 22   
 23  """ 
 24   
 25  # Generic Python stuff. 
 26  import os, pickle 
 27   
28 -class Popsdat:
29 - def __parse(self):
30 """Determine default values and string lengths for all AIPS 31 tasks by parsing POPSDAT.HLP.""" 32 33 # POPSDAT.HLP contains some POPS statements that set default 34 # values for arrays and strings. Parse those statements first 35 # such that we can use those default values below. 36 self.__parse_statements() 37 38 input = open(self.path) 39 40 for line in input: 41 # A line of dashes starts the parameter definitions. 42 if line.startswith('--------'): 43 break 44 continue 45 46 for line in input: 47 # Comment lines start with ';' or 'C-'. 48 if line.startswith(';') or line.startswith('C-'): 49 continue 50 51 split_line = line.split() 52 name = split_line[0].lower() 53 type = int(split_line[2]) 54 55 if type == 1: # Float 56 self.default_dict[name] = float(split_line[3]) 57 elif type == 2: # Array of floats. 58 if name in self.default_dict: 59 value = [self.default_dict[name]] 60 else: 61 value = [0.0] 62 pass 63 64 dimensions = int(split_line[3]) 65 if dimensions == 1: # Vector of floats. 66 length = int(float(split_line[4])) 67 self.default_dict[name] = [None] + length * value 68 elif dimensions == 2: # Matrix of floats. 69 dimy = int(float(split_line[4])) 70 dimx = int(float(split_line[5])) 71 self.default_dict[name] = [None] \ 72 + dimx * [[None] + dimy * value] 73 else: 74 msg = "Cannot handle float arrays of dimension %d" \ 75 % dimension 76 raise AssertionError, msg 77 elif type == 4: # Verb 78 self.verb_dict[name] = int(split_line[2]) 79 elif type == 6: # End of adverbs. 80 break 81 elif type == 7: # Array of characters. 82 if name in self.default_dict: 83 value = self.default_dict[name] 84 else: 85 value = '' 86 pass 87 88 dimensions = int(split_line[3]) 89 self.strlen_dict[name] = int(float(split_line[4])) 90 if dimensions == 1: # String 91 self.default_dict[name] = value 92 elif dimensions == 2: # Vector of strings. 93 length = int(float(split_line[5])) 94 self.default_dict[name] = [None] + length * [value] 95 else: 96 msg = "Cannot handle character arrays of dimension %d" \ 97 % dimension 98 raise AssertionError, msg 99 else: 100 continue 101 102 for line in input: 103 # Older AIPS versions have some essential additional 104 # adverbs that are defined in PROC DEFADV. 105 if line.startswith('PROC DEFADV'): 106 break 107 continue 108 109 for line in input: 110 # The end of a PROC is marked by FINISH. 111 if line.startswith('FINISH'): 112 break 113 114 split_line = line.split() 115 type = split_line[0] 116 117 if type == 'ARRAY': 118 for array in split_line[1:]: 119 lparen = array.find('(') 120 rparen = array.find(')') 121 if lparen == -1 or rparen == -1: 122 continue 123 name = array[:lparen].lower() 124 dim = array[lparen+1:rparen] 125 dim = dim.split(',') 126 if len(dim) == 1: 127 length = int(dim[0]) 128 self.default_dict[name] = [None] + length * [0.0] 129 elif len(dim) == 2: 130 dimx = int(dim[0]) 131 dimy = int(dim[1]) 132 self.default_dict[name] = [None] \ 133 + dimx * [[None] \ 134 + dimy *[0.0]] 135 else: 136 continue 137 continue 138 pass 139 elif type == 'SCALAR': 140 for scalar in split_line[1:]: 141 name = scalar.rstrip(',').lower() 142 if name: 143 self.default_dict[name] = 0.0 144 pass 145 continue 146 pass 147 continue
148
149 - def __parse_statements(self):
150 """Parse statements in POPSDAT.HLP.""" 151 152 input = open(self.path) 153 154 for line in input: 155 # Statements follow the adverbs. 156 if line.startswith('QUITPOPS'): 157 break; 158 continue 159 160 for line in input: 161 if line.startswith('*'): 162 break 163 continue 164 165 for line in input: 166 if line.startswith('*'): 167 break 168 169 # If we have multiple statements on a line, split them. 170 split_line = line.strip().split(';') 171 for statement in split_line: 172 if not statement: 173 continue 174 175 # Parse statement. This really only parses 176 # assignments, but that's all we care about (for now). 177 words = statement.strip().split('=') 178 name = words[0].strip().lower() 179 value = words[1].strip() 180 if value.startswith("'"): 181 value = value.strip("'") 182 else: 183 value = float(value) 184 pass 185 186 # Set temporary default value. The permanent default 187 # value will be set later. 188 self.default_dict[name] = value 189 continue
190
191 - def __init__(self, version):
192 self.default_dict = {} 193 self.strlen_dict = {} 194 self.verb_dict = {} 195 196 self.version = version 197 self.path = self.version + '/HELP/POPSDAT.HLP' 198 if not os.path.exists(self.path): 199 self.version = os.environ['AIPS_VERSION'] 200 self.path = self.version + '/HELP/POPSDAT.HLP' 201 202 path = os.environ['HOME'] + '/.ParselTongue/' \ 203 + os.path.basename(self.version) + '/' + 'popsdat.pickle' 204 205 try: 206 unpickler = pickle.Unpickler(open(path)) 207 self.default_dict = unpickler.load() 208 self.strlen_dict = unpickler.load() 209 self.verb_dict = unpickler.load() 210 except IOError, EOFError: 211 self.__parse() 212 213 # Make sure the directory exists. 214 if not os.path.exists(os.path.dirname(path)): 215 os.makedirs(os.path.dirname(path)) 216 217 pickler = pickle.Pickler(open(path, mode='w')) 218 pickler.dump(self.default_dict) 219 pickler.dump(self.strlen_dict) 220 pickler.dump(self.verb_dict)
221