-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfigParserExtended.py
30 lines (26 loc) · 992 Bytes
/
ConfigParserExtended.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
#from configparser import RawConfigParser
import configparser
class ConfigParserExtended(configparser.ConfigParser):
def as_dict(self,section="all"):
if section == "all":
d = self.__dict__['_sections']
else:
d = self.__dict__['_sections'][section]
return d
def as_json(self,section="all"):
import json
if section == "all":
d = self.__dict__['_sections']
else:
d = self.__dict__['_sections'][section]
return json.dumps(d,separators=(',',':'),indent=4,sort_keys=True,
ensure_ascii=False).encode('utf8')
def print_ini(self,section="all"):
if section == "all":
sections = self.sections()
else:
sections = [section]
for section_name in sections:
print("[{}]".format(section_name))
for key, value in self.items(section_name):
print('{} = {}'.format(key, value))