listeningpy.config

This module contains functions for reading and parsing configuration files.

 1"""
 2This module contains functions for reading and parsing configuration files.
 3"""
 4import configparser
 5import logging
 6from typing import Dict
 7
 8
 9
10def person_identifiers(path: str) -> Dict[str, str]:
11    """
12    Read a configuration file and extract person identifiers.
13
14    Parameters
15    ----------
16    path : str
17        The path to the configuration file.
18
19    Returns
20    -------
21    dict
22        A dictionary containing person identifiers.
23
24    Raises
25    ------
26    IOError
27        If something goes wrong while reading the config file.
28    """
29    person_dict = {}
30    try:
31        cfg = configparser.ConfigParser()
32        cfg.read(path)
33        logging.info('Config file read.')
34        keys = cfg['Identifiers'].keys()
35        if 'FirstName' in keys:
36            person_dict['first_name'] = cfg.get('Identifiers', 'FirstName')
37        if 'SecondName' in keys:
38            person_dict['second_name'] = cfg.get('Identifiers', 'SecondName')
39        if 'DateOfBirth' in keys:
40            person_dict['date_of_birth'] = cfg.get('Identifiers', 'DateOfBirth')
41        if 'Gender' in keys:
42            person_dict['date_of_birth_day'] = cfg.get('Identifiers', 'Gender')
43        if 'HearingImpaired' in keys:
44            person_dict['hear_impaired'] = cfg.get('Identifiers', 'HearingImpaired')
45    except:
46        IOError('Something went wrong while reading config file.')
47    return person_dict
48
49
50
51def test_settings(path: str) -> Dict[str, str]:
52    """
53    Read and parse the configuration file at the given path.
54
55    Parameters
56    ----------
57    path : str
58        The path to the configuration file.
59
60    Returns
61    -------
62    Dict[str, str]
63        A dictionary containing the settings parsed from the configuration file.
64
65    Raises
66    ------
67    IOError
68        If something goes wrong while reading the configuration file.
69
70    """
71    settings = {}
72    try:
73        cfg = configparser.ConfigParser()
74        cfg.optionxform = str
75        cfg.read(path)
76        logging.info('Config file read.')
77        settings = dict(cfg.items('Settings'))
78    except:
79        raise IOError('Something went wrong while reading config file.')
80    print(settings)
81    return settings
82
83if __name__ == '__main__':
84    test_settings('config.ini')
def person_identifiers(path: str) -> Dict[str, str]:
11def person_identifiers(path: str) -> Dict[str, str]:
12    """
13    Read a configuration file and extract person identifiers.
14
15    Parameters
16    ----------
17    path : str
18        The path to the configuration file.
19
20    Returns
21    -------
22    dict
23        A dictionary containing person identifiers.
24
25    Raises
26    ------
27    IOError
28        If something goes wrong while reading the config file.
29    """
30    person_dict = {}
31    try:
32        cfg = configparser.ConfigParser()
33        cfg.read(path)
34        logging.info('Config file read.')
35        keys = cfg['Identifiers'].keys()
36        if 'FirstName' in keys:
37            person_dict['first_name'] = cfg.get('Identifiers', 'FirstName')
38        if 'SecondName' in keys:
39            person_dict['second_name'] = cfg.get('Identifiers', 'SecondName')
40        if 'DateOfBirth' in keys:
41            person_dict['date_of_birth'] = cfg.get('Identifiers', 'DateOfBirth')
42        if 'Gender' in keys:
43            person_dict['date_of_birth_day'] = cfg.get('Identifiers', 'Gender')
44        if 'HearingImpaired' in keys:
45            person_dict['hear_impaired'] = cfg.get('Identifiers', 'HearingImpaired')
46    except:
47        IOError('Something went wrong while reading config file.')
48    return person_dict

Read a configuration file and extract person identifiers.

Parameters
  • path (str): The path to the configuration file.
Returns
  • dict: A dictionary containing person identifiers.
Raises
  • IOError: If something goes wrong while reading the config file.
def test_settings(path: str) -> Dict[str, str]:
52def test_settings(path: str) -> Dict[str, str]:
53    """
54    Read and parse the configuration file at the given path.
55
56    Parameters
57    ----------
58    path : str
59        The path to the configuration file.
60
61    Returns
62    -------
63    Dict[str, str]
64        A dictionary containing the settings parsed from the configuration file.
65
66    Raises
67    ------
68    IOError
69        If something goes wrong while reading the configuration file.
70
71    """
72    settings = {}
73    try:
74        cfg = configparser.ConfigParser()
75        cfg.optionxform = str
76        cfg.read(path)
77        logging.info('Config file read.')
78        settings = dict(cfg.items('Settings'))
79    except:
80        raise IOError('Something went wrong while reading config file.')
81    print(settings)
82    return settings

Read and parse the configuration file at the given path.

Parameters
  • path (str): The path to the configuration file.
Returns
  • Dict[str, str]: A dictionary containing the settings parsed from the configuration file.
Raises
  • IOError: If something goes wrong while reading the configuration file.