DiffSK/configobj

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

joeydumont opened this issue · 2 comments

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?

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:

  • leads to the surprising result of "I made sure there were extra quotes and now they're stripped"
  • seems to make it impossible to have a string fully enclosed by quotes (since it'll be stripped)

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.