Improved reading in fileformat.py.
* Handles comments (lines starting with '#'). * Remembers line numbers, and uses them in error messages.
This commit is contained in:
parent
066d3a4364
commit
c614e4b06a
|
@ -1,3 +1,4 @@
|
||||||
|
import re
|
||||||
import types
|
import types
|
||||||
|
|
||||||
# The possible fields for each type of object.
|
# The possible fields for each type of object.
|
||||||
|
@ -52,7 +53,9 @@ action_fields = {
|
||||||
'required': ['id'] } }
|
'required': ['id'] } }
|
||||||
|
|
||||||
class CommitFormatSyntaxError(Exception):
|
class CommitFormatSyntaxError(Exception):
|
||||||
pass
|
def __init__(self, msg, linenr):
|
||||||
|
super(CommitFormatSyntaxError, self).__init__(self, 'Syntax error on line %d: %s' %
|
||||||
|
(linenr, msg))
|
||||||
|
|
||||||
def read_field_value_str(val):
|
def read_field_value_str(val):
|
||||||
if val.strip() == '':
|
if val.strip() == '':
|
||||||
|
@ -79,30 +82,31 @@ def read_field_value_dict(val):
|
||||||
def read_field_value_list(val):
|
def read_field_value_list(val):
|
||||||
return val.strip().split(' ')
|
return val.strip().split(' ')
|
||||||
|
|
||||||
def read_action(text):
|
def read_action(lines):
|
||||||
'''
|
'''
|
||||||
Parse text as an action, returning a dictionary.
|
Parse text as an action, returning a dictionary.
|
||||||
'''
|
'''
|
||||||
lines = text.split('\n')
|
|
||||||
print 'reading action'
|
print 'reading action'
|
||||||
print 'lines:'
|
print 'lines:'
|
||||||
print lines
|
print lines
|
||||||
d = {}
|
d = {}
|
||||||
lastfield = None
|
lastfield = None
|
||||||
for line in lines:
|
for (linenr, line) in lines:
|
||||||
if len(line) == 0:
|
if len(line) == 0:
|
||||||
raise CommitFormatSyntaxError('Empty line in action')
|
raise CommitFormatSyntaxError('Empty line in action', linenr)
|
||||||
if line[0] in [' ', '\t']: # continuation line
|
if line[0] in [' ', '\t']: # continuation line
|
||||||
if not lastfield:
|
if not lastfield:
|
||||||
raise CommitFormatSyntaxError('First line is continuation line: ' + line)
|
raise CommitFormatSyntaxError('First line is continuation line: ' + line, linenr)
|
||||||
d[lastfield] = d[lastfield] + '\n' + line.strip()
|
d[lastfield] = d[lastfield] + '\n' + line.strip()
|
||||||
else:
|
else:
|
||||||
field, value = line.split(':', 1)
|
field, value = line.split(':', 1)
|
||||||
d[field] = value.strip()
|
d[field] = value.strip()
|
||||||
lastfield = field
|
lastfield = field
|
||||||
|
|
||||||
|
firstlinenr = lines[0][0]
|
||||||
|
|
||||||
if 'action' not in d:
|
if 'action' not in d:
|
||||||
raise CommitFormatSyntaxError('Missing \'action\' field')
|
raise CommitFormatSyntaxError('Missing \'action\' field', firstlinenr)
|
||||||
action = d['action']
|
action = d['action']
|
||||||
|
|
||||||
print 'dict:'
|
print 'dict:'
|
||||||
|
@ -110,7 +114,9 @@ def read_action(text):
|
||||||
|
|
||||||
for field in action_fields[action]['required']:
|
for field in action_fields[action]['required']:
|
||||||
if field not in d:
|
if field not in d:
|
||||||
raise CommitFormatSyntaxError('Missing required field \'%s\' in \'%s\' action' % (field, action))
|
raise CommitFormatSyntaxError('Missing required field \'%s\' in \'%s\' action' %
|
||||||
|
(field, action),
|
||||||
|
firstlinenr)
|
||||||
data_type = action_fields[action]['type']
|
data_type = action_fields[action]['type']
|
||||||
result = { 'action': action }
|
result = { 'action': action }
|
||||||
for field, ftype in fields[data_type]:
|
for field, ftype in fields[data_type]:
|
||||||
|
@ -128,14 +134,38 @@ def read_action(text):
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def read_paragraphs(text):
|
||||||
|
lines = text.split('\n')
|
||||||
|
current_para = []
|
||||||
|
paragraphs = []
|
||||||
|
comment_re = r'^#.*$'
|
||||||
|
blank_re = r'^[ \t]*$'
|
||||||
|
for i in xrange(len(lines)):
|
||||||
|
l = lines[i]
|
||||||
|
if re.match(comment_re, l):
|
||||||
|
print 'comment:', l
|
||||||
|
continue
|
||||||
|
elif re.match(blank_re, l):
|
||||||
|
print 'blank:', l
|
||||||
|
if len(current_para) > 0:
|
||||||
|
paragraphs.append(current_para)
|
||||||
|
current_para = []
|
||||||
|
else:
|
||||||
|
current_para.append((i+1, l))
|
||||||
|
if len(current_para) > 0:
|
||||||
|
paragraphs.append(current_para)
|
||||||
|
current_para = []
|
||||||
|
return paragraphs
|
||||||
|
|
||||||
def read_actionlist(text):
|
def read_actionlist(text):
|
||||||
'''
|
'''
|
||||||
Parse text as a list of actions.
|
Parse text as a list of actions.
|
||||||
|
|
||||||
The result is a list of dictionaries.
|
The result is a list of dictionaries.
|
||||||
'''
|
'''
|
||||||
return map(lambda x: read_action(x.strip()),
|
paragraphs = read_paragraphs(text)
|
||||||
text.split('\n\n'))
|
print 'paragraphs:', paragraphs
|
||||||
|
return map(read_action, paragraphs)
|
||||||
|
|
||||||
def write_field_value_str(val):
|
def write_field_value_str(val):
|
||||||
lines = ''
|
lines = ''
|
||||||
|
|
Reference in New Issue