xtd.core.config.manager module

class xtd.core.config.manager.Option(p_section, p_name, p_prop=None)[source]

Bases: object

Option object for ConfigManager

Available option properties:

config
Allow option to be read from configuration file, default True
cmdline
Allow option to be read from command line, default True
default
Internal default value for option, default None
valued
Expects a value for option. Default True if default value profived. For non-valued options, default value is False and reading them from command line will store a True value
description
Option description to display on usage message
checks
Array of functions to validate option value. You may provide a single function. Default []. See xtd.core.config.checkers for standard check functions.
longopt
Override long option name. Long options has be be unique. Default --<section>-<name>.
mandatory
Option is mandatory on command line, often used with non-valued options. Default False

Note

Provided check callback must respect the following signature :

def function(p_section, p_section, p_value)

They must return the input p_value (possible possibly trans-typed) and raise ConfigError if value is rejected

See xtd.core.config.checkers for standard check functions.

Parameters:
  • p_section (str) – option’s section name
  • p_name (str) – option’s name
  • p_props (dict) – option definition
Raises:

xtd.core.error.ConfigError – encountered unknown property

__init__(p_section, p_name, p_prop=None)[source]
validate(p_value)[source]
__dict__ = mappingproxy({'__dict__': <attribute '__dict__' of 'Option' objects>, '__weakref__': <attribute '__weakref__' of 'Option' objects>, '__init__': <function Option.__init__ at 0x7f0b923bbd90>, '_update': <function Option._update at 0x7f0b923d0048>, '__module__': 'xtd.core.config.manager', 'validate': <function Option.validate at 0x7f0b923d00d0>, '__doc__': " Option object for :py:class:`ConfigManager`\n\n Available option properties:\n\n config\n Allow option to be read from configuration file, default ``True``\n\n cmdline\n Allow option to be read from command line, default ``True``\n\n default\n Internal default value for option, default ``None``\n\n valued\n Expects a value for option. Default ``True`` if default value profived.\n For non-valued options, default value is ``False`` and reading them\n from command line will store a ``True`` value\n\n description\n Option description to display on usage message\n\n checks\n Array of functions to validate option value. You may provide a single\n function. Default ``[]``. See :py:mod:`xtd.core.config.checkers` for\n standard check functions.\n\n longopt\n Override long option name. Long options has be be unique. Default ``--<section>-<name>``.\n\n mandatory\n Option is mandatory on command line, often used with non-valued options. Default\n ``False``\n\n Note:\n\n Provided check callback must respect the following signature :\n\n .. code-block:: python\n\n def function(p_section, p_section, p_value)\n\n They must return the input ``p_value`` (possible possibly trans-typed)\n and raise :py:exc:`~xtd.core.error.ConfigError` if value is\n rejected\n\n See :py:mod:`xtd.core.config.checkers` for standard check functions.\n\n Args:\n p_section (str): option's section name\n p_name (str): option's name\n p_props (dict) : option definition\n\n\n Raises:\n xtd.core.error.ConfigError: encountered unknown property\n\n "})
__module__ = 'xtd.core.config.manager'
__weakref__

list of weak references to the object (if defined)

class xtd.core.config.manager.ConfigManager[source]

Bases: object

Unified command-line & file config option manager

The main user methods are :

Main documentation for option definition : Option

__metaclass__

xtd.core.mixin.Singleton

makes this object a singleton

__init__()[source]
register_section(p_section, p_title, p_options)[source]

Register a set of options to a given section

See Option for full documentation of option properties

Parameters:
  • p_section (str) – section tag
  • p_title (str) – the section title in the command-line usage
  • p_options (list of dict) – options definition
Returns:

self

Return type:

ConfigManager

Raises:

xtd.core.error.ConfigError – invalid option definition

register(p_section, p_name, p_props)[source]

Register an option in a specific section

See Option for full documentation of option properties

Parameters:
  • p_name (str) – option name
  • p_section (str) – section name
  • p_props (dict) – option properties
Returns:

self

Return type:

ConfigManager

sections()[source]

Get sections tags

Returns:array of str of all section names
Return type:(list)
section_exists(p_section)[source]

Indicates if specified section has been registered

Parameters:p_section (str) – section name
Returns:true is p_section is registered
Return type:bool
options(p_section)[source]

Get the list of all registered option names for specefic a section

Parameters:p_section (str) – section name
Raises:xtd.core.error.ConfigErrorp_section not registered
Returns:array of str of option names
Return type:list
option_exists(p_section, p_name)[source]

Indicates if specified option has been registered in section

Parameters:
  • p_section (str) – section name
  • p_option (str) – option name
Returns:

true is p_section is registered and contains p_option

Return type:

bool

get(p_section, p_name)[source]

Get option value

Parameters:
  • p_section (str) – section name
  • p_option (str) – option name
Raises:

xtd.core.error.ConfigValueError – section/option not found

Returns:

current option value

Return type:

(undefined)

set(p_section, p_name, p_value)[source]

set option value

Warning

This method stores the input value immediately without validating it against option’s checks.

Parameters:
  • p_section (str) – section name
  • p_option (str) – option name
Raises:

xtd.core.error.ConfigValueError – section/option not found

help(p_file=None)[source]

Display command line help message

Parameters:p_file (file) – output stream, defaults to sys.stdout
initialize()[source]

Initializes object

Usually called by Application object.

parse(p_argv=None)[source]

Parses command line and file options

Usually called by Application object.

Parameters:p_argv (list of str) – list of command line arguments
get_name()[source]

Get parsed application name sys.argv[0]

Returns:program’s sys.argv[0]
Return type:str
get_args()[source]

Get command line post-parse remaining options

Returns:unparsed command line options
Return type:list
set_usage(p_usage)[source]

Set command line usage message

See optparse.OptionParser

Parameters:p_usage (str) – usage string
option_cmdline_given(p_section, p_option)[source]
__dict__ = mappingproxy({'get': <function ConfigManager.get at 0x7f0b923d0598>, 'get_args': <function ConfigManager.get_args at 0x7f0b923d08c8>, '__init__': <function ConfigManager.__init__ at 0x7f0b923d0158>, '_get_option': <function ConfigManager._get_option at 0x7f0b923d09d8>, 'get_name': <function ConfigManager.get_name at 0x7f0b923d0840>, 'sections': <function ConfigManager.sections at 0x7f0b923d0378>, 'help': <function ConfigManager.help at 0x7f0b923d06a8>, 'option_cmdline_given': <function ConfigManager.option_cmdline_given at 0x7f0b923d0c80>, '__module__': 'xtd.core.config.manager', 'options': <function ConfigManager.options at 0x7f0b923d0488>, '__doc__': 'Unified command-line & file config option manager\n\n The main user methods are :\n\n * :py:meth:`register_section`\n * :py:meth:`get`\n * :py:meth:`set_usage`\n\n\n Main documentation for option definition : :py:class:`Option`\n\n Attributes:\n __metaclass__ (:py:class:`xtd.core.mixin.Singleton`) : makes this object a singleton\n ', '_load_data': <function ConfigManager._load_data at 0x7f0b923d0a60>, 'section_exists': <function ConfigManager.section_exists at 0x7f0b923d0400>, 'parse': <function ConfigManager.parse at 0x7f0b923d07b8>, '__weakref__': <attribute '__weakref__' of 'ConfigManager' objects>, '_file_parser_load': <function ConfigManager._file_parser_load at 0x7f0b923d0d08>, '_cmd_parser_create': <function ConfigManager._cmd_parser_create at 0x7f0b923d0b70>, 'register': <function ConfigManager.register at 0x7f0b923d02f0>, '_validate': <function ConfigManager._validate at 0x7f0b923d0d90>, '_cmd_attribute_name': <staticmethod object at 0x7f0b92346cc0>, 'set': <function ConfigManager.set at 0x7f0b923d0620>, 'register_section': <function ConfigManager.register_section at 0x7f0b923d0268>, 'option_exists': <function ConfigManager.option_exists at 0x7f0b923d0510>, 'initialize': <function ConfigManager.initialize at 0x7f0b923d0730>, 'set_usage': <function ConfigManager.set_usage at 0x7f0b923d0950>, '__dict__': <attribute '__dict__' of 'ConfigManager' objects>, '_cmd_parser_load': <function ConfigManager._cmd_parser_load at 0x7f0b923d0bf8>})
__module__ = 'xtd.core.config.manager'
__weakref__

list of weak references to the object (if defined)