changeset 2182:827d351c7c96

_get_data_diff_text method for different Content types
author Ana Balica <ana.balica@gmail.com>
date Sat, 07 Sep 2013 21:36:43 +0300
parents 0cfaeae59452
children aaefa8cfa989
files MoinMoin/items/_tests/test_Content.py MoinMoin/items/content.py
diffstat 2 files changed, 44 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/items/_tests/test_Content.py	Tue Sep 03 23:50:11 2013 +0300
+++ b/MoinMoin/items/_tests/test_Content.py	Sat Sep 07 21:36:43 2013 +0300
@@ -8,6 +8,7 @@
 """
 
 import pytest
+from io import BytesIO
 
 from flask import Markup
 
@@ -258,5 +259,27 @@
         assert u'<pre class="highlight">test_data\n' in result
         assert item2.content.data == ''
 
+    def test__get_diff_text(self):
+        item_name = u'Text_Item'
+        item = Item.create(item_name)
+        contenttypes = dict(texttypes=[u'text/plain;charset=utf-8',
+                                       u'text/x-markdown;charset=utf-8', ],
+                            othertypes=[u'image/png', u'audio/wave',
+                                        u'video/ogg',
+                                        u'application/x-svgdraw',
+                                        u'application/octet-stream', ])
+        for key in contenttypes:
+            for contenttype in contenttypes[key]:
+                meta = {CONTENTTYPE: contenttype}
+                item._save(meta)
+                item_ = Item.create(item_name)
+                oldfile = BytesIO("x")
+                newfile = BytesIO("xx")
+                difflines = item_.content._get_data_diff_text(oldfile, newfile)
+                if key == 'texttypes':
+                    assert difflines == ['- x', '+ xx']
+                else:
+                    assert difflines == []
+
 
 coverage_modules = ['MoinMoin.items.content']
--- a/MoinMoin/items/content.py	Tue Sep 03 23:50:11 2013 +0300
+++ b/MoinMoin/items/content.py	Sat Sep 07 21:36:43 2013 +0300
@@ -60,6 +60,7 @@
 from MoinMoin.util.mime import Type, type_moin_document
 from MoinMoin.util.tree import moin_page, html, xlink, docbook
 from MoinMoin.util.iri import Iri
+from MoinMoin.util.diff_text import diff
 from MoinMoin.util.crypto import cache_key
 from MoinMoin.util.clock import timed
 from MoinMoin.forms import File
@@ -270,6 +271,15 @@
         # override this in child classes
         return ''
 
+    def _get_data_diff_text(self, oldfile, newfile):
+        """ Get the text diff of 2 versions of file contents
+
+        :param oldfile: file that contains old content data (bytes)
+        :param newfile: file that contains new content data (bytes)
+        :return: list of diff lines in a unified format without trailing linefeeds
+        """
+        return []
+
     def get_templates(self, contenttype=None):
         """ create a list of templates (for some specific contenttype) """
         terms = [Term(WIKINAME, app.cfg.interwikiname), Term(TAGS, u'template')]
@@ -852,6 +862,17 @@
                                diffs=diffs,
                                )
 
+    def _get_data_diff_text(self, oldfile, newfile):
+        """ Get the text diff of 2 versions of file contents
+
+        :param oldfile: file that contains old content data (bytes)
+        :param newfile: file that contains new content data (bytes)
+        :return: list of diff lines in a unified format without trailing linefeeds
+        """
+        old_text = self.data_storage_to_internal(oldfile.read())
+        new_text = self.data_storage_to_internal(newfile.read())
+        return diff(old_text.splitlines(), new_text.splitlines())
+
     def _render_data_diff_atom(self, oldrev, newrev):
         """ renders diff in HTML for atom feed """
         return self._get_data_diff_html(oldrev, newrev, 'diff_text_atom.html')