diff --git a/python/fileformat.py b/python/fileformat.py index 9361466..2906f98 100644 --- a/python/fileformat.py +++ b/python/fileformat.py @@ -1,5 +1,10 @@ import types +# The possible fields for each type of object. +# +# Each field is a tuple (fieldname, fieldtype). Fieldtype is either +# 's' (string), 'd' (dictionary, where the values are lists of +# strings), 'l' (list of strings). fields = { 'book': [('isbn', 's'), ('title', 's'), ('category', 's'), @@ -11,6 +16,12 @@ fields = { [('id', 's'), ('first_name', 's'), ('last_name', 's')], 'category': [('id', 's'), ('name', 's'), ('placement', 'l')] } + +# Fields associated with each action. +# +# The 'type' is a key in the fields dictionary, indicating the set of +# fields which can be used with the action. The 'required' list is a +# list of fields which must be present for the action to be accepted. action_fields = { 'new-book': { 'type': 'book', @@ -40,18 +51,25 @@ action_fields = { { 'type': 'category', 'required': ['id'] } } +class CommitFormatSyntaxError(Exception): + pass + def read_action(lines): d = {} lastfield = None for line in lines: if len(line) == 0: - raise 'Empty line in action' + raise CommitFormatSyntaxError('Empty line in action') if line[0] in [' ', '\t']: # continuation line if not lastfield: - raise 'First line is continuation line' + raise CommitFormatSyntaxError('First line is continuation line: ' + line) 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 def write_field_value_str(val):