Configuration class

This page documents the Config class. For learning how to configure Flexx, see configuring flexx.

class flexx.util.config.Config(name, *sources, **options)

Class for configuration objects.

A Config object has a set of options, which can be str, int, float, bool, or a tuple of any of the above. Options can be set from different sources:

  • Each option has a default value.
  • From .cfg or .ini files.
  • From strings in ini format.
  • From environment variables.
  • From command-line arguments.
  • By setting the config option directly.
Parameters:
  • name (str) – the name by which to identify this config. This name is used as a prefix in environment variables and command line arguments, and optionally as a section header in .cfg files.
  • *sources – Sources to initialize the option values with. These can be strings in ini format, or .ini or .cfg filenames. If a file is given that does not exist, it is simply ignored. Special prefixes ~/ and ~appdata/ are expanded to the home dir and appdata dir.
  • **options – The options specification: each option consists of a 3-element tuple (default, type, docstring).

Example

config = Config('myconfig', '~appdata/.myconfig.cfg',
                foo=(False, bool, 'Whether to foo'),
                bar=(0.0, float, 'The size of the bar'),
                spam=('1,2,3', [int], 'A tuple of ints'))

With this, options can be set:

  • With an entry foo = 3 in “~appdata/.myconfig.cfg”.
  • With a string "foo = 3" passed at initialization.
  • With an environment variable named MYCONFIG_FOO.
  • With a command line argument --myconfig-foo=3.
  • By doing config.foo = 3, or config['foo'] = 3 in Python.

Notes

  • Option names are case insensitive, except for attribute access and environment variables (the latter must be all uppercase).
  • All values can be set as a Python object or a string; they are automatically converted to the correct type.
  • Each instance gets a docstring that lists all options, so it can easily be used in e.g. Sphynx docs.
load_from_file(filename)

Load config options from a file, as if it was given as a source during initialization. This means that options set via argv, environ or directly will not be influenced.

load_from_string(text, filename='<string>')

Load config options from a string, as if it was given as a source during initialization. This means that options set via argv, environ or directly will not be influenced.