Package theia :: Package running :: Module parser
[hide private]
[frames] | no frames]

Source Code for Module theia.running.parser

  1  '''Module for the parsing on input data from .tia file.''' 
  2   
  3  # Provides: 
  4  #   readIn 
  5  #   dicOf 
  6   
  7  import numpy as np 
  8  from ..helpers import settings 
  9  from ..helpers.units import * 
 10  from ..helpers.tools import InputError 
 11   
 12  # these defs are for evaluation from input file 
 13  cos = np.cos 
 14  sin = np.sin 
 15  tan = np.tan 
 16  arcsin = np.arcsin 
 17  arccos = np.arccos 
 18  arctan = np.arctan 
 19  sqrt = np.sqrt 
 20  exp = np.exp 
 21   
22 -def readIn(name):
23 '''Finds the input data in a file. 24 25 Returns a list of tuples where tuple[0] identifies the object of which data 26 has been found and tuple[1] the data itself. tuple[1] may be a simple value 27 or a dictionary for constructors, etc. 28 29 Example return value: [ ('bd', {'X': 0., 'Y': 0., 'Z': 1.}), #constructor 30 ('LName', 'foo')] #string data. 31 32 name: file to read. [string] 33 34 May raise an InputError. 35 36 Returns a list of tuples. 37 38 ''' 39 ans = [] 40 j = 0 #counts lines 41 with open(name, 'r') as inF: 42 for line in inF.readlines(): 43 j = j+1 44 line = line.translate(None, ' ') #no spaces nor tabs 45 line = line.translate(None, '\t') 46 if line.find('#') > -1: 47 line = line[0:line.find('#')] #no comments 48 if len(line) < 2: 49 continue 50 elif line[0:5] == 'order': 51 word = line[6:line.find('\n')] 52 try: 53 ans.append(('order', int(eval(word)))) 54 except (SyntaxError, NameError): 55 raise InputError("Malformed input in "\ 56 +fileName+", line "+ str(j)\ 57 +". Could not parse '" +word+"'.") 58 except TypeError: 59 raise InputError("Malformed input in "\ 60 +fileName+", line "+ str(j)\ 61 +". Could not cast '" +word+"' to int.") 62 63 elif line[0:9] == 'threshold': 64 word = line[10:line.find('\n')] 65 try: 66 ans.append(('threshold', float(eval(word)))) 67 except (SyntaxError, NameError): 68 raise InputError("Malformed input in "\ 69 +fileName+", line "+ str(j)\ 70 +". Could not parse '" +word+"'.") 71 except TypeError: 72 raise InputError("Malformed input in "\ 73 +fileName+", line "+ str(j)\ 74 +". Could not cast '" +word+"' to float.") 75 76 elif line[0:2] == 'bm': 77 ans.append(('bm',dicOf('bm',line[2:line.find('\n')],name,j))) 78 elif line[0:2] == 'mr': 79 ans.append(('mr',dicOf('mr',line[2:line.find('\n')],name,j))) 80 elif line[0:2] == 'th': 81 ans.append(('th',dicOf('th',line[2:line.find('\n')],name,j))) 82 elif line[0:2] == 'tk': 83 ans.append(('tk',dicOf('tk',line[2:line.find('\n')],name,j))) 84 elif line[0:2] == 'bd': 85 ans.append(('bd',dicOf('bd',line[2:line.find('\n')],name,j))) 86 else: 87 ans.append(('LName', line[0:line.find('\n')])) 88 89 return ans
90
91 -def dicOf(st, line, fileName, lineNumber):
92 '''Extract the initializer dictionary from a line. 93 94 st: object tag, 'bm', 'th', ... [string] 95 line: line of data in .tia format (supposed no spaces nor tabs nor comments) 96 and without the obect tag. [string] 97 fileName: name of file (used to write errors). [string] 98 lineNumber: number fo this line in the file (used to write errors). [int] 99 100 May raise an InputError 101 Returns a dictionary ready for construction. 102 103 ''' 104 105 ans = {} 106 107 #allow empty constructor 108 if line == '': 109 return ans 110 111 words = line.split(',') 112 i = 0 113 while i < len(words): #inputs without '=' 114 if '=' in words[i]: 115 break 116 try: 117 ans[settings.inOrder[st][i]] = eval(words[i]) 118 except SyntaxError: 119 raise InputError("Malformed input in "\ 120 +fileName+", line " + str(lineNumber)+", entry "+ str(i+1)\ 121 +". Could not parse '" +words[i]+"'.") 122 except NameError: 123 raise InputError("Malformed input in "\ 124 +fileName+", line " + str(lineNumber)+", entry "+ str(i+1)\ 125 +". Did not recognize reference in '" +words[i]+"'.") 126 except IndexError: 127 raise InputError("Malformed input in "\ 128 +fileName+", line " + str(lineNumber)\ 129 +". To many arguments given.") 130 i = i + 1 131 132 while i < len(words): #inputs with '=' 133 word = words[i] 134 if word.find('=') == -1: 135 raise InputError("Malformed input in "\ 136 +fileName+", line " + str(lineNumber)+", entry "+ str(i+1)\ 137 +". Found non explicit entry '"+word\ 138 +"' among explicit entries.") 139 var = word[0:word.find('=')] 140 if var not in settings.inOrder[st]: 141 raise InputError("Malformed input in "\ 142 +fileName+", line " + str(lineNumber)+", entry "+ str(i+1)\ 143 +". Unknown constructor parameter '" +var+"'.") 144 val = word[word.find('=')+1:] 145 try: 146 ans[var] = eval(val) 147 except SyntaxError: 148 raise InputError("Malformed input in "\ 149 +fileName+", line " + str(lineNumber)+", entry "+ str(i+1)\ 150 +". Could not parse '" +val+"'.") 151 except NameError: 152 raise InputError("Malformed input in "\ 153 +fileName+", line " + str(lineNumber)+", entry "+ str(i+1)\ 154 +". Did not recognize reference in '" +val+"'.") 155 i = i+1 156 157 return ans
158