configupdater icon indicating copy to clipboard operation
configupdater copied to clipboard

How to wrap values in quotes?

Open chefranov opened this issue 1 year ago • 3 comments

I need to make config file like this:

[common]
skipSaveTrainName = "true"
vendor = "Delphi"
skipCheckSignatureAndVariant = "true"
region = "Europe"
region2 = "RoW"
region3 = "USA"

As you can see values in quotes. When I create it with ConfigUpdater I get:

[common]
skipSaveTrainName = true
vendor = Delphi
skipCheckSignatureAndVariant = true
region = Europe
region2 = RoW
region3 = USA

How can I add quotes to values?

chefranov avatar Jan 16 '24 16:01 chefranov

The .ini/.cfg syntax supported by ConfigUpdater is the same as the one supported by Python's ConfigParser, and does not use any string delimiters in the values.

This means that if you want to use ConfigUpdater, you need to manually add the " characters yourself.

Alternatively, if the file you are trying to produce is a valid TOML file, you can have a look at an alternative library called `tomlkit

Note that the TOML syntax is different from Python's ConfigParser syntax, and requires string delimiters.

abravalheri avatar Jan 17 '24 01:01 abravalheri

@abravalheri In ConfigParser I used like this:

                with open(file_path, 'r+') as configfile:
                    for section in config.sections():
                        configfile.write(f"[{section}]\n")
                        for key, value in config.items(section):
                            configfile.write(f'{key} = "{value}"\n')
                        configfile.write('\n')

but when it tried it with ConfigUpdater I get final file without comments and strings like this:

[common]
skipsavetrainname = "skipSaveTrainName = true
"
vendor = "vendor = Delphi
"
skipchecksignatureandvariant = "skipCheckSignatureAndVariant = true
"
region = "region = Europe
"
region2 = "region2 = RoW
"
region3 = "region3 = USA

chefranov avatar Jan 17 '24 09:01 chefranov

Ah, I see...

That happens because ConfigUpdater.items(section) iterates over (str, Option) tuples, instead of (str, str). To obtain a string value from Option, you can use Option.value.

This will probably skip lines starting with comments, though...

abravalheri avatar Jan 17 '24 10:01 abravalheri