Back to index

moin  1.9.0~rc2
Functions | Variables
MoinMoin.util.bdiff Namespace Reference

Functions

def compress
def decompress
def diff
def textdiff
def patchtext
def patch
def test

Variables

string BDIFF_PATT = ">lll"
tuple BDIFF_PATT_SIZE = struct.calcsize(BDIFF_PATT)

Detailed Description

    MoinMoin - Binary patching and diffing

    @copyright: 2005 Matt Mackall <mpm@selenic.com>,
        2006 MoinMoin:AlexanderSchremmer

    Algorithm taken from mercurial's mdiff.py

    @license: GNU GPL, see COPYING for details.

Function Documentation

Definition at line 17 of file bdiff.py.

00017 
00018 def compress(text):
00019     return zlib.compress(text) # here we could tune the compression level

Here is the caller graph for this function:

Definition at line 20 of file bdiff.py.

00020 
00021 def decompress(bin):
00022     return zlib.decompress(bin)

Here is the caller graph for this function:

def MoinMoin.util.bdiff.diff (   a,
  b 
)
Generates a binary diff of the passed strings.
    Note that you can pass arrays of strings as well.
    This might give you better results for text files. 

Definition at line 23 of file bdiff.py.

00023 
00024 def diff(a, b):
00025     """ Generates a binary diff of the passed strings.
00026         Note that you can pass arrays of strings as well.
00027         This might give you better results for text files. """
00028     if not a:
00029         s = "".join(b)
00030         return s and (struct.pack(BDIFF_PATT, 0, 0, len(s)) + s)
00031 
00032     bin = []
00033     la = lb = 0
00034 
00035     p = [0]
00036     for i in a: p.append(p[-1] + len(i))
00037 
00038     for am, bm, size in difflib.SequenceMatcher(None, a, b).get_matching_blocks():
00039         s = "".join(b[lb:bm])
00040         if am > la or s:
00041             bin.append(struct.pack(BDIFF_PATT, p[la], p[am], len(s)) + s)
00042         la = am + size
00043         lb = bm + size
00044 
00045     return "".join(bin)

Here is the caller graph for this function:

def MoinMoin.util.bdiff.patch (   a,
  bin 
)
Patches the string a with the binary patch bin. 

Definition at line 61 of file bdiff.py.

00061 
00062 def patch(a, bin):
00063     """ Patches the string a with the binary patch bin. """
00064     c = last = pos = 0
00065     r = []
00066 
00067     while pos < len(bin):
00068         p1, p2, l = struct.unpack(BDIFF_PATT, bin[pos:pos + BDIFF_PATT_SIZE])
00069         pos += BDIFF_PATT_SIZE
00070         r.append(a[last:p1])
00071         r.append(bin[pos:pos + l])
00072         pos += l
00073         last = p2
00074         c += 1
00075     r.append(a[last:])
00076 
00077     return "".join(r)

Here is the caller graph for this function:

Returns the new hunks that are contained in a binary diff.

Definition at line 50 of file bdiff.py.

00050 
00051 def patchtext(bin):
00052     """ Returns the new hunks that are contained in a binary diff."""
00053     pos = 0
00054     t = []
00055     while pos < len(bin):
00056         p1, p2, l = struct.unpack(BDIFF_PATT, bin[pos:pos + BDIFF_PATT_SIZE])
00057         pos += BDIFF_PATT_SIZE
00058         t.append(bin[pos:pos + l])
00059         pos += l
00060     return "".join(t)

Here is the caller graph for this function:

Definition at line 78 of file bdiff.py.

00078 
00079 def test():
00080     a = ("foo\n" * 30)
00081     b = ("  fao" * 30)
00082 
00083     a = file(r"test.1").read()
00084     b = file(r"test.2").read()
00085     a = a.splitlines(1)
00086     b = b.splitlines(1)
00087 
00088     d = diff(a, b)
00089     z = compress(d)
00090     print repr(patchtext(d))
00091     print repr(d)
00092     print "".join(b) == patch("".join(a), d)
00093     print len(d), len(z)
00094 

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.util.bdiff.textdiff (   a,
  b 
)
A diff function optimised for text files. Works with binary files as well. 

Definition at line 46 of file bdiff.py.

00046 
00047 def textdiff(a, b):
00048     """ A diff function optimised for text files. Works with binary files as well. """
00049     return diff(a.splitlines(1), b.splitlines(1))

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 14 of file bdiff.py.

Definition at line 15 of file bdiff.py.