Commit 4f9b42fb authored by Michael Murtaugh's avatar Michael Murtaugh

etherpad changeset parser

parent a479a2f0
from __future__ import print_function
import re
# e.g. Z:9kj>1|8=al=o4*1a|1+1$
def changeset_parse (c) :
changeset_pat = re.compile(r'^Z:([0-9a-z]+)([><])([0-9a-z]+)(.+?)\$')
op_pat = re.compile(r'(\|([0-9a-z]+)([\+\-\=])([0-9a-z]+))|([\*\+\-\=])([0-9a-z]+)')
def parse_op (m):
g = m.groups()
if g[0]:
if g[2] == "+":
op = "insert"
elif g[2] == "-":
op = "delete"
else:
op = "hold"
return {
'raw': m.group(0),
'op': op,
'lines': int(g[1], 36),
'chars': int(g[3], 36)
}
elif g[4] == "*":
return {
'raw': m.group(0),
'op': 'attr',
'index': int(g[5], 36)
}
else:
if g[4] == "+":
op = "insert"
elif g[4] == "-":
op = "delete"
else:
op = "hold"
return {
'raw': m.group(0),
'op': op,
'chars': int(g[5], 36)
}
m = changeset_pat.search(c)
bank = c[m.end():]
g = m.groups()
ops_raw = g[3]
op = None
ret = {}
ret['raw'] = c
ret['source_length'] = int(g[0], 36)
ret['final_op'] = g[1]
ret['final_diff'] = int(g[2], 36)
ret['ops_raw'] = ops_raw
ret['ops'] = ops = []
ret['bank'] = bank
ret['bank_length'] = len(bank)
for m in op_pat.finditer(ops_raw):
ops.append(parse_op(m))
return ret
if __name__ == "__main__":
import json
print (json.dumps(changeset_parse("Z:9kj>1|8=al=o4*1a|1+1$"), indent=2))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment