import configparser
import ast


def extract_config_from_cfg(cfg_path):
    """Extract input data from *.cfg file.

    Parameters
    ----------
    cfg_path : Path
        Config file to read from.

    Returns
    -------
    dict
        Config keywords: python objects pairs.
    """
    # Start parser engine and read cfg file.
    cfg = configparser.ConfigParser()
    cfg.read(cfg_path)

    # Gather all input variables and merge them in one dict.
    input_data = ({k.lower(): v for k, v in cfg.items(i)}
                  for i in cfg.sections())
    config_dict = {}
    for i in input_data:
        config_dict.update(i)

    # Try to convert variables to Python objects.
    output_data = {}
    for k, value in config_dict.items():
        try:
            output_data[k] = ast.literal_eval(value)
        except SyntaxError:
            output_data[k] = value
        except TypeError:
            output_data[k] = value
    return output_data