[extensions-web] Python 3 migration



commit 89908e1b987e233acce595166355202a3ac8a34a
Author: Claude Paroz <claude 2xlibre net>
Date:   Mon Sep 24 17:25:48 2018 +0200

    Python 3 migration

 sweettooth/auth/forms.py                          |  6 +++---
 sweettooth/auth/tests.py                          |  2 +-
 sweettooth/errorreports/forms.py                  |  2 +-
 sweettooth/exceptions.py                          |  2 +-
 sweettooth/extensions/models.py                   | 24 ++++++++++-----------
 sweettooth/extensions/search.py                   |  2 +-
 sweettooth/extensions/tests.py                    | 26 +++++++++--------------
 sweettooth/extensions/views.py                    | 14 ++++++------
 sweettooth/review/diffutils.py                    | 20 ++++++++---------
 sweettooth/review/tests/tests.py                  |  2 +-
 sweettooth/templates/templatetags/static_paths.py |  4 ++--
 sweettooth/testutils.py                           |  2 +-
 sweettooth/utils.py                               |  6 +++---
 13 files changed, 53 insertions(+), 59 deletions(-)
---
diff --git a/sweettooth/auth/forms.py b/sweettooth/auth/forms.py
index 32300e9..0f05813 100644
--- a/sweettooth/auth/forms.py
+++ b/sweettooth/auth/forms.py
@@ -15,15 +15,15 @@ class PlainOutputForm(object):
 
 class AutoFocusForm(object):
     def __init__(self, *a, **kw):
-        super(AutoFocusForm, self).__init__(*a, **kw)
+        super().__init__(*a, **kw)
         for field in self.fields:
             self.fields[field].widget.attrs['autofocus'] = 'autofocus'
             break
 
 class InlineForm(object):
     def __init__(self, *a, **kw):
-        super(InlineForm, self).__init__(*a, **kw)
-        for field in self.fields.itervalues():
+        super().__init__(*a, **kw)
+        for field in self.fields.values():
             field.widget.attrs['placeholder'] = field.label
             field.widget.attrs['class'] = 'form-control'
 
diff --git a/sweettooth/auth/tests.py b/sweettooth/auth/tests.py
index 4e91eb0..816fbf9 100644
--- a/sweettooth/auth/tests.py
+++ b/sweettooth/auth/tests.py
@@ -1,9 +1,9 @@
 from registration import validators
 from registration.tests.base import RegistrationTestCase
 
-from forms import AutoFocusRegistrationForm
 from django.contrib.auth import get_user_model
 from django.utils.six import text_type
+from .forms import AutoFocusRegistrationForm
 
 User = get_user_model()
 
diff --git a/sweettooth/errorreports/forms.py b/sweettooth/errorreports/forms.py
index 3097a72..d16b89c 100644
--- a/sweettooth/errorreports/forms.py
+++ b/sweettooth/errorreports/forms.py
@@ -15,7 +15,7 @@ class ErrorReportForm(forms.ModelForm):
         return self.cleaned_data['comment'].strip()
 
     def save(self, request, extension, commit=True):
-        report = super(ErrorReportForm, self).save(commit=False)
+        report = super().save(commit=False)
         report.user = request.user
         report.extension = extension
         if commit:
diff --git a/sweettooth/exceptions.py b/sweettooth/exceptions.py
index 9ab5286..6a5b8fd 100644
--- a/sweettooth/exceptions.py
+++ b/sweettooth/exceptions.py
@@ -3,5 +3,5 @@ from django.db import DatabaseError
 
 class DatabaseErrorWithMessages(DatabaseError):
     def __init__(self, messages = None):
-        super(DatabaseErrorWithMessages, self).__init__()
+        super().__init__()
         self.messages = messages if messages is not None else []
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index ed4d452..dd37751 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -15,7 +15,7 @@ import re
  STATUS_REJECTED,
  STATUS_INACTIVE,
  STATUS_ACTIVE,
- STATUS_WAITING) = xrange(5)
+ STATUS_WAITING) = range(5)
 
 STATUSES = {
     STATUS_UNREVIEWED: u"Unreviewed",
@@ -57,7 +57,7 @@ def build_shell_version_map(versions):
             if version.version > shell_version_map[key].version:
                 shell_version_map[key] = version
 
-    for key, version in shell_version_map.iteritems():
+    for key, version in shell_version_map.items():
         shell_version_map[key] = dict(pk = version.pk,
                                       version = version.version)
 
@@ -109,7 +109,7 @@ class Extension(models.Model):
 
     objects = ExtensionManager()
 
-    def __unicode__(self):
+    def __str__(self):
         return self.uuid
 
     def parse_metadata_json(self, metadata):
@@ -125,13 +125,13 @@ class Extension(models.Model):
             raise ValidationError("Your extension has an invalid UUID")
 
     def save(self, replace_metadata_json=True, *args, **kwargs):
-        super(Extension, self).save(*args, **kwargs)
+        super().save(*args, **kwargs)
         if replace_metadata_json:
             for version in self.versions.all():
                 if version.source:
                     try:
                         version.replace_metadata_json()
-                    except BadZipfile, e:
+                    except BadZipfile:
                         # Ignore bad zipfiles, we don't care
                         pass
 
@@ -187,14 +187,14 @@ def parse_version_string(version_string):
     try:
         major, minor = version[:2]
         major, minor = int(major), int(minor)
-    except ValueError, e:
+    except ValueError:
         raise InvalidShellVersion()
 
     if len(version) in (3, 4):
         # 3.0.1, 3.1.4
         try:
             point = int(version[2])
-        except ValueError, e:
+        except ValueError:
             raise InvalidShellVersion()
 
     elif len(version) == 2 and minor % 2 == 0:
@@ -232,7 +232,7 @@ class ShellVersion(models.Model):
 
     objects = ShellVersionManager()
 
-    def __unicode__(self):
+    def __str__(self):
         return self.version_string
 
     @property
@@ -265,7 +265,7 @@ def parse_zipfile_metadata(uploaded_file):
         raise InvalidExtensionData("Zip file is too large")
 
     try:
-        metadata = json.load(zipfile.open('metadata.json', 'r'))
+        metadata = json.loads(zipfile.open('metadata.json', 'r'))
     except KeyError:
         # no metadata.json in archive, raise error
         raise InvalidExtensionData("Missing metadata.json")
@@ -305,7 +305,7 @@ class ExtensionVersion(models.Model):
         unique_together = ('extension', 'version'),
         get_latest_by = 'version'
 
-    def __unicode__(self):
+    def __str__(self):
         return "Version %d of %s" % (self.version, self.extension)
 
     source = models.FileField(upload_to=make_filename,
@@ -364,7 +364,7 @@ class ExtensionVersion(models.Model):
             filemap[info] = contents
 
         zipfile = self.get_zipfile("w")
-        for info, contents in filemap.iteritems():
+        for info, contents in filemap.items():
             zipfile.writestr(info, contents)
 
         metadata = self.make_metadata_json()
@@ -383,7 +383,7 @@ class ExtensionVersion(models.Model):
             except self.DoesNotExist:
                 self.version = 1
 
-        super(ExtensionVersion, self).save(*args, **kwargs)
+        super().save(*args, **kwargs)
 
     def parse_metadata_json(self, metadata):
         """
diff --git a/sweettooth/extensions/search.py b/sweettooth/extensions/search.py
index 4bc919e..2ee07c7 100644
--- a/sweettooth/extensions/search.py
+++ b/sweettooth/extensions/search.py
@@ -27,7 +27,7 @@ def index_extension(extension):
 
     idterm = "Q%s" % (extension.pk,)
     doc.add_boolean_term(idterm)
-    for shell_version in extension.visible_shell_version_map.iterkeys():
+    for shell_version in extension.visible_shell_version_map.keys():
         doc.add_boolean_term("V%s" % (shell_version,))
 
     db.replace_document(idterm, doc)
diff --git a/sweettooth/extensions/tests.py b/sweettooth/extensions/tests.py
index 9fcc920..5f98bf6 100644
--- a/sweettooth/extensions/tests.py
+++ b/sweettooth/extensions/tests.py
@@ -3,14 +3,10 @@ import os.path
 import json
 import tempfile
 import unittest
+from io import BytesIO
 from uuid import uuid4
 from zipfile import ZipFile
 
-try:
-    from cStringIO import StringIO
-except ImportError:
-    from StringIO import StringIO
-
 from django.test import TestCase, TransactionTestCase
 from django.core.files.base import File
 from django.core.urlresolvers import reverse
@@ -36,7 +32,7 @@ class UUIDPolicyTest(TestCase):
         self.assertTrue(models.validate_uuid("foo-3 mecheye net"))
         self.assertTrue(models.validate_uuid("Foo4 mecheye net"))
 
-        for i in xrange(10):
+        for i in range(10):
             self.assertTrue(models.validate_uuid(str(uuid4())))
 
         self.assertFalse(models.validate_uuid("<Wonderful>"))
@@ -124,23 +120,21 @@ class ParseZipfileTest(BasicUserTestCase, TestCase):
         self.assertTrue("url" not in extra)
 
     def test_bad_zipfile_metadata(self):
-        bad_data = StringIO("deadbeef")
+        bad_data = BytesIO(b"deadbeef")
         self.assertRaises(models.InvalidExtensionData, models.parse_zipfile_metadata, bad_data)
 
         with get_test_zipfile('TooLarge') as f:
-            with self.assertRaises(models.InvalidExtensionData) as cm:
+            with self.assertRaisesMessage(models.InvalidExtensionData, "Zip file is too large"):
                 models.parse_zipfile_metadata(f)
-            self.assertEqual(cm.exception.message, "Zip file is too large")
 
         with get_test_zipfile('NoMetadata') as f:
-            with self.assertRaises(models.InvalidExtensionData) as cm:
+            with self.assertRaisesMessage(models.InvalidExtensionData, "Missing metadata.json"):
                 models.parse_zipfile_metadata(f)
-            self.assertEqual(cm.exception.message, "Missing metadata.json")
 
         with get_test_zipfile('BadMetadata') as f:
-            with self.assertRaises(models.InvalidExtensionData) as cm:
+            with self.assertRaisesMessage(models.InvalidExtensionData, "Invalid JSON data"):
                 models.parse_zipfile_metadata(f)
-            self.assertEqual(cm.exception.message, "Invalid JSON data")
+
 
 class ReplaceMetadataTest(BasicUserTestCase, TestCase):
     @unittest.expectedFailure
@@ -469,14 +463,14 @@ class UpdateVersionTest(TestCase):
         reject_uuid: u'blacklist'}
 
     def build_response(self, installed):
-        return dict((k, dict(version=v)) for k, v in installed.iteritems())
+        return dict((k, dict(version=v)) for k, v in installed.items())
 
     def grab_response(self, installed):
         installed = self.build_response(installed)
         response = self.client.get(reverse('extensions-shell-update'),
                                    dict(installed=json.dumps(installed), shell_version='3.2.0'))
 
-        return json.loads(response.content)
+        return json.loads(response.content.decode('utf-8'))
 
     def test_upgrade_me(self):
         uuid = self.upgrade_uuid
@@ -543,7 +537,7 @@ class UpdateVersionTest(TestCase):
 class QueryExtensionsTest(BasicUserTestCase, TestCase):
     def get_response(self, params):
         response = self.client.get(reverse('extensions-query'), params)
-        return json.loads(response.content)
+        return json.loads(response.content.decode('utf-8'))
 
     def gather_uuids(self, params):
         if 'sort' not in params:
diff --git a/sweettooth/extensions/views.py b/sweettooth/extensions/views.py
index 1c54444..a7c15da 100644
--- a/sweettooth/extensions/views.py
+++ b/sweettooth/extensions/views.py
@@ -59,8 +59,8 @@ def grab_proper_extension_version(extension, shell_version, disable_version_vali
         supported_shell_versions = sorted(supported_shell_versions, key=lambda x: (x.major, x.minor, 
x.point))
         requested_shell_version = models.parse_version_string(shell_version)
 
-        if cmp((supported_shell_versions[0].major, supported_shell_versions[0].minor,
-                supported_shell_versions[0].point), requested_shell_version) > 0:
+        if (supported_shell_versions[0].major, supported_shell_versions[0].minor,
+                supported_shell_versions[0].point) > requested_shell_version:
             versions = visible_versions.filter(shell_versions=supported_shell_versions[0])
         else:
             versions = visible_versions.filter(shell_versions=supported_shell_versions[-1])
@@ -116,7 +116,7 @@ def shell_update(request):
 
     operations = {}
 
-    for uuid, meta in installed.iteritems():
+    for uuid, meta in installed.items():
         try:
             version = int(meta['version'])
         except (KeyError, TypeError):
@@ -237,7 +237,7 @@ def ajax_query_view(request):
             return redirect((settings.STATIC_URL + "extensions.json"), permanent=True)
 
         n_per_page = min(n_per_page, 25)
-    except (KeyError, ValueError), e:
+    except (KeyError, ValueError):
         n_per_page = 10
 
     version_strings = request.GET.getlist('shell_version')
@@ -417,7 +417,7 @@ def create_version(request, file_source):
             try:
                 metadata = models.parse_zipfile_metadata(file_source)
                 uuid = metadata['uuid']
-            except (models.InvalidExtensionData, KeyError), e:
+            except (models.InvalidExtensionData, KeyError) as e:
                 messages.error(request, "Invalid extension data: %s" % (e.message,))
                 raise DatabaseErrorWithMessages
 
@@ -435,7 +435,7 @@ def create_version(request, file_source):
 
             try:
                 extension.full_clean()
-            except ValidationError, e:
+            except ValidationError as e:
                 raise DatabaseErrorWithMessages(e.messages)
 
             version = models.ExtensionVersion.objects.create(extension=extension,
@@ -446,7 +446,7 @@ def create_version(request, file_source):
             version.save()
 
             return version, []
-    except DatabaseErrorWithMessages, e:
+    except DatabaseErrorWithMessages as e:
         return None, e.messages
 
 @login_required
diff --git a/sweettooth/review/diffutils.py b/sweettooth/review/diffutils.py
index a56a4b1..f87677d 100644
--- a/sweettooth/review/diffutils.py
+++ b/sweettooth/review/diffutils.py
@@ -4,7 +4,7 @@
 # Copyright 2011 Review Board Team
 
 import re
-from itertools import izip_longest
+from itertools import zip_longest
 from difflib import SequenceMatcher
 
 class MyersDiffer:
@@ -237,7 +237,7 @@ class MyersDiffer:
                 down_max -= 1
 
             # Extend the forward path
-            for k in xrange(down_max, down_min - 1, -2):
+            for k in range(down_max, down_min - 1, -2):
                 tlo = down_vector[self.downoff + k - 1]
                 thi = down_vector[self.downoff + k + 1]
 
@@ -278,7 +278,7 @@ class MyersDiffer:
             else:
                 up_max -= 1
 
-            for k in xrange(up_max, up_min - 1, -2):
+            for k in range(up_max, up_min - 1, -2):
                 tlo = up_vector[self.upoff + k - 1]
                 thi = up_vector[self.upoff + k + 1]
 
@@ -352,7 +352,7 @@ class MyersDiffer:
     def _find_diagonal(self, minimum, maximum, k, best, diagoff, vector,
                        vdiff_func, check_x_range, check_y_range,
                        discard_index, k_offset, cost):
-        for d in xrange(maximum, minimum - 1, -2):
+        for d in range(maximum, minimum - 1, -2):
             dd = d - k
             x = vector[diagoff + d]
             y = x - d
@@ -526,7 +526,7 @@ class MyersDiffer:
         def scan_run(discards, i, length, index_func):
             consec = 0
 
-            for j in xrange(length):
+            for j in range(length):
                 index = index_func(i, j)
                 discard = discards[index]
 
@@ -557,7 +557,7 @@ class MyersDiffer:
 
                     # Find the end of this run of discardable lines and count
                     # how many are provisionally discardable.
-                    #for j in xrange(i, data.length):
+                    #for j in range(i, data.length):
                     j = i
                     while j < data.length:
                         if discards[j] == self.DISCARD_NONE:
@@ -708,7 +708,7 @@ def new_chunk(lines, collapsable=False, tag='equal'):
     }
 
 def get_fake_chunk(numlines, tag):
-    lines = [new_line(oldindex=n, newindex=n) for n in xrange(numlines)]
+    lines = [new_line(oldindex=n, newindex=n) for n in range(numlines)]
     return new_chunk(lines, tag=tag)
 
 def get_linenum(idx):
@@ -757,10 +757,10 @@ def get_chunks(a, b):
     for tag, i1, i2, j1, j2 in differ.get_opcodes():
         numlines = max(i2-i1, j2-j1)
 
-        oldlines = zip(xrange(i1, i2), a[i1:i2])
-        newlines = zip(xrange(j1, j2), b[j1:j2])
+        oldlines = zip(range(i1, i2), a[i1:i2])
+        newlines = zip(range(j1, j2), b[j1:j2])
 
-        lines = [diff_line(old, new) for old, new in izip_longest(oldlines, newlines, fillvalue=(None, 
None))]
+        lines = [diff_line(old, new) for old, new in zip_longest(oldlines, newlines, fillvalue=(None, None))]
 
         if tag == 'equal' and numlines > collapse_threshold:
             last_range_start = numlines - context_num_lines
diff --git a/sweettooth/review/tests/tests.py b/sweettooth/review/tests/tests.py
index d1b4556..2a851bc 100644
--- a/sweettooth/review/tests/tests.py
+++ b/sweettooth/review/tests/tests.py
@@ -7,7 +7,7 @@ from sweettooth.review.views import get_old_version, should_auto_approve_changes
 
 from sweettooth.testutils import BasicUserTestCase
 
-from tests_diff import DiffTest
+from .tests_diff import DiffTest
 
 class DiffViewTest(BasicUserTestCase, TestCase):
     def test_get_zipfiles(self):
diff --git a/sweettooth/templates/templatetags/static_paths.py 
b/sweettooth/templates/templatetags/static_paths.py
index 608475f..1298305 100644
--- a/sweettooth/templates/templatetags/static_paths.py
+++ b/sweettooth/templates/templatetags/static_paths.py
@@ -28,7 +28,7 @@ def static_js_paths():
         if js_paths is None:
             js_paths = {};
 
-            for base_file, hashed_file in staticfiles_storage.hashed_files.iteritems():
+            for base_file, hashed_file in staticfiles_storage.hashed_files.items():
                 if base_file.endswith('.js') and base_file.startswith('js/'):
                     js_paths[base_file[3:-3]] = hashed_file[3:-3]
 
@@ -48,7 +48,7 @@ def static_img_paths():
         if img_paths is None:
             img_paths = {};
 
-            for base_file, hashed_file in staticfiles_storage.hashed_files.iteritems():
+            for base_file, hashed_file in staticfiles_storage.hashed_files.items():
                 if base_file.startswith('images/'):
                     img_paths[base_file] = hashed_file
 
diff --git a/sweettooth/testutils.py b/sweettooth/testutils.py
index 57dc2c8..af298ff 100644
--- a/sweettooth/testutils.py
+++ b/sweettooth/testutils.py
@@ -3,7 +3,7 @@ from django.contrib.auth.models import User
 
 class BasicUserTestCase(object):
     def setUp(self):
-        super(BasicUserTestCase, self).setUp()
+        super().setUp()
         self.username = 'TestUser1'
         self.email = 'non-existant non-existant tld'
         self.password = 'a random password'
diff --git a/sweettooth/utils.py b/sweettooth/utils.py
index b97b03e..d2c414d 100644
--- a/sweettooth/utils.py
+++ b/sweettooth/utils.py
@@ -1,10 +1,10 @@
 
-import urllib
 import hashlib
+from urllib.parse import urlencode
 
 GRAVATAR_BASE = "https://secure.gravatar.com/avatar/%s?%s";
 
 def gravatar_url(request, email, size=70):
-    email_md5 = hashlib.md5(email.lower()).hexdigest()
-    options = urllib.urlencode(dict(d="mm", s=size))
+    email_md5 = hashlib.md5(email.lower().encode('utf-8')).hexdigest()
+    options = urlencode({'d': "mm", 's': size})
     return GRAVATAR_BASE % (email_md5, options)


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