[meld] Add minimal compat module for unicode/str compatibility
- From: Kai Willadsen <kaiw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [meld] Add minimal compat module for unicode/str compatibility
- Date: Fri, 30 Nov 2012 19:29:52 +0000 (UTC)
commit 33021688491a9329038525c53769125128efbf7a
Author: Kai Willadsen <kai willadsen gmail com>
Date: Sat Oct 27 12:26:32 2012 +1000
Add minimal compat module for unicode/str compatibility
meld/filediff.py | 17 +++++++++--------
meld/meldbuffer.py | 6 ++++--
meld/patchdialog.py | 5 +++--
meld/ui/historyentry.py | 4 +++-
meld/util/compat.py | 5 +++++
5 files changed, 24 insertions(+), 13 deletions(-)
---
diff --git a/meld/filediff.py b/meld/filediff.py
index 2bc0e6a..c13dbd4 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -45,6 +45,7 @@ from .ui import findbar
from .ui import gnomeglade
from .meldapp import app
+from .util.compat import text_type
from .util.sourceviewer import srcviewer
@@ -823,13 +824,13 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
self.undosequence.end_group()
def on_text_insert_text(self, buf, it, text, textlen):
- text = unicode(text, 'utf8')
+ text = text_type(text, 'utf8')
self.undosequence.add_action(
meldbuffer.BufferInsertionAction(buf, it.get_offset(), text))
buf.create_mark("insertion-start", it, True)
def on_text_delete_range(self, buf, it0, it1):
- text = unicode(buf.get_text(it0, it1, False), 'utf8')
+ text = text_type(buf.get_text(it0, it1, False), 'utf8')
assert self.deleted_lines_pending == -1
self.deleted_lines_pending = it1.get_line() - it0.get_line()
self.undosequence.add_action(
@@ -855,7 +856,7 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
buf = self.textbuffer[pane]
sel = buf.get_selection_bounds()
if sel:
- return unicode(buf.get_text(sel[0], sel[1], False), 'utf8')
+ return text_type(buf.get_text(sel[0], sel[1], False), 'utf8')
return None
def on_find_activate(self, *args):
@@ -1157,9 +1158,9 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
# We don't use self.buffer_texts here, as removing line
# breaks messes with inline highlighting in CRLF cases
text1 = bufs[0].get_text(starts[0], ends[0], False)
- text1 = unicode(text1, 'utf8')
+ text1 = text_type(text1, 'utf8')
textn = bufs[1].get_text(starts[1], ends[1], False)
- textn = unicode(textn, 'utf8')
+ textn = text_type(textn, 'utf8')
# For very long sequences, bail rather than trying a very slow comparison
inline_limit = 8000 # arbitrary constant
@@ -1379,7 +1380,7 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
else:
return False
start, end = buf.get_bounds()
- text = unicode(buf.get_text(start, end, False), 'utf8')
+ text = text_type(buf.get_text(start, end, False), 'utf8')
if bufdata.newlines:
if type(bufdata.newlines) == type(""):
if(bufdata.newlines) != '\n':
@@ -1643,7 +1644,7 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
b0, b1 = self.textbuffer[src], self.textbuffer[dst]
start = b0.get_iter_at_line_or_eof(chunk[1])
end = b0.get_iter_at_line_or_eof(chunk[2])
- t0 = unicode(b0.get_text(start, end, False), 'utf8')
+ t0 = text_type(b0.get_text(start, end, False), 'utf8')
if copy_up:
if chunk[2] >= b0.get_line_count() and \
@@ -1673,7 +1674,7 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
src_end = b0.get_iter_at_line_or_eof(chunk[2])
dst_start = b1.get_iter_at_line_or_eof(chunk[3])
dst_end = b1.get_iter_at_line_or_eof(chunk[4])
- t0 = unicode(b0.get_text(src_start, src_end, False), 'utf8')
+ t0 = text_type(b0.get_text(src_start, src_end, False), 'utf8')
mark0 = b1.create_mark(None, dst_start, True)
self.on_textbuffer__begin_user_action()
b1.delete(dst_start, dst_end)
diff --git a/meld/meldbuffer.py b/meld/meldbuffer.py
index 286e727..e23c817 100644
--- a/meld/meldbuffer.py
+++ b/meld/meldbuffer.py
@@ -23,6 +23,8 @@ from gettext import gettext as _
from .util import sourceviewer
+from .util.compat import text_type
+
class MeldBuffer(sourceviewer.srcviewer.GtkTextBuffer):
@@ -115,7 +117,7 @@ class BufferLines(object):
# this will return the last line.
start = self.buf.get_iter_at_line_or_eof(lo)
end = self.buf.get_iter_at_line_or_eof(hi)
- txt = unicode(self.buf.get_text(start, end, False), 'utf8')
+ txt = text_type(self.buf.get_text(start, end, False), 'utf8')
filter_txt = self.textfilter(txt)
lines = filter_txt.splitlines()
@@ -158,7 +160,7 @@ class BufferLines(object):
if not line_end.ends_line():
line_end.forward_to_line_end()
txt = self.buf.get_text(line_start, line_end, False)
- return unicode(self.textfilter(txt), 'utf8')
+ return text_type(self.textfilter(txt), 'utf8')
def __len__(self):
return self.buf.get_line_count()
diff --git a/meld/patchdialog.py b/meld/patchdialog.py
index c3e81c3..3f474bd 100644
--- a/meld/patchdialog.py
+++ b/meld/patchdialog.py
@@ -26,6 +26,7 @@ import pango
from . import paths
from .ui import gnomeglade
+from .util.compat import text_type
from .util.sourceviewer import srcviewer
@@ -84,7 +85,7 @@ class PatchDialog(gnomeglade.Component):
texts = []
for b in self.filediff.textbuffer:
start, end = b.get_bounds()
- text = unicode(b.get_text(start, end, False), 'utf8')
+ text = text_type(b.get_text(start, end, False), 'utf8')
lines = text.splitlines(True)
texts.append(lines)
@@ -108,7 +109,7 @@ class PatchDialog(gnomeglade.Component):
buf = self.textview.get_buffer()
start, end = buf.get_bounds()
- txt = unicode(buf.get_text(start, end, False), 'utf8')
+ txt = text_type(buf.get_text(start, end, False), 'utf8')
# Copy patch to clipboard
if result == 1:
diff --git a/meld/ui/historyentry.py b/meld/ui/historyentry.py
index 22e4213..36a8c4c 100644
--- a/meld/ui/historyentry.py
+++ b/meld/ui/historyentry.py
@@ -25,6 +25,8 @@ import atk
# gconf is also imported; see end of HistoryEntry class for details
from gettext import gettext as _
+from ..util.compat import text_type
+
# This file is a Python translation of:
# * gedit/gedit/gedit-history-entry.c
# * libgnomeui/libgnomeui/gnome-file-entry.c
@@ -397,7 +399,7 @@ class HistoryFileEntry(gtk.HBox, gtk.Editable):
encoding = sys.getfilesystemencoding()
if encoding:
- filename = unicode(filename, encoding)
+ filename = text_type(filename, encoding)
entry = self.__gentry.get_entry()
entry.set_text(filename)
self._set_last_open(filename)
diff --git a/meld/util/compat.py b/meld/util/compat.py
new file mode 100644
index 0000000..bd580f0
--- /dev/null
+++ b/meld/util/compat.py
@@ -0,0 +1,5 @@
+
+import sys
+
+# Python 2/3 compatibility, named as per six
+text_type = str if sys.version_info[0] == 3 else unicode
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]