-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
51 lines (39 loc) · 1.63 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
import configparser
import os
from version import __version__
from dotenv import load_dotenv
load_dotenv()
class FlaskMonitoringDashboardConfigParser(configparser.ConfigParser):
def optionxform(self, optionstr):
return optionstr
def write(self, fp):
if self._defaults:
fp.write("[%s]\n" % configparser.DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s=%s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
for section in self._sections:
fp.write("[%s]\n" % section)
for (key, value) in self._sections[section].items():
if key == "__name__":
continue
if (value is not None) or (self._optcre == self.OPTCRE):
key = "=".join((key, str(value).replace('\n', '\n\t')))
fp.write("%s\n" % (key))
fp.write("\n")
def build_config_file():
# Initialize the ConfigParser object
config = FlaskMonitoringDashboardConfigParser()
# Read the configuration file
config.read('config.template.cfg')
# Update the values in the configuration file
def overwrite(section: str, options: list[str]):
for option in options:
config[section][option] = os.getenv(
option, config.get(section, option))
overwrite('authentication', ['USERNAME', 'PASSWORD', 'SECURITY_TOKEN'])
# Update the app version
config['dashboard']['APP_VERSION'] = __version__
# Write the updated values to the config file
with open('config.cfg', 'w') as configfile:
config.write(configfile)