1 '''Module for the parsing on input data from .tia file.'''
2
3
4
5
6
7 import numpy as np
8 from ..helpers import settings
9 from ..helpers.units import *
10 from ..helpers.tools import InputError
11
12
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
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
40
41 malformed = "Malformed input in %s, line %s. Could not %s '%s'"
42 ans = []
43 j = 0
44 with open(name, 'r') as inF:
45 for line in inF.readlines():
46 j = j+1
47 line = line.translate(None, ' ')
48 line = line.translate(None, '\t')
49 if line.find('#') > -1:
50 line = line[0:line.find('#')]
51 if len(line) < 2:
52 continue
53 elif line[0:5] == 'order':
54 word = line[6:line.find('\n')]
55 try:
56 ans.append(('order', int(eval(word))))
57 except (SyntaxError, NameError):
58 raise InputError(malformed \
59 %(fileName, str(j), 'parse', word) + ".")
60 except TypeError:
61 raise InputError((malformed \
62 %(fileName, str(j), 'cast', word)) \
63 + "to int.")
64
65 elif line[0:9] == 'threshold':
66 word = line[10:line.find('\n')]
67 try:
68 ans.append(('threshold', float(eval(word))))
69 except (SyntaxError, NameError):
70 raise InputError(malformed \
71 %(fileName, str(j), 'parse', word) + ".")
72 except TypeError:
73 raise InputError((malformed \
74 %(fileName, str(j), 'cast', word)) \
75 + "to float.")
76
77 elif line[0:2] == 'bm':
78 ans.append(('bm',dicOf('bm',line[2:line.find('\n')],name,j)))
79 elif line[0:2] == 'mr':
80 ans.append(('mr',dicOf('mr',line[2:line.find('\n')],name,j)))
81 elif line[0:2] == 'th':
82 ans.append(('th',dicOf('th',line[2:line.find('\n')],name,j)))
83 elif line[0:2] == 'tk':
84 ans.append(('tk',dicOf('tk',line[2:line.find('\n')],name,j)))
85 elif line[0:2] == 'bd':
86 ans.append(('bd',dicOf('bd',line[2:line.find('\n')],name,j)))
87 elif line[0:2] == 'gh':
88 ans.append(('gh',dicOf('gh',line[2:line.find('\n')],name,j)))
89 elif line[0:2] == 'bo':
90 ans.append(('bo',dicOf('bo',line[2:line.find('\n')],name,j)))
91 else:
92 ans.append(('LName', line[0:line.find('\n')]))
93
94 return ans
95
96 -def dicOf(st, line, fileName, lineNumber):
97 '''Extract the initializer dictionary from a line.
98
99 st: object tag, 'bm', 'th', ... [string]
100 line: line of data in .tia format (supposed no spaces nor tabs nor comments)
101 and without the obect tag. [string]
102 fileName: name of file (used to write errors). [string]
103 lineNumber: number fo this line in the file (used to write errors). [int]
104
105 May raise an InputError
106 Returns a dictionary ready for construction.
107
108 '''
109
110 malformed = "Malformed input in %s, line %s, entry %s"
111 ans = {}
112
113
114 if line == '':
115 return ans
116
117 words = line.split(',')
118 i = 0
119 while i < len(words):
120 if '=' in words[i]:
121 break
122 try:
123 ans[settings.inOrder[st][i]] = eval(words[i])
124 except SyntaxError:
125 raise InputError((malformed %(fileName, lineNumber, str(i+1)))\
126 + ". Could not parse '%s'." %words[i])
127 except NameError:
128 raise InputError((malformed %(fileName, lineNumber, str(i+1)))\
129 + ". Did not recognize reference in '%s'." %words[i])
130 except IndexError:
131 raise InputError((malformed %(fileName, lineNumber, str(i+1)))\
132 + ". To many arguments given.")
133 i = i + 1
134
135 while i < len(words):
136 word = words[i]
137 if word.find('=') == -1:
138 raise InputError((malformed %(fileName, lineNumber, str(i+1)))\
139 + ". Found non explicit entry '%s' among explicit entries."\
140 %word)
141 var = word[0:word.find('=')]
142 if var not in settings.inOrder[st]:
143 raise InputError((malformed %(fileName, lineNumber, str(i+1)))\
144 + ". Unknown constructor parameter '%s'." %var)
145 val = word[word.find('=')+1:]
146 try:
147 ans[var] = eval(val)
148 except SyntaxError:
149 raise InputError((malformed %(fileName, lineNumber, str(i+1)))\
150 + ". Could not parse '%s'." %val)
151 except NameError:
152 raise InputError((malformed %(fileName, lineNumber, str(i+1)))\
153 + ". Did not recognize reference in '%s'." %val)
154 i = i+1
155
156 return ans
157