mirror of https://github.com/CIRCL/AIL-framework
Keep original section order while updating
parent
e225090e07
commit
c79d4d65c8
|
@ -2,8 +2,9 @@
|
||||||
# -*-coding:UTF-8 -*
|
# -*-coding:UTF-8 -*
|
||||||
|
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
from ConfigParser import RawConfigParser
|
from ConfigParser import ConfigParser as cfgP
|
||||||
import os
|
import os
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -20,8 +21,8 @@ def main():
|
||||||
cfgSample = ConfigParser.ConfigParser()
|
cfgSample = ConfigParser.ConfigParser()
|
||||||
cfgSample.read(configfileSample)
|
cfgSample.read(configfileSample)
|
||||||
|
|
||||||
sections = RawConfigParser.sections(cfg)
|
sections = cfgP.sections(cfg)
|
||||||
sectionsSample = RawConfigParser.sections(cfgSample)
|
sectionsSample = cfgP.sections(cfgSample)
|
||||||
|
|
||||||
missingSection = []
|
missingSection = []
|
||||||
dicoMissingSection = {}
|
dicoMissingSection = {}
|
||||||
|
@ -31,10 +32,10 @@ def main():
|
||||||
for sec in sectionsSample:
|
for sec in sectionsSample:
|
||||||
if sec not in sections:
|
if sec not in sections:
|
||||||
missingSection += [sec]
|
missingSection += [sec]
|
||||||
dicoMissingSection[sec] = RawConfigParser.items(cfgSample, sec)
|
dicoMissingSection[sec] = cfgP.items(cfgSample, sec)
|
||||||
else:
|
else:
|
||||||
setSample = set(RawConfigParser.options(cfgSample, sec))
|
setSample = set(cfgP.options(cfgSample, sec))
|
||||||
setNormal = set(RawConfigParser.options(cfg, sec))
|
setNormal = set(cfgP.options(cfg, sec))
|
||||||
if setSample != setNormal:
|
if setSample != setNormal:
|
||||||
missing_items = list(setSample.difference(setNormal))
|
missing_items = list(setSample.difference(setNormal))
|
||||||
missingItem += [sec]
|
missingItem += [sec]
|
||||||
|
@ -57,23 +58,43 @@ def main():
|
||||||
for item in dicoMissingItem[section]:
|
for item in dicoMissingItem[section]:
|
||||||
print(" - "+item[0])
|
print(" - "+item[0])
|
||||||
print("+--------------------------------------------------------------------+")
|
print("+--------------------------------------------------------------------+")
|
||||||
|
|
||||||
resp = raw_input("Do you want to auto fix it? [y/n] ")
|
resp = raw_input("Do you want to auto fix it? [y/n] ")
|
||||||
|
|
||||||
if resp != 'y':
|
if resp != 'y':
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
for section in missingSection:
|
#Do not keep item order in section. New items appened
|
||||||
cfg.add_section(section)
|
|
||||||
for item, value in dicoMissingSection[section]:
|
|
||||||
cfg.set(section, item, value)
|
|
||||||
for section in missingItem:
|
for section in missingItem:
|
||||||
for item, value in dicoMissingItem[section]:
|
for item, value in dicoMissingItem[section]:
|
||||||
cfg.set(section, item, value)
|
cfg.set(section, item, value)
|
||||||
|
|
||||||
|
#Keep sections order while updating the config file
|
||||||
|
new_dico = add_items_to_correct_position(cfgSample._sections, cfg._sections, missingSection, dicoMissingSection)
|
||||||
|
cfg._sections = new_dico
|
||||||
|
|
||||||
with open(configfile, 'w') as f:
|
with open(configfile, 'w') as f:
|
||||||
cfg.write(f)
|
cfg.write(f)
|
||||||
|
|
||||||
|
|
||||||
|
''' Return a new dico with the section ordered as the old configuration with the updated one added '''
|
||||||
|
def add_items_to_correct_position(sample_dico, old_dico, missingSection, dicoMissingSection):
|
||||||
|
new_dico = OrderedDict()
|
||||||
|
|
||||||
|
positions = {}
|
||||||
|
for pos_i, sec in enumerate(sample_dico):
|
||||||
|
if sec in missingSection:
|
||||||
|
positions[pos_i] = sec
|
||||||
|
|
||||||
|
for pos_i, sec in enumerate(old_dico):
|
||||||
|
if pos_i in positions:
|
||||||
|
missSection = positions[pos_i]
|
||||||
|
new_dico[missSection] = sample_dico[missSection]
|
||||||
|
|
||||||
|
new_dico[sec] = old_dico[sec]
|
||||||
|
return new_dico
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue