-
Notifications
You must be signed in to change notification settings - Fork 3
/
assdiff3.py
460 lines (366 loc) · 15.8 KB
/
assdiff3.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/bin/env python3
import argparse
import collections
import collections.abc
import heapq
import io
import re
import sys
parser = argparse.ArgumentParser(description='Three-way merge of ASS files')
parser.add_argument('myfile', help="The locally changed file")
parser.add_argument('oldfile', help="The parent file that both files diverged from")
parser.add_argument('yourfile', help="The remotely changed file")
parser.add_argument('--output', '-o', help="File to output to")
parser.add_argument('--conflict-marker', choices=['diff3', 'description'], default='description',
help="Type of conflict marker to use: diff3-style markers or textual descriptions")
parser.add_argument('--conflict-marker-size', type=int, default=7,
help="Size of conflict marker when using diff3-style conflict markers")
parser.add_argument('--diff3', action='store_true', help="Use diff3-style three-way diffing")
parser.add_argument('--script-info', choices=['ours', 'theirs'], default='ours',
help="Whether to keep 'our' or 'their' changes for the script info and Aegisub project sections")
parser.add_argument('--newline', '-n', choices=['LF', 'CRLF'], default='LF',
help="Line ending to enforce. Uses LF if left unset.")
args = parser.parse_args()
CONFLICT_LINE = "Comment: 0,0:00:00.00,0:00:00.00,Default,,0,0,0,CONFLICT,{}"
CONFLICT_MARKERS = {
"diff3": {
"ours": "<" * args.conflict_marker_size,
"ancestor": "|" * args.conflict_marker_size,
"theirs": "=" * args.conflict_marker_size,
"end": ">" * args.conflict_marker_size
},
"description": {
"ours": "Start of own hunk",
"ancestor": "End of own hunk; Start of common ancestor's hunk",
"theirs": "End of {} hunk; Start of other hunk".format(
"common ancestor's" if args.diff3 else "own"),
"end": "End of other hunk"
}
}
NEWLINE = args.newline.replace("LF", "\n").replace("CR", "\r")
class ASSLine(collections.abc.Mapping):
VALID_TYPES = None
def __init__(self, line=None, fields=None, source_file=None):
self.source_file = source_file
if line is not None:
line_type, line_data = line.split(": ", 1)
field_values = line_data.split(",", len(self.FIELDS) - 1)
if len(field_values) != len(self.FIELDS):
raise ValueError("Malformed line: {}".format(line))
self.fields = dict(zip(self.FIELDS, field_values))
self.fields["Type"] = line_type
elif fields is not None:
self.fields = fields
if self.VALID_TYPES is not None and self.Type not in self.VALID_TYPES:
raise ValueError("Not a valid line type")
@classmethod
def merge(cls, a, parent, b):
changed_a = {field: value for field, value in a.items()
if value != parent[field]}
changed_b = {field: value for field, value in b.items()
if value != parent[field]}
if len(changed_a.keys() & changed_b.keys()) > 0:
return None
return cls(fields={**parent, **changed_a, **changed_b}, source_file="?")
def __str__(self):
ordered_fields = [str(self[field]) for field in self.FIELDS]
return f"{self.Type}: {','.join(ordered_fields)}"
def __getattr__(self, key):
return self.fields[key]
def __getitem__(self, item):
return getattr(self, item)
def __len__(self):
return len(self.fields) + 1
def __iter__(self):
yield "Type"
yield from self.fields
class DialogueLine(ASSLine):
FIELDS = ['Layer', 'Start', 'End', 'Style', 'Name', 'MarginL',
'MarginR', 'MarginV', 'Effect', 'Text']
VALID_TYPES = {"Dialogue", "Comment"}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.extra_indices = []
match = re.match(r"^\{((?:=\d+)+)\}(.*)$", self.fields["Text"])
if match:
self.extra_indices = list(map(int, match.group(1).split("=")[1:]))
self.fields["Text"] = match.group(2)
@property
def Text(self):
if len(self.extra_indices) > 0:
return "{{={}}}{}".format("=".join(str(x) for x in self.extra_indices),
self.fields["Text"])
else:
return self.fields["Text"]
class StyleLine(ASSLine):
FIELDS = ['Name', 'Fontname', 'Fontsize', 'PrimaryColour', 'SecondaryColour',
'OutlineColour', 'BackColour', 'Bold', 'Italic', 'Underline',
'StrikeOut', 'ScaleX', 'ScaleY', 'Spacing', 'Angle', 'BorderStyle',
'Outline', 'Shadow', 'Alignment', 'MarginL', 'MarginR', 'MarginV', 'Encoding']
VALID_TYPES = {"Style"}
class DataLine(ASSLine):
FIELDS = ["Id", "Key", "Value"]
VALID_TYPES = {"Data"}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["Id"] = int(self.fields["Id"])
class KeyValueLine(ASSLine):
FIELDS = ["Value"]
SECTIONS = {
"Script Info": KeyValueLine,
"Aegisub Project Garbage": KeyValueLine,
"V4+ Styles": StyleLine,
"Events": DialogueLine,
"Aegisub Extradata": DataLine
}
def parse_file(fname, indicator=None):
sections = collections.defaultdict(list)
current_section = "?"
with open(fname, 'r', encoding='utf-8-sig') as f:
for raw_line in f:
line = raw_line.strip()
section = re.match(r"^\[(.*)\]$", line)
if section:
current_section = section.group(1)
elif len(line) > 0:
factory = SECTIONS.get(current_section, lambda x, **kwargs: x)
try:
sections[current_section].append(factory(line, source_file=indicator))
except ValueError: # ignore unexpected lines
pass
return sections
def merge_keyval(A, O, B):
lines_to_map = lambda lines: collections.OrderedDict(
(line.Type, line.Value) for line in lines)
a_map = lines_to_map(A)
o_map = lines_to_map(O)
b_map = lines_to_map(B)
a_changed = collections.OrderedDict((key, value) for key, value in a_map.items()
if key not in o_map or o_map[key] != value)
a_removed = {key for key in o_map if key not in a_map}
b_changed = collections.OrderedDict((key, value) for key, value in b_map.items()
if key not in o_map or o_map[key] != value)
b_removed = {key for key in o_map if key not in b_map}
if args.script_info == 'ours':
first_removed, first_changed, second_removed, second_changed = \
b_removed, b_changed, a_removed, a_changed
else:
first_removed, first_changed, second_removed, second_changed = \
a_removed, a_changed, b_removed, b_changed
for key in first_removed:
o_map.pop(key, None)
o_map.update(first_changed)
for key in second_removed:
o_map.pop(key, None)
o_map.update(second_changed)
return [KeyValueLine(fields={"Type": key, "Value": value})
for key, value in o_map.items()]
def merge_extradata(mine, parent, their):
extradata_to_id = {}
id_to_extradata = {}
largest_id = 0
for f in (parent, mine, their):
id_map = {}
for data_line in f["Aegisub Extradata"]:
original_id = data_line.Id
if (data_line.Key, data_line.Value) in extradata_to_id:
data_line.Id = extradata_to_id[(data_line.Key, data_line.Value)]
else:
if data_line.Id in id_to_extradata:
data_line.Id = largest_id + 1
extradata_to_id[(data_line.Key, data_line.Value)] = data_line.Id
id_to_extradata[data_line.Id] = data_line
largest_id = max(largest_id, data_line.Id)
id_map[original_id] = data_line.Id
for dialogue_line in f["Events"]:
dialogue_line.extra_indices = [id_map[i] for i in dialogue_line.extra_indices
if i in id_map]
return [id_to_extradata[i] for i in sorted(id_to_extradata)]
class LineMatcher:
def __init__(self, a, b, memoizers=(lambda x: x,)):
self.a = a
self.b = b
self.memoizers = memoizers
self.index_maps = [{} for _ in self.memoizers]
for i, line in enumerate(b):
for imap, memoizer in zip(self.index_maps, self.memoizers):
imap.setdefault(memoizer(line), []).append(i)
def _lcs(self, ai, aj, bi, bj):
longest_match = 0
start_a, start_b = ai, bi
match_lengths = {}
for i in range(ai, aj):
updated_match_lengths = {}
line_a = self.a[i]
b_matches = heapq.merge(
*(imap.get(memoizer(line_a), [])
for imap, memoizer in zip(self.index_maps, self.memoizers)))
for j in b_matches:
if j < bi:
continue
elif j >= bj:
break
match_length = match_lengths.get(j - 1, 0) + 1
updated_match_lengths[j] = match_length
if match_length > longest_match:
longest_match = match_length
start_a = i - match_length + 1
start_b = j - match_length + 1
match_lengths = updated_match_lengths
return start_a, start_b, longest_match
def find_matches(self):
matches = []
queue = collections.deque()
queue.append((0, len(self.a), 0, len(self.b)))
while len(queue) > 0:
ai, aj, bi, bj = queue.popleft()
if aj - ai <= 0 or bj - bi <= 0:
continue
match = self._lcs(ai, aj, bi, bj)
start_a, start_b, match_length = match
if match_length > 0:
queue.append((ai, start_a, bi, start_b))
queue.append((start_a + match_length, aj, start_b + match_length, bj))
matches.append(match)
matches.sort()
return matches
dialogue_conflict = False
def dialogue_conflict_handler(a_hunk, b_hunk, o_hunk):
global dialogue_conflict
dialogue_conflict = True
yield DialogueLine(CONFLICT_LINE.format(CONFLICT_MARKERS[args.conflict_marker]["ours"]))
yield from a_hunk
if args.diff3:
yield DialogueLine(CONFLICT_LINE.format(CONFLICT_MARKERS[args.conflict_marker]["ancestor"]))
yield from o_hunk
yield DialogueLine(CONFLICT_LINE.format(CONFLICT_MARKERS[args.conflict_marker]["theirs"]))
yield from b_hunk
yield DialogueLine(CONFLICT_LINE.format(CONFLICT_MARKERS[args.conflict_marker]["end"]))
style_conflict = False
def style_conflict_handler(a_hunk, b_hunk, o_hunk):
global style_conflict
style_conflict = True
for line in a_hunk:
line.Name = "Own$" + line.Name
yield line
for line in b_hunk:
line.Name = "Other$" + line.Name
yield line
def diff3(A, O, B, conflict_handler, **kwargs):
matches_OA = LineMatcher(O, A, **kwargs).find_matches()
matches_OB = LineMatcher(O, B, **kwargs).find_matches()
def map_indices(matches):
ind_map = collections.OrderedDict()
for (o_start, t_start, match_length) in matches:
for i in range(match_length):
ind_map[o_start + i] = t_start + i
return ind_map
def hunks_equal(list1, hunk1, list2, hunk2):
return len(hunk1) == len(hunk2) and \
all(list1[i] == list2[j] for i, j in zip(hunk1, hunk2))
def process_hunks(a_hunk, b_hunk, o_hunk):
a_changed = not hunks_equal(A, a_hunk, O, o_hunk)
b_changed = not hunks_equal(B, b_hunk, O, o_hunk)
ab_equal = hunks_equal(A, a_hunk, B, b_hunk)
if ab_equal or (a_changed and not b_changed):
for i in a_hunk:
yield A[i]
elif b_changed and not a_changed:
for i in b_hunk:
yield B[i]
else:
yield from conflict_handler((A[i] for i in a_hunk),
(B[i] for i in b_hunk),
(O[i] for i in o_hunk))
o_to_a = map_indices(matches_OA)
o_to_b = map_indices(matches_OB)
prev_a = prev_b = prev_o = -1
# dict is sorted by index
for o in o_to_a:
if o not in o_to_b:
continue
a = o_to_a[o]
b = o_to_b[o]
line = O[o].merge(A[a], O[o], B[b])
if line is None:
continue
if a > prev_a + 1 or b > prev_b + 1:
yield from process_hunks(range(prev_a + 1, a),
range(prev_b + 1, b),
range(prev_o + 1, o))
yield line
prev_a = a
prev_b = b
prev_o = o
yield from process_hunks(range(prev_a + 1, len(A)),
range(prev_b + 1, len(B)),
range(prev_o + 1, len(O)))
def main():
global style_conflict
mine = parse_file(args.myfile, 'Own')
parent = parse_file(args.oldfile, 'Parent')
theirs = parse_file(args.yourfile, 'Other')
script_info = merge_keyval(mine["Script Info"],
parent["Script Info"],
theirs["Script Info"])
project_garbage = merge_keyval(mine["Aegisub Project Garbage"],
parent["Aegisub Project Garbage"],
theirs["Aegisub Project Garbage"])
extradata = merge_extradata(mine, parent, theirs)
styles = list(diff3(
mine["V4+ Styles"], parent["V4+ Styles"], theirs["V4+ Styles"],
style_conflict_handler, memoizers=(lambda line: line.Name,)))
events = list(diff3(mine["Events"], parent["Events"], theirs["Events"],
dialogue_conflict_handler,
memoizers=(lambda line: line.Text,
lambda line: (line.Start, line.End))))
# sanity check: find duplicate style names and disambiguate
seen_styles = {}
for line in styles:
if line.Name in seen_styles:
seen_line = seen_styles[line.Name]
seen_line.Name = seen_line.source_file + "$" + seen_line.Name
line.Name = line.source_file + "$" + line.Name
style_conflict = True
else:
seen_styles[line.Name] = line
if style_conflict:
events.insert(0, DialogueLine(
CONFLICT_LINE.format(
"Style conflict detected. Please resolve the conflict "
"through the style manager.")))
used_extradata = {i for line in events for i in line.extra_indices}
extradata = [line for line in extradata if line.Id in used_extradata]
output = io.StringIO()
output.write("[Script Info]\n")
for line in script_info:
output.write(str(line) + "\n")
output.write("\n")
if len(project_garbage) > 0:
output.write("[Aegisub Project Garbage]\n")
for line in project_garbage:
output.write(str(line) + "\n")
output.write("\n")
output.write("[V4+ Styles]\n")
output.write("Format: {}".format(", ".join(StyleLine.FIELDS)) + "\n")
for line in styles:
output.write(str(line) + "\n")
output.write("\n")
output.write("[Events]\n")
output.write("Format: {}".format(", ".join(DialogueLine.FIELDS)) + "\n")
for line in events:
output.write(str(line) + "\n")
if len(extradata) > 0:
output.write("\n")
output.write("[Aegisub Extradata]\n")
for line in extradata:
output.write(str(line) + "\n")
if args.output is None:
sys.stdout.buffer.write(output.getvalue().encode('utf-8-sig'))
else:
with open(args.output, 'w', encoding='utf-8-sig', newline=NEWLINE) as f:
f.write(output.getvalue())
if dialogue_conflict or style_conflict:
sys.exit(1)
if __name__ == '__main__':
main()