plastic.config — Configuration mapping

class plastic.config.Config

Bases: dict

Mapping object (subtype of dict) to store configurations.

update_from_file(filename, overwrite=False)

Updates configuration from Python file. For example, if there’s dev.cfg:

debug = False
database_uri = 'sqlite://'

so you can load it using update_from_file() method:

config.update_from_file('dev.cfg')

Like update_from_object() method, it also ignores variables that start with underscore.

Parameters:
  • filename (basestring) – the path of Python file to load
  • overwrite (bool) – keys that already exist are ignored by default. if True has given to this parameter, these are not overwritten
update_from_object(object_, overwrite=False)

Updates configuration from arbitrary object_.

@config.update_from_object
class default_config:
    debug = False
    database_uri = 'sqlite://'

It ignores attributes that start with underscore and keys that already exist until overwrite is True.

Parameters:
  • object – arbitrary object to update from, or import path of that if it’s a string e.g. 'myapp.configs:prod
  • overwrite (bool) – keys that already exist are ignored by default. if True has given to this parameter, these are not overwritten
update_unless_exists(mapping=(), **keywords)

Almost equivalent to update() except it ignores keys already exist.

>>> config = Config(a=1, b=2)
>>> config.update({'b': 1, 'c': 2})
>>> config
plastic.config.Config(a=1, b=2, c=3)
plastic.config.config_property

Maps application properties to configuration values.

plastic.config.get_typename(cls)

Finds the typename string of the given cls.

Parameters:cls (type) – the class object to find its typename
Returns:the typename
Return type:basestring
plastic.config.import_instance(expression, type_)

This function provides a minimal language to import a class from a package/module and make an instance of it. For example, the following code:

val = import_instance('abc.defg:ClassName(3.14, hello, world=False)')

is equivalent to the following normal Python code:

from abc.defg import ClassName

val = ClassName(3.14, 'hello', world=False)

As you can see its syntax is slightly different from normal Python. You can pass arguments to class’ constructor using its own syntax. You can pass limited types of values:

Booleans
You can pass True and False.
Numbers
It can take integers and floating numbers e.g. 123, 3.14.
Strings

You can 'single quote' and "double quote" both for string literals, and r'raw string literals' are also available. There are u'Unicode string literals' as well.

Moreover, if there re unquoted barewords these are also interpreted as strings.

None
You can pass None.

Project Versions

Previous topic

plastic.rendering — Content rendering

Next topic

plastic.resourcedir — Resources directory

This Page