-
Notifications
You must be signed in to change notification settings - Fork 4
/
AppConfig.py
76 lines (55 loc) · 2.82 KB
/
AppConfig.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
72
73
74
75
76
"""
Helpers for configuring the app/plugins
"""
from configparser import ConfigParser
class AppConfig(ConfigParser):
"""Application Configuration"""
def __init__(self, config_file) -> None:
super().__init__(allow_no_value=True, inline_comment_prefixes=";")
self.read(config_file)
def get_boolean_or_default(self, section_name: str, option_name: str, default_value: bool) -> bool:
"""Gets a boolean value from config or returns the default value if not found
Args:
section_name (str): The section name
option_name (str): The option name (key) in the section
default_value (bool): The default value to return if the config value does not exist
Returns:
bool: The boolean value of the supplied option or the default value if it does not exist
"""
if self.has_option(section_name, option_name):
value = self.get(section_name, option_name)
return True if value is None else value.lower() == 'true'
return default_value
def get_float_or_default(self, section_name: str, option_name: str, default_value: float) -> float:
"""Gets a float value from config or returns the default value if not found
Args:
section_name (str): The section name
option_name (str): The option name (key) in the section
default_value (float): The default value to return if the config value does not exist
Returns:
bool: The flot value of the supplied option or the default value if it does not exist
"""
if self.has_option(section_name, option_name):
value = self.getfloat(section_name, option_name)
return value
return default_value
def get_string_or_default(self, section_name: str, option_name: str, default_value: str) -> str:
"""Gets a string value from config or returns the default value if not found
Args:
section_name (str): The section name
option_name (str): The option name (key) in the section
default_value (str): The default value to return if the config vlaue does not exist
Returns:
str: The string value of the supplied option or the default value if it does not exist
"""
if self.has_option(section_name, option_name):
value = self.get(section_name, option_name)
if value is not None and value != '':
return value
return self.get_string_or_default('DEFAULT', option_name, default_value)
return default_value
def is_debugging_enabled(self, section: str) -> bool:
"""
Determines if debuggins is enabled for a given section (usually related to a plugin)
"""
return self.get_boolean_or_default(section, 'debug', False)