diff options
author | Dmitry Shachnev <mitya57@gmail.com> | 2014-01-19 14:17:10 +0400 |
---|---|---|
committer | Dmitry Shachnev <mitya57@gmail.com> | 2014-01-19 14:17:10 +0400 |
commit | 344417db950d6e816ab2efb21737c2bdf9d1ad53 (patch) | |
tree | 97b00b55be0e28a73399acc0f80e21e6a4e24b28 /sphinx/cmdline.py | |
parent | 59b355edaa4f17aff910bf2371526d5ce46fb648 (diff) | |
download | sphinx-344417db950d6e816ab2efb21737c2bdf9d1ad53.tar.gz |
Modernize the code now that Python 2.5 is no longer supported
- Use print function instead of print statement;
- Use new exception handling;
- Use in operator instead of has_key();
- Do not use tuple arguments in functions;
- Other miscellaneous improvements.
This is based on output of `futurize --stage1`, with some
manual corrections.
Diffstat (limited to 'sphinx/cmdline.py')
-rw-r--r-- | sphinx/cmdline.py | 97 |
1 files changed, 49 insertions, 48 deletions
diff --git a/sphinx/cmdline.py b/sphinx/cmdline.py index 2d7146c3..de73efa4 100644 --- a/sphinx/cmdline.py +++ b/sphinx/cmdline.py @@ -8,6 +8,7 @@ :copyright: Copyright 2007-2013 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. """ +from __future__ import print_function import os import sys @@ -28,9 +29,9 @@ from sphinx.util.pycompat import terminal_safe, bytes def usage(argv, msg=None): if msg: - print >>sys.stderr, msg - print >>sys.stderr - print >>sys.stderr, """\ + print(msg, file=sys.stderr) + print(file=sys.stderr) + print("""\ Sphinx v%s Usage: %s [options] sourcedir outdir [filenames...] @@ -74,7 +75,7 @@ Standard options ^^^^^^^^^^^^^^^^ -h, --help show this help and exit --version show version information and exit -""" % (__version__, argv[0]) +""" % (__version__, argv[0]), file=sys.stderr) def main(argv): @@ -88,41 +89,41 @@ def main(argv): allopts = set(opt[0] for opt in opts) if '-h' in allopts or '--help' in allopts: usage(argv) - print >>sys.stderr - print >>sys.stderr, 'For more information, see '\ - '<http://sphinx-doc.org/>.' + print(file=sys.stderr) + print('For more information, see <http://sphinx-doc.org/>.', + file=sys.stderr) return 0 if '--version' in allopts: - print 'Sphinx (sphinx-build) %s' % __version__ + print('Sphinx (sphinx-build) %s' % __version__) return 0 srcdir = confdir = abspath(args[0]) if not path.isdir(srcdir): - print >>sys.stderr, 'Error: Cannot find source directory `%s\'.' % ( - srcdir,) + print('Error: Cannot find source directory `%s\'.' % srcdir, + file=sys.stderr) return 1 if not path.isfile(path.join(srcdir, 'conf.py')) and \ '-c' not in allopts and '-C' not in allopts: - print >>sys.stderr, ('Error: Source directory doesn\'t ' - 'contain conf.py file.') + print('Error: Source directory doesn\'t contain conf.py file.', + file=sys.stderr) return 1 outdir = abspath(args[1]) - except getopt.error, err: + except getopt.error as err: usage(argv, 'Error: %s' % err) return 1 except IndexError: usage(argv, 'Error: Insufficient arguments.') return 1 except UnicodeError: - print >>sys.stderr, ( + print( 'Error: Multibyte filename not supported on this filesystem ' - 'encoding (%r).' % fs_encoding) + 'encoding (%r).' % fs_encoding, file=sys.stderr) return 1 filenames = args[2:] err = 0 for filename in filenames: if not path.isfile(filename): - print >>sys.stderr, 'Error: Cannot find file %r.' % filename + print('Error: Cannot find file %r.' % filename, file=sys.stderr) err = 1 if err: return 1 @@ -161,8 +162,8 @@ def main(argv): elif opt == '-c': confdir = abspath(val) if not path.isfile(path.join(confdir, 'conf.py')): - print >>sys.stderr, ('Error: Configuration directory ' - 'doesn\'t contain conf.py file.') + print('Error: Configuration directory doesn\'t contain conf.py file.', + file=sys.stderr) return 1 elif opt == '-C': confdir = None @@ -170,8 +171,8 @@ def main(argv): try: key, val = val.split('=') except ValueError: - print >>sys.stderr, ('Error: -D option argument must be ' - 'in the form name=value.') + print('Error: -D option argument must be in the form name=value.', + file=sys.stderr) return 1 if likely_encoding and isinstance(val, bytes): try: @@ -183,8 +184,8 @@ def main(argv): try: key, val = val.split('=') except ValueError: - print >>sys.stderr, ('Error: -A option argument must be ' - 'in the form name=value.') + print('Error: -A option argument must be in the form name=value.', + file=sys.stderr) return 1 try: val = int(val) @@ -221,8 +222,8 @@ def main(argv): try: parallel = int(val) except ValueError: - print >>sys.stderr, ('Error: -j option argument must be an ' - 'integer.') + print('Error: -j option argument must be an integer.', + file=sys.stderr) return 1 if warning and warnfile: @@ -232,7 +233,7 @@ def main(argv): if not path.isdir(outdir): if status: - print >>status, 'Making output directory...' + print('Making output directory...', file=status) os.makedirs(outdir) app = None @@ -242,39 +243,39 @@ def main(argv): warningiserror, tags, verbosity, parallel) app.build(force_all, filenames) return app.statuscode - except (Exception, KeyboardInterrupt), err: + except (Exception, KeyboardInterrupt) as err: if use_pdb: import pdb - print >>error, red('Exception occurred while building, ' - 'starting debugger:') + print(red('Exception occurred while building, starting debugger:'), + file=error) traceback.print_exc() pdb.post_mortem(sys.exc_info()[2]) else: - print >>error + print(file=error) if show_traceback: traceback.print_exc(None, error) - print >>error + print(file=error) if isinstance(err, KeyboardInterrupt): - print >>error, 'interrupted!' + print('interrupted!', file=error) elif isinstance(err, SystemMessage): - print >>error, red('reST markup error:') - print >>error, terminal_safe(err.args[0]) + print(red('reST markup error:'), file=error) + print(terminal_safe(err.args[0]), file=error) elif isinstance(err, SphinxError): - print >>error, red('%s:' % err.category) - print >>error, terminal_safe(unicode(err)) + print(red('%s:' % err.category), file=error) + print(terminal_safe(unicode(err)), file=error) else: - print >>error, red('Exception occurred:') - print >>error, format_exception_cut_frames().rstrip() + print(red('Exception occurred:'), file=error) + print(format_exception_cut_frames().rstrip(), file=error) tbpath = save_traceback(app) - print >>error, red('The full traceback has been saved ' - 'in %s, if you want to report the ' - 'issue to the developers.' % tbpath) - print >>error, ('Please also report this if it was a user ' - 'error, so that a better error message ' - 'can be provided next time.') - print >>error, ( - 'Either send bugs to the mailing list at ' - '<http://groups.google.com/group/sphinx-users/>,\n' - 'or report them in the tracker at ' - '<http://bitbucket.org/birkenfeld/sphinx/issues/>. Thanks!') + print(red('The full traceback has been saved in %s, if you ' + 'want to report the issue to the developers.' % tbpath), + file=error) + print('Please also report this if it was a user error, so ' + 'that a better error message can be provided next time.', + file=error) + print('Either send bugs to the mailing list at ' + '<http://groups.google.com/group/sphinx-users/>,\n' + 'or report them in the tracker at ' + '<http://bitbucket.org/birkenfeld/sphinx/issues/>. Thanks!', + file=error) return 1 |