|
From: Jeffrey B. <jef...@ea...> - 2009-01-12 16:03:53
|
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.
--
Jeffrey Barish
|
|
From: Michael F. <fuz...@vo...> - 2009-01-16 11:07:53
|
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
--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog
|
|
From: Jeffrey B. <jef...@ea...> - 2009-01-16 14:35:47
|
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
Not really. The problem occurs only on one platform (maemo) when I interrupt
the program (^c). I circumvented the problem by catching the SIGINT so that
I can write the configuration file. I do not understand why configobj
otherwise writes the right data but without the quotes nor why on another
platform it writes exactly the right data including the quotes. In fact, I
don't really understand why it writes anything as the program is exiting.
There could be an issue with my program, but since the program is exiting
anyway, I am satisfied with my remedy.
--
Jeffrey Barish
|
|
From: Michael F. <fuz...@vo...> - 2009-01-16 14:55:25
|
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
>>
>
> Not really. The problem occurs only on one platform (maemo) when I interrupt
> the program (^c). I circumvented the problem by catching the SIGINT so that
> I can write the configuration file. I do not understand why configobj
> otherwise writes the right data but without the quotes nor why on another
> platform it writes exactly the right data including the quotes. In fact, I
> don't really understand why it writes anything as the program is exiting.
> There could be an issue with my program, but since the program is exiting
> anyway, I am satisfied with my remedy.
>
>
Interesting. It shouldn't be possible for this to happen (of course) -
it possibly happens as a result of code execution during the exception
handling of the KeyboardInterrupt (?).
Unfortunately I don't have access to maemo to test this. :-) Which of
course is a great excuse to not do anything about it.
There is no exception handling code in the write method of ConfigObj and
it only writes to the file once it has built the text representation as
a string. In other words *I* can't see how this could happen I'm afraid. :-(
If you do find any cause in ConfigObj for this problem I'd be happy to
explore workarounds and alternatives.
Michael Foord
--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog
|
|
From: Jeffrey B. <jef...@ea...> - 2009-02-26 21:54:10
|
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()
--
Jeffrey Barish
|
|
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/
|