~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wiki/diff_match_patch.py

  • Committer: franku
  • Date: 2019-04-11 15:06:09 UTC
  • mto: This revision was merged to the branch mainline in revision 540.
  • Revision ID: somal@arcor.de-20190411150609-801l72ffxgr6gkui
converted to python 3.6 using 2to3 script

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
import math
33
33
import time
34
 
import urllib
 
34
import urllib.request, urllib.parse, urllib.error
35
35
import re
36
36
 
37
37
 
281
281
                lineStart = lineEnd + 1
282
282
 
283
283
                if line in lineHash:
284
 
                    chars.append(unichr(lineHash[line]))
 
284
                    chars.append(chr(lineHash[line]))
285
285
                else:
286
286
                    lineArray.append(line)
287
287
                    lineHash[line] = len(lineArray) - 1
288
 
                    chars.append(unichr(len(lineArray) - 1))
 
288
                    chars.append(chr(len(lineArray) - 1))
289
289
            return ''.join(chars)
290
290
 
291
291
        chars1 = diff_linesToCharsMunge(text1)
301
301
          lineArray: Array of unique strings.
302
302
 
303
303
        """
304
 
        for x in xrange(len(diffs)):
 
304
        for x in range(len(diffs)):
305
305
            text = []
306
306
            for char in diffs[x][1]:
307
307
                text.append(lineArray[ord(char)])
337
337
        # If the total number of characters is odd, then the front path will
338
338
        # collide with the reverse path.
339
339
        front = (text1_length + text2_length) % 2
340
 
        for d in xrange(max_d):
 
340
        for d in range(max_d):
341
341
            # Bail out if timeout reached.
342
342
            if self.Diff_Timeout > 0 and time.time() > s_end:
343
343
                return None
344
344
 
345
345
            # Walk the front path one step.
346
346
            v_map1.append({})
347
 
            for k in xrange(-d, d + 1, 2):
 
347
            for k in range(-d, d + 1, 2):
348
348
                if k == -d or k != d and v1[k - 1] < v1[k + 1]:
349
349
                    x = v1[k + 1]
350
350
                else:
383
383
            if doubleEnd:
384
384
                # Walk the reverse path one step.
385
385
                v_map2.append({})
386
 
                for k in xrange(-d, d + 1, 2):
 
386
                for k in range(-d, d + 1, 2):
387
387
                    if k == -d or k != d and v2[k - 1] < v2[k + 1]:
388
388
                        x = v2[k + 1]
389
389
                    else:
434
434
        x = len(text1)
435
435
        y = len(text2)
436
436
        last_op = None
437
 
        for d in xrange(len(v_map) - 2, -1, -1):
 
437
        for d in range(len(v_map) - 2, -1, -1):
438
438
            while True:
439
439
                if (x - 1, y) in v_map[d]:
440
440
                    x -= 1
480
480
        x = len(text1)
481
481
        y = len(text2)
482
482
        last_op = None
483
 
        for d in xrange(len(v_map) - 2, -1, -1):
 
483
        for d in range(len(v_map) - 2, -1, -1):
484
484
            while True:
485
485
                if (x - 1, y) in v_map[d]:
486
486
                    x -= 1
1024
1024
        chars2 = 0
1025
1025
        last_chars1 = 0
1026
1026
        last_chars2 = 0
1027
 
        for x in xrange(len(diffs)):
 
1027
        for x in range(len(diffs)):
1028
1028
            (op, text) = diffs[x]
1029
1029
            if op != self.DIFF_INSERT:  # Equality or deletion.
1030
1030
                chars1 += len(text)
1146
1146
                # High ascii will raise UnicodeDecodeError.  Use Unicode
1147
1147
                # instead.
1148
1148
                data = data.encode('utf-8')
1149
 
                text.append('+' + urllib.quote(data, "!~*'();/?:@&=+$,# "))
 
1149
                text.append('+' + urllib.parse.quote(data, "!~*'();/?:@&=+$,# "))
1150
1150
            elif op == self.DIFF_DELETE:
1151
1151
                text.append('-%d' % len(data))
1152
1152
            elif op == self.DIFF_EQUAL:
1169
1169
          ValueError: If invalid input.
1170
1170
 
1171
1171
        """
1172
 
        if type(delta) == unicode:
 
1172
        if type(delta) == str:
1173
1173
            # Deltas should be composed of a subset of ascii chars, Unicode not
1174
1174
            # required.  If this encode raises UnicodeEncodeError, delta is
1175
1175
            # invalid.
1185
1185
            # operation of this token (delete, insert, equality).
1186
1186
            param = token[1:]
1187
1187
            if token[0] == '+':
1188
 
                param = urllib.unquote(param).decode('utf-8')
 
1188
                param = urllib.parse.unquote(param).decode('utf-8')
1189
1189
                diffs.append((self.DIFF_INSERT, param))
1190
1190
            elif token[0] == '-' or token[0] == '=':
1191
1191
                try:
1192
1192
                    n = int(param)
1193
1193
                except ValueError:
1194
 
                    raise ValueError, 'Invalid number in diff_fromDelta: ' + param
 
1194
                    raise ValueError('Invalid number in diff_fromDelta: ' + param)
1195
1195
                if n < 0:
1196
 
                    raise ValueError, 'Negative number in diff_fromDelta: ' + param
 
1196
                    raise ValueError('Negative number in diff_fromDelta: ' + param)
1197
1197
                text = text1[pointer: pointer + n]
1198
1198
                pointer += n
1199
1199
                if token[0] == '=':
1202
1202
                    diffs.append((self.DIFF_DELETE, text))
1203
1203
            else:
1204
1204
                # Anything else is an error.
1205
 
                raise ValueError, ('Invalid diff operation in diff_fromDelta: ' +
 
1205
                raise ValueError('Invalid diff operation in diff_fromDelta: ' +
1206
1206
                                   token[0])
1207
1207
        if pointer != len(text1):
1208
 
            raise ValueError, (
 
1208
            raise ValueError(
1209
1209
                'Delta length (%d) does not equal source text length (%d).' %
1210
1210
                (pointer, len(text1)))
1211
1211
        return diffs
1299
1299
        bin_max = len(pattern) + len(text)
1300
1300
        # Empty initialization added to appease pychecker.
1301
1301
        last_rd = None
1302
 
        for d in xrange(len(pattern)):
 
1302
        for d in range(len(pattern)):
1303
1303
            # Scan for the best match each iteration allows for one more error.
1304
1304
            # Run a binary search to determine how far from 'loc' we can stray at
1305
1305
            # this error level.
1317
1317
            start = max(1, loc - bin_mid + 1)
1318
1318
            finish = min(loc + bin_mid, len(text)) + len(pattern)
1319
1319
 
1320
 
            rd = range(finish + 1)
 
1320
            rd = list(range(finish + 1))
1321
1321
            rd.append((1 << d) - 1)
1322
 
            for j in xrange(finish, start - 1, -1):
 
1322
            for j in range(finish, start - 1, -1):
1323
1323
                if len(text) <= j - 1:
1324
1324
                    # Out of range.
1325
1325
                    charMatch = 0
1364
1364
        s = {}
1365
1365
        for char in pattern:
1366
1366
            s[char] = 0
1367
 
        for i in xrange(len(pattern)):
 
1367
        for i in range(len(pattern)):
1368
1368
            s[pattern[i]] |= 1 << (len(pattern) - i - 1)
1369
1369
        return s
1370
1370
 
1440
1440
        text1 = None
1441
1441
        diffs = None
1442
1442
        # Note that texts may arrive as 'str' or 'unicode'.
1443
 
        if isinstance(a, basestring) and isinstance(b, basestring) and c is None:
 
1443
        if isinstance(a, str) and isinstance(b, str) and c is None:
1444
1444
            # Method 1: text1, text2
1445
1445
            # Compute diffs from text1 and text2.
1446
1446
            text1 = a
1453
1453
            # Compute text1 from diffs.
1454
1454
            diffs = a
1455
1455
            text1 = self.diff_text1(diffs)
1456
 
        elif isinstance(a, basestring) and isinstance(b, list) and c is None:
 
1456
        elif isinstance(a, str) and isinstance(b, list) and c is None:
1457
1457
            # Method 3: text1, diffs
1458
1458
            text1 = a
1459
1459
            diffs = b
1460
 
        elif (isinstance(a, basestring) and isinstance(b, basestring) and
 
1460
        elif (isinstance(a, str) and isinstance(b, str) and
1461
1461
              isinstance(c, list)):
1462
1462
            # Method 4: text1, text2, diffs
1463
1463
            # text2 is not used.
1475
1475
        # Recreate the patches to determine context info.
1476
1476
        prepatch_text = text1
1477
1477
        postpatch_text = text1
1478
 
        for x in xrange(len(diffs)):
 
1478
        for x in range(len(diffs)):
1479
1479
            (diff_type, diff_text) = diffs[x]
1480
1480
            if len(patch.diffs) == 0 and diff_type != self.DIFF_EQUAL:
1481
1481
                # A new patch starts here.
1654
1654
        """
1655
1655
        paddingLength = self.Patch_Margin
1656
1656
        nullPadding = ''
1657
 
        for x in xrange(1, paddingLength + 1):
 
1657
        for x in range(1, paddingLength + 1):
1658
1658
            nullPadding += chr(x)
1659
1659
 
1660
1660
        # Bump all the patches forward.
1710
1710
        """
1711
1711
        if self.Match_MaxBits == 0:
1712
1712
            return
1713
 
        for x in xrange(len(patches)):
 
1713
        for x in range(len(patches)):
1714
1714
            if patches[x].length1 > self.Match_MaxBits:
1715
1715
                bigpatch = patches[x]
1716
1716
                # Remove the big old patch.
1817
1817
          ValueError: If invalid input.
1818
1818
 
1819
1819
        """
1820
 
        if type(textline) == unicode:
 
1820
        if type(textline) == str:
1821
1821
            # Patches should be composed of a subset of ascii chars, Unicode not
1822
1822
            # required.  If this encode raises UnicodeEncodeError, patch is
1823
1823
            # invalid.
1829
1829
        while len(text) != 0:
1830
1830
            m = re.match('^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$', text[0])
1831
1831
            if not m:
1832
 
                raise ValueError, 'Invalid patch string: ' + text[0]
 
1832
                raise ValueError('Invalid patch string: ' + text[0])
1833
1833
            patch = patch_obj()
1834
1834
            patches.append(patch)
1835
1835
            patch.start1 = int(m.group(1))
1859
1859
                    sign = text[0][0]
1860
1860
                else:
1861
1861
                    sign = ''
1862
 
                line = urllib.unquote(text[0][1:])
 
1862
                line = urllib.parse.unquote(text[0][1:])
1863
1863
                line = line.decode('utf-8')
1864
1864
                if sign == '+':
1865
1865
                    # Insertion.
1878
1878
                    pass
1879
1879
                else:
1880
1880
                    # WTF?
1881
 
                    raise ValueError, "Invalid patch mode: '%s'\n%s" % (sign, line)
 
1881
                    raise ValueError("Invalid patch mode: '%s'\n%s" % (sign, line))
1882
1882
                del text[0]
1883
1883
        return patches
1884
1884
 
1925
1925
                text.append(' ')
1926
1926
            # High ascii will raise UnicodeDecodeError.  Use Unicode instead.
1927
1927
            data = data.encode('utf-8')
1928
 
            text.append(urllib.quote(data, "!~*'();/?:@&=+$,# ") + '\n')
 
1928
            text.append(urllib.parse.quote(data, "!~*'();/?:@&=+$,# ") + '\n')
1929
1929
        return ''.join(text)