[PATCH 3 of 5 seo] Fix py3 compatibility

Philippe Pepiot philippe.pepiot at logilab.fr
Thu Mar 5 14:54:30 CET 2020


# HG changeset patch
# User Philippe Pepiot <philippe.pepiot at logilab.fr>
# Date 1583408465 -3600
#      Thu Mar 05 12:41:05 2020 +0100
# Node ID 6e22a270b9a20e51a3b607470f6173ee1e556b37
# Parent  0b8e81f446086c0a6bc21a6fe56e73f5c5bad997
Fix py3 compatibility

Make various encoding fixes and rename urllib2 import to urllib

diff --git a/cubicweb_seo/views.py b/cubicweb_seo/views.py
--- a/cubicweb_seo/views.py
+++ b/cubicweb_seo/views.py
@@ -17,12 +17,12 @@
 
 """cubicweb-seo views/forms/actions/components for web ui"""
 
-from cStringIO import StringIO
+from io import StringIO
 from datetime import date
 import gzip
 import os
 import tempfile
-import urllib2
+import urllib.parse
 import zipfile
 
 from cubicweb import Unauthorized
@@ -199,7 +199,7 @@ class GzippedSitemap(Sitemaps):
 
     def add_url(self, buf, url, lastmod=None, chfreq=None, priority=None):
         buf.write(u'<url>')
-        buf.write(u'<loc>{0}</loc>'.format(urllib2.quote(url.encode('utf-8'), safe=':/')))
+        buf.write(u'<loc>{0}</loc>'.format(urllib.parse.quote(url.encode('utf-8'), safe=':/')))
         if lastmod:
             buf.write(u'<lastmod>{0}</lastmod>'.format(lastmod.strftime('%Y-%m-%d')))
         if chfreq:
@@ -242,11 +242,11 @@ class GzippedSitemap(Sitemaps):
         buf.write(u'</urlset>')
         with tempfile.NamedTemporaryFile(delete=False) as f:
             with gzip.open(f.name, mode='wb') as gz:
-                gz.write(buf.getvalue())
+                gz.write(buf.getvalue().encode('utf-8'))
         self._cw.set_content_type(self.content_type,
                                   filename='sitemap.xml.gz',
                                   disposition='attachment')
-        with open(f.name) as gz:
+        with open(f.name, 'rb') as gz:
             self.w(gz.read())
         os.remove(f.name)
 
@@ -282,7 +282,7 @@ class ZippedMultipleSitemaps(GzippedSite
         ``index_buf``."""
         sitemap_buf.write(u'</urlset>')
         with gzip.open(fname, mode='wb') as gz:
-            gz.write(sitemap_buf.getvalue())
+            gz.write(sitemap_buf.getvalue().encode('utf-8'))
         index_buf.write('<sitemap>')
         index_buf.write('<loc>{0}</loc>'.format(self._cw.build_url(os.path.basename(fname))))
         index_buf.write('<lastmod>{0}</lastmod>'.format(date.today().strftime('%y-%m-%d')))
@@ -293,7 +293,7 @@ class ZippedMultipleSitemaps(GzippedSite
         ``index_buf``."""
         index_buf.write(u'</sitemapindex>')
         with gzip.open(index_fname, mode='wb') as gz:
-            gz.write(index_buf.getvalue())
+            gz.write(index_buf.getvalue().encode('utf-8'))
 
     def call(self):
         index_fname = os.path.join(tempfile.gettempdir(), 'sitemap.xml.gz')
@@ -331,7 +331,7 @@ class ZippedMultipleSitemaps(GzippedSite
         self._cw.set_content_type(self.content_type,
                                   filename='sitemaps.zip',
                                   disposition='attachment')
-        with open(f.name) as zf:
+        with open(f.name, 'rb') as zf:
             self.w(zf.read())
         os.remove(f.name)
         for i in range(1, file_number + 1):
diff --git a/test/unittest_views.py b/test/unittest_views.py
--- a/test/unittest_views.py
+++ b/test/unittest_views.py
@@ -16,7 +16,7 @@
 
 """Tests for cubicweb-seo views."""
 
-from cStringIO import StringIO
+from io import BytesIO
 from datetime import date, datetime
 import gzip
 import os
@@ -66,7 +66,7 @@ class SitemapXMLCheckMixin(object):
     def assertSitemapContains(self, sitemap_data, expected_url, expected_lastmod=None,
                               expected_priority=None, expected_chfreq=None):
         """Return True if the entry described by the parameters is found in the Sitemap data."""
-        root_node = etree.fromstring(str(sitemap_data))
+        root_node = etree.fromstring(sitemap_data)
         for url_node in root_node.findall(_sitemap_tag('url')):
             if expected_lastmod is not None:
                 expected_lastmod = (expected_lastmod.strftime('%Y-%m-%d')
@@ -91,7 +91,7 @@ class SitemapXMLCheckMixin(object):
 
     def assertSitemapLengthEqual(self, sitemap_data, expected_length):
         """Return True if the Sitemap data has the correct number of entries."""
-        root_node = etree.fromstring(str(sitemap_data))
+        root_node = etree.fromstring(sitemap_data)
         length = len(list(root_node.findall(_sitemap_tag('url'))))
         if length != expected_length:
             raise AssertionError('Sitemap length is {0} (expected: {1})'.format(length,
@@ -99,7 +99,7 @@ class SitemapXMLCheckMixin(object):
 
     def assertIndexContains(self, index_data, expected_url):
         """Return True if the given URL is found in the Sitemap index data."""
-        root_node = etree.fromstring(str(index_data))
+        root_node = etree.fromstring(index_data)
         for url_node in root_node.findall(_sitemap_tag('sitemap')):
             if url_node.findtext(_sitemap_tag('loc')) == expected_url:
                 break
@@ -122,7 +122,7 @@ class SitemapViewTC(SitemapXMLCheckMixin
                 cnx.commit()
             with self.admin_access.web_request() as req:
                 thing = req.entity_from_eid(thing.eid)
-                sitemap_data = req.view('sitemapxml')
+                sitemap_data = req.view('sitemapxml').encode('utf-8')
                 self.assertSitemapLengthEqual(sitemap_data, 2)  # Counting the root URL
                 self.assertSitemapContains(sitemap_data, thing.absolute_url(),
                                            thing.modification_date, _TITLEDTHING_PRIORITY,
@@ -138,7 +138,7 @@ class SitemapViewTC(SitemapXMLCheckMixin
             with self.admin_access.web_request() as req:
                 thing1 = req.entity_from_eid(thing1.eid)
                 thing2 = req.entity_from_eid(thing2.eid)
-                sitemap_data = req.view('sitemapxml')
+                sitemap_data = req.view('sitemapxml').encode('utf-8')
                 self.assertSitemapLengthEqual(sitemap_data, 3)  # Counting the root URL
                 self.assertSitemapContains(sitemap_data, thing1.absolute_url(),
                                            thing1.modification_date)
@@ -158,7 +158,7 @@ class SitemapViewTC(SitemapXMLCheckMixin
                 thing0 = req.entity_from_eid(thing0.eid)
                 thing1 = req.entity_from_eid(thing1.eid)
                 thing2 = req.entity_from_eid(thing2.eid)
-                sitemap_data = req.view('sitemapxml')
+                sitemap_data = req.view('sitemapxml').encode('utf-8')
                 self.assertSitemapLengthEqual(sitemap_data, 4)  # Counting the root URL
                 self.assertSitemapContains(sitemap_data, thing0.absolute_url(),
                                            thing0.modification_date, _TITLEDTHING_PRIORITY,
@@ -182,7 +182,7 @@ class GzippedSitemapViewTC(SitemapXMLChe
                 thing = req.entity_from_eid(thing.eid)
                 with tempfile.NamedTemporaryFile(delete=False) as f:
                     f.write(req.view('sitemapxmlgz'))
-                sitemap_data = StringIO()
+                sitemap_data = BytesIO()
                 with gzip.open(f.name, mode='rb') as gz:
                     sitemap_data.write(gz.read())
                 os.remove(f.name)
@@ -200,6 +200,8 @@ class ZippedSitemapsViewTC(SitemapXMLChe
         """Create as many as ``n`` ``NamedThing`` using the given connection."""
         for i in range(n):
             cnx.create_entity('NamedThing', name=u'Thing {0}'.format(i))
+            if (i % 1000 == 0):
+                print(i)
         cnx.commit()
 
     def extract_file(self, fname):
@@ -209,9 +211,9 @@ class ZippedSitemapsViewTC(SitemapXMLChe
         with zipfile.ZipFile(fname, mode='r') as zf:
             zf.extractall(tmpdir)
             for gz_fname in zf.namelist():
-                sitemap_data = StringIO()
+                sitemap_data = BytesIO()
                 with gzip.open(os.path.join(tmpdir, gz_fname), mode='rb') as gz:
-                        sitemap_data.write(gz.read())
+                    sitemap_data.write(gz.read())
                 sitemaps.append(sitemap_data)
         return sitemaps
 




More information about the cubicweb-devel mailing list