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