|
1
|
|
|
# Copyright (C) 2014-2021 Greenbone Networks GmbH |
|
2
|
|
|
# |
|
3
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later |
|
4
|
|
|
# |
|
5
|
|
|
# This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
# it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
# published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
# License, or (at your option) any later version. |
|
9
|
|
|
# |
|
10
|
|
|
# This program is distributed in the hope that it will be useful, |
|
11
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
# GNU Affero General Public License for more details. |
|
14
|
|
|
# |
|
15
|
|
|
# You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
|
|
18
|
|
|
""" |
|
19
|
|
|
Module to store ospd configuration settings |
|
20
|
|
|
""" |
|
21
|
|
|
|
|
22
|
|
|
import configparser |
|
23
|
|
|
import logging |
|
24
|
|
|
|
|
25
|
|
|
from pathlib import Path |
|
26
|
|
|
from typing import Dict |
|
27
|
|
|
|
|
28
|
|
|
logger = logging.getLogger(__name__) |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
class Config: |
|
32
|
|
|
def __init__(self, section: str = 'main') -> None: |
|
33
|
|
|
self._parser = configparser.ConfigParser(default_section=section) |
|
34
|
|
|
self._config = {} # type: Dict |
|
35
|
|
|
self._defaults = {} # type: Dict |
|
36
|
|
|
|
|
37
|
|
|
def load(self, filepath: Path, def_section: str = 'main') -> None: |
|
38
|
|
|
path = filepath.expanduser() |
|
39
|
|
|
parser = configparser.ConfigParser(default_section=def_section) |
|
40
|
|
|
|
|
41
|
|
|
with path.open() as f: |
|
42
|
|
|
parser.read_file(f) |
|
43
|
|
|
|
|
44
|
|
|
self._defaults.update(parser.defaults()) |
|
45
|
|
|
|
|
46
|
|
|
for key, value in parser.items(def_section): |
|
47
|
|
|
self._config.setdefault(def_section, dict())[key] = value |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def defaults(self) -> Dict: |
|
50
|
|
|
return self._defaults |
|
51
|
|
|
|