Fixed IniConfigParser line number calculation, syntax error reporting and handling empty values

This commit is contained in:
Maxim Kulkin
2013-09-19 16:11:28 +04:00
parent b2829f3a92
commit c45d215798

View File

@@ -5,7 +5,7 @@ from ostack_validator.model import *
from ostack_validator.config_formats.common import * from ostack_validator.config_formats.common import *
class IniConfigParser: class IniConfigParser:
key_value_re = re.compile("^(\w+)\s*([:=])\s*('.*'|\".*\"|.*)\s*$") key_value_re = re.compile("^(\S+?)\s*([:=])\s*('.*'|\".*\"|.*)\s*$")
def parse(self, name, base_mark, io): def parse(self, name, base_mark, io):
if not hasattr(io, 'readlines'): if not hasattr(io, 'readlines'):
@@ -22,10 +22,12 @@ class IniConfigParser:
sections = [] sections = []
parameters = [] parameters = []
line_number = 0 line_number = -1
for line in io.readlines(): for line in io.readlines():
line = line.rstrip() line = line.rstrip()
line_number += 1
if current_param_name and (current_param_value.quotechar or (line == '' or not line[0].isspace())): if current_param_name and (current_param_value.quotechar or (line == '' or not line[0].isspace())):
param = ConfigParameter(current_param_name.start_mark, current_param_value.end_mark, current_param_name, current_param_value, current_param_delimiter) param = ConfigParameter(current_param_name.start_mark, current_param_value.end_mark, current_param_name, current_param_value, current_param_delimiter)
parameters.append(param) parameters.append(param)
@@ -92,7 +94,7 @@ class IniConfigParser:
# Unquote value # Unquote value
value = m.group(3) value = m.group(3)
quotechar = None quotechar = None
if (value[0] == value[-1] and value[0] in "\"'"): if len(value) > 0 and (value[0] == value[-1] and value[0] in "\"'"):
quotechar = value[0] quotechar = value[0]
value = value[1:-1] value = value[1:-1]
@@ -103,9 +105,7 @@ class IniConfigParser:
quotechar=quotechar quotechar=quotechar
) )
else: else:
errors.append(ParseError('Syntax error', mark(line_number))) errors.append(ParseError('Syntax error in line "%s"' % line, mark(line_number)))
line_number += 1
if current_param_name: if current_param_name:
param = ConfigParameter(current_param_name.start_mark, current_param_value.end_mark, current_param_name, current_param_value, current_param_delimiter) param = ConfigParameter(current_param_name.start_mark, current_param_value.end_mark, current_param_name, current_param_value, current_param_delimiter)