Lesing av filformatet.

This commit is contained in:
Øystein Ingmar Skartsæterhagen 2011-03-05 21:33:26 +00:00
parent ef48ba039c
commit 54d4288f95

View File

@ -54,6 +54,22 @@ action_fields = {
class CommitFormatSyntaxError(Exception):
pass
def read_field_value_str(val):
return '\n'.join(map(lambda x: x.strip(), val.split('\n'))).strip()
def read_field_value_dict(val):
d = {}
for line in val.strip().split('\n'):
key, value = line.strip().split(' ', 1)
if key in d:
d[key].append(value)
else:
d[key] = [value]
return d
def read_field_value_list(val):
return val.strip().split(' ')
def read_action(lines):
d = {}
lastfield = None
@ -66,11 +82,27 @@ def read_action(lines):
d[lastfield] = d[lastfield] + '\n' + line.strip()
else:
field, value = line.split(':', 1)
# reader = { 's': read_field_value_str,
# 'd': read_field_value_dict,
# 'l': read_field_value_list }[
# translated_value =
# TODO skriv ferdig
d[field] = value.strip()
lastfield = field
if 'action' not in d:
raise CommitFormatSyntaxError('Missing \'action\' field')
action = d['action']
for field in action_fields[action]['required']:
if field not in d:
raise CommitFormatSyntaxError('Missing required field \'%s\' in \'%s\' action' % (field, action))
data_type = action_fields[action]['type']
result = { 'action': action }
for field, ftype in fields[data_type]:
if field in d:
reader = { 's': read_field_value_str,
'd': read_field_value_dict,
'l': read_field_value_list }[ftype]
result[field] = reader(d[field])
return result
def read_actionlist(text):
return map(lambda x: read_action(x.strip().split('\n')),
text.split('\n\n'))
def write_field_value_str(val):
lines = ''