-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparsediff.py
More file actions
51 lines (50 loc) · 1.79 KB
/
Copy pathparsediff.py
File metadata and controls
51 lines (50 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class ParsediffResult(dict):
def __init__(self, name):
self.name = self['name'] = name
self.before = self['before'] = []
self.after = self['after'] = []
def parsediff(filename):
with open(filename) as f:
try:
state = 1
SS = []
for i,l in enumerate(x.strip() for x in f):
i += 1
# print('number', i, 'state', state, 'line', l)
if state == 1:
assert not(l.startswith('> ') or l.startswith('< '))
S = ParsediffResult(l)
state = 2
# print(S)
elif state == 2:
if l.startswith('< '):
l = l[2:]
S.before.append(l)
elif l.startswith('> '):
state = 3
# goto state 3
l = l[2:]
S.after.append(l)
elif l == '---':
state = 3
else:
SS.append(S)
state = 1
S = ParsediffResult(l)
state = 2
# print(S)
elif state == 3:
assert not(l.startswith('< ') or l == '---')
if l.startswith('> '):
l = l[2:]
S.after.append(l)
else:
SS.append(S)
state = 1
S = ParsediffResult(l)
state = 2
# print(S)
SS.append(S)
return SS
except AssertionError:
raise ValueError('Error line {}: {}'.format(i, l))