Handling Configuration Files#

processor_tools provides some utilities for handling of configuration files of various formats.

Writing Configuration Files#

The write_config function provides the capability to write a configuration file by defining:

  • path - where the file extension defines the format of file that is written. Currently ".yaml" files are supported.

  • config_dict - a dictionary of configuration values to write to the file.

This can be achieved as follows:

In [1]: from processor_tools import write_config

In [2]: config_dict = {"val1": True, "val2": {"subval": "data"}}

In [3]: path = "config_file.yaml"

In [4]: write_config(path, config_dict)

This writes the file "config_file.yaml" with content:

val1: true
val2:
  subval: data

Reading configuration files#

The write_config function provides the capability to read configuration files. This is done as follows:

In [5]: from processor_tools import read_config

In [6]: config = read_config(path)

In [7]: print(config)
{'val1': True, 'val2': {'subval': 'data'}}

This supports file types:

  • default python - with file extension ".cfg" or ".config"

  • yaml - with file extension ".yaml" or ".yml"

Writing Configuration Directory#

The build_configdir function provides the capability to write a directory of configuration files by defining:

  • path - configuration directory path (created if doesn’t exist)

  • configs - definition of configuration files as a dictionary, with an entry per configuration file to write - where the key should be the filename to write and the value should define the file content, either as:
    • path of config file to copy to configuration directory

    • configuration values dictionary

For example:

configs = {
    "copied_config.yaml": "path/to/old_config.yaml",
    "new_config.yaml": {"entry1": "value1"}
}