-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
53 lines (43 loc) · 1.57 KB
/
utils.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import codecs
import os
# python 3
try:
import ConfigParser as cp
except Exception:
import configparser as cp
class VicConfigParser():
def __init__(self, filename):
# self.filename = os.path.join(sys.path[0]+filename)
self.filename = filename
self.configparser = cp.ConfigParser()
self._read()
def set(self, name, value=None, section='DEFAULT'):
if section != 'DEFAULT':
if section not in self.configparser.sections():
self.configparser.add_section(section)
self.configparser.set(section=section, option=name, value=value)
self._save()
def get(self, name, section='DEFAULT'):
try:
return self.configparser.get(section=section, option=name)
except cp.NoOptionError:
return None
def delkey(self, name, section='DEFAULT'):
self.configparser.remove_option(section=section, option=name)
self._save()
def delsection(self, section='DEFAULT'):
self.configparser.remove_section(section=section)
self._save()
def sections(self):
return self.configparser.sections()
def keys(self, section='DEFAULT'):
return self.configparser.options(section)
def _read(self):
if os.path.exists(self.filename):
self.configparser.readfp(codecs.open(self.filename,
"r", "utf-8"))
def _save(self):
with codecs.open(self.filename, "w", "utf-8") as f:
self.configparser.write(f)