本文整理汇总了Python中ConfigParser.DuplicateSectionError方法的典型用法代码示例。如果您正苦于以下问题:Python ConfigParser.DuplicateSectionError方法的具体用法?Python ConfigParser.DuplicateSectionError怎么用?Python ConfigParser.DuplicateSectionError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConfigParser
的用法示例。
在下文中一共展示了ConfigParser.DuplicateSectionError方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import DuplicateSectionError [as 别名]
def __init__(self, config_file):
if sys.version[0] == "2":
CP.__init__(self)
else:
super().__init__()
if not os.path.exists(config_file):
raise Exception("Could not find " + config_file)
f = open(config_file)
id_string = f.readline().split("=")
if id_string[0].strip().upper() in ["CAPI", "SAPI"]:
self.type = id_string[0]
else:
raise SyntaxError("Could not find API type in " + config_file)
try:
self.version = int(id_string[1].strip())
except ValueError:
raise SyntaxError("Unknown version '{}'".format(id_string[1].strip()))
except IndexError:
raise SyntaxError("Could not find API version in " + config_file)
if sys.version[0] == "2":
exceptions = (configparser.ParsingError, configparser.DuplicateSectionError)
else:
exceptions = (
configparser.ParsingError,
configparser.DuplicateSectionError,
configparser.DuplicateOptionError,
)
try:
if sys.version[0] == "2":
self.readfp(f)
else:
self.read_file(f)
except configparser.MissingSectionHeaderError:
raise SyntaxError("Missing section header")
except exceptions as e:
raise SyntaxError(e.message)
示例2: get
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import DuplicateSectionError [as 别名]
def get(self, section, item):
result = ''
try:
result = self.data_conf.get(section, item)
except ConfigParser.NoSectionError:
self.read()
try:
self.data_conf.add_section(section)
except ConfigParser.DuplicateSectionError: pass
self.data_conf.set(section, item, '')
self.write()
except ConfigParser.NoOptionError:
self.set(section, item, '')
return result
示例3: test_weird_errors
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import DuplicateSectionError [as 别名]
def test_weird_errors(self):
cf = self.newconfig()
cf.add_section("Foo")
self.assertRaises(ConfigParser.DuplicateSectionError,
cf.add_section, "Foo")
示例4: test_duplicatesectionerror
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import DuplicateSectionError [as 别名]
def test_duplicatesectionerror(self):
import pickle
e1 = ConfigParser.DuplicateSectionError('section')
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(e1, proto)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
self.assertEqual(e1.section, e2.section)
self.assertEqual(repr(e1), repr(e2))
示例5: test_duplicatesectionerror
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import DuplicateSectionError [as 别名]
def test_duplicatesectionerror(self):
import pickle
e1 = ConfigParser.DuplicateSectionError('section')
pickled = pickle.dumps(e1)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
self.assertEqual(e1.section, e2.section)
self.assertEqual(repr(e1), repr(e2))
示例6: ensure_required_groups
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import DuplicateSectionError [as 别名]
def ensure_required_groups(self, groups):
for group in groups:
try:
self.debug("Adding group {0}".format(group))
self.config.add_section(group)
except configparser.DuplicateSectionError:
pass
示例7: build_main_config
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import DuplicateSectionError [as 别名]
def build_main_config(add_conf, main_config):
"""Build configuration from all found conf files.
:param add_conf: ``object`` ConfigParser object
:param main_config: ``object`` ConfigParser object
"""
for section in add_conf.sections():
try:
main_config.add_section(section)
except ConfigParser.DuplicateSectionError:
pass
for k, v in add_conf.items(section):
main_config.set(section, k, v)
示例8: build_install_section
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import DuplicateSectionError [as 别名]
def build_install_section(main_dir_path, main_config):
"""Build the install section with links.
:param main_dir_path: ``str`` Directory path
:param main_config: ``object`` ConfigParser object
"""
links = []
links_dir = os.path.join(main_dir_path, 'links.d')
if os.path.isdir(links_dir):
_link = config_files(config_dir_path=links_dir, extension='.link')
links.extend(pip_links(_link))
# Add install section if not already found
try:
main_config.add_section('install')
except ConfigParser.DuplicateSectionError:
pass
# Get all items from the install section
try:
install_items = main_config.items('install')
except ConfigParser.NoSectionError:
install_items = None
link_strings = set_links(links)
if install_items:
for item in install_items:
if item[0] != 'find-links':
main_config.set('install', *item)
main_config.set('install', 'find-links', link_strings)
示例9: save
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import DuplicateSectionError [as 别名]
def save(self, filename=None):
#TODO: Maybe unused
"""Save configuration settings from the file filename, if filename
is None then the value from get_configfile() is used
:param filename: file name to be used for config saving.
:type filename: str.
"""
fname = self.get_configfile()
if filename is not None and len(filename) > 0:
fname = filename
if fname is None or len(fname) == 0:
return
config = ConfigParser.RawConfigParser()
try:
config.add_section(self._sectionname)
except ConfigParser.DuplicateSectionError:
pass # ignored
for key in self._get_ac_keys():
ackey = '_ac__%s' % key
config.set(self._sectionname, key, str(self.__dict__[ackey]))
fileh = open(self._configfile, 'wb')
config.write(fileh)
fileh.close()