Convert all PEP-related code to be Python 2/3 compatible.

This commit is contained in:
Brett Cannon 2015-08-08 14:04:29 -07:00
parent 6040fa4e49
commit d7ef54d600
4 changed files with 97 additions and 86 deletions

View File

@ -15,6 +15,7 @@ With the PEP information collected, to create the index itself you must:
"""
from __future__ import absolute_import, with_statement
from __future__ import print_function
import sys
import os
@ -48,10 +49,10 @@ def main(argv):
raise PEPError('PEP number does not match file name',
file_path, pep.number)
peps.append(pep)
except PEPError, e:
except PEPError as e:
errmsg = "Error processing PEP %s (%s), excluding:" % \
(e.number, e.filename)
print >>sys.stderr, errmsg, e
print(errmsg, e, file=sys.stderr)
sys.exit(1)
peps.sort(key=attrgetter('number'))
elif os.path.isfile(path):

View File

@ -1,7 +1,14 @@
# -*- coding: utf-8 -*-
import sys
if sys.version_info[0] > 2:
text_type = str
else:
text_type = unicode
title_length = 55
column_format = (u' %(type)1s%(status)1s %(number)4s %(title)-' +
unicode(title_length) + u's %(authors)-s')
text_type(title_length) + u's %(authors)-s')
header = u"""PEP: 0
Title: Index of Python Enhancement Proposals (PEPs)

View File

@ -1,4 +1,6 @@
"""Code to handle the output of PEP 0."""
from __future__ import absolute_import
from __future__ import print_function
import datetime
import sys
import unicodedata
@ -28,11 +30,11 @@ def write_column_headers(output):
"""Output the column headers for the PEP indices."""
column_headers = {'status': u'', 'type': u'', 'number': u'num',
'title': u'title', 'authors': u'owner'}
print>>output, constants.column_format % column_headers
print(constants.column_format % column_headers, file=output)
underline_headers = {}
for key, value in column_headers.items():
underline_headers[key] = unicode(len(value) * '-')
print>>output, constants.column_format % underline_headers
underline_headers[key] = constants.text_type(len(value) * '-')
print(constants.column_format % underline_headers, file=output)
def sort_peps(peps):
@ -120,7 +122,7 @@ def verify_email_addresses(peps):
def sort_authors(authors_dict):
authors_list = authors_dict.keys()
authors_list = list(authors_dict.keys())
authors_list.sort(key=attrgetter('sort_by'))
return authors_list
@ -130,109 +132,109 @@ def normalized_last_first(name):
def write_pep0(peps, output=sys.stdout):
today = datetime.date.today().strftime("%Y-%m-%d")
print>>output, constants.header % today
print>>output
print>>output, u"Introduction"
print>>output, constants.intro
print>>output
print>>output, u"Index by Category"
print>>output
print(constants.header % today, file=output)
print(file=output)
print(u"Introduction", file=output)
print(constants.intro, file=output)
print(file=output)
print(u"Index by Category", file=output)
print(file=output)
write_column_headers(output)
(meta, info, accepted, open_, finished,
historical, deferred, dead) = sort_peps(peps)
print>>output
print>>output, u" Meta-PEPs (PEPs about PEPs or Processes)"
print>>output
print(file=output)
print(u" Meta-PEPs (PEPs about PEPs or Processes)", file=output)
print(file=output)
for pep in meta:
print>>output, unicode(pep)
print>>output
print>>output, u" Other Informational PEPs"
print>>output
print(constants.text_type(pep), file=output)
print(file=output)
print(u" Other Informational PEPs", file=output)
print(file=output)
for pep in info:
print>>output, unicode(pep)
print>>output
print>>output, u" Accepted PEPs (accepted; may not be implemented yet)"
print>>output
print(constants.text_type(pep), file=output)
print(file=output)
print(u" Accepted PEPs (accepted; may not be implemented yet)", file=output)
print(file=output)
for pep in accepted:
print>>output, unicode(pep)
print>>output
print>>output, u" Open PEPs (under consideration)"
print>>output
print(constants.text_type(pep), file=output)
print(file=output)
print(u" Open PEPs (under consideration)", file=output)
print(file=output)
for pep in open_:
print>>output, unicode(pep)
print>>output
print>>output, u" Finished PEPs (done, implemented in code repository)"
print>>output
print(constants.text_type(pep), file=output)
print(file=output)
print(u" Finished PEPs (done, implemented in code repository)", file=output)
print(file=output)
for pep in finished:
print>>output, unicode(pep)
print>>output
print>>output, u" Historical Meta-PEPs and Informational PEPs"
print>>output
print(constants.text_type(pep), file=output)
print(file=output)
print(u" Historical Meta-PEPs and Informational PEPs", file=output)
print(file=output)
for pep in historical:
print>>output, unicode(pep)
print>>output
print>>output, u" Deferred PEPs"
print>>output
print(constants.text_type(pep), file=output)
print(file=output)
print(u" Deferred PEPs", file=output)
print(file=output)
for pep in deferred:
print>>output, unicode(pep)
print>>output
print>>output, u" Abandoned, Withdrawn, and Rejected PEPs"
print>>output
print(constants.text_type(pep), file=output)
print(file=output)
print(u" Abandoned, Withdrawn, and Rejected PEPs", file=output)
print(file=output)
for pep in dead:
print>>output, unicode(pep)
print>>output
print>>output
print>>output, u"Numerical Index"
print>>output
print(constants.text_type(pep), file=output)
print(file=output)
print(file=output)
print(u"Numerical Index", file=output)
print(file=output)
write_column_headers(output)
prev_pep = 0
for pep in peps:
if pep.number - prev_pep > 1:
print>>output
print>>output, unicode(pep)
print(file=output)
print(constants.text_type(pep), file=output)
prev_pep = pep.number
print>>output
print>>output
print>>output, u'Reserved PEP Numbers'
print>>output
print(file=output)
print(file=output)
print(u'Reserved PEP Numbers', file=output)
print(file=output)
write_column_headers(output)
for number, claimants in sorted(RESERVED):
print>>output, constants.column_format % {
print(constants.column_format % {
'type': '',
'status': '',
'number': number,
'title': 'RESERVED',
'authors': claimants,
}
print>>output
print>>output
print>>output, u"Key"
print>>output
}, file=output)
print(file=output)
print(file=output)
print(u"Key", file=output)
print(file=output)
for type_ in PEP.type_values:
print>>output, u" %s - %s PEP" % (type_[0], type_)
print>>output
print(u" %s - %s PEP" % (type_[0], type_), file=output)
print(file=output)
for status in PEP.status_values:
print>>output, u" %s - %s proposal" % (status[0], status)
print(u" %s - %s proposal" % (status[0], status), file=output)
print>>output
print>>output
print>>output, u"Owners"
print>>output
print(file=output)
print(file=output)
print(u"Owners", file=output)
print(file=output)
authors_dict = verify_email_addresses(peps)
max_name = max(authors_dict.keys(), key=normalized_last_first)
max_name_len = len(max_name.last_first)
print>>output, u" %s %s" % ('name'.ljust(max_name_len), 'email address')
print>>output, u" %s %s" % ((len('name')*'-').ljust(max_name_len),
len('email address')*'-')
print(u" %s %s" % ('name'.ljust(max_name_len), 'email address'), file=output)
print(u" %s %s" % ((len('name')*'-').ljust(max_name_len),
len('email address')*'-'), file=output)
sorted_authors = sort_authors(authors_dict)
for author in sorted_authors:
# Use the email from authors_dict instead of the one from 'author' as
# the author instance may have an empty email.
print>>output, (u" %s %s" %
(author.last_first.ljust(max_name_len), authors_dict[author]))
print>>output
print>>output
print>>output, u"References"
print>>output
print>>output, constants.references
print>>output, constants.footer
print((u" %s %s" %
(author.last_first.ljust(max_name_len), authors_dict[author])), file=output)
print(file=output)
print(file=output)
print(u"References", file=output)
print(file=output)
print(constants.references, file=output)
print(constants.footer, file=output)

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
"""Code for handling object representation of a PEP."""
from __future__ import absolute_import
import re
import textwrap
import unicodedata
@ -179,9 +180,9 @@ class PEP(object):
header_order = iter(self.headers)
try:
for header_name in metadata.keys():
current_header, required = header_order.next()
current_header, required = next(header_order)
while header_name != current_header and not required:
current_header, required = header_order.next()
current_header, required = next(header_order)
if header_name != current_header:
raise PEPError("did not deal with "
"%r before having to handle %r" %
@ -193,7 +194,7 @@ class PEP(object):
required = False
try:
while not required:
current_header, required = header_order.next()
current_header, required = next(header_order)
else:
raise PEPError("PEP is missing its %r" % (current_header,),
pep_file.name)
@ -239,9 +240,9 @@ class PEP(object):
"""Return a list of author names and emails."""
# XXX Consider using email.utils.parseaddr (doesn't work with names
# lacking an email address.
angled = ur'(?P<author>.+?) <(?P<email>.+?)>'
paren = ur'(?P<email>.+?) \((?P<author>.+?)\)'
simple = ur'(?P<author>[^,]+)'
angled = constants.text_type(r'(?P<author>.+?) <(?P<email>.+?)>')
paren = constants.text_type(r'(?P<email>.+?) \((?P<author>.+?)\)')
simple = constants.text_type(r'(?P<author>[^,]+)')
author_list = []
for regex in (angled, paren, simple):
# Watch out for commas separating multiple names.