Schema

Configuration schemas can be used to group configuration values together. These schemas can be instantiated at import time, and values can be retrieved from them by accessing the attributes of the schema object. Each field on the schema turns into an accessor for a configuration value. These accessors will cache the return value of the validator that they use, so expensive operations are not repeated.

Example

from staticconf import schema

class MyClassSchema(object):
    __metaclass__ = schema.SchemaMeta

    # Namespace to retrieve configuration values from
    namespace = 'my_package'

    # (optional) Config path to prepend to all config keys in this schema
    config_path = 'my_class.foo'

    # Attributes accept the same values as a getter (default, help, etc)
    ratio = schema.float(default=0.2) # configured at my_class.foo.ratio

    # You can optionally specify a different name from the attribute name
    max_threshold = schema.int(config_key='max') # configued at my_class.foo.max

You can also create your schema objects by subclassing Schema

from staticconf import schema

class MyClassSchema(schema.Schema):
    ...

Access the values from a schema by instantiating the schema class.

config = MyClassSchema()
print config.ratio

Arguments

Schema accessors accept the following kwargs:

config_key
string configuration key
default
if no default is given, the key must be present in the configuration. Raises staticconf.errors.ConfigurationError on missing key.
help
a help string describing the purpose of the config value. See staticconf.config.view_help().

Custom schema types

You can also create your own custom types using build_value_type().

from staticconf import schema

def validator(value):
    try:
        return do_some_casting(value)
    except Exception:
        raise ConfigurationError("%s can't be validated as a foo" % value)

foo_type = schema.build_value_type(validator)


class MySchema(object):
    __metaclass__ = schema.SchemaMeta

    something = foo_type(default=...)
class staticconf.schema.SchemaMeta[source]

Metaclass to construct config schema object.

classmethod build_attributes(attributes, namespace)[source]

Return an attributes dictionary with ValueTokens replaced by a property which returns the config value.

class staticconf.schema.Schema[source]

Base class for configuration schemas, uses SchemaMeta.

staticconf.schema.build_value_type(validator)[source]

A factory function to create a new schema type.

Parameters:validator – a function which accepts one argument and returns that value as the correct type.