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