DiffSK / configobj

Python 3+ compatible port of the configobj library
https://configobj.readthedocs.org
Other
321 stars 76 forks source link

Write quoted strings without having them wrapped in an additional layer of quotes #198

Closed joeydumont closed 1 year ago

joeydumont commented 4 years ago

I have an app that expects a value to be in the format:

pre_install_args = "each word is an argument"

I tried writing

config['pre_install_args'] = "\"each word is an argument\""

but this results in

pre_install_args = '"each word is an argument"'

after calling ConfigObj.write().

To fix the issue, I've modified ConfigObj._get_single_quote(self, value) to

    def _get_single_quote(self, value):
        if ("'" in value) and ('"' in value):
            raise ConfigObjError('Value "%s" cannot be safely quoted.' % value)
        elif re.match('".*"', value) is not None:
            quot = noquot
        elif re.match("'.*'", value) is not None:
            quot = noquot
        elif '"' in value:
            quot = squot
        else:
            quot = dquot
        return quot

Note the two new re.match branches in the conditional. Works for my needs, but does that make sense in general?

robdennis commented 1 year ago

I appreciate there was a MR for this, and apologies for asking a question about this after 2.5 years, but I want to understand your expected values.

I'm tracking this as "I want a specific string value for pre_install_args and it shouldn't have any quotes in the final value"

I'd have expected that to be accomplished by:

config['pre_install_args'] = "each word is an argument"

(note only one set of quotes)

Am I misunderstanding the use case?

I am slightly concerned that the updated code:

joeydumont commented 1 year ago

Re-reading this 2.5 years, I realize that my explanation wasn't that great. In any case, I've sinced refactored that code away from my application. Given your concerns, I think it's best to close both this issue and the associated pull request.