0

How can I update config file with libconfig ? I want to update without removing other content of file

https://pypi.python.org/pypi/libconf

for example

RTL_test: {
  My_model : {
     tests = ["test1","test2","test3","test4"];
     ignore = ["test2"];
};
};

cfg['RTL_test']['My_model']['ignore']='' 

1 Answers1

1

With libconf.dump(cfg, f):

import libconf

# read
with open('example.cfg') as f:
  config = libconf.load(f)

config['RTL_test']['My_model']['ignore'] = 'updated'

# write
with open('example.cfg', 'w') as f:
  libconf.dump(config, f)      

Other contents like comments get lost by design and it's not possible to preserve them without modifying the source of the libconf package. You may want to look for another package or solutions like writing your own serializer/deserializer.

Yannic Hamann
  • 4,655
  • 32
  • 50
  • But the example.cfg has other keys. when I do write the other keys are removed. I also want to save comments @Yannic Hamann –  Mar 02 '18 at 09:31
  • Please clarify and update your question. The only thing you are asking right now is how to update a config file. That's exactly what my code is doing. – Yannic Hamann Mar 02 '18 at 09:34
  • Why ? I want to update without remove other content of file. @Yannic Hamann –  Mar 02 '18 at 09:37
  • The answer is: It is not possible without modifying the source of ``libconf`` the information is getting lost by design. – Yannic Hamann Mar 02 '18 at 09:45
  • Only for a specific case, e.g. a static key or highly dynamic? If the latter would definitely be beyond the scope of a SO question. Anyways, asking about ``regex`` is a separate question since you are asking explicitly how to achieve that with ``libconf``. – Yannic Hamann Mar 02 '18 at 09:52