Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to wrap values in quotes? #132

Open
chefranov opened this issue Jan 16, 2024 · 3 comments
Open

How to wrap values in quotes? #132

chefranov opened this issue Jan 16, 2024 · 3 comments

Comments

@chefranov
Copy link

chefranov commented Jan 16, 2024

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?

@abravalheri
Copy link
Contributor

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.

@chefranov
Copy link
Author

@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

@abravalheri
Copy link
Contributor

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants