2011-10-09 16:39:07 +02:00
|
|
|
import re
|
2011-10-08 11:06:15 +02:00
|
|
|
import types
|
2011-10-12 21:26:21 +02:00
|
|
|
from exc import WorblehatException
|
2011-10-11 17:44:17 +02:00
|
|
|
|
2011-10-08 11:06:15 +02:00
|
|
|
# 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':
|
2011-10-08 19:08:46 +02:00
|
|
|
[('isbn', 's'), ('id', 's'), ('title', 's'), ('category', 's'),
|
2011-10-08 11:06:15 +02:00
|
|
|
('subtitle', 's'), ('persons', 'd'), ('publisher', 's'),
|
2011-10-08 19:08:46 +02:00
|
|
|
('published_year', 'i'), ('edition', 'i'), ('pages', 'i'),
|
2011-10-08 11:06:15 +02:00
|
|
|
('series', 's'), ('description', 's'), # TODO picture, thumbnail
|
|
|
|
('references', 'd')],
|
|
|
|
'person':
|
2011-10-08 15:53:52 +02:00
|
|
|
[('id', 's'), ('firstname', 's'), ('lastname', 's')],
|
2011-10-08 11:06:15 +02:00
|
|
|
'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',
|
|
|
|
'required': ['isbn', 'title', 'category'] },
|
|
|
|
'edit-book':
|
|
|
|
{ 'type': 'book',
|
|
|
|
'required': ['isbn'] },
|
|
|
|
'delete-book':
|
|
|
|
{ 'type': 'book',
|
|
|
|
'required': ['isbn'] },
|
|
|
|
'new-person':
|
|
|
|
{ 'type': 'person',
|
2011-10-08 15:53:52 +02:00
|
|
|
'required': ['id', 'firstname', 'lastname'] },
|
2011-10-08 11:06:15 +02:00
|
|
|
'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'] } }
|
|
|
|
|
2011-10-12 21:26:21 +02:00
|
|
|
class CommitFormatSyntaxError(WorblehatException):
|
2011-10-09 16:39:07 +02:00
|
|
|
def __init__(self, msg, linenr):
|
2011-10-12 21:26:21 +02:00
|
|
|
WorblehatException.__init__(self, 'Syntax error on line %d: %s' % (linenr, msg))
|
|
|
|
|
|
|
|
class CommitFormatError(WorblehatException):
|
|
|
|
def __init__(self, msg):
|
|
|
|
WorblehatException.__init__(self, msg)
|
2011-10-08 11:06:15 +02:00
|
|
|
|
|
|
|
def read_field_value_str(val):
|
2011-10-08 21:49:28 +02:00
|
|
|
if val.strip() == '':
|
|
|
|
return None
|
2011-10-08 11:06:15 +02:00
|
|
|
return '\n'.join(map(lambda x: x.strip(), val.split('\n'))).strip()
|
|
|
|
|
|
|
|
def read_field_value_int(val):
|
|
|
|
if val.strip() == '':
|
|
|
|
return None
|
2011-10-12 21:26:21 +02:00
|
|
|
try:
|
|
|
|
return int(val.strip())
|
|
|
|
except ValueError, TypeError:
|
|
|
|
raise WorblehatException('%s is not an integer' % val)
|
2011-10-08 11:06:15 +02:00
|
|
|
|
|
|
|
def read_field_value_dict(val):
|
|
|
|
d = {}
|
|
|
|
if val.strip() == '':
|
|
|
|
return 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(' ')
|
|
|
|
|
2011-10-09 16:39:07 +02:00
|
|
|
def read_action(lines):
|
2011-10-08 11:06:15 +02:00
|
|
|
'''
|
|
|
|
Parse text as an action, returning a dictionary.
|
|
|
|
'''
|
|
|
|
print 'reading action'
|
|
|
|
print 'lines:'
|
|
|
|
print lines
|
|
|
|
d = {}
|
|
|
|
lastfield = None
|
2011-10-09 16:39:07 +02:00
|
|
|
for (linenr, line) in lines:
|
2011-10-08 11:06:15 +02:00
|
|
|
if len(line) == 0:
|
2011-10-09 16:39:07 +02:00
|
|
|
raise CommitFormatSyntaxError('Empty line in action', linenr)
|
2011-10-08 11:06:15 +02:00
|
|
|
if line[0] in [' ', '\t']: # continuation line
|
|
|
|
if not lastfield:
|
2011-10-09 16:39:07 +02:00
|
|
|
raise CommitFormatSyntaxError('First line is continuation line: ' + line, linenr)
|
2011-10-08 11:06:15 +02:00
|
|
|
d[lastfield] = d[lastfield] + '\n' + line.strip()
|
|
|
|
else:
|
|
|
|
field, value = line.split(':', 1)
|
|
|
|
d[field] = value.strip()
|
|
|
|
lastfield = field
|
|
|
|
|
2011-10-09 16:39:07 +02:00
|
|
|
firstlinenr = lines[0][0]
|
|
|
|
|
2011-10-08 11:06:15 +02:00
|
|
|
if 'action' not in d:
|
2011-10-09 16:39:07 +02:00
|
|
|
raise CommitFormatSyntaxError('Missing \'action\' field', firstlinenr)
|
2011-10-08 11:06:15 +02:00
|
|
|
action = d['action']
|
|
|
|
|
|
|
|
print 'dict:'
|
|
|
|
print d
|
|
|
|
|
|
|
|
for field in action_fields[action]['required']:
|
|
|
|
if field not in d:
|
2011-10-09 16:39:07 +02:00
|
|
|
raise CommitFormatSyntaxError('Missing required field \'%s\' in \'%s\' action' %
|
|
|
|
(field, action),
|
|
|
|
firstlinenr)
|
2011-10-08 11:06:15 +02:00
|
|
|
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,
|
|
|
|
'i': read_field_value_int,
|
|
|
|
'd': read_field_value_dict,
|
|
|
|
'l': read_field_value_list }[ftype]
|
|
|
|
result[field] = reader(d[field])
|
2011-10-08 19:08:46 +02:00
|
|
|
else:
|
|
|
|
result[field] = None
|
|
|
|
|
|
|
|
print 'final dict:'
|
|
|
|
print result
|
|
|
|
|
2011-10-08 11:06:15 +02:00
|
|
|
return result
|
|
|
|
|
2011-10-09 16:39:07 +02:00
|
|
|
def read_paragraphs(text):
|
|
|
|
lines = text.split('\n')
|
|
|
|
current_para = []
|
|
|
|
paragraphs = []
|
|
|
|
comment_re = r'^#.*$'
|
|
|
|
blank_re = r'^[ \t]*$'
|
|
|
|
for i in xrange(len(lines)):
|
|
|
|
l = lines[i]
|
|
|
|
if re.match(comment_re, l):
|
|
|
|
print 'comment:', l
|
|
|
|
continue
|
|
|
|
elif re.match(blank_re, l):
|
|
|
|
print 'blank:', l
|
|
|
|
if len(current_para) > 0:
|
|
|
|
paragraphs.append(current_para)
|
|
|
|
current_para = []
|
|
|
|
else:
|
|
|
|
current_para.append((i+1, l))
|
|
|
|
if len(current_para) > 0:
|
|
|
|
paragraphs.append(current_para)
|
|
|
|
current_para = []
|
|
|
|
return paragraphs
|
|
|
|
|
2011-10-08 11:06:15 +02:00
|
|
|
def read_actionlist(text):
|
|
|
|
'''
|
|
|
|
Parse text as a list of actions.
|
|
|
|
|
|
|
|
The result is a list of dictionaries.
|
|
|
|
'''
|
2011-10-09 16:39:07 +02:00
|
|
|
paragraphs = read_paragraphs(text)
|
|
|
|
print 'paragraphs:', paragraphs
|
|
|
|
return map(read_action, paragraphs)
|
2011-10-08 11:06:15 +02:00
|
|
|
|
|
|
|
def write_field_value_str(val):
|
|
|
|
lines = ''
|
|
|
|
if not val:
|
|
|
|
val = ''
|
|
|
|
value_lines = val.split('\n')
|
|
|
|
for l in value_lines:
|
|
|
|
lines += ' ' + l + '\n'
|
2011-10-11 17:44:17 +02:00
|
|
|
if len(value_lines) > 1:
|
|
|
|
lines = '\n' + lines
|
2011-10-08 11:06:15 +02:00
|
|
|
return lines
|
|
|
|
|
2011-10-11 17:44:17 +02:00
|
|
|
def write_field_value_int(val):
|
|
|
|
return ' %s\n' % str(val)
|
|
|
|
|
2011-10-08 11:06:15 +02:00
|
|
|
def write_field_value_dict(val):
|
|
|
|
lines = '\n'
|
|
|
|
for (key,values) in val.items():
|
|
|
|
for single_value in values:
|
2011-10-11 17:44:17 +02:00
|
|
|
lines += ' ' + key + ' ' + single_value + '\n'
|
2011-10-08 11:06:15 +02:00
|
|
|
return lines
|
|
|
|
|
|
|
|
def write_field_value_list(val):
|
|
|
|
lines = ''
|
|
|
|
for single_value in val:
|
2011-10-11 17:44:17 +02:00
|
|
|
lines += ' ' + single_value
|
2011-10-08 11:06:15 +02:00
|
|
|
return lines
|
|
|
|
|
|
|
|
def make_comment(s):
|
|
|
|
return '\n'.join(map(lambda x: '# ' + x,
|
|
|
|
s.split('\n'))) + '\n'
|
|
|
|
|
|
|
|
def write_action(d):
|
|
|
|
if type(d) in types.StringTypes:
|
|
|
|
return make_comment(d)
|
2011-10-11 17:44:17 +02:00
|
|
|
lines = u''
|
2011-10-08 11:06:15 +02:00
|
|
|
if 'comment' in d:
|
|
|
|
lines += make_comment(d['comment'])
|
|
|
|
action = d['action']
|
|
|
|
lines += 'action: ' + action + '\n'
|
|
|
|
data_type = action_fields[action]['type']
|
|
|
|
for field, ftype in fields[data_type]:
|
|
|
|
if field in d:
|
|
|
|
value_writer = {'s': write_field_value_str,
|
2011-10-11 17:44:17 +02:00
|
|
|
'i': write_field_value_int,
|
2011-10-08 11:06:15 +02:00
|
|
|
'd': write_field_value_dict,
|
|
|
|
'l': write_field_value_list}[ftype]
|
|
|
|
lines += field + ':' + value_writer(d[field])
|
2011-10-12 21:26:21 +02:00
|
|
|
return lines
|
2011-10-08 11:06:15 +02:00
|
|
|
|
|
|
|
def write_actionlist(actions):
|
2011-10-12 21:26:21 +02:00
|
|
|
return '\n'.join(map(write_action, actions))
|
2011-10-08 11:06:15 +02: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']}])
|