Python Paste


inischema

Probably will be deprecated (13 Aug 2006): I'm not sure if this is worth keeping around.

.ini file schemas

You can define a schema for your .ini configuration files, which defines the types and defaults for the values. You can also define a catchall attribute.

TODO

Currently, this does not deal with sections at all, and sections are not allowed. That wil take some more thought, as the schemas will probably be per-section (though one section may inherit from another, or defaults may be inherited, etc, which should be allowed for).

Documentation isn't kept track of, nor is it generated. It should possible to indicate with opt(help=...), and as an option to INISchema.ini_repr() you should be able to put in documentation in comments.

Comments aren't kept track of.

Key order isn't kept track of.

Minimal .ini files should be possible to generate -- only generating keys when the default doesn't match the actual value.

There should be a way to check that the entire config is loaded, and there are no missing values (options which weren't set and have no default).

Usage

class VHostSchema(INISchema):

    server_name = opt()
    port = optint(default=80)
    # optlist means this can show up multiple times:
    server_alias = optlist(default=[])
    document_root = opt()

vhost = VHostSchema()
vhost.load('config.ini')
connect(vhost.server_name, vhost.port)
# etc.

Any schema can contain an optdefault() object, which will pick up any keys that aren't specified otherwise (if not present, extra keys are an error). Then you'll get a dictionary of lists, for all the extra config values. (Use optdefault(allow_multiple=False) if you want a dictionary of strings).

If you expect multiple values, use optlist(subtype=optsomething()) for a key. The values will be collected in a list; if you don't indicate subtype then opt() is used. Other types are fairly easy to make through subclassing.

You can generate config files by using schema.ini_repr() which will return a string version of the ini file.

Implementation

This makes heavy use of descriptors. If you are not familiar with descriptors, see http://users.rcn.com/python/download/Descriptor.htm

It also makes light use of metaclasses.


Classes

C INIMeta(...) ...

This class contains 13 members.

C ParseValueError(...) ...

This class contains 2 members.

C NoDefault(...) ...

C INISchema(...) ...

This class contains 9 members.

C opt(...) ...

This class contains 12 members.

C optstring(...) ...

This class contains 12 members.

C optint(...) ...

This class contains 17 members.

C optfloat(...) ...

This class contains 17 members.

C optbool(...) ...

This class contains 14 members.

C optlist(...) ...

This class contains 13 members.

C optdefault(...) ...

This class contains 14 members.

C optconverter(...) ...

This class contains 15 members.

C SchemaINIParser(...) ...

This class contains 3 members.

See the source for more information.