|
From: Michael F. <fuz...@vo...> - 2009-02-27 17:29:21
|
Jeffrey Barish wrote:
> On Friday 16 January 2009 04:07:43 Michael Foord wrote:
>
>> Jeffrey Barish wrote:
>>
>>> On Monday 12 January 2009 08:16:57 Michael Foord wrote:
>>>
>>>> Jeffrey Barish wrote:
>>>>
>>>>> I write the repr of a tuple to my configuration file on exit. Usually,
>>>>> I find "<tuple>" in my configuration file afterward, but occasionally
>>>>> the quotation marks are missing. I have not been able to detect a
>>>>> pattern. Any suggestions?
>>>>>
>>>> Is it actually causing any problems?
>>>>
>>> Or worse: If the value in the configuration file is
>>>
>>> ('a', ['b', 'c'])
>>>
>>> then I get a ParseError (at line 8) when I try to read it. That error I
>>> cannot fix.
>>>
>>> BTW, I am using version 4.5.3.
>>>
>> Did you have any luck reproducing this?
>>
>> Michael
>>
>
> I finally figured out what was going on here, but I am still unclear about one
> point. In my configuration file, I have a line like
>
> atuple = "('a', ['b', 'c'])"
>
> I created a validator for the purpose of converting that string to a tuple.
> The problem arises when I call the write method because a tuple gets written
> back to the configuration file rather than a repr of a tuple. I suspect that
> configobj is working the way it is supposed to work, but what I wanted was to
> be handed a tuple yet still have the write method write the repr of the
> tuple. Currently, I have turned off the conversion in the validator and
> instead perform the conversion in my program. I am not happy that I must
> have eval's scattered throughout my code to perform the conversion previously
> performed centrally in the validator. I'm wondering whether there is a way
> to restore the conversion provided in the validator but still have the write
> method write the value in its original form.
>
> FWIW, here's a test program configtest.py:
>
> import configobj, validate, os
>
> CONFIGRC = os.path.join(os.getcwd(), 'configrc')
>
> class Configurator(configobj.ConfigObj):
> def __init__(self):
> configobj.ConfigObj.__init__(self, CONFIGRC,
> configspec=CONFIGRC+'.spec')
> val = validate.Validator({'check_tuple': self._tuple_validator})
> res = self.validate(val, preserve_errors=True)
>
> def _tuple_validator(self, val, default=None):
> return eval(val)
> #return val
>
> if __name__ == '__main__':
> config = Configurator()
> config.write()
>
> configrc:
>
> atuple = "('One', ['Two'])"
>
> configrc.spec:
>
> atuple = check_tuple()
>
>
You could try unrepr mode which should allow you to read and write
tuples without the need for validation functions - the syntax is
slightly different from normal ConfigObj files though as it uses Python
syntax for strings, lists, tuples and dicts.
If ConfigObj writes tuples out in a way that it can't read back in then
I can understand how that is frustrating - although ConfigObj is not
really intended to be *able* to do that, particularly tuples with nested
lists. You could easily use ConfigObj.walk to transform tuple values
back into *correct* strings before calling write.
Michael
--
http://www.ironpythoninaction.com/
|