profig icon indicating copy to clipboard operation
profig copied to clipboard

the first sync() call erase my config value

Open RonaldinhoL opened this issue 3 years ago • 4 comments

RonaldinhoL avatar Sep 04 '21 03:09 RonaldinhoL

Hi @RonaldinhoL. Can you share the code you are using that caused the issue? I need to be able to reproduce the problem in order to fix it.

sync() will first read config values from the file that you provide. If any values have been set on the Config object, they will not be overwritten by the value from the file. sync() will then write the values from the Config object to the file.

dhagrow avatar Sep 04 '21 14:09 dhagrow

i init by None,dose it matter?

RonaldinhoL avatar Sep 05 '21 07:09 RonaldinhoL

my sence dose not need a default value,for example: username and password and token

RonaldinhoL avatar Sep 05 '21 07:09 RonaldinhoL

@RonaldinhoL Based on your comments, I understand that you are trying to do something like this:

import profig

c = profig.Config('test.cfg')
c.init('username', None)
c.sync()

And what happens is that the value from the config file is being overwritten. Is that correct?

If that is the case, yes this should be considered a bug, or at least unexpected behavior. What is happening is that profig is not able to determine the type of the value you are expecting to read. It is using type() on your default value, which in this case is NoneType. So, when it tries to read the value from the config file, it is converting it to None. That is then written back to the config file.

I will fix this, but I can't say when. In the meantime, everything should work if you set the type that you expect to read from the config file. In your case, something like this should work:

c.init('username', None, str)
# or
c.init('username', '')

Thanks for the report.

dhagrow avatar Sep 07 '21 20:09 dhagrow