yfrake.config.helpers.convert_to_dict()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
# ==================================================================================== #
2
#    helpers.py - This file is part of the YFrake package.                             #
3
# ------------------------------------------------------------------------------------ #
4
#                                                                                      #
5
#    MIT License                                                                       #
6
#                                                                                      #
7
#    Copyright (c) 2022 Mattias Aabmets                                                #
8
#                                                                                      #
9
#    Permission is hereby granted, free of charge, to any person obtaining a copy      #
10
#    of this software and associated documentation files (the "Software"), to deal     #
11
#    in the Software without restriction, including without limitation the rights      #
12
#    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell         #
13
#    copies of the Software, and to permit persons to whom the Software is             #
14
#    furnished to do so, subject to the following conditions:                          #
15
#                                                                                      #
16
#    The above copyright notice and this permission notice shall be included in all    #
17
#    copies or substantial portions of the Software.                                   #
18
#                                                                                      #
19
#    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR        #
20
#    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,          #
21
#    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE       #
22
#    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER            #
23
#    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,     #
24
#    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     #
25
#    SOFTWARE.                                                                         #
26
#                                                                                      #
27
# ==================================================================================== #
28
from .validator import validate_config
29
from configparser import ConfigParser
30
from pathlib import Path
31
32
33
# ==================================================================================== #
34
def read_config_from(path: Path | str) -> dict:
35
    cp = ConfigParser()
36
    cp.read(path)
37
    config = convert_to_dict(cp)
38
    config = convert_datatypes(config)
39
    validate_config(config)
40
    return config
41
42
43
# ------------------------------------------------------------------------------------ #
44
def convert_to_dict(cp: ConfigParser) -> dict:
45
    config = dict()
46
    for section in cp.sections():
47
        sect = section.lower()
48
        config[section.lower()] = dict()
49
        for key, value in cp.items(section):
50
            config[sect][key] = value
51
    return config
52
53
54
# ------------------------------------------------------------------------------------ #
55
def convert_datatypes(config: dict) -> dict:
56
    bool_map = dict(true=True, false=False)
57
    for section in config.values():
58
        for key, value in section.items():
59
            try:
60
                section[key] = int(value)
61
            except ValueError:
62
                section[key] = bool_map.get(value, value)
63
    return config
64