• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DaveFoss / DAVE_data / 10689721794

03 Sep 2024 07:57PM UTC coverage: 78.98% (-7.4%) from 86.4%
10689721794

Pull #10

github

web-flow
Merge fd6209350 into 226e5e483
Pull Request #10: Add basic functions

66 of 90 branches covered (73.33%)

Branch coverage included in aggregate %.

135 of 168 new or added lines in 6 files covered. (80.36%)

2 existing lines in 1 file now uncovered.

321 of 400 relevant lines covered (80.25%)

4.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.69
/src/dave_data/config.py
1
"""
2
Project config reader.
3
Based on Steffen (https://github.com/steffenGit)
4

5
License: MIT
6
"""
7

8
__all__ = [
5✔
9
    "has_option",
10
    "has_section",
11
    "get",
12
    "get_list",
13
    "get_dict",
14
    "get_dict_list",
15
    "tmp_set",
16
    "init",
17
]
18

19
import configparser as cp
5✔
20
import logging
5✔
21
from pathlib import Path
5✔
22

23
BLACKLIST = ["tox.ini", "pytest.ini"]
5✔
24

25
cfg = cp.RawConfigParser()
5✔
26
cfg.optionxform = str
5✔
27
_loaded = False
5✔
28
FILES = []
5✔
29

30

31
def get_ini_filenames():
5✔
32
    """Returns a list of ini files to use."""
33
    paths = []
5✔
34

35
    package_path = Path(__file__).resolve().parent
5✔
36
    files = list(package_path.rglob("*.ini"))
5✔
37

38
    local_path = Path(Path.home(), ".dave")
5✔
39
    if local_path.is_dir():
5✔
NEW
40
        paths.append(local_path)
×
41
    logging.debug(f"Searching for .ini-files in the following paths {paths}")
5✔
42
    for p in paths:
5✔
NEW
43
        files.extend(list(p.glob("*.ini")))
×
44
    files = [f for f in files if f not in BLACKLIST]
5✔
45
    return files
5✔
46

47

48
def init(files=None):
5✔
49
    """Read config file(s).
50

51
    Parameters
52
    ----------
53
    files : list or None
54
        Absolute path to config file (incl. filename)
55
    """
56
    if files is None:
5✔
57
        files = get_ini_filenames()
5✔
58
    global FILES
59
    FILES = files
5✔
60
    cfg.read(files, encoding="utf-8")
5✔
61
    global _loaded
62
    _loaded = True
5✔
63

64

65
def load():
5✔
66
    if not _loaded:
5✔
67
        init()
5✔
68

69

70
def has_option(section, option):
5✔
71
    """Returns True if the given option exists in the given section."""
NEW
72
    return cfg.has_option(section, option)
×
73

74

75
def has_section(section):
5✔
76
    """Returns True if the given section exists."""
NEW
77
    return cfg.has_section(section)
×
78

79

80
def get(section, key):
5✔
81
    """Returns the value of a given key in a given section."""
82
    load()
5✔
83
    try:
5✔
84
        return cfg.getint(section, key)
5✔
85
    except ValueError:
5✔
86
        try:
5✔
87
            return cfg.getfloat(section, key)
5✔
88
        except ValueError:
5✔
89
            try:
5✔
90
                return cfg.getboolean(section, key)
5✔
91
            except ValueError:
5✔
92
                value = cfg.get(section, key)
5✔
93
                if value == "None":
5✔
94
                    value = None
5✔
95
                return value
5✔
96

97

98
def get_list(section, parameter, sep=",", string=False):
5✔
99
    """Returns the values (separated by sep) of a given key in a given
100
    section as a list.
101
    """
102
    try:
5✔
103
        my_list = get(section, parameter).split(sep)
5✔
104
        my_list = [x.strip() for x in my_list]
5✔
105

NEW
106
    except AttributeError:
×
NEW
107
        if string is True:
×
NEW
108
            my_list = list(cfg.get(section, parameter))
×
109
        else:
NEW
110
            my_list = list(get(section, parameter))
×
111
    return my_list
5✔
112

113

114
def get_dict(section):
5✔
115
    """Returns the values of a section as dictionary"""
116
    load()
5✔
117
    section_keys = dict(cfg.items(section)).keys()
5✔
118
    return {key: get(section, key) for key in section_keys}
5✔
119

120

121
def get_dict_list(section, string=False):
5✔
122
    """
123
    Returns the values of a section as dictionary. The values will be
124
    interpreted as list.
125
    """
NEW
126
    load()
×
NEW
127
    dc = {}
×
NEW
128
    for key, _value in cfg.items(section):
×
NEW
129
        dc[key] = get_list(section, key, string=string)
×
NEW
130
    return dc
×
131

132

133
def tmp_set(section, key, value):
5✔
134
    """
135
    Set/Overwrite a value temporarily for the actual section.
136
    """
137
    load()
5✔
138
    return cfg.set(section, key, value)
5✔
139

140

141
def get_base_path(sub_dir=None):
5✔
142
    if get("path", "base") is None:
5✔
143
        base_path = Path(Path.home(), "dave_data")
5✔
144
        tmp_set("path", "base", str(base_path))
5✔
145
    else:
146
        base_path = Path(get("path", "base"))
5✔
147
    if sub_dir is not None:
5✔
148
        allowed = get_list("path", "sub_dirs")
5✔
149
        if sub_dir in allowed:
5✔
150
            base_path = Path(base_path, sub_dir)
5✔
151
        else:
NEW
152
            raise ValueError(
×
153
                f"Subdir {sub_dir} is not valid. Use one of the following "
154
                f"subdirectories: {allowed}"
155
            )
156
    base_path.mkdir(exist_ok=True, parents=True)
5✔
157
    return base_path
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc