Package logilab :: Package common :: Module configuration

[frames] | no frames]

# Module configuration

source code

    Classes to handle advanced configuration in simple to complex applications.

    Allows to load the configuration from a file or from command line
    options, to generate a sample configuration file or to display
    program's usage. Fills the gap between optik/optparse and ConfigParser
    by adding data types (which are also available as a standalone optik
    extension in the `optik_ext` module).

    Quick start: simplest usage
    ---------------------------

    .. python ::

      >>> import sys
      >>> from logilab.common.configuration import Configuration
      >>> options = [('dothis', {'type':'yn', 'default': True, 'metavar': '<y or n>'}),
      ...            ('value', {'type': 'string', 'metavar': '<string>'}),
      ...            ('multiple', {'type': 'csv', 'default': ('yop',),
      ...                          'metavar': '<comma separated values>',
      ...                          'help': 'you can also document the option'}),
      ...            ('number', {'type': 'int', 'default':2, 'metavar':'<int>'}),
      ...           ]
      >>> config = Configuration(options=options, name='My config')
      >>> print config['dothis']
      True
      >>> print config['value']
      None
      >>> print config['multiple']
      ('yop',)
      >>> print config['number']
      2
      >>> print config.help()
      Usage:  [options]

      Options:
        -h, --help            show this help message and exit
        --dothis=<y or n>
        --value=<string>
        --multiple=<comma separated values>
                              you can also document the option [current: none]
        --number=<int>

      >>> f = open('myconfig.ini', 'w')
      >>> f.write('''[MY CONFIG]
      ... number = 3
      ... dothis = no
      ... multiple = 1,2,3
      ... ''')
      >>> f.close()
      >>> config.load_file_configuration('myconfig.ini')
      >>> print config['dothis']
      False
      >>> print config['value']
      None
      >>> print config['multiple']
      ['1', '2', '3']
      >>> print config['number']
      3
      >>> sys.argv = ['mon prog', '--value', 'bacon', '--multiple', '4,5,6',
      ...             'nonoptionargument']
      >>> print config.load_command_line_configuration()
      ['nonoptionargument']
      >>> print config['value']
      bacon
      >>> config.generate_config()
      # class for simple configurations which don't need the
      # manager / providers model and prefer delegation to inheritance
      #
      # configuration values are accessible through a dict like interface
      #
      [MY CONFIG]

      dothis=no

      value=bacon

      # you can also document the option
      multiple=4,5,6

      number=3

      Note : starting with Python 2.7 ConfigParser is able to take into
      account the order of occurrences of the options into a file (by
      using an OrderedDict). If you have two options changing some common
      state, like a 'disable-all-stuff' and a 'enable-some-stuff-a', their
      order of appearance will be significant : the last specified in the
      file wins. For earlier version of python and logilab.common newer
      than 0.61 the behaviour is unspecified.

Classes
| OptionsManagerMixIn MixIn to handle a configuration from both a configuration file and...
| OptionsProviderMixIn Mixin to provide options to an OptionsManager
| ConfigurationMixIn basic mixin for simple configurations which don't need the...
| Configuration class for simple configurations which don't need the manager / providers model and prefer delegation to inheritance
| OptionsManager2ConfigurationAdapter Adapt an option manager to behave like a...