diff --git a/pep2html.py b/pep2html.py index c92e10dd4..cd020a3a0 100755 --- a/pep2html.py +++ b/pep2html.py @@ -34,6 +34,8 @@ Options: The optional arguments ``peps`` are either pep numbers or .txt files. """ +from __future__ import print_function, unicode_literals + import sys import os import re @@ -43,8 +45,9 @@ import getopt import errno import random import time +from io import open -REQUIRES = {'python': '2.2', +REQUIRES = {'python': '2.6', 'docutils': '0.2.7'} PROGRAM = sys.argv[0] RFCURL = 'http://www.faqs.org/rfcs/rfc%d.html' @@ -85,9 +88,9 @@ def usage(code, msg=''): out = sys.stdout else: out = sys.stderr - print >> out, __doc__ % globals() + print(__doc__ % globals(), file=out) if msg: - print >> out, msg + print(msg, file=out) sys.exit(code) @@ -105,7 +108,7 @@ def fixanchor(current, match): ltext.append(c) break link = EMPTYSTRING.join(ltext) - elif text.startswith('pep-') and text <> current: + elif text.startswith('pep-') and text != current: link = os.path.splitext(text)[0] + ".html" elif text.startswith('PEP'): pepnum = int(match.group('pepnum')) @@ -143,14 +146,17 @@ def linkemail(address, pepno): def fixfile(inpath, input_lines, outfile): - from email.Utils import parseaddr + try: + from email.Utils import parseaddr + except ImportError: + from email.utils import parseaddr basename = os.path.basename(inpath) infile = iter(input_lines) # convert plaintext pep to minimal XHTML markup - print >> outfile, DTD - print >> outfile, '' - print >> outfile, COMMENT - print >> outfile, '' + print(DTD, file=outfile) + print('', file=outfile) + print(COMMENT, file=outfile) + print('', file=outfile) # head header = [] pep = "" @@ -176,9 +182,9 @@ def fixfile(inpath, input_lines, outfile): if pep: title = "PEP " + pep + " -- " + title if title: - print >> outfile, ' %s' % cgi.escape(title) - r = random.choice(range(64)) - print >> outfile, ( + print(' %s' % cgi.escape(title), file=outfile) + r = random.choice(list(range(64))) + print(( ' \n' '\n' '\n' @@ -189,18 +195,18 @@ def fixfile(inpath, input_lines, outfile): '[Python]\n' '\n' - '[Python Home]' % r) - if basename <> 'pep-0000.txt': - print >> outfile, '[PEP Index]' + '[Python Home]' % r), file=outfile) + if basename != 'pep-0000.txt': + print('[PEP Index]', file=outfile) if pep: try: - print >> outfile, ('[PEP Source' - ']' % int(pep)) - except ValueError, error: - print >> sys.stderr, ('ValueError (invalid PEP number): %s' - % error) - print >> outfile, '' - print >> outfile, '
\n' + print(('[PEP Source' + ']' % int(pep)), file=outfile) + except ValueError as error: + print(('ValueError (invalid PEP number): %s' + % error), file=sys.stderr) + print('
', file=outfile) + print('
\n', file=outfile) for k, v in header: if k.lower() in ('author', 'bdfl-delegate', 'discussions-to'): mailtos = [] @@ -236,7 +242,7 @@ def fixfile(inpath, input_lines, outfile): try: url = PEPCVSURL % int(pep) v = '%s ' % (url, cgi.escape(date)) - except ValueError, error: + except ValueError as error: v = date elif k.lower() in ('content-type',): url = PEPURL % 9 @@ -247,12 +253,12 @@ def fixfile(inpath, input_lines, outfile): v = cgi.escape(v[11:-2]) else: v = cgi.escape(v) - print >> outfile, ' ' \ - % (cgi.escape(k), v) - print >> outfile, '
%s: %s
' - print >> outfile, '
' - print >> outfile, '
' - print >> outfile, '
' + print(' %s: %s' \ + % (cgi.escape(k), v), file=outfile) + print('', file=outfile) + print('
', file=outfile) + print('
', file=outfile) + print('
', file=outfile) need_pre = 1 for line in infile: if line[0] == '\f': @@ -261,8 +267,8 @@ def fixfile(inpath, input_lines, outfile): break if line[0].strip(): if not need_pre: - print >> outfile, '' - print >> outfile, '

%s

' % line.strip() + print('', file=outfile) + print('

%s

' % line.strip(), file=outfile) need_pre = 1 elif not line.strip() and need_pre: continue @@ -274,32 +280,32 @@ def fixfile(inpath, input_lines, outfile): # This is a PEP summary line, which we need to hyperlink url = PEPURL % int(parts[1]) if need_pre: - print >> outfile, '
'
+                        print('
', file=outfile)
                         need_pre = 0
-                    print >> outfile, re.sub(
+                    print(re.sub(
                         parts[1],
                         '%s' % (url, parts[1]),
-                        line, 1),
+                        line, 1), end=' ', file=outfile)
                     continue
                 elif parts and '@' in parts[-1]:
                     # This is a pep email address line, so filter it.
                     url = fixemail(parts[-1], pep)
                     if need_pre:
-                        print >> outfile, '
'
+                        print('
', file=outfile)
                         need_pre = 0
-                    print >> outfile, re.sub(
-                        parts[-1], url, line, 1),
+                    print(re.sub(
+                        parts[-1], url, line, 1), end=' ', file=outfile)
                     continue
             line = fixpat.sub(lambda x, c=inpath: fixanchor(c, x), line)
             if need_pre:
-                print >> outfile, '
'
+                print('
', file=outfile)
                 need_pre = 0
             outfile.write(line)
     if not need_pre:
-        print >> outfile, '
' - print >> outfile, '
' - print >> outfile, '' - print >> outfile, '' + print('', file=outfile) + print('
', file=outfile) + print('', file=outfile) + print('', file=outfile) docutils_settings = None @@ -320,7 +326,7 @@ def fix_rst_pep(inpath, input_lines, outfile): settings=docutils_settings, # Allow Docutils traceback if there's an exception: settings_overrides={'traceback': 1}) - outfile.write(output) + outfile.write(output.decode('utf-8')) def get_pep_type(input_lines): @@ -345,10 +351,10 @@ def get_pep_type(input_lines): def get_input_lines(inpath): try: - infile = open(inpath) - except IOError, e: - if e.errno <> errno.ENOENT: raise - print >> sys.stderr, 'Error: Skipping missing PEP file:', e.filename + infile = open(inpath, encoding='utf-8') + except IOError as e: + if e.errno != errno.ENOENT: raise + print('Error: Skipping missing PEP file:', e.filename, file=sys.stderr) sys.stderr.flush() return None lines = infile.read().splitlines(1) # handles x-platform line endings @@ -369,12 +375,12 @@ def make_html(inpath, verbose=0): return None pep_type = get_pep_type(input_lines) if pep_type is None: - print >> sys.stderr, 'Error: Input file %s is not a PEP.' % inpath + print('Error: Input file %s is not a PEP.' % inpath, file=sys.stderr) sys.stdout.flush() return None - elif not PEP_TYPE_DISPATCH.has_key(pep_type): - print >> sys.stderr, ('Error: Unknown PEP type for input file %s: %s' - % (inpath, pep_type)) + elif pep_type not in PEP_TYPE_DISPATCH: + print(('Error: Unknown PEP type for input file %s: %s' + % (inpath, pep_type)), file=sys.stderr) sys.stdout.flush() return None elif PEP_TYPE_DISPATCH[pep_type] == None: @@ -382,12 +388,12 @@ def make_html(inpath, verbose=0): return None outpath = os.path.splitext(inpath)[0] + ".html" if verbose: - print inpath, "(%s)" % pep_type, "->", outpath + print(inpath, "(%s)" % pep_type, "->", outpath) sys.stdout.flush() - outfile = open(outpath, "w") + outfile = open(outpath, "w", encoding='utf-8') PEP_TYPE_DISPATCH[pep_type](inpath, input_lines, outfile) outfile.close() - os.chmod(outfile.name, 0664) + os.chmod(outfile.name, 0o664) return outpath def push_pep(htmlfiles, txtfiles, username, verbose, local=0): @@ -425,9 +431,8 @@ PEP_TYPE_MESSAGES = {} def check_requirements(): # Check Python: - try: - from email.Utils import parseaddr - except ImportError: + # This is pretty much covered by the __future__ imports... + if sys.version_info < (2, 6, 0): PEP_TYPE_DISPATCH['text/plain'] = None PEP_TYPE_MESSAGES['text/plain'] = ( 'Python %s or better required for "%%(pep_type)s" PEP ' @@ -453,7 +458,7 @@ def check_requirements(): % (REQUIRES['docutils'], docutils.__version__)) def pep_type_error(inpath, pep_type): - print >> sys.stderr, 'Error: ' + PEP_TYPE_MESSAGES[pep_type] % locals() + print('Error: ' + PEP_TYPE_MESSAGES[pep_type] % locals(), file=sys.stderr) sys.stdout.flush() @@ -492,7 +497,7 @@ def main(argv=None): opts, args = getopt.getopt( argv, 'bilhqu:', ['browse', 'install', 'local', 'help', 'quiet', 'user=']) - except getopt.error, msg: + except getopt.error as msg: usage(1, msg) for opt, arg in opts: