-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfigparsing.py
30 lines (27 loc) · 1.12 KB
/
configparsing.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
#!/usr/bin/env python3
from configparser import SafeConfigParser
class ConfigParsing():
# This class returns an associative key=value
# array for the specified section
# from the indicated config file. defaults to fail2ban-cluster.conf
def __init__(self, configfile='fail2ban-cluster.conf'):
self.validsections = ['general', 'monitor', 'subscriber', 'publisher']
self.parser = SafeConfigParser()
try:
self.parser.read(configfile)
except Exception:
raise ValueError('configuration file does not exist')
def Section(self, section=None):
if section is None:
raise ValueError('no section specified')
dict1 = {}
if section not in self.validsections:
raise ValueError('requested section is invalid or inexistant')
options = self.parser.options(section)
for option in options:
try:
dict1[option] = self.parser.get(section, option)
except Exception:
dict1[option] = None
# TODO: add stderr error, must check daemon.py
return dict1