Skip to content

Commit

Permalink
Add optional color support to decapod CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
9seconds committed Nov 24, 2016
1 parent dc6f9d6 commit 80d8f52
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
25 changes: 25 additions & 0 deletions decapodcli/decapodcli/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

from decapodlib import exceptions

try:
import pygments
except ImportError:
pygments = None


def catch_errors(func):
"""Decorator which catches all errors and tries to print them."""
Expand Down Expand Up @@ -76,6 +81,26 @@ def decorator(ctx, *args, **kwargs):
return decorator


def with_color(func):
"""Decorator which adds --color option if available."""

if pygments is None:
def decorator(*args, **kwargs):
kwargs["color"] = None
return func(*args, **kwargs)
else:
decorator = click.option(
"--color",
default=None,
type=click.Choice(["light", "dark"]),
help="Colorize output. By default no color is used."
)(func)

decorator = six.wraps(func)(decorator)

return decorator


def with_pagination(func):
"""Add pagination-related commandline options."""

Expand Down
13 changes: 8 additions & 5 deletions decapodcli/decapodcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@
type=click.Choice(["json"]),
help="How to format output. Currently only JSON is supported."
)
@decorators.with_color
@click.pass_context
def cli(ctx, url, login, password, no_verify, ssl_certificate, debug,
timeout, no_pager, pager, output_format):
timeout, no_pager, pager, color, output_format):
"""Decapod command line tool.
With this CLI it is possible to access all API endpoints of Decapod.
Expand Down Expand Up @@ -130,7 +131,6 @@ def cli(ctx, url, login, password, no_verify, ssl_certificate, debug,
"pager support, please use --pager option.",
PendingDeprecationWarning
)
pagerize = False
if pager:
pagerize = True

Expand All @@ -146,9 +146,12 @@ def cli(ctx, url, login, password, no_verify, ssl_certificate, debug,
"timeout": timeout,
"format": output_format,
"pager": pagerize,
"client": decapodlib.Client(url, login, password,
timeout=timeout, verify=not no_verify,
certificate_file=ssl_certificate)
"color": color,
"client": decapodlib.Client(
url, login, password,
timeout=timeout, verify=not no_verify,
certificate_file=ssl_certificate
)
}
utils.configure_logging(debug)

Expand Down
20 changes: 20 additions & 0 deletions decapodcli/decapodcli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
except ImportError:
import json

try:
import pygments
import pygments.lexers
import pygments.formatters
except ImportError:
pygments = None


def json_loads(data):
if isinstance(data, bytes):
Expand All @@ -46,6 +53,7 @@ def parameter_name(name):

def format_output_json(ctx, response, error=False):
response = json_dumps(response)
response = colorize(response, ctx.obj["color"], "json")

if error:
click.echo(response, err=True)
Expand Down Expand Up @@ -84,3 +92,15 @@ def configure_logging(debug):
six.moves.http_client.HTTPConnection.debuglevel = 0
logging.getLogger().setLevel(logging.CRITICAL)
requests_log.setLevel(logging.CRITICAL)


def colorize(text, color, lexer):
if pygments is None or not color:
return text

lexer_obj = pygments.lexers.get_lexer_by_name(lexer, ensurenl=False)
formatter_obj = pygments.formatters.get_formatter_by_name(
"terminal", bg=color)
colorized = pygments.highlight(text, lexer_obj, formatter_obj)

return colorized
3 changes: 2 additions & 1 deletion decapodcli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
]
},
extras_require={
"simplejson": ["simplejson"]
"simplejson": ["simplejson"],
"color": ["pygments"]
},
classifiers=(
"Intended Audience :: Information Technology",
Expand Down

0 comments on commit 80d8f52

Please sign in to comment.