|
4 | 4 | from PyQt5.QtWidgets import QSpinBox
|
5 | 5 |
|
6 | 6 | class updateini:
|
| 7 | + generated_hal_items = [ |
| 8 | + 'main.hal', |
| 9 | + 'io.hal', |
| 10 | + 'sserial.hal', |
| 11 | + 'custom.hal', |
| 12 | + 'postgui.hal', |
| 13 | + 'shutdown.hal', |
| 14 | + 'HALUI' |
| 15 | + ] |
| 16 | + |
7 | 17 | def __init__(self):
|
8 | 18 | super().__init__()
|
9 |
| - self.content = '' |
| 19 | + self.content = [] |
10 | 20 | self.sections = {}
|
11 | 21 |
|
12 | 22 | def update(self, parent, iniFile):
|
@@ -271,61 +281,31 @@ def update(self, parent, iniFile):
|
271 | 281 | for item in traj:
|
272 | 282 | self.update_key(item[0], item[1], item[2])
|
273 | 283 |
|
274 |
| - # [HAL] missing all the hal files |
275 |
| - custom = False |
276 |
| - postgui = False |
277 |
| - shutdown = False |
278 |
| - start = self.sections['[HAL]'][0] |
279 |
| - end = self.sections['[HAL]'][1] |
280 |
| - for i in range(start, end): # find out if custom.hal is there |
281 |
| - if 'custom.hal' in self.content[i]: |
282 |
| - custom = True |
283 |
| - if 'postgui.hal' in self.content[i]: |
284 |
| - postgui = True |
285 |
| - if 'shutdown.hal' in self.content[i]: |
286 |
| - shutdown = True |
287 |
| - if parent.customhalCB.isChecked(): |
288 |
| - start = self.sections['[HAL]'][0] |
289 |
| - end = self.sections['[HAL]'][1] |
290 |
| - if not custom: # insert it after main.hal |
291 |
| - for i in range(start, end): # find main.hal |
292 |
| - if 'main.hal' in self.content[i]: |
293 |
| - self.content.insert(i + 1, 'HALFILE = custom.hal\n') |
294 |
| - custom = True |
295 |
| - self.get_sections() |
296 |
| - if parent.postguiCB.isChecked(): |
297 |
| - start = self.sections['[HAL]'][0] |
298 |
| - end = self.sections['[HAL]'][1] |
299 |
| - if custom and not postgui: # insert after custom |
300 |
| - for i in range(start, end): # find custom.hal |
301 |
| - if 'custom.hal' in self.content[i]: |
302 |
| - self.content.insert(i + 1, 'HALFILE = postgui.hal\n') |
303 |
| - postgui = True |
304 |
| - self.get_sections() |
305 |
| - else: # insert after main |
306 |
| - for i in range(start, end): # find main.hal |
307 |
| - if 'main.hal' in self.content[i]: |
308 |
| - self.content.insert(i + 1, 'HALFILE = postgui.hal\n') |
309 |
| - postgui = True |
310 |
| - self.get_sections() |
311 |
| - if parent.shutdownCB.isChecked(): |
312 |
| - start = self.sections['[HAL]'][0] |
313 |
| - end = self.sections['[HAL]'][1] |
314 |
| - if postgui and not shutdown: # insert after postgui |
315 |
| - for i in range(start, end): # find postgui.hal |
316 |
| - if 'postgui.hal' in self.content[i]: |
317 |
| - self.content.insert(i + 1, 'HALFILE = shutdown.hal\n') |
318 |
| - shutdown = True |
319 |
| - self.get_sections() |
320 |
| - else: # insert after main |
321 |
| - for i in range(start, end): # find main.hal |
322 |
| - if 'main.hal' in self.content[i]: |
323 |
| - self.content.insert(i + 1, 'HALFILE = shutdown.hal\n') |
324 |
| - shutdown = True |
325 |
| - self.get_sections() |
| 284 | + # Update [HAL] section using same rules as for INI file building |
| 285 | + start, end = self.get_section_bounds('[HAL]') |
| 286 | + # remove all generated files |
| 287 | + for i in reversed(range(start, end)): |
| 288 | + if any(hal_file in self.content[i] for hal_file in self.generated_hal_items): |
| 289 | + del self.content[i] |
| 290 | + |
| 291 | + self.get_sections() |
| 292 | + start, end = self.get_section_bounds('[HAL]') |
326 | 293 |
|
| 294 | + # add in reversed order to avoid indexes calculation |
327 | 295 | if parent.haluiCB.isChecked():
|
328 |
| - self.update_key('HAL', 'HALUI', 'halui') |
| 296 | + self.content.insert(end, 'HALUI = halui\n') |
| 297 | + if parent.shutdownCB.isChecked(): |
| 298 | + self.content.insert(end, 'SHUTDOWN = shutdown.hal\n') |
| 299 | + if parent.postguiCB.isChecked(): |
| 300 | + self.content.insert(end, 'POSTGUI_HALFILE = postgui.hal\n') |
| 301 | + if parent.customhalCB.isChecked(): |
| 302 | + self.content.insert(start + 1, 'HALFILE = custom.hal\n') |
| 303 | + if parent.ssCardCB.currentData(): |
| 304 | + self.content.insert(start + 1, 'HALFILE = sserial.hal\n') |
| 305 | + self.content.insert(start + 1, 'HALFILE = io.hal\n') |
| 306 | + self.content.insert(start + 1, 'HALFILE = main.hal\n') |
| 307 | + |
| 308 | + self.get_sections() |
329 | 309 |
|
330 | 310 | # [HALUI]
|
331 | 311 | if parent.haluiCB.isChecked() and '[HALUI]' not in self.sections:
|
@@ -769,9 +749,11 @@ def insert_section(self, index, section):
|
769 | 749 | self.get_sections() # update section start/end
|
770 | 750 |
|
771 | 751 | def delete_section(self, section):
|
772 |
| - start = self.sections[section][0] |
773 |
| - end = self.sections[section][1] |
| 752 | + start, end = self.get_section_bounds(section) |
774 | 753 | del self.content[start:end]
|
775 | 754 | self.get_sections() # update section start/end
|
776 | 755 |
|
777 |
| - |
| 756 | + def get_section_bounds(self, section): |
| 757 | + start = self.sections[section][0] |
| 758 | + end = self.sections[section][1] |
| 759 | + return start, end |
0 commit comments