本文整理汇总了Python中ConfigParser.ParsingError方法的典型用法代码示例。如果您正苦于以下问题:Python ConfigParser.ParsingError方法的具体用法?Python ConfigParser.ParsingError怎么用?Python ConfigParser.ParsingError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConfigParser
的用法示例。
在下文中一共展示了ConfigParser.ParsingError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: do_check
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def do_check(self, configuration_file):
configuration = ConfigParser.ConfigParser()
try:
configuration.read(configuration_file)
except ConfigParser.ParsingError as e:
if self.verbose:
print('Ignoring parsing errors:\n' + str(e))
try:
bind_address = configuration.get('httpd', 'bind_address')
if '127.0.0.1' == bind_address or 'localhost' == bind_address:
self.result['level'] = 'GREEN'
self.result['output'] = 'Database listening on localhost only. (' + str(bind_address) + ')'
else:
self.result['level'] = 'YELLOW'
self.result['output'] = 'Database listening is not localhost only (' + str(bind_address) + ')'
except ConfigParser.NoOptionError as e:
self.result['level'] = 'GREEN'
self.result['output'] = 'bind-address option not set, default is 127.0.0.1 or localhost.'
return self.result
示例2: do_check
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def do_check(self, configuration_file):
configuration = ConfigParser.ConfigParser()
try:
configuration.read(configuration_file)
except ConfigParser.ParsingError as e:
if self.verbose:
print('Ignoring parsing errors:\n' + str(e))
try:
bind_address = configuration.get('mysqld', 'bind-address')[0]
if '127.0.0.1' == bind_address or 'localhost' == bind_address:
self.result['level'] = 'GREEN'
self.result['output'] = 'Database listening on localhost only. (' + str(bind_address) + ')'
else:
self.result['level'] = 'YELLOW'
self.result['output'] = 'Database listening is not localhost only (' + str(bind_address) + ')'
except ConfigParser.NoOptionError as e:
self.result['level'] = 'YELLOW'
self.result['output'] = 'bind-address option not set, default is (\'*\')'
return self.result
示例3: do_check
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def do_check(self, configuration_file):
configuration = ConfigParser.ConfigParser()
try:
configuration.read(configuration_file)
except ConfigParser.ParsingError as e:
if self.verbose:
print('Ignoring parsing errors:\n' + str(e))
try:
general_log_file = configuration.get('client', 'password')
# if the option is found, then red!
self.result['level'] = 'RED'
self.result['output'] = 'Client password is in use.'
except ConfigParser.NoOptionError as e:
self.result['level'] = 'GREEN'
self.result['output'] = 'Client password not used.'
return self.result
示例4: parse_settings_file
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def parse_settings_file(settings_file):
# dummy section name because ConfigParser needs sections
PARSER_SECTION = 'settings'
parser = ConfigParser.SafeConfigParser()
# preserve uppercase in settings names
parser.optionxform = lambda option: option.upper()
try:
with open(settings_file) as stream:
# Add a section, since ConfigParser requires it
stream = StringIO("[%s]\n" % PARSER_SECTION + stream.read())
parser.readfp(stream)
except IOError:
raise SystemExit(
'ERROR! Could not open file to read settings at "%s".'
% settings_file)
except ConfigParser.ParsingError as e:
raise SystemExit(
'ERROR! Could not parse settings in file "%s".\n %s'
% (settings_file, e.message))
if parser.sections() != [PARSER_SECTION]:
raise SystemExit(
'ERROR! Found extra sections in settings file: "%s". '
'Sections are not needed.' % parser.sections())
return dict(parser.items(PARSER_SECTION))
示例5: load
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def load(self, path):
parser = ConfigParser.SafeConfigParser()
try:
cfg_str = '[root]\n' + open(path, 'r').read()
cfg_fp = StringIO.StringIO(cfg_str)
parser = ConfigParser.RawConfigParser(allow_no_value=False)
parser.readfp(cfg_fp)
self.__dict__.update(parser.items("root"))
except (ConfigParser.ParsingError) as e:
error = str(e)
line = error[error.find("[line")+5:error.find("]")].strip()
raise ConfigParser.ParsingError("Failed to parse config file. Error on line: " + line)
self.checkConfig()
示例6: __init__
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [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)
示例7: test_parse_errors
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def test_parse_errors(self):
self.newconfig()
self.parse_error(ConfigParser.ParsingError,
"[Foo]\n extra-spaces: splat\n")
self.parse_error(ConfigParser.ParsingError,
"[Foo]\n extra-spaces= splat\n")
self.parse_error(ConfigParser.ParsingError,
"[Foo]\n:value-without-option-name\n")
self.parse_error(ConfigParser.ParsingError,
"[Foo]\n=value-without-option-name\n")
self.parse_error(ConfigParser.MissingSectionHeaderError,
"No Section!\n")
示例8: test_parsingerror
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def test_parsingerror(self):
import pickle
e1 = ConfigParser.ParsingError('source')
e1.append(1, 'line1')
e1.append(2, 'line2')
e1.append(3, 'line3')
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.filename, e2.filename)
self.assertEqual(e1.errors, e2.errors)
self.assertEqual(repr(e1), repr(e2))
示例9: __init__
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def __init__(self, defaults=None, confFile=None, *args, **kwds):
"""Initialize the parser.
*defaults* -- defaults values.
*confFile* -- the file (or list of files) to parse."""
ConfigParser.ConfigParser.__init__(self, defaults=defaults)
if confFile is None:
dotFileName = '.' + confFileName
# Current and home directory.
confFile = [os.path.join(os.getcwd(), confFileName),
os.path.join(os.getcwd(), dotFileName),
os.path.join(os.path.expanduser('~'), confFileName),
os.path.join(os.path.expanduser('~'), dotFileName)]
if os.name == 'posix':
sep = getattr(os.path, 'sep', '/')
# /etc/ and /etc/conf.d/
confFile.append(os.path.join(sep, 'etc', confFileName))
confFile.append(os.path.join(sep, 'etc', 'conf.d',
confFileName))
else:
# etc subdirectory of sys.prefix, for non-unix systems.
confFile.append(os.path.join(sys.prefix, 'etc', confFileName))
for fname in confFile:
try:
self.read(fname)
except (ConfigParser.MissingSectionHeaderError,
ConfigParser.ParsingError), e:
_aux_logger.warn('Troubles reading config file: %s' % e)
# Stop at the first valid file.
if self.has_section('imdbpy'):
break
示例10: get_local_facts_from_file
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def get_local_facts_from_file(filename):
""" Retrieve local facts from fact file
Args:
filename (str): local facts file
Returns:
dict: the retrieved facts
"""
local_facts = dict()
try:
# Handle conversion of INI style facts file to json style
ini_facts = ConfigParser.SafeConfigParser()
ini_facts.read(filename)
for section in ini_facts.sections():
local_facts[section] = dict()
for key, value in ini_facts.items(section):
local_facts[section][key] = value
except (ConfigParser.MissingSectionHeaderError,
ConfigParser.ParsingError):
try:
with open(filename, 'r') as facts_file:
local_facts = json.load(facts_file)
except (ValueError, IOError):
pass
return local_facts
示例11: get_config
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def get_config(aws_creds_path):
config = configparser.RawConfigParser()
try:
config.read(aws_creds_path)
except configparser.ParsingError:
e = sys.exc_info()[1]
log_error_and_exit(logger, "There was a problem reading or parsing "
"your credentials file: %s" % (e.args[0],))
return config
示例12: test_parsingerror
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def test_parsingerror(self):
import pickle
e1 = ConfigParser.ParsingError('source')
e1.append(1, 'line1')
e1.append(2, 'line2')
e1.append(3, 'line3')
pickled = pickle.dumps(e1)
e2 = pickle.loads(pickled)
self.assertEqual(e1.message, e2.message)
self.assertEqual(e1.args, e2.args)
self.assertEqual(e1.filename, e2.filename)
self.assertEqual(e1.errors, e2.errors)
self.assertEqual(repr(e1), repr(e2))
示例13: get_config_value
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def get_config_value(configuration_file, option, verbose=False):
# postgresql config files do not have sections, so inject a dummy section
# see: https://stackoverflow.com/questions/2819696/parsing-properties-file-in-python/
config = StringIO.StringIO()
config.write('[dbdat]\n')
config.write(open(configuration_file).read())
config.seek(0, os.SEEK_SET)
value = None
try:
configuration = ConfigParser.ConfigParser()
configuration.readfp(config)
except ConfigParser.ParsingError as e:
if verbose:
print('Ignoring parsing errors:\n' + str(e))
try:
value = configuration.get('dbdat', option)
# clean up required
value = value.split()[0]
value = value.translate(None, "'")
except ConfigParser.NoOptionError as e:
value = None
return value
示例14: do_check
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def do_check(self, configuration_file):
configuration = ConfigParser.ConfigParser()
count = 0
try:
configuration.read(configuration_file)
except ConfigParser.ParsingError as e:
if self.verbose:
print('Ignoring parsing errors:\n' + str(e))
try:
error_log_file = configuration.get('mysqld', 'log_error')[0]
if os.path.isfile(str(error_log_file)):
with open(str(error_log_file), 'r') as log:
for line in log:
if "Access denied for user '" + self.appuser + "'" in line:
count += 1
if 0 == count:
self.result['level'] = 'GREEN'
self.result['output'] = 'No access denied events found for user.'
elif count < 5:
self.result['level'] = 'YELLOW'
self.result['output'] = 'Access denied events found for user.'
else:
self.result['level'] = 'RED'
self.result['output'] = 'Excessive access denied events found for user.'
else:
self.result['level'] = 'YELLOW'
self.result['output'] = 'Could not access error log file ' + str(error_log_file) + '.'
except ConfigParser.NoOptionError as e:
self.result['level'] = 'RED'
self.result['output'] = 'Error log file not enabled.'
return self.result
示例15: do_check
# 需要导入模块: import ConfigParser [as 别名]
# 或者: from ConfigParser import ParsingError [as 别名]
def do_check(self, configuration_file):
configuration = ConfigParser.ConfigParser()
try:
configuration.read(configuration_file)
except ConfigParser.ParsingError as e:
if self.verbose:
print('Ignoring parsing errors:\n' + str(e))
try:
general_log_file = configuration.get('mysqld', 'log_error')
self.result['level'] = 'GREEN'
self.result['output'] = 'Error log file enabled.'
log_warnings = configuration.get('mysqld', 'log_warnings')
if log_warnings < 2:
self.result['level'] = 'RED'
self.result['output'] = 'Error log not capturing access denied events.'
except ConfigParser.NoOptionError as e:
self.result['level'] = 'RED'
self.result['output'] = 'Error log file not enabled.'
return self.result