|
From: Michael F. <fuz...@vo...> - 2009-10-14 22:52:45
|
Jeremy Gray wrote:
> Hi Michael and everyone,
>
> I'm new to using configobj, and like it a lot. thanks for your work on this!
>
> I have 3 hopefully simple questions that I could not figure out after
> reading the online manuals / help pages or past postings to the list.
>
> my goal: I want allow users to edit their application preferences, eg,
> key-bindings and other settings for the app. I'm storing the settings
> in text files with syntax as expected by configobj, and have a spec
> file for default values and for validation purposes. the idea is to
> let users edit their config files in a text window, validate their
> entries, and save. Mostly this works fabulously. the eventual goal is
> to do this through a GUI, but its text for now. I'm using python 2.5,
> and need to end up with code that works cross-platform (mac, win,
> linux).
>
> here's what I'm asking about: if a user enters a badly formed value
> (i.e., that fails validation against the spec file), I've been using
> the flatten_errors() function to process it further, like this
> mock-code (hopefully indentation will be preserved when posted...):
> <code>
> for (section_list, key, _) in configobj.flatten_errors(cfg,
> resultOfValidate):
> if key is not None:
> print "Bad value"
> # code here to replace the bad value with the default
> given in the spec file
> else:
> print "Missing section"
> # code here to handle missing sections
> </code>
>
> my questions:
> 1. how to restore a default value?
> I tried using restore_default(), but I kept getting key errors (and
> tried several things as keys).
This is very odd. There is a passing test for restore_default, but when
I call it myself it doesn't work.
I need to investigate, but at least I have a failing test.
restore_default and restore_defaults should just work...
> I also tried get_default_value(), but
> am confused by how to call it, and what to pass to it.
get_default_value takes the configspec check string. You could call it
like this:
>>> from configobj import ConfigObj
>>> from validate import Validator
>>> a = ['foo = fish']
>>> b = ['foo = integer(default=3)']
>>> c = ConfigObj(a, configspec=b)
>>> c
ConfigObj({'foo': 'fish'})
>>> v = Validator()
>>> c.validate(v)
False
>>> v.get_default_value(c.configspec['foo'])
3
It shouldn't be necessary though.
I have a version 4.7 in the works that I am part way through with
various improvements / bugfixes so I can make sure this issue is included.
> I wrote a few
> lines to extract the default value from the spec file as a string and
> then convert the type (which mostly works, I'm just having trouble
> getting default lists to work properly). it would be nice to use the
> existing functions. any ideas?
>
> 2. how are missing sections reported by a Validator()? I was thinking
> that they would be indicated by the key being None, based on the
> example(s) on the web, but my code never hits the "missing section"
> part. if a user deletes a section, the section name appears to be
> deleted in the config file, but there's no missing section flagged as
> a key == None in flatten_errors(). if the user then adds the [section]
> back into the text, I get a duplicate section name error. so I am
> confused -- what should I be looking for?
>
I'm not seeing that behavior, can you provide a minimal repro. Here is a
small example of using flatten_errors where a missing section is
represented with None for the key value:
>>> from configobj import ConfigObj, flatten_errors
>>> from validate import Validator
>>> a = ['baz = 3']
>>> b = ['baz = integer', '[foo]', 'bar=integer']
>>> c = ConfigObj(a, configspec=b)
>>> v = Validator()
>>> r = c.validate(v)
>>> r
{'foo': False, 'baz': True}
>>> flatten_errors(c, r)
[(['foo'], None, False)]
> 3. finally, does anyone know of an existing project I could look at
> (and borrow from) that uses wxPython for a GUI for letting users
> manipulate configobj data? I found a relevant post to this list by
> Sebastian Wiesner on Aug 12, 2008 that linked to examples using PyQt4
> (http://paste.pocoo.org/show/79974/ ) -- is there anything using
> wxPython?
>
>
I can't help with this one I'm afraid.
Michael
> thanks!
>
> --Jeremy
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog
|