From 54d4288f957df499fbd4d89832bf827e068093d2 Mon Sep 17 00:00:00 2001 From: oysteini Date: Sat, 5 Mar 2011 21:33:26 +0000 Subject: [PATCH] Lesing av filformatet. --- python/web/library/fileformat.py | 42 ++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/python/web/library/fileformat.py b/python/web/library/fileformat.py index 2906f98..8825164 100644 --- a/python/web/library/fileformat.py +++ b/python/web/library/fileformat.py @@ -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 = ''