-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
71 lines (61 loc) · 2.32 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Config module
This module defines the class for getting configuration options
:license: MIT, see LICENSE for more details
:copyright: (c) 2016 by NETHINKS GmbH, see AUTORS for more details
"""
import os
import configparser
class Config(object):
"""Class for handling configuration.
This class handles the configuration. The configuration is
in an ini-file. An instance of this class is used for
getting values.
"""
def __init__(self):
"""Creates a new config object from the config file etc/alarmforwarder.conf"""
# get directory name
basedir = os.path.dirname(os.path.abspath(__file__))
self.__filename = basedir + "/etc/alarmforwarder.conf"
self.__config = configparser.ConfigParser()
self.__config.read(self.__filename)
def get_value(self, section_name, key, default_value):
"""Gets a configuration value.
This function returns the configuration value of a specific
key within a section. If the section/key does not exist,
the default_value will be returned.
Args:
section_name: name of the configuration section.
key: key of the configuration option.
default_value: default value.
Returns:
The value of the configuration option, or the default
value, if the configuration option does not exist.
"""
# set default value
output = default_value
# get value from config
try:
output = self.__config.get(section_name, key)
except:
pass
# return value
return output
def set_value(self, section_name, key, value):
"""Sets a configuration value.
This function sets a configuration value and write the changes
to the configuration file.
Args:
section_name: name of the configuration section.
key: key of the configuration option.
value: value of the configuration option.
"""
# set value in data structure
try:
self.__config[section_name]
except:
self.__config[section_name] = {}
self.__config[section_name][key] = value
# save configuration file
with open(self.__filename, 'w') as configfile:
self.__config.write(configfile)