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 except IOError as err:
53 self._errors.append(err.strerror)
57 """Boundary condition object"""
58 class BoundaryCondition():
59 def __init__(self, lihbor, liubor, livbor, litbor, group):
66 """Boundary conditions file reader"""
67 class BoundaryConditionReader():
68 def __init__(self, file_name):
69 self.file_name = file_name
72 def __get_condition(self, str):
76 cstr = " ".join(str.split())
77 values = cstr.strip().split(BND_SEP)
80 lihbor = int(values[0])
81 liubor = int(values[1])
82 livbor = int(values[2])
83 litbor = int(values[3])
86 condition = BoundaryCondition(lihbor, liubor, livbor, litbor, group)
102 with open(self.file_name) as f:
103 # Read the number of conditions
105 first_line = f.readline().strip()
106 if not first_line.isdigit():
107 self._errors.append("First line is not a number.")
109 nb_conditions = int(first_line)
111 # Read conditions line by line
116 cnd = self.__get_condition(line)
119 if groups.count(cnd.group) == 0:
120 conditions.append(cnd)
121 groups.append(cnd.group)
124 self._errors.append("Line #%s: group name '%s' is already used." % (line_number, cnd.group))
126 self._errors.append("Line #%s: wrong format." % line_number)
130 if cnd_count != nb_conditions:
131 self._errors.append("Number of conditions does not match (%s instead of %s)" % (cnd_count, nb_conditions))
132 except IOError as err:
133 self._errors.append(err.strerror)
137 """Boundary conditions file writer"""
138 class BoundaryConditionWriter():
139 def __init__(self, file_name):
140 self.file_name = file_name
143 def __get_string(self, condition):
144 lihbor = str(condition.lihbor)
145 liubor = str(condition.liubor)
146 livbor = str(condition.livbor)
147 litbor = str(condition.litbor)
148 values = (lihbor, liubor, livbor, litbor, condition.group)
149 line = BND_SEP.join(values)
157 def write(self, conditions):
161 with open(self.file_name, 'w') as f:
164 # The number of conditions
165 lines.append(str(len(conditions)) + '\n')
168 for cnd in conditions:
169 lines.append(self.__get_string(cnd) + '\n')
174 except IOError as err:
175 self._errors.append(err.strerror)