2020-01-21 01:08:28 -07:00
|
|
|
"""Helper functions for CLI message printing."""
|
2020-02-14 16:28:45 -07:00
|
|
|
# Standard Library
|
|
|
|
import re
|
|
|
|
|
2020-02-03 02:35:11 -07:00
|
|
|
# Third Party
|
2020-02-14 16:28:45 -07:00
|
|
|
from click import echo, style
|
2020-01-21 01:08:28 -07:00
|
|
|
|
2020-02-03 02:35:11 -07:00
|
|
|
# Project
|
2020-02-14 16:28:45 -07:00
|
|
|
from hyperglass.cli.static import CMD_HELP, Message
|
|
|
|
from hyperglass.cli.exceptions import CliError
|
2020-01-21 01:08:28 -07:00
|
|
|
|
|
|
|
|
|
|
|
def cmd_help(emoji="", help_text=""):
|
|
|
|
"""Print formatted command help."""
|
2020-02-14 16:28:45 -07:00
|
|
|
return emoji + style(help_text, **CMD_HELP)
|
|
|
|
|
|
|
|
|
|
|
|
def _base_formatter(state, text, callback, **kwargs):
|
|
|
|
"""Format text block, replace template strings with keyword arguments.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
state {dict} -- Text format attributes
|
|
|
|
label {dict} -- Keyword format attributes
|
|
|
|
text {[type]} -- Text to format
|
|
|
|
callback {function} -- Callback function
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
{str|ClickException} -- Formatted output
|
|
|
|
"""
|
|
|
|
fmt = Message(state)
|
|
|
|
|
|
|
|
if callback is None:
|
|
|
|
callback = style
|
|
|
|
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
if not isinstance(v, str):
|
|
|
|
v = str(v)
|
|
|
|
kwargs[k] = style(v, **fmt.kw)
|
|
|
|
|
|
|
|
text_all = re.split(r"(\{\w+\})", text)
|
|
|
|
text_all = [style(i, **fmt.msg) for i in text_all]
|
|
|
|
text_all = [i.format(**kwargs) for i in text_all]
|
|
|
|
|
|
|
|
if fmt.emoji:
|
|
|
|
text_all.insert(0, fmt.emoji)
|
|
|
|
|
|
|
|
text_fmt = "".join(text_all)
|
|
|
|
|
|
|
|
return callback(text_fmt)
|
|
|
|
|
|
|
|
|
|
|
|
def info(text, callback=echo, **kwargs):
|
|
|
|
"""Generate formatted informational text.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
text {str} -- Text to format
|
|
|
|
callback {callable} -- Callback function (default: {echo})
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
{str} -- Informational output
|
|
|
|
"""
|
|
|
|
return _base_formatter(state="info", text=text, callback=callback, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def error(text, callback=CliError, **kwargs):
|
|
|
|
"""Generate formatted exception.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
text {str} -- Text to format
|
|
|
|
callback {callable} -- Callback function (default: {echo})
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ClickException: Raised after formatting
|
|
|
|
"""
|
|
|
|
raise _base_formatter(state="error", text=text, callback=callback, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def success(text, callback=echo, **kwargs):
|
|
|
|
"""Generate formatted success text.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
text {str} -- Text to format
|
|
|
|
callback {callable} -- Callback function (default: {echo})
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
{str} -- Success output
|
|
|
|
"""
|
|
|
|
return _base_formatter(state="success", text=text, callback=callback, **kwargs)
|
2020-01-21 01:08:28 -07:00
|
|
|
|
|
|
|
|
2020-02-14 16:28:45 -07:00
|
|
|
def warning(text, callback=echo, **kwargs):
|
|
|
|
"""Generate formatted warning text.
|
2020-01-21 01:08:28 -07:00
|
|
|
|
2020-02-14 16:28:45 -07:00
|
|
|
Arguments:
|
|
|
|
text {str} -- Text to format
|
|
|
|
callback {callable} -- Callback function (default: {echo})
|
2020-01-21 01:08:28 -07:00
|
|
|
|
2020-02-14 16:28:45 -07:00
|
|
|
Returns:
|
|
|
|
{str} -- Warning output
|
|
|
|
"""
|
|
|
|
return _base_formatter(state="warning", text=text, callback=callback, **kwargs)
|
2020-01-21 01:08:28 -07:00
|
|
|
|
|
|
|
|
2020-02-14 16:28:45 -07:00
|
|
|
def label(text, callback=echo, **kwargs):
|
|
|
|
"""Generate formatted info text with accented labels.
|
2020-01-21 01:08:28 -07:00
|
|
|
|
2020-02-14 16:28:45 -07:00
|
|
|
Arguments:
|
|
|
|
text {str} -- Text to format
|
|
|
|
callback {callable} -- Callback function (default: {echo})
|
2020-01-21 01:08:28 -07:00
|
|
|
|
2020-02-14 16:28:45 -07:00
|
|
|
Returns:
|
|
|
|
{str} -- Label output
|
|
|
|
"""
|
|
|
|
return _base_formatter(state="label", text=text, callback=callback, **kwargs)
|
2020-01-21 01:08:28 -07:00
|
|
|
|
|
|
|
|
2020-02-14 16:28:45 -07:00
|
|
|
def status(text, callback=echo, **kwargs):
|
|
|
|
"""Generate formatted status text.
|
2020-01-21 01:08:28 -07:00
|
|
|
|
2020-02-14 16:28:45 -07:00
|
|
|
Arguments:
|
|
|
|
text {str} -- Text to format
|
|
|
|
callback {callable} -- Callback function (default: {echo})
|
2020-01-21 01:08:28 -07:00
|
|
|
|
2020-02-14 16:28:45 -07:00
|
|
|
Returns:
|
|
|
|
{str} -- Status output
|
|
|
|
"""
|
|
|
|
return _base_formatter(state="status", text=text, callback=callback, **kwargs)
|