1
0
Fork 0
forked from github/pelican

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,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import logging
@ -40,7 +39,7 @@ def decode_wp_content(content, br=True):
if start == -1:
content = content + pre_part
continue
name = "<pre wp-pre-tag-{0}></pre>".format(pre_index)
name = "<pre wp-pre-tag-{}></pre>".format(pre_index)
pre_tags[name] = pre_part[start:] + "</pre>"
content = content + pre_part[0:start] + name
pre_index += 1
@ -154,7 +153,7 @@ def wp2fields(xml, wp_custpost=False):
content = item.find('encoded').string
raw_date = item.find('post_date').string
if raw_date == u'0000-00-00 00:00:00':
if raw_date == '0000-00-00 00:00:00':
date = None
else:
date_object = SafeDatetime.strptime(
@ -258,7 +257,7 @@ def dc2fields(file):
category_list = {}
posts = []
with open(file, 'r', encoding='utf-8') as f:
with open(file, encoding='utf-8') as f:
for line in f:
# remove final \n
@ -394,7 +393,7 @@ def posterous2fields(api_token, email, password):
def get_posterous_posts(api_token, email, password, page=1):
base64string = base64.encodestring(
("%s:%s" % (email, password)).encode('utf-8')).replace('\n', '')
("{}:{}".format(email, password)).encode('utf-8')).replace('\n', '')
url = ("http://posterous.com/api/v2/users/me/sites/primary/"
"posts?api_token=%s&page=%d") % (api_token, page)
request = urllib_request.Request(url)
@ -553,7 +552,7 @@ def build_header(title, date, author, categories, tags, slug,
from docutils.utils import column_width
header = '%s\n%s\n' % (title, '#' * column_width(title))
header = '{}\n{}\n'.format(title, '#' * column_width(title))
if date:
header += ':date: %s\n' % date
if author:
@ -738,7 +737,7 @@ def download_attachments(output_path, urls):
try:
urlretrieve(url, os.path.join(full_path, filename))
locations[url] = os.path.join(localpath, filename)
except (URLError, IOError) as e:
except (URLError, OSError) as e:
# Python 2.7 throws an IOError rather Than URLError
logger.warning("No file could be downloaded from %s\n%s", url, e)
return locations
@ -828,7 +827,7 @@ def fields2pelican(
new_content = decode_wp_content(content)
else:
paragraphs = content.splitlines()
paragraphs = ['<p>{0}</p>'.format(p) for p in paragraphs]
paragraphs = ['<p>{}</p>'.format(p) for p in paragraphs]
new_content = ''.join(paragraphs)
fp.write(new_content)
@ -862,7 +861,7 @@ def fields2pelican(
os.remove(html_filename)
with open(out_filename, 'r', encoding='utf-8') as fs:
with open(out_filename, encoding='utf-8') as fs:
content = fs.read()
if out_markup == 'markdown':
# In markdown, to insert a <br />, end a line with two

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import locale
@ -87,9 +86,9 @@ def ask(question, answer=str, default=None, length=None):
r = ''
while True:
if default:
r = input('> {0} [{1}] '.format(question, default))
r = input('> {} [{}] '.format(question, default))
else:
r = input('> {0} '.format(question, default))
r = input('> {} '.format(question, default))
r = r.strip()
@ -101,7 +100,7 @@ def ask(question, answer=str, default=None, length=None):
print('You must enter something')
else:
if length and len(r) != length:
print('Entry must be {0} characters long'.format(length))
print('Entry must be {} characters long'.format(length))
else:
break
@ -111,11 +110,11 @@ def ask(question, answer=str, default=None, length=None):
r = None
while True:
if default is True:
r = input('> {0} (Y/n) '.format(question))
r = input('> {} (Y/n) '.format(question))
elif default is False:
r = input('> {0} (y/N) '.format(question))
r = input('> {} (y/N) '.format(question))
else:
r = input('> {0} (y/n) '.format(question))
r = input('> {} (y/n) '.format(question))
r = r.strip().lower()
@ -135,9 +134,9 @@ def ask(question, answer=str, default=None, length=None):
r = None
while True:
if default:
r = input('> {0} [{1}] '.format(question, default))
r = input('> {} [{}] '.format(question, default))
else:
r = input('> {0} '.format(question))
r = input('> {} '.format(question))
r = r.strip()
@ -167,7 +166,7 @@ def ask_timezone(question, default, tzurl):
break
else:
print('Please enter a valid time zone:\n'
' (check [{0}])'.format(tzurl))
' (check [{}])'.format(tzurl))
return r
@ -199,7 +198,7 @@ needed by Pelican.
os.environ.get('VIRTUAL_ENV', os.curdir), '.project')
no_path_was_specified = hasattr(args.path, 'is_default_path')
if os.path.isfile(project) and no_path_was_specified:
CONF['basedir'] = open(project, 'r').read().rstrip("\n")
CONF['basedir'] = open(project).read().rstrip("\n")
print('Using project associated with current virtual environment. '
'Will save to:\n%s\n' % CONF['basedir'])
else:
@ -300,12 +299,12 @@ needed by Pelican.
try:
os.makedirs(os.path.join(CONF['basedir'], 'content'))
except OSError as e:
print('Error: {0}'.format(e))
print('Error: {}'.format(e))
try:
os.makedirs(os.path.join(CONF['basedir'], 'output'))
except OSError as e:
print('Error: {0}'.format(e))
print('Error: {}'.format(e))
try:
with open(os.path.join(CONF['basedir'], 'pelicanconf.py'),
@ -318,7 +317,7 @@ needed by Pelican.
fd.write(_template.render(**conf_python))
fd.close()
except OSError as e:
print('Error: {0}'.format(e))
print('Error: {}'.format(e))
try:
with open(os.path.join(CONF['basedir'], 'publishconf.py'),
@ -327,7 +326,7 @@ needed by Pelican.
fd.write(_template.render(**CONF))
fd.close()
except OSError as e:
print('Error: {0}'.format(e))
print('Error: {}'.format(e))
if automation:
try:
@ -337,7 +336,7 @@ needed by Pelican.
fd.write(_template.render(**CONF))
fd.close()
except OSError as e:
print('Error: {0}'.format(e))
print('Error: {}'.format(e))
try:
with open(os.path.join(CONF['basedir'], 'Makefile'),
'w', encoding='utf-8') as fd:
@ -346,7 +345,7 @@ needed by Pelican.
fd.write(_template.render(py_v=py_v, **CONF))
fd.close()
except OSError as e:
print('Error: {0}'.format(e))
print('Error: {}'.format(e))
print('Done. Your new project is available at %s' % CONF['basedir'])

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import os
@ -11,7 +10,7 @@ def err(msg, die=None):
"""Print an error message and exits if an exit code is given"""
sys.stderr.write(msg + '\n')
if die:
sys.exit((die if type(die) is int else 1))
sys.exit(die if type(die) is int else 1)
try:
@ -49,7 +48,7 @@ def main():
help="Show the themes path and exit")
excl.add_argument(
'-V', '--version', action='version',
version='pelican-themes v{0}'.format(__version__),
version='pelican-themes v{}'.format(__version__),
help='Print the version of this script')
parser.add_argument(
@ -249,12 +248,12 @@ def clean(v=False):
if os.path.islink(path):
if is_broken_link(path):
if v:
print('Removing {0}'.format(path))
print('Removing {}'.format(path))
try:
os.remove(path)
except OSError:
print('Error: cannot remove {0}'.format(path))
print('Error: cannot remove {}'.format(path))
else:
c += 1
print("\nRemoved {0} broken links".format(c))
print("\nRemoved {} broken links".format(c))