Modernize code base to Python 3+ syntax

Replaces syntax that was relevant in earlier Python versions but that
now has modernized equivalents.
This commit is contained in:
Justin Mayer 2020-04-26 09:55:08 +02:00
commit d43b786b30
37 changed files with 104 additions and 171 deletions

View file

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
AUTHOR = 'Alexis Métaireau'
SITENAME = "Alexis' log"
SITEURL = 'http://blog.notmyidea.org'

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import locale
import logging
import os
@ -134,7 +132,7 @@ def skipIfNoExecutable(executable):
res = None
if res is None:
return unittest.skip('{0} executable not found'.format(executable))
return unittest.skip('{} executable not found'.format(executable))
return lambda func: func

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import os
from shutil import rmtree
from tempfile import mkdtemp

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import datetime
import locale
import logging
@ -27,7 +25,7 @@ class TestBase(LoggedTestCase):
def setUp(self):
super().setUp()
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
self.page_kwargs = {
'content': TEST_CONTENT,
'context': {
@ -56,13 +54,13 @@ class TestBase(LoggedTestCase):
def _copy_page_kwargs(self):
# make a deep copy of page_kwargs
page_kwargs = dict([(key, self.page_kwargs[key]) for key in
self.page_kwargs])
page_kwargs = {key: self.page_kwargs[key] for key in self.page_kwargs}
for key in page_kwargs:
if not isinstance(page_kwargs[key], dict):
break
page_kwargs[key] = dict([(subkey, page_kwargs[key][subkey])
for subkey in page_kwargs[key]])
page_kwargs[key] = {
subkey: page_kwargs[key][subkey] for subkey in page_kwargs[key]
}
return page_kwargs

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import locale
import os
from shutil import copy, rmtree
@ -27,7 +25,7 @@ CONTENT_DIR = os.path.join(CUR_DIR, 'content')
class TestGenerator(unittest.TestCase):
def setUp(self):
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
self.settings = get_settings()
self.settings['READERS'] = {'asc': None}
self.generator = Generator(self.settings.copy(), self.settings,
@ -403,7 +401,7 @@ class TestArticlesGenerator(unittest.TestCase):
'period' : a tuple of year, month, day according to the time period
"""
old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
settings = get_settings()
settings['YEAR_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/index.html'
@ -788,7 +786,7 @@ class TestTemplatePagesGenerator(unittest.TestCase):
self.temp_content = mkdtemp(prefix='pelicantests.')
self.temp_output = mkdtemp(prefix='pelicantests.')
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
def tearDown(self):
rmtree(self.temp_content)
@ -823,7 +821,7 @@ class TestTemplatePagesGenerator(unittest.TestCase):
self.assertTrue(os.path.exists(output_path))
# output content is correct
with open(output_path, 'r') as output_file:
with open(output_path) as output_file:
self.assertEqual(output_file.read(), 'foo: bar')
@ -1013,7 +1011,7 @@ class TestStaticGenerator(unittest.TestCase):
f.write("staticcontent")
self.generator.generate_context()
self.generator.generate_output(None)
with open(self.endfile, "r") as f:
with open(self.endfile) as f:
self.assertEqual(f.read(), "staticcontent")
@unittest.skipUnless(MagicMock, 'Needs Mock module')
@ -1162,7 +1160,7 @@ class TestJinja2Environment(unittest.TestCase):
self.temp_content = mkdtemp(prefix='pelicantests.')
self.temp_output = mkdtemp(prefix='pelicantests.')
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
def tearDown(self):
rmtree(self.temp_content)
@ -1197,7 +1195,7 @@ class TestJinja2Environment(unittest.TestCase):
self.assertTrue(os.path.exists(output_path))
# output content is correct
with open(output_path, 'r') as output_file:
with open(output_path) as output_file:
self.assertEqual(output_file.read(), expected)
def test_jinja2_filter(self):

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import locale
import os
import re
@ -41,7 +39,7 @@ class TestBloggerXmlImporter(unittest.TestCase):
def setUp(self):
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
self.posts = blogger2fields(BLOGGER_XML_SAMPLE)
def tearDown(self):
@ -90,7 +88,7 @@ class TestWordpressXmlImporter(unittest.TestCase):
def setUp(self):
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
self.posts = wp2fields(WORDPRESS_XML_SAMPLE)
self.custposts = wp2fields(WORDPRESS_XML_SAMPLE, True)
@ -282,9 +280,9 @@ class TestWordpressXmlImporter(unittest.TestCase):
def test_decode_wp_content(self):
""" Check that we can decode a wordpress content string."""
with open(WORDPRESS_ENCODED_CONTENT_SAMPLE, 'r') as encoded_file:
with open(WORDPRESS_ENCODED_CONTENT_SAMPLE) as encoded_file:
encoded_content = encoded_file.read()
with open(WORDPRESS_DECODED_CONTENT_SAMPLE, 'r') as decoded_file:
with open(WORDPRESS_DECODED_CONTENT_SAMPLE) as decoded_file:
decoded_content = decoded_file.read()
self.assertEqual(
decode_wp_content(encoded_content, br=False),
@ -405,7 +403,7 @@ class TestBuildHeader(unittest.TestCase):
class TestWordpressXMLAttachements(unittest.TestCase):
def setUp(self):
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
self.attachments = get_attachments(WORDPRESS_XML_SAMPLE)
def tearDown(self):

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import locale
from jinja2.utils import generate_lorem_ipsum
@ -19,7 +17,7 @@ class TestPage(unittest.TestCase):
def setUp(self):
super().setUp()
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
self.page_kwargs = {
'content': TEST_CONTENT,
'context': {

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import locale
import logging
import os
@ -47,7 +45,7 @@ class TestPelican(LoggedTestCase):
self.temp_cache = mkdtemp(prefix='pelican_cache.')
self.maxDiff = None
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
def tearDown(self):
read_settings() # cleanup PYGMENTS_RST_OPTIONS
@ -60,7 +58,7 @@ class TestPelican(LoggedTestCase):
out, err = subprocess.Popen(
['git', 'diff', '--no-ext-diff', '--exit-code',
'-w', left_path, right_path],
env={str('PAGER'): str('')},
env={'PAGER': ''},
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()
@ -131,9 +129,9 @@ class TestPelican(LoggedTestCase):
def test_custom_locale_generation_works(self):
'''Test that generation with fr_FR.UTF-8 locale works'''
if sys.platform == 'win32':
our_locale = str('French')
our_locale = 'French'
else:
our_locale = str('fr_FR.UTF-8')
our_locale = 'fr_FR.UTF-8'
settings = read_settings(path=SAMPLE_FR_CONFIG, override={
'PATH': INPUT_PATH,

View file

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import os
from contextlib import contextmanager
@ -84,9 +81,9 @@ class PluginTest(unittest.TestCase):
def test_load_plugins(self):
def get_plugin_names(plugins):
return set(
return {
plugin.NAME if hasattr(plugin, 'NAME') else plugin.__name__
for plugin in plugins)
for plugin in plugins}
# existing namespace plugins
existing_ns_plugins = load_plugins({})

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import os
from pelican import readers
@ -92,9 +90,9 @@ class DefaultReaderTest(ReaderTest):
for tag in content:
readers.find_empty_alt(tag, '/test/path')
log_mock.warning.assert_called_with(
u'Empty alt attribute for image %s in %s',
u'test-image.png',
u'/test/path',
'Empty alt attribute for image %s in %s',
'test-image.png',
'/test/path',
extra={'limit_msg':
'Other images have empty alt attributes'}
)

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
from pelican.tests.support import unittest
try:

View file

@ -7,12 +7,12 @@ from pelican.server import ComplexHTTPRequestHandler
from pelican.tests.support import unittest
class MockRequest(object):
class MockRequest:
def makefile(self, *args, **kwargs):
return BytesIO(b"")
class MockServer(object):
class MockServer:
pass

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import copy
import locale
import os
@ -21,7 +19,7 @@ class TestSettingsConfiguration(unittest.TestCase):
"""
def setUp(self):
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
self.PATH = abspath(dirname(__file__))
default_conf = join(self.PATH, 'default_conf.py')
self.settings = read_settings(default_conf)
@ -143,7 +141,7 @@ class TestSettingsConfiguration(unittest.TestCase):
# Test that the default locale is set if not specified in settings
# Reset locale to Python's default locale
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
self.assertEqual(self.settings['LOCALE'], DEFAULT_CONFIG['LOCALE'])
configure_settings(self.settings)

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import warnings
from pelican.tests.support import unittest

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
from pelican.tests.support import unittest
from pelican.urlwrappers import Author, Category, Tag, URLWrapper
@ -40,7 +38,7 @@ class TestURLWrapper(unittest.TestCase):
self.assertNotEqual(tag, author)
# should be equal vs text representing the same name
self.assertEqual(tag, u'test')
self.assertEqual(tag, 'test')
# should not be equal vs binary
self.assertNotEqual(tag, b'test')
@ -54,7 +52,7 @@ class TestURLWrapper(unittest.TestCase):
self.assertEqual(author, author_equal)
cat_ascii = Category('指導書', settings={})
self.assertEqual(cat_ascii, u'zhi dao shu')
self.assertEqual(cat_ascii, 'zhi dao shu')
def test_slugify_with_substitutions_and_dots(self):
tag = Tag('Tag Dot', settings={'TAG_REGEX_SUBSTITUTIONS': [

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import locale
import logging
import os
@ -480,9 +478,9 @@ class TestUtils(LoggedTestCase):
old_locale = locale.setlocale(locale.LC_ALL)
if platform == 'win32':
locale.setlocale(locale.LC_ALL, str('Turkish'))
locale.setlocale(locale.LC_ALL, 'Turkish')
else:
locale.setlocale(locale.LC_ALL, str('tr_TR.UTF-8'))
locale.setlocale(locale.LC_ALL, 'tr_TR.UTF-8')
d = utils.SafeDatetime(2012, 8, 29)
@ -514,9 +512,9 @@ class TestUtils(LoggedTestCase):
old_locale = locale.setlocale(locale.LC_ALL)
if platform == 'win32':
locale.setlocale(locale.LC_ALL, str('French'))
locale.setlocale(locale.LC_ALL, 'French')
else:
locale.setlocale(locale.LC_ALL, str('fr_FR.UTF-8'))
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
d = utils.SafeDatetime(2012, 8, 29)
@ -557,7 +555,7 @@ class TestCopy(unittest.TestCase):
def setUp(self):
self.root_dir = mkdtemp(prefix='pelicantests.')
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
def tearDown(self):
shutil.rmtree(self.root_dir)
@ -666,26 +664,26 @@ class TestDateFormatter(unittest.TestCase):
# This test tries to reproduce an issue that
# occurred with python3.3 under macos10 only
if platform == 'win32':
locale.setlocale(locale.LC_ALL, str('French'))
locale.setlocale(locale.LC_ALL, 'French')
else:
locale.setlocale(locale.LC_ALL, str('fr_FR.UTF-8'))
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
date = utils.SafeDatetime(2014, 8, 14)
# we compare the lower() dates since macos10 returns
# "Jeudi" for %A whereas linux reports "jeudi"
self.assertEqual(
u'jeudi, 14 août 2014',
'jeudi, 14 août 2014',
utils.strftime(date, date_format="%A, %d %B %Y").lower())
df = utils.DateFormatter()
self.assertEqual(
u'jeudi, 14 août 2014',
'jeudi, 14 août 2014',
df(date, date_format="%A, %d %B %Y").lower())
# Let us now set the global locale to C:
locale.setlocale(locale.LC_ALL, str('C'))
locale.setlocale(locale.LC_ALL, 'C')
# DateFormatter should still work as expected
# since it is the whole point of DateFormatter
# (This is where pre-2014/4/15 code fails on macos10)
df_date = df(date, date_format="%A, %d %B %Y").lower()
self.assertEqual(u'jeudi, 14 août 2014', df_date)
self.assertEqual('jeudi, 14 août 2014', df_date)
@unittest.skipUnless(locale_available('fr_FR.UTF-8') or
locale_available('French'),