pax_global_header00006660000000000000000000000064131037122370014511gustar00rootroot0000000000000052 comment=2d602ecd7be59fd99ad7157e4e8bf8d64a516f58 python-colored-traceback-0.3.0/000077500000000000000000000000001310371223700163745ustar00rootroot00000000000000python-colored-traceback-0.3.0/.gitattributes000066400000000000000000000001561310371223700212710ustar00rootroot00000000000000*.py whitespace=tab-in-indent,tabwidth=4 Makefile whitespace=indent-with-non-tab,tabwidth=4 *.py diff=python python-colored-traceback-0.3.0/.gitignore000066400000000000000000000000361310371223700203630ustar00rootroot00000000000000build/ dist/ *.pyc *.egg-info python-colored-traceback-0.3.0/LICENSE000066400000000000000000000013561310371223700174060ustar00rootroot00000000000000Copyright (c) 2014, Anton Backer Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. python-colored-traceback-0.3.0/MANIFEST.in000066400000000000000000000000231310371223700201250ustar00rootroot00000000000000include README.rst python-colored-traceback-0.3.0/Makefile000066400000000000000000000005131310371223700200330ustar00rootroot00000000000000SHELL := /bin/bash .PHONY: sdist sdist: @python setup.py sdist .PHONY: bdist_wheel bdist_wheel: @python setup.py bdist_wheel .PHONY: publish publish: sdist bdist_wheel @twine upload dist/* .PHONY: clean clean: @rm -rvf MANIFEST *.egg-info *.pyc {,colored_traceback/{,auto/,always/}}{*.pyc,__pycache__} @rm -rvf build dist python-colored-traceback-0.3.0/README.rst000066400000000000000000000073101310371223700200640ustar00rootroot00000000000000Colored Traceback ================= Automatically color Python's uncaught exception tracebacks. This one's for anybody who's ever struggled to read python's stacktraces on the terminal. Something about the two-lines-per-frame approach really just makes them tough to scan visually. Compare this: :: Traceback (most recent call last): File "./workflowy.py", line 525, in main() File "./workflowy.py", line 37, in main projects = cli.load_json(args, input_is_pipe) File "./workflowy.py", line 153, in load_json return json.load(sys.stdin) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load **kw) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads return _default_decoder.decode(s) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded To this: .. code-block:: python Traceback (most recent call last): File "./workflowy.py", line 525, in main() File "./workflowy.py", line 37, in main projects = cli.load_json(args, input_is_pipe) File "./workflowy.py", line 153, in load_json return json.load(sys.stdin) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load **kw) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads return _default_decoder.decode(s) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded Installation ------------ Through pip: .. code-block:: bash pip install colored-traceback Or directly: .. code-block:: bash git clone http://www.github.com/staticshock/colored-traceback.py python setup.py install On Windows, which has no real support for ANSI escape sequences, there's an additional dependency on `colorama`: .. code-block:: bash pip install colorama Usage ----- Colored Traceback can be executed as a module: .. code-block:: bash python -m colored_traceback somefile.py Colored Traceback also works well within a script or even directly in the interpreter REPL. Standard usage will color the output, unless it's being redirected to a pipe: .. code-block:: python import colored_traceback colored_traceback.add_hook() If want to retain color even when stderr is being piped, tack on an `always=True` argument: .. code-block:: python import colored_traceback colored_traceback.add_hook(always=True) There are also a couple of convenience imports, which get the footprint down to one line: .. code-block:: python # Same as add_hook() import colored_traceback.auto # Same as add_hook(always=True) import colored_traceback.always It goes without saying that you might want to catch `ImportError`, making the presence of the package optional: .. code-block:: python try: import colored_traceback.auto except ImportError: pass python-colored-traceback-0.3.0/colored_traceback/000077500000000000000000000000001310371223700220225ustar00rootroot00000000000000python-colored-traceback-0.3.0/colored_traceback/__init__.py000066400000000000000000000000631310371223700241320ustar00rootroot00000000000000from .colored_traceback import add_hook, Colorizer python-colored-traceback-0.3.0/colored_traceback/__main__.py000066400000000000000000000013071310371223700241150ustar00rootroot00000000000000import os import sys import colored_traceback colored_traceback.add_hook() # Check that a script was passed in. if len(sys.argv) <= 1: raise SystemExit("Usage: python -m colored_traceback my_script.py") # Check that the script exists. script_path = sys.argv[1] if not os.path.exists(script_path): raise SystemExit("Error: '%s' does not exist" % script_path) # Replace colored_traceback's dir with script's dir in the module search path. sys.path[0] = os.path.dirname(script_path) # Remove colored_traceback from the arg list. del sys.argv[0] with open(script_path) as script_file: code = compile(script_file.read(), script_path, 'exec') variables = {} exec(code, variables, variables) python-colored-traceback-0.3.0/colored_traceback/always/000077500000000000000000000000001310371223700233225ustar00rootroot00000000000000python-colored-traceback-0.3.0/colored_traceback/always/__init__.py000066400000000000000000000000751310371223700254350ustar00rootroot00000000000000from colored_traceback import add_hook add_hook(always=True) python-colored-traceback-0.3.0/colored_traceback/auto/000077500000000000000000000000001310371223700227725ustar00rootroot00000000000000python-colored-traceback-0.3.0/colored_traceback/auto/__init__.py000066400000000000000000000000621310371223700251010ustar00rootroot00000000000000from colored_traceback import add_hook add_hook() python-colored-traceback-0.3.0/colored_traceback/colored_traceback.py000066400000000000000000000042101310371223700260170ustar00rootroot00000000000000import sys def add_hook(always=False, style='default', debug=False): isatty = getattr(sys.stderr, 'isatty', lambda: False) if always or isatty(): try: import pygments # flake8:noqa colorizer = Colorizer(style, debug) sys.excepthook = colorizer.colorize_traceback except ImportError: if debug: sys.stderr.write("Failed to add coloring hook; pygments not available\n") class Colorizer(object): def __init__(self, style, debug=False): self.style = style self.debug = debug def colorize_traceback(self, type, value, tb): import traceback import pygments.lexers tb_text = "".join(traceback.format_exception(type, value, tb)) lexer = pygments.lexers.get_lexer_by_name("pytb", stripall=True) tb_colored = pygments.highlight(tb_text, lexer, self.formatter) self.stream.write(tb_colored) @property def formatter(self): colors = _get_term_color_support() if self.debug: sys.stderr.write("Detected support for %s colors\n" % colors) if colors == 256: fmt_options = {'style': self.style} elif self.style in ('light', 'dark'): fmt_options = {'bg': self.style} else: fmt_options = {'bg': 'dark'} from pygments.formatters import get_formatter_by_name import pygments.util fmt_alias = 'terminal256' if colors == 256 else 'terminal' try: return get_formatter_by_name(fmt_alias, **fmt_options) except pygments.util.ClassNotFound as ex: if self.debug: sys.stderr.write(str(ex) + "\n") return get_formatter_by_name(fmt_alias) @property def stream(self): try: import colorama return colorama.AnsiToWin32(sys.stderr) except ImportError: return sys.stderr def _get_term_color_support(): try: import curses except ImportError: # Probably Windows, which doesn't have great curses support return 16 curses.setupterm() return curses.tigetnum('colors') python-colored-traceback-0.3.0/setup.py000077500000000000000000000022361310371223700201140ustar00rootroot00000000000000try: from setuptools import setup has_setuptools = True except: from distutils.core import setup has_setuptools = False extra_args = {} if has_setuptools: extra_args['install_requires'] = ['pygments'] setup( name='colored-traceback', version='0.3.0', description='Automatically color uncaught exception tracebacks', long_description=open("README.rst").read(), author='Anton Backer', author_email='olegov@gmail.com', url='http://www.github.com/staticshock/colored-traceback.py', packages=['colored_traceback', 'colored_traceback.auto', 'colored_traceback.always'], license='ISC', classifiers=( 'Development Status :: 4 - Beta', 'Environment :: Console', 'Intended Audience :: Developers', 'Natural Language :: English', 'License :: OSI Approved :: ISC License (ISCL)', 'Operating System :: POSIX :: Linux', 'Operating System :: MacOS :: MacOS X', 'Operating System :: Microsoft :: Windows', 'Programming Language :: Python', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.3', ), **extra_args ) python-colored-traceback-0.3.0/test.py000077500000000000000000000012501310371223700177260ustar00rootroot00000000000000#!/usr/bin/env python from __future__ import absolute_import def main(): args = parse_args() if args.shortcut: if args.always: import colored_traceback.always else: import colored_traceback.auto else: from colored_traceback import add_hook add_hook(always=args.always, debug=args.debug) x = object() x.thing() def parse_args(): import argparse parser = argparse.ArgumentParser() parser.add_argument("--shortcut", action='store_true') parser.add_argument("--always", action='store_true') parser.add_argument("--debug", action='store_true') return parser.parse_args() main()