6 """Preset file reader"""
8 def __init__(self, file_name):
9 self.file_name = file_name
16 def __get_value(self, str):
30 with open(self.file_name) as f:
31 # Read presets line by line
35 cstr = " ".join(line.split())
36 values = cstr.strip().split(PRS_SEP)
39 self._errors.append("Line #%s: wrong number of values in the preset." % line_number)
40 elif values[0].strip() == "":
41 self._errors.append("Line #%s: empty name of the preset." % line_number)
42 elif presets.has_key(values[0]):
43 self._errors.append("Line #%s: preset name %s is already used." % (line_number, values[0]))
46 lihbor = self.__get_value(values[1])
47 liubor = self.__get_value(values[2])
48 livbor = self.__get_value(values[3])
49 litbor = self.__get_value(values[4])
51 presets[name] = (lihbor, liubor, livbor, litbor)
52 #print name, presets[name]
54 except IOError as err:
55 self._errors.append(err.strerror)
58 """Boundary condition object"""
59 class BoundaryCondition():
60 def __init__(self, lihbor, liubor, livbor, litbor, group):
68 return min( self.lihbor, self.liubor, self.livbor, self.litbor )
71 return max( self.lihbor, self.liubor, self.livbor, self.litbor )
73 def set_range(self, min_val, max_val):
74 self.lihbor = min(self.lihbor, max_val)
75 self.lihbor = max(self.lihbor, min_val)
76 self.liubor = min(self.liubor, max_val)
77 self.liubor = max(self.liubor, min_val)
78 self.livbor = min(self.livbor, max_val)
79 self.livbor = max(self.livbor, min_val)
80 self.litbor = min(self.litbor, max_val)
81 self.litbor = max(self.litbor, min_val)
83 """Boundary conditions file reader"""
84 class BoundaryConditionReader():
85 def __init__(self, file_name):
86 self.file_name = file_name
89 def __get_condition(self, str):
93 cstr = " ".join(str.split())
94 values = cstr.strip().split(BND_SEP)
97 lihbor = int(values[0])
98 liubor = int(values[1])
99 livbor = int(values[2])
100 litbor = int(values[3])
103 condition = BoundaryCondition(lihbor, liubor, livbor, litbor, group)
119 with open(self.file_name) as f:
120 # Read the number of conditions
122 first_line = f.readline().strip()
123 if not first_line.isdigit():
124 self._errors.append("First line is not a number.")
126 nb_conditions = int(first_line)
128 # Read conditions line by line
133 cnd = self.__get_condition(line)
136 if groups.count(cnd.group) == 0:
137 if cnd.get_min() < 0 or cnd.get_max() > 6:
139 self._errors.append("Line #%s: values out of range [0, 6] have been adjusted." % line_number)
141 conditions.append(cnd)
142 groups.append(cnd.group)
145 self._errors.append("Line #%s: group name '%s' is already used." % (line_number, cnd.group))
147 self._errors.append("Line #%s: wrong format." % line_number)
151 if cnd_count != nb_conditions:
152 self._errors.append("Number of conditions does not match (%s instead of %s)" % (cnd_count, nb_conditions))
153 except IOError as err:
154 self._errors.append(err.strerror)
157 """Boundary conditions file writer"""
158 class BoundaryConditionWriter():
159 def __init__(self, file_name):
160 self.file_name = file_name
163 def __get_string(self, condition):
164 lihbor = str(condition.lihbor)
165 liubor = str(condition.liubor)
166 livbor = str(condition.livbor)
167 litbor = str(condition.litbor)
168 values = (lihbor, liubor, livbor, litbor, condition.group)
169 line = BND_SEP.join(values)
177 def write(self, conditions):
181 with open(self.file_name, 'w') as f:
184 # The number of conditions
185 lines.append(str(len(conditions)) + '\n')
188 for cnd in conditions:
189 lines.append(self.__get_string(cnd) + '\n')
194 except IOError as err:
195 self._errors.append(err.strerror)