Projects/worblehat-old
Projects
/
worblehat-old
Archived
12
0
Fork 0

Lesing av filformatet.

This commit is contained in:
Øystein Ingmar Skartsæterhagen 2011-03-05 21:33:26 +00:00
parent ef48ba039c
commit 54d4288f95
1 changed files with 37 additions and 5 deletions

View File

@ -54,6 +54,22 @@ action_fields = {
class CommitFormatSyntaxError(Exception): class CommitFormatSyntaxError(Exception):
pass 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): def read_action(lines):
d = {} d = {}
lastfield = None lastfield = None
@ -66,11 +82,27 @@ def read_action(lines):
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)
# reader = { 's': read_field_value_str, d[field] = value.strip()
# 'd': read_field_value_dict, lastfield = field
# 'l': read_field_value_list }[ if 'action' not in d:
# translated_value = raise CommitFormatSyntaxError('Missing \'action\' field')
# TODO skriv ferdig 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): def write_field_value_str(val):
lines = '' lines = ''