Skip to content

Commit cf79f6e

Browse files
authored
Merge pull request #24 from vladimir-poleh/fix_hal_section_update
Fix [HAL] section update.
2 parents 62f8387 + 533835c commit cf79f6e

File tree

1 file changed

+39
-57
lines changed

1 file changed

+39
-57
lines changed

mesact/src/libmesact/updateini.py

+39-57
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@
44
from PyQt5.QtWidgets import QSpinBox
55

66
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+
717
def __init__(self):
818
super().__init__()
9-
self.content = ''
19+
self.content = []
1020
self.sections = {}
1121

1222
def update(self, parent, iniFile):
@@ -271,61 +281,31 @@ def update(self, parent, iniFile):
271281
for item in traj:
272282
self.update_key(item[0], item[1], item[2])
273283

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]')
326293

294+
# add in reversed order to avoid indexes calculation
327295
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()
329309

330310
# [HALUI]
331311
if parent.haluiCB.isChecked() and '[HALUI]' not in self.sections:
@@ -769,9 +749,11 @@ def insert_section(self, index, section):
769749
self.get_sections() # update section start/end
770750

771751
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)
774753
del self.content[start:end]
775754
self.get_sections() # update section start/end
776755

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

Comments
 (0)