[damned-lies] Use io.open to open and decode po files for diff view



commit b1dd6f801e908beddaa33d0ac65138adc1ce6c20
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Dec 27 14:33:58 2014 +0100

    Use io.open to open and decode po files for diff view

 vertimus/views.py |   19 +++++++------------
 1 files changed, 7 insertions(+), 12 deletions(-)
---
diff --git a/vertimus/views.py b/vertimus/views.py
index f749a5e..44f2bad 100644
--- a/vertimus/views.py
+++ b/vertimus/views.py
@@ -19,6 +19,7 @@
 # along with this program; if not, see <http://www.gnu.org/licenses/>.
 
 import difflib
+import io
 import os
 
 from django.conf import settings
@@ -162,11 +163,6 @@ def vertimus_diff(request, action_id_1, action_id_2, level):
     if not os.path.exists(file_path_1):
         raise Http404("File not found")
 
-    try:
-        content_1 = [l.decode('utf-8') for l in open(file_path_1, 'U').readlines()]
-    except UnicodeDecodeError:
-        return render(request, 'error.html',
-                      {'error': _("Error: The file %s contains invalid characters.") % 
file_path_1.split('/')[-1]})
     descr_1 = _('<a href="%(url)s">Uploaded file</a> by %(name)s on %(date)s') % {
         'url': action_1.most_uptodate_file.url,
         'name': action_1.person.name,
@@ -212,14 +208,13 @@ def vertimus_diff(request, action_id_1, action_id_2, level):
             file_path_2 = stats.po_path(reduced=reduced)
     if not os.path.exists(file_path_2):
         raise Http404("File not found")
-    try:
-        content_2 = [l.decode('utf-8') for l in open(file_path_2, 'U').readlines()]
-    except UnicodeDecodeError:
-        return render(request, 'error.html',
-                      {'error': _("Error: The file %s contains invalid characters.") % 
file_path_2.split('/')[-1]})
+
     d = difflib.HtmlDiff(wrapcolumn=80)
-    diff_content = d.make_table(content_2, content_1,
-                                descr_2, descr_1, context=True)
+    with io.open(file_path_1, encoding='utf-8', errors='replace') as fh1, \
+            io.open(file_path_2, encoding='utf-8', errors='replace') as fh2:
+        diff_content = d.make_table(
+            fh2.readlines(), fh1.readlines(),
+             descr_2, descr_1, context=True)
 
     context = {
         'diff_content': diff_content,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]