在项目中要实现字典访问方式的ConfigParser, 在原有SafeConfigParser上实现如下类似功能:
- config[section][option]能够获取option的值
- 所有在默认配置参数中的值能够正确解析成对应的类型
- config[section][option]能够设置string类型的option的值
二话不说,上代码,所有解释都在代码中:
-
from ConfigParser import SafeConfigParser
-
import os, sys
-
import codecs
-
import logging
-
-
"""
-
Configuration layout for default base config
-
"""
-
DEFAULT_CONFIG = { "general":
-
{
-
"debug": ("bool", False),
-
},
-
"file":
-
{
-
"download_dir": ("str", "/mnt/SR"),
-
"user": ("str", "root"),
-
"group": ("str", "root"),
-
"file_permission": ("str", "0644"),
-
"dir_permission": ("str", "0755"),
-
},
-
"download":
-
{
-
"max_downloads": ("int", 3),
-
"min_free_space": ("int", 200),
-
"limit_speed": ("bool", False),
-
"max_speed": ("int", -1),
-
"skip_existing": ("bool", False),
-
"chunks": ("int", 3),
-
"restart_failed": ("bool", False),
-
}
-
}
-
-
class Section:
-
"""provides dictionary like access for configparser"""
-
-
def __init__(self, parser, section):
-
"""Constructor"""
-
self.parser = parser
-
self.section = section
-
-
def __getitem__(self, item):
-
"""getitem"""
-
if item not in self.parser.options(self.section):
-
raise KeyError("'%s' not in config section[%s]" % (item, self.section))
-
-
try:
-
type = DEFAULT_CONFIG[self.section][item][0]
-
except:
-
type = str
-
-
if type == 'int':
-
return self.parser.getint(self.section, item)
-
elif type == 'float':
-
return self.parser.getfloat(self.section, item)
-
elif type == 'bool':
-
return self.parser.getboolean(self.section, item)
-
-
# else type == 'str'
-
return self.parser.get(self.section, item)
-
-
def __setitem__(self, item, value):
-
"""setitem"""
-
self.parser.set(self.section, item, value)
-
-
-
class DictConfigParser(SafeConfigParser):
-
def __init__(self, configfile):
-
self.configfile = configfile
-
self.configParser = SafeConfigParser()
-
-
# exception flag to indicate if some error happnes when load config from file
-
failed = False
-
try:
-
localFSCoding = sys.getfilesystemencoding()
-
with codecs.open(self.configfile, 'r', encoding=localFSCoding) as f:
-
self.configParser.readfp(f)
-
except Exception as e:
-
logging.warn("Cannot read config file: %s because of %s" % (self.configfile, e))
-
failed = True
-
-
if failed:
-
logging.debug("Load defaul config as we failed to load/validate current config")
-
self.loadDefaultConfig()
-
-
def loadDefaultConfig(self):
-
for sec in DEFAULT_CONFIG.keys():
-
self.configParser.add_section(sec)
-
items = DEFAULT_CONFIG[sec]
-
for (name, value) in items:
-
self.configParser.set(sec, name, str(value[1]))
-
-
def save(self):
-
with open(self.configfile, 'wb') as configfile:
-
self.configParser.write(configfile)
-
-
def __getitem__(self, section):
-
"""provides dictionary like access: c['section']['option']"""
-
if section not in self.configParser.sections():
-
raise KeyError("section '%s' not in config" % section)
-
-
return Section(self.configParser, section)
-
-
def __contains__(self, section):
-
""" checks if parser contains section """
-
return section in self.configParser.sections()
阅读(1186) | 评论(0) | 转发(0) |