cement.core.config

Cement core config module.

class cement.core.config.ConfigHandler(**kw: Any)[source]

Bases: ConfigInterface, Handler

Config handler implementation.

class Meta[source]

Bases: Meta

abstractmethod _parse_file(file_path: str) bool[source]

Parse a configuration file at file_path and store it. This function must be provided by the handler implementation (that is sub-classing this).

Parameters:

file_path (str) – The file system path to the configuration file.

Returns:

True if file was read properly, False otherwise

Return type:

bool

parse_file(file_path: str) bool[source]

Ensure we are using the absolute/expanded path to file_path, and then call self._parse_file to parse config file settings from it, overwriting existing config settings.

Developers sub-classing from here should generally override _parse_file which handles just the parsing of the file and leaving this function to wrap any checks/logging/etc.

Parameters:

file_path (str) – The file system path to the configuration file.

Returns:

True if the given file_path was parsed, and False

otherwise.

Return type:

bool

class cement.core.config.ConfigInterface(**kw: Any)[source]

Bases: Interface

This class defines the Config Interface. Handlers that implement this interface must provide the methods and attributes defined below. In general, most implementations should sub-class from the provided ConfigHandler base class as a starting point.

class Meta[source]

Bases: Meta

Handler meta-data.

interface: str = 'config'

The string identifier of the interface.

abstractmethod add_section(section: str) None[source]

Add a new section if it doesn’t already exist.

Parameters:

section – The section label to create.

Returns:

None

abstractmethod get(section: str, key: str) Any[source]

Return a configuration value based on section.key. Must honor environment variables if they exist to override the config… for example config['myapp']['foo']['bar'] must be overridable by the environment variable MYAPP_FOO_BAR…. Note that MYAPP_ must prefix all vars, therefore config['redis']['foo'] would be overridable by MYAPP_REDIS_FOO … but config['myapp']['foo']['bar'] would not have a double prefix of MYAPP_MYAPP_FOO_BAR.

Parameters:
  • section (str) – The section of the configuration to pull key values from.

  • key (str) – The configuration key to get the value for.

Returns:

The value of the key in section.

Return type:

unknown

abstractmethod get_dict() Dict[str, Any][source]

Return a dict of the entire configuration.

Returns:

A dictionary of the entire config.

Return type:

dict

abstractmethod get_section_dict(section: str) Dict[str, Any][source]

Return a dict of configuration parameters for section.

Parameters:

section (str) – The config section to generate a dict from (using that sections’ keys).

Returns:

A dictionary of the config section.

Return type:

dict

abstractmethod get_sections() List[str][source]

Return a list of configuration sections.

Returns:

A list of config sections.

Return type:

list

abstractmethod has_section(section: str) bool[source]

Returns whether or not the section exists.

Parameters:

section (str) – The section to test for.

Returns:

True if the configuration section exists, False

otherwise.

Return type:

bool

abstractmethod keys(section: str) List[str][source]

Return a list of configuration keys from section.

Parameters:

section (list) – The config section to pull keys from.

Returns:

A list of keys in section.

Return type:

list

abstractmethod merge(dict_obj: dict, override: bool = True) None[source]

Merges a dict object into the configuration.

Parameters:
  • dict_obj (dict) – The dictionary to merge into the config

  • override (bool) – Whether to override existing values or not.

Returns:

None

abstractmethod parse_file(file_path: str) bool[source]

Parse config file settings from file_path. Returns True if the file existed, and was parsed successfully. Returns False otherwise.

Parameters:

file_path (str) – The path to the config file to parse.

Returns:

True if the file was parsed, False otherwise.

Return type:

bool

abstractmethod set(section: str, key: str, value: Any) None[source]

Set a configuration value based at section.key.

Parameters:
  • section (str) – The section of the configuration to pull key value from.

  • key (str) – The configuration key to set the value at.

  • value – The value to set.

Returns:

None