0

I have a simple config file like below and i wrote a quote to update the file.

 cat /tmp/cfg 
global key = value
section key = val
[section]
section key = val

[section2]
section key =10

My code works fine , but removes any comments i have in the file example if file was

 cat /tmp/cfg2 
global key = value
section key = val
[section]
section key = val

[section2]
section key =10
#section key =20

The below code is run using command

python  newmodconfig  --section all --config /tmp/cfg --key "section key" --value newval

My attempt is below https://pastebin.com/tbL6iNb2

pythonRcpp
  • 2,042
  • 6
  • 26
  • 48
  • What should happen if the original config file has the lines in a different order? A comment might later be at a completely misleading position – lucidbrot Jul 13 '18 at 15:27
  • I am not sure if i get you correctly ? We are only changing /adding key values , its ok for the comment to go anywhere within the same section – pythonRcpp Jul 13 '18 at 15:35
  • I have not really looked at your code, and maybe it is not an issue for your use case. What I mean is that it technically could happen that the original config file was manually modified by somebody. If you know that the config file will always be ordered exactly the same way, the whole thing is probably way easier to do. Because then you can just keep track of the line numbers where the comments were. – lucidbrot Jul 13 '18 at 15:38
  • a way could be to add a prefix to each comment before reading it, so now everything becomes a key value for parser, but thats an overkill it seems – pythonRcpp Jul 13 '18 at 15:40

0 Answers0