Skip to content

Commit

Permalink
Depend on and use Colorama on Windows? (as suggested in #1)
Browse files Browse the repository at this point in the history
I can't actually test this because I don't have access to a Windows
system, but I guess some day someone will complain if this doesn't work
as intended ;-).
  • Loading branch information
xolox committed Oct 23, 2015
1 parent 52f6dd0 commit 008cb7e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ The `coloredlogs` package enables colored terminal output for Python's logging_
module. The ColoredFormatter_ class inherits from `logging.Formatter`_ and uses
`ANSI escape sequences`_ to render your logging messages in color. It uses
only standard colors so it should work on any UNIX terminal. It's currently
tested on Python 2.6, 2.7, 3.4 and PyPy. This module does not support non-UNIX
terminals (e.g. the Windows console) but will fall back to plain text. Here is
a screen shot of the demo that is printed when the command ``coloredlogs
--demo`` is executed:
tested on Python 2.6, 2.7, 3.4 and PyPy. On Windows `coloredlogs` automatically
pulls in Colorama_ as a dependency and enables ANSI escape sequence translation
using Colorama. Here is a screen shot of the demo that is printed when the
command ``coloredlogs --demo`` is executed:

.. image:: https://peterodding.com/code/python/coloredlogs/screenshots/terminal.png

Expand Down
28 changes: 25 additions & 3 deletions coloredlogs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"""

# Semi-standard module versioning.
__version__ = '3.0'
__version__ = '3.1'

# Standard library modules.
import collections
Expand All @@ -120,6 +120,16 @@
from humanfriendly.terminal import ANSI_COLOR_CODES, ansi_wrap, terminal_supports_colors
from humanfriendly.text import split

# Optional external dependency (only needed on Windows).
NEED_COLORAMA = (sys.platform == 'win32')
HAVE_COLORAMA = False
if NEED_COLORAMA:
try:
import colorama
HAVE_COLORAMA = True
except ImportError:
pass

DEFAULT_LOG_FORMAT = '%(asctime)s %(hostname)s %(name)s[%(process)d] %(levelname)s %(message)s'
"""The default log format for :class:`ColoredFormatter` objects (a string)."""

Expand Down Expand Up @@ -236,8 +246,20 @@ def install(level=None, **kw):
stream = kw.get('stream', sys.stderr)
# Figure out whether we can use ANSI escape sequences.
use_colors = kw.get('isatty', None)
if use_colors is None:
use_colors = terminal_supports_colors(stream)
if use_colors or use_colors is None:
if NEED_COLORAMA:
if HAVE_COLORAMA:
# On Windows we can only use ANSI escape
# sequences if Colorama is available.
colorama.init()
use_colors = True
else:
# If Colorama isn't available then we specifically
# shouldn't emit ANSI escape sequences!
use_colors = False
elif use_colors is None:
# Auto-detect terminal support on other platforms.
use_colors = terminal_supports_colors(stream)
# Create a stream handler.
root_handler = logging.StreamHandler(stream)
if level is None:
Expand Down
14 changes: 11 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import codecs
import os
import re
import sys

# De-facto standard solution for Python packaging.
from setuptools import find_packages, setup
Expand All @@ -33,6 +34,15 @@
with codecs.open(readme_file, 'r', 'utf-8') as handle:
readme_text = handle.read()

# External dependencies.
install_requires = [
'humanfriendly >= 1.42',
]

# Conditional dependency (Windows only).
if sys.platform == 'win32':
install_requires.append('colorama')

setup(name='coloredlogs',
version=version_string,
description="Colored terminal output for Python's logging module",
Expand All @@ -44,9 +54,7 @@
entry_points=dict(console_scripts=[
'coloredlogs = coloredlogs.cli:main',
]),
install_requires=[
'humanfriendly >= 1.42',
],
install_requires=install_requires,
test_suite='coloredlogs.tests',
tests_require=[
'capturer',
Expand Down

1 comment on commit 008cb7e

@xolox
Copy link
Owner Author

@xolox xolox commented on 008cb7e Oct 23, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intended to refer to issue #2 from this commit but typed #1 instead, so here's a manual notification :-).

Please sign in to comment.