2011-03-05 20:31:31 +01:00
|
|
|
import types
|
|
|
|
|
2011-03-05 18:53:13 +01:00
|
|
|
fields = {
|
|
|
|
'book':
|
|
|
|
[('isbn', 's'), ('title', 's'), ('category', 's'),
|
|
|
|
('subtitle', 's'), ('persons', 'd'), ('publisher', 's'),
|
|
|
|
('published_year', 's'), ('edition', 's'), ('num_pages', 's'),
|
|
|
|
('series', 's'), ('description', 's'), # TODO picture, thumbnail
|
|
|
|
('references', 'd')],
|
|
|
|
'person':
|
|
|
|
[('id', 's'), ('first_name', 's'), ('last_name', 's')],
|
|
|
|
'category':
|
|
|
|
[('id', 's'), ('name', 's'), ('placement', 'l')] }
|
|
|
|
action_fields = {
|
|
|
|
'new-book':
|
|
|
|
{ 'type': 'book',
|
|
|
|
'required': ['isbn', 'title', 'category'] },
|
|
|
|
'edit-book':
|
|
|
|
{ 'type': 'book',
|
|
|
|
'required': ['isbn'] },
|
|
|
|
'delete-book':
|
|
|
|
{ 'type': 'book',
|
|
|
|
'required': ['isbn'] },
|
|
|
|
'new-person':
|
|
|
|
{ 'type': 'person',
|
|
|
|
'required': ['id', 'first_name', 'last_name'] },
|
|
|
|
'edit-person':
|
|
|
|
{ 'type': 'person',
|
|
|
|
'required': ['id'] },
|
|
|
|
'delete-person':
|
|
|
|
{ 'type': 'person',
|
|
|
|
'required': ['id'] },
|
|
|
|
'new-category':
|
|
|
|
{ 'type': 'category',
|
|
|
|
'required': ['id', 'name'] },
|
|
|
|
'edit-category':
|
|
|
|
{ 'type': 'category',
|
|
|
|
'required': ['id'] },
|
|
|
|
'delete-category':
|
|
|
|
{ 'type': 'category',
|
|
|
|
'required': ['id'] } }
|
|
|
|
|
|
|
|
def read_action(lines):
|
|
|
|
d = {}
|
|
|
|
lastfield = None
|
|
|
|
for line in lines:
|
|
|
|
if len(line) == 0:
|
|
|
|
raise 'Empty line in action'
|
|
|
|
if line[0] in [' ', '\t']: # continuation line
|
|
|
|
if not lastfield:
|
|
|
|
raise 'First line is continuation line'
|
|
|
|
d[lastfield] = d[lastfield] + '\n' + line.strip()
|
|
|
|
else:
|
|
|
|
field, value = line.split(':', 1)
|
|
|
|
# TODO skriv ferdig
|
|
|
|
|
|
|
|
def write_field_value_str(val):
|
|
|
|
lines = ''
|
2011-03-05 19:11:07 +01:00
|
|
|
if not val:
|
|
|
|
val = ''
|
|
|
|
val = unicode(val)
|
2011-03-05 18:53:13 +01:00
|
|
|
value_lines = val.split('\n')
|
|
|
|
for l in value_lines:
|
|
|
|
lines += ' ' + l + '\n'
|
|
|
|
return lines
|
|
|
|
|
|
|
|
def write_field_value_dict(val):
|
|
|
|
lines = '\n'
|
|
|
|
for (key,values) in val.items():
|
|
|
|
for single_value in values:
|
2011-03-05 19:11:07 +01:00
|
|
|
lines += ' ' + key + ' ' + unicode(single_value) + '\n'
|
2011-03-05 18:53:13 +01:00
|
|
|
return lines
|
|
|
|
|
|
|
|
def write_field_value_list(val):
|
|
|
|
lines = ''
|
|
|
|
for single_value in val:
|
2011-03-05 19:11:07 +01:00
|
|
|
lines += ' ' + unicode(single_value)
|
2011-03-05 18:53:13 +01:00
|
|
|
return lines
|
|
|
|
|
2011-03-05 20:31:31 +01:00
|
|
|
def make_comment(s):
|
|
|
|
return '\n'.join(map(lambda x: '# ' + x,
|
|
|
|
s.split('\n'))) + '\n'
|
|
|
|
|
2011-03-05 18:53:13 +01:00
|
|
|
def write_action(d):
|
2011-03-05 20:31:31 +01:00
|
|
|
if type(d) in types.StringTypes:
|
|
|
|
return make_comment(d)
|
|
|
|
lines = ''
|
|
|
|
if 'comment' in d:
|
|
|
|
lines += make_comment(d['comment'])
|
2011-03-05 18:53:13 +01:00
|
|
|
action = d['action']
|
2011-03-05 20:31:31 +01:00
|
|
|
lines += 'action: ' + action + '\n'
|
2011-03-05 18:53:13 +01:00
|
|
|
data_type = action_fields[action]['type']
|
|
|
|
for field, ftype in fields[data_type]:
|
|
|
|
if field in d:
|
|
|
|
value_writer = {'s': write_field_value_str,
|
|
|
|
'd': write_field_value_dict,
|
|
|
|
'l': write_field_value_list}[ftype]
|
|
|
|
lines += field + ':' + value_writer(d[field])
|
|
|
|
return lines
|
|
|
|
|
|
|
|
def write_actionlist(actions):
|
|
|
|
return '\n'.join(map(write_action, actions))
|
|
|
|
|
2011-03-05 20:31:31 +01:00
|
|
|
# test: print write_actionlist([{'comment':'Foo!\nBar!','action':'new-book','isbn':'434545'},{'action':'edit-book','isbn':'654654745','persons':{'author':['ab','foo'],'illustrator':['moo']}},'This\nis\na\ncomment.',{'action':'edit-category','id':'matematikk','name':'Matematikk','placement':['T10','T11']}])
|