本文整理汇总了Python中configobj.ConfigObjError方法的典型用法代码示例。如果您正苦于以下问题:Python configobj.ConfigObjError方法的具体用法?Python configobj.ConfigObjError怎么用?Python configobj.ConfigObjError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类configobj
的用法示例。
在下文中一共展示了configobj.ConfigObjError方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse
# 需要导入模块: import configobj [as 别名]
# 或者: from configobj import ConfigObjError [as 别名]
def parse(self, config, defaults=None):
"""Parse the conf
:param config: Configuration to parse
:type config: str, list or File
:param defaults: Default options
:type defaults: dict
"""
self.conf = {}
self.conffile = config
self.section = None
if defaults is not None or not hasattr(self, 'defaults'):
self.defaults = defaults
self.validator = validate.Validator()
try:
self.conf = configobj.ConfigObj(config, encoding='utf-8')
self.mtime = os.path.getmtime(self.conffile)
except configobj.ConfigObjError as exp:
# We were unable to parse the config
self.logger.critical('Unable to parse configuration')
raise exp
示例2: read_config_file
# 需要导入模块: import configobj [as 别名]
# 或者: from configobj import ConfigObjError [as 别名]
def read_config_file(f):
"""Read a config file."""
if isinstance(f, basestring):
f = os.path.expanduser(f)
try:
config = ConfigObj(f, interpolation=False, encoding='utf8')
except ConfigObjError as e:
log(LOGGER, logging.ERROR, "Unable to parse line {0} of config file "
"'{1}'.".format(e.line_number, f))
log(LOGGER, logging.ERROR, "Using successfully parsed config values.")
return e.config
except (IOError, OSError) as e:
log(LOGGER, logging.WARNING, "You don't have permission to read "
"config file '{0}'.".format(e.filename))
return None
return config
示例3: read_config_file
# 需要导入模块: import configobj [as 别名]
# 或者: from configobj import ConfigObjError [as 别名]
def read_config_file(self, f):
"""Read a config file *f*.
:param str f: The path to a file to read.
"""
configspec = self.default_file if self.validate else None
try:
config = ConfigObj(infile=f, configspec=configspec,
interpolation=False, encoding='utf8')
except ConfigObjError as e:
logger.warning(
'Unable to parse line {} of config file {}'.format(
e.line_number, f))
config = e.config
valid = True
if self.validate:
valid = config.validate(Validator(), preserve_errors=True,
copy=True)
if bool(config):
self.config_filenames.append(config.filename)
return config, valid
示例4: parse_config_file
# 需要导入模块: import configobj [as 别名]
# 或者: from configobj import ConfigObjError [as 别名]
def parse_config_file(config_file, config_spec_file):
assert validators.PanoptesValidators.valid_nonempty_string(config_file), u'config_file must be a non-empty str'
assert validators.PanoptesValidators.valid_nonempty_string(config_spec_file), \
u'config_spec_file must be a non empty str'
try:
config = ConfigObj(config_file, configspec=config_spec_file, interpolation=u'template', file_error=True)
except IOError as e:
raise PanoptesConfigurationParsingError(u'Error reading file: %s' % str(e))
except ConfigObjError as e:
raise PanoptesConfigurationParsingError(u'Error parsing config file "%s": %s' % (config_file, str(e)))
validator = Validator()
result = config.validate(validator, preserve_errors=True)
if result is not True:
errors = u''
for (section_list, key, error) in flatten_errors(config, result):
if key is None:
errors += u'Section(s) ' + u','.join(section_list) + u' are missing\n'
else:
errors += u'The "' + key + u'" key in section "' + u','.join(section_list) + u'" failed validation\n'
raise PanoptesConfigurationParsingError(u'Error parsing the configuration file: %s' % errors)
return config
示例5: write_picochess_ini
# 需要导入模块: import configobj [as 别名]
# 或者: from configobj import ConfigObjError [as 别名]
def write_picochess_ini(key: str, value):
"""Update picochess.ini config file with key/value."""
try:
config = ConfigObj('picochess.ini')
config[key] = value
config.write()
except (ConfigObjError, DuplicateError) as conf_exc:
logging.exception(conf_exc)
示例6: test_config_init
# 需要导入模块: import configobj [as 别名]
# 或者: from configobj import ConfigObjError [as 别名]
def test_config_init():
casters = ['string_lower_list', 'force_string', 'boolean_or_string']
fd, tmpfile = mkstemp()
os.write(fd, TEST_CONFIG)
os.close(fd)
fd, wrong = mkstemp()
os.write(fd, TEST_CONFIG_FAILURE)
os.close(fd)
config = BUIConfig(tmpfile)
with pytest.raises(configobj.ConfigObjError):
BUIConfig(wrong, defaults={})
assert config.safe_get('backend', section='Global') == 'something'
assert config.safe_get('timeout', 'integer', 'Global') == 12
config.default_section('Production')
assert config.safe_get('duplicate') == 'cat'
assert config.safe_get('duplicate', section='Global') == 'nyan'
assert config.safe_get('run', 'boolean_or_string') is True
assert config.safe_get('sql', 'boolean_or_string') == 'none'
array = config.safe_get('array', 'string_lower_list')
assert array[1] == 'values'
assert array[0] == 'some'
assert isinstance(config.safe_get('array'), list)
assert config.safe_get('array', 'force_string') == 'some,VALUES'
for cast in casters:
# safe_get is safe and shouldn't raise any exception
assert config.safe_get('i iz not in ze config!', cast) is None
os.unlink(tmpfile)
os.unlink(wrong)
示例7: _get_config_object
# 需要导入模块: import configobj [as 别名]
# 或者: from configobj import ConfigObjError [as 别名]
def _get_config_object(self, alternative_config=None):
"""Create a L{ConfigObj} consistent with our preferences.
@param config_source: Optional readable source to read from instead of
the default configuration file.
"""
config_source = alternative_config or self.get_config_filename()
# Setting list_values to False prevents ConfigObj from being "smart"
# about lists (it now treats them as strings). See bug #1228301 for
# more context.
# Setting raise_errors to False causes ConfigObj to batch all parsing
# errors into one ConfigObjError raised at the end of the parse instead
# of raising the first one and then exiting. This also allows us to
# recover the good config values in the error handler below.
# Setting write_empty_values to True prevents configObj writes
# from writing "" as an empty value, which get_plugins interprets as
# '""' which search for a plugin named "". See bug #1241821.
try:
config_obj = ConfigObj(config_source, list_values=False,
raise_errors=False, write_empty_values=True)
except ConfigObjError as e:
logger = getLogger()
logger.warn(str(e))
# Good configuration values are recovered here
config_obj = e.config
return config_obj