La til int som egen type i fileformat-feltene.
This commit is contained in:
parent
8eafa21103
commit
73be593cd8
|
@ -9,7 +9,7 @@ fields = {
|
|||
'book':
|
||||
[('isbn', 's'), ('title', 's'), ('category', 's'),
|
||||
('subtitle', 's'), ('persons', 'd'), ('publisher', 's'),
|
||||
('published_year', 's'), ('edition', 's'), ('num_pages', 's'),
|
||||
('published_year', 'i'), ('edition', 'i'), ('num_pages', 'i'),
|
||||
('series', 's'), ('description', 's'), # TODO picture, thumbnail
|
||||
('references', 'd')],
|
||||
'person':
|
||||
|
@ -57,6 +57,11 @@ class CommitFormatSyntaxError(Exception):
|
|||
def read_field_value_str(val):
|
||||
return '\n'.join(map(lambda x: x.strip(), val.split('\n'))).strip()
|
||||
|
||||
def read_field_value_int(val):
|
||||
if val.strip() == '':
|
||||
return None
|
||||
return int(val.strip())
|
||||
|
||||
def read_field_value_dict(val):
|
||||
d = {}
|
||||
for line in val.strip().split('\n'):
|
||||
|
@ -71,7 +76,13 @@ def read_field_value_list(val):
|
|||
return val.strip().split(' ')
|
||||
|
||||
def read_action(text):
|
||||
'''
|
||||
Parse text as an action, returning a dictionary.
|
||||
'''
|
||||
lines = text.split('\n')
|
||||
print 'reading action'
|
||||
print 'lines:'
|
||||
print lines
|
||||
d = {}
|
||||
lastfield = None
|
||||
for line in lines:
|
||||
|
@ -85,9 +96,14 @@ def read_action(text):
|
|||
field, value = line.split(':', 1)
|
||||
d[field] = value.strip()
|
||||
lastfield = field
|
||||
|
||||
if 'action' not in d:
|
||||
raise CommitFormatSyntaxError('Missing \'action\' field')
|
||||
action = d['action']
|
||||
|
||||
print 'dict:'
|
||||
print d
|
||||
|
||||
for field in action_fields[action]['required']:
|
||||
if field not in d:
|
||||
raise CommitFormatSyntaxError('Missing required field \'%s\' in \'%s\' action' % (field, action))
|
||||
|
@ -96,12 +112,18 @@ def read_action(text):
|
|||
for field, ftype in fields[data_type]:
|
||||
if field in d:
|
||||
reader = { 's': read_field_value_str,
|
||||
'i': read_field_value_int,
|
||||
'd': read_field_value_dict,
|
||||
'l': read_field_value_list }[ftype]
|
||||
result[field] = reader(d[field])
|
||||
return result
|
||||
|
||||
def read_actionlist(text):
|
||||
'''
|
||||
Parse text as a list of actions.
|
||||
|
||||
The result is a list of dictionaries.
|
||||
'''
|
||||
return map(lambda x: read_action(x.strip()),
|
||||
text.split('\n\n'))
|
||||
|
||||
|
@ -144,6 +166,7 @@ def write_action(d):
|
|||
for field, ftype in fields[data_type]:
|
||||
if field in d:
|
||||
value_writer = {'s': write_field_value_str,
|
||||
'i': write_field_value_str,
|
||||
'd': write_field_value_dict,
|
||||
'l': write_field_value_list}[ftype]
|
||||
lines += field + ':' + value_writer(d[field])
|
||||
|
|
Reference in New Issue