ConfigParser module and alternatives (flad, flan)

Jim Meier fatjim at home.com
Mon Apr 12 11:59:05 EDT 1999


Mike Orr wrote:

> I'm not sure if I'll use ConfigParser for the next version.  I don't
> really need the sections (multiple files work just fine, using the filename
> for the section name), and I haven't found a use for the the %(other_key)s
> substitutions.  On the other hand, it doesn't handle multiple values (the
> same key repeated within a record), which I sometimes need.
>
> Attached is a wrapper class for ConfigParser which supports booleans
> and can also return an entire section as a dictionary (or all the sections
> as nested dictionaries).  I find these make it a little more convenient.
>

Why not simply use a file of python expressions? Like this:

{'section1': {
    'key1':[1,2,3,'value',['useful','nesting','eh?']],
    'key2':'anotherval'
    },
'section2': {
    'subsection':{
        'subkey':5
        }
    }
}

I beleive you can even use variable names in such files for application-specified
substitutions, like this:

(python app defines variable "rootdir")

{'pathssection': {
    'fontpath': rootdir+'/font/'
    }
}

This introduces some major security problems, and is a little difficult to edit,
but there is very little parsing needed to make it usable.  Does anyone know of a
way to limit the damage a user can do to such a file?

Another idea is to run your config file through a simple macro utility (or do the
macro conversion in python itself) to convert it into python code to eval.

Jim Meier





More information about the Python-list mailing list