Skip to content
Snippets Groups Projects
Commit 383fd84c authored by Susanne Fuchs's avatar Susanne Fuchs
Browse files

changed comment handling in compconfig and diffconfig

parent c82cb159
No related branches found
No related tags found
No related merge requests found
......@@ -17,20 +17,19 @@ from configobj import Section
from expconfig import ConfigObj
# Extract overlap of two config objects to new config object
def findConfigShared(c1, c2, cnew, depth = 0):
if depth == 0:
main = cnew
else:
main = cnew.main
def removeNotShared(c1, c2):
for k in c1:
if k in c2:
if k not in c2:
del c1[k]
else:
if type(c1[k]) is Section:
cnew[k] = Section(cnew, depth+1, main)
findConfigShared(c1[k],c2[k], cnew[k], depth+1)
else:
if c1[k] == c2[k]:
cnew[k] = c1[k]
#??? TODO: save differences separately?
removeNotShared(c1[k],c2[k])
if not c1[k].keys():
del c1[k]
else:
for comm in c1.comments[k]:
if comm not in c2.comments[k]:
c1.comments[k].remove(comm)
#
......@@ -40,7 +39,6 @@ def findConfigShared(c1, c2, cnew, depth = 0):
# Check command line
command_line = argparse.ArgumentParser(description=__doc__.split('\n', 1)[0])
# TODO: print differences option, ...
command_line.add_argument('--indent-string', default=' ', help='set indent string [%(default)s]')
command_line.add_argument('--inline-comments' , '-c', action='store_true',
help='compact white space before inline comments'
......@@ -64,21 +62,20 @@ try:
config_data = list()
for i, config_file in enumerate(config_files):
config_data.append(ConfigObj(config_file,
interpolation=False, file_error=True))
interpolation=False, file_error=True, write_empty_values=True))
except IOError as error:
die(error.message)
# Compare configs
for i in range(len(config_files)-1):
shared_config_data = ConfigObj(indent_type=args.indent_string)
findConfigShared(config_data[i],config_data[i+1],shared_config_data)
config_data[i+1] = shared_config_data
for i in range(1,len(config_files)):
removeNotShared(config_data[0],config_data[i])
# Ready to roll out
lines = io.BytesIO()
shared_config_data.write(lines)
config_data[0].write(lines)
lines.seek(0)
for line in io.TextIOWrapper(lines):
......
......@@ -17,44 +17,16 @@ from configobj import Section
from expconfig import ConfigObj
# creates config object cnew which only contains elements from c1 missing in c2
def createDiffConfig(c1,c2,cnew):
createConfigDiffWithAllSections(c1, c2, cnew)
cleanConfig(cnew)
# recursively saves differences between two config objects c1 and c2 in cnew,
# keeps all subsections of c1 in cnew
def createConfigDiffWithAllSections(c1, c2, cnew):
depth = c1.depth
if depth == 0:
main = cnew
else:
main = cnew.main
# recursively removes differences between two config objects c1 and c2
def subConfig(c1, c2):
for k in c1:
if type(c1[k]) is Section:
cnew[k] = Section(cnew, depth+1, main)
if k not in c2:
cnew[k] = c1[k]
else:
createConfigDiffWithAllSections(c1[k],c2[k], cnew[k])
else:
if (k not in c2) or c2[k]!=c1[k]:
cnew[k] = c1[k]
# recursively cleans empty subsections of config object, returns True if config is empty
def cleanConfig(config):
empty = True
for k in config.keys():
if type(config[k]) is not Section:
empty = False
if (type(c1[k]) is Section) and (k in c2):
subConfig(c1[k],c2[k])
if not c1[k].keys():
del c1[k]
else:
if cleanConfig(config[k]):
del config[k]
else:
empty = False
return empty
if (k in c2) and c2[k]==c1[k]:
del c1[k]
#
# Main routine
......@@ -83,21 +55,18 @@ try:
config_file1 = args.config1
config_file2 = args.config2
config_data1 = ConfigObj(config_file1,
interpolation=False, file_error=True)
interpolation=False, file_error=True,write_empty_values=True)
config_data2 = ConfigObj(config_file2,
interpolation=False, file_error=True)
except IOError as error:
die(error.message)
# Create config with elements which are only in c1:
shared_config_data = ConfigObj(indent_type=args.indent_string)
createDiffConfig(config_data1,config_data2,shared_config_data)
# Removes elements which are also in c2 from c1:
subConfig(config_data1,config_data2)
# Ready to roll out
lines = io.BytesIO()
shared_config_data.write(lines)
config_data1.write(lines)
lines.seek(0)
for line in io.TextIOWrapper(lines):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment