Editconf: merge from v60
This commit is contained in:
parent
6c1a98dc4b
commit
97a84bc2b8
1 changed files with 16 additions and 8 deletions
|
@ -34,7 +34,8 @@ def edit_conf(filename,
|
|||
delimiter,
|
||||
comment_char,
|
||||
folded_lines=False,
|
||||
testing=False):
|
||||
testing=False,
|
||||
erase_setting=False):
|
||||
found = set()
|
||||
buf = ""
|
||||
input_lines = list(open(filename, "r+"))
|
||||
|
@ -50,7 +51,7 @@ def edit_conf(filename,
|
|||
|
||||
# See if this line is for any settings passed on the command line.
|
||||
for i in range(len(settings)):
|
||||
# Check that this line contain this setting from the command-line arguments.
|
||||
# Check whether this line contain this setting from the command-line arguments.
|
||||
name, val = settings[i].split("=", 1)
|
||||
m = re.match(
|
||||
"(\s*)" + "(" + re.escape(comment_char) + "\s*)?" +
|
||||
|
@ -59,8 +60,10 @@ def edit_conf(filename,
|
|||
continue
|
||||
indent, is_comment, existing_val = m.groups()
|
||||
|
||||
# If this is already the setting, do nothing.
|
||||
if is_comment is None and existing_val == val:
|
||||
# If this is already the setting, keep it in the file, except:
|
||||
# * If we've already seen it before, then remove this duplicate line.
|
||||
# * If val is empty and erase_setting is on, then comment it out.
|
||||
if is_comment is None and existing_val == val and not (not val and erase_setting):
|
||||
# It may be that we've already inserted this setting higher
|
||||
# in the file so check for that first.
|
||||
if i in found:
|
||||
|
@ -78,7 +81,8 @@ def edit_conf(filename,
|
|||
buf += line
|
||||
|
||||
# if this option oddly appears more than once, don't add the setting again
|
||||
if i in found:
|
||||
# Or if we're clearing it, don't add it
|
||||
if (i in found) or (not val and erase_setting):
|
||||
break
|
||||
|
||||
# add the new setting
|
||||
|
@ -92,9 +96,9 @@ def edit_conf(filename,
|
|||
# If did not match any setting names, pass this line through.
|
||||
buf += line
|
||||
|
||||
# Put any settings we didn't see at the end of the file.
|
||||
# Put any settings we didn't see at the end of the file, except those being erased.
|
||||
for i in range(len(settings)):
|
||||
if i not in found:
|
||||
if (i not in found) and not (not val and erase_setting):
|
||||
name, val = settings[i].split("=", 1)
|
||||
buf += name + delimiter + val + "\n"
|
||||
|
||||
|
@ -125,12 +129,16 @@ if __name__ == "__main__":
|
|||
comment_char = "#"
|
||||
folded_lines = False
|
||||
testing = False
|
||||
erase_setting = False
|
||||
while settings[0][0] == "-" and settings[0] != "--":
|
||||
opt = settings.pop(0)
|
||||
if opt == "-s":
|
||||
# Space is the delimiter
|
||||
delimiter = " "
|
||||
delimiter_re = r"\s+"
|
||||
elif opt == "-e":
|
||||
# Erase settings that have empty values.
|
||||
erase_setting = True
|
||||
elif opt == "-w":
|
||||
# Line folding is possible in this file.
|
||||
folded_lines = True
|
||||
|
@ -153,4 +161,4 @@ if __name__ == "__main__":
|
|||
sys.exit(1)
|
||||
|
||||
edit_conf(filename, settings, delimiter_re, delimiter, comment_char,
|
||||
folded_lines, testing)
|
||||
folded_lines, testing, erase_setting)
|
||||
|
|
Loading…
Reference in a new issue