mirror of
https://github.com/vale981/ray
synced 2025-03-06 02:21:39 -05:00
[cli] CliLogger typing (#10027)
This commit is contained in:
parent
5b4a10368f
commit
d6226b80bb
1 changed files with 53 additions and 34 deletions
|
@ -13,6 +13,8 @@ import logging
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from typing import Any, Dict, Tuple, Optional, List
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
import colorama
|
import colorama
|
||||||
|
@ -75,12 +77,12 @@ def _parent_frame_info():
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _format_msg(msg,
|
def _format_msg(msg: str,
|
||||||
*args,
|
*args: Any,
|
||||||
_tags=None,
|
_tags: Dict[str, Any] = None,
|
||||||
_numbered=None,
|
_numbered: Tuple[str, int, int] = None,
|
||||||
_no_format=None,
|
_no_format: bool = None,
|
||||||
**kwargs):
|
**kwargs: Any):
|
||||||
"""Formats a message for printing.
|
"""Formats a message for printing.
|
||||||
|
|
||||||
Renders `msg` using the built-in `str.format` and the passed-in
|
Renders `msg` using the built-in `str.format` and the passed-in
|
||||||
|
@ -141,11 +143,8 @@ def _format_msg(msg,
|
||||||
numbering_str = ""
|
numbering_str = ""
|
||||||
if _numbered is not None:
|
if _numbered is not None:
|
||||||
chars, i, n = _numbered
|
chars, i, n = _numbered
|
||||||
|
numbering_str = cf.gray(chars[0] + str(i) + "/" + str(n) +
|
||||||
i = str(i)
|
chars[1]) + " "
|
||||||
n = str(n)
|
|
||||||
|
|
||||||
numbering_str = cf.gray(chars[0] + i + "/" + n + chars[1]) + " "
|
|
||||||
|
|
||||||
if _no_format:
|
if _no_format:
|
||||||
# todo: throw if given args/kwargs?
|
# todo: throw if given args/kwargs?
|
||||||
|
@ -195,6 +194,15 @@ class _CliLogger():
|
||||||
|
|
||||||
! Currently unused.
|
! Currently unused.
|
||||||
"""
|
"""
|
||||||
|
strip: bool
|
||||||
|
old_style: bool
|
||||||
|
color_mode: str
|
||||||
|
# color_mode: Union[Literal["auto"], Literal["false"], Literal["true"]]
|
||||||
|
indent_level: int
|
||||||
|
verbosity: int
|
||||||
|
|
||||||
|
dump_command_output: bool
|
||||||
|
_autodetected_cf_colormode: int
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.strip = False
|
self.strip = False
|
||||||
|
@ -239,7 +247,7 @@ class _CliLogger():
|
||||||
"""
|
"""
|
||||||
self.print("")
|
self.print("")
|
||||||
|
|
||||||
def _print(self, msg, linefeed=True):
|
def _print(self, msg: str, linefeed: bool = True):
|
||||||
"""Proxy for printing messages.
|
"""Proxy for printing messages.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -275,7 +283,7 @@ class _CliLogger():
|
||||||
|
|
||||||
return IndentedContextManager()
|
return IndentedContextManager()
|
||||||
|
|
||||||
def timed(self, msg, *args, **kwargs):
|
def timed(self, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""
|
"""
|
||||||
TODO: Unimplemented special type of output grouping that displays
|
TODO: Unimplemented special type of output grouping that displays
|
||||||
a timer for its execution. The method was not removed so we
|
a timer for its execution. The method was not removed so we
|
||||||
|
@ -286,7 +294,7 @@ class _CliLogger():
|
||||||
"""
|
"""
|
||||||
return self.group(msg, *args, **kwargs)
|
return self.group(msg, *args, **kwargs)
|
||||||
|
|
||||||
def group(self, msg, *args, **kwargs):
|
def group(self, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""Print a group title in a special color and start an indented block.
|
"""Print a group title in a special color and start an indented block.
|
||||||
|
|
||||||
For arguments, see `_format_msg`.
|
For arguments, see `_format_msg`.
|
||||||
|
@ -295,7 +303,7 @@ class _CliLogger():
|
||||||
|
|
||||||
return self.indented()
|
return self.indented()
|
||||||
|
|
||||||
def verbatim_error_ctx(self, msg, *args, **kwargs):
|
def verbatim_error_ctx(self, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""Context manager for printing multi-line error messages.
|
"""Context manager for printing multi-line error messages.
|
||||||
|
|
||||||
Displays a start sequence "!!! {optional message}"
|
Displays a start sequence "!!! {optional message}"
|
||||||
|
@ -316,7 +324,7 @@ class _CliLogger():
|
||||||
|
|
||||||
return VerbatimErorContextManager()
|
return VerbatimErorContextManager()
|
||||||
|
|
||||||
def labeled_value(self, key, msg, *args, **kwargs):
|
def labeled_value(self, key: str, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""Displays a key-value pair with special formatting.
|
"""Displays a key-value pair with special formatting.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -330,7 +338,7 @@ class _CliLogger():
|
||||||
self._print(
|
self._print(
|
||||||
cf.cyan(key) + ": " + _format_msg(cf.bold(msg), *args, **kwargs))
|
cf.cyan(key) + ": " + _format_msg(cf.bold(msg), *args, **kwargs))
|
||||||
|
|
||||||
def verbose(self, msg, *args, **kwargs):
|
def verbose(self, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""Prints a message if verbosity is not 0.
|
"""Prints a message if verbosity is not 0.
|
||||||
|
|
||||||
For arguments, see `_format_msg`.
|
For arguments, see `_format_msg`.
|
||||||
|
@ -346,7 +354,7 @@ class _CliLogger():
|
||||||
if self.verbosity > 0:
|
if self.verbosity > 0:
|
||||||
self.warning(msg, *args, **kwargs)
|
self.warning(msg, *args, **kwargs)
|
||||||
|
|
||||||
def verbose_error(self, msg, *args, **kwargs):
|
def verbose_error(self, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""Logs an error if verbosity is not 0.
|
"""Logs an error if verbosity is not 0.
|
||||||
|
|
||||||
For arguments, see `_format_msg`.
|
For arguments, see `_format_msg`.
|
||||||
|
@ -354,7 +362,7 @@ class _CliLogger():
|
||||||
if self.verbosity > 0:
|
if self.verbosity > 0:
|
||||||
self.error(msg, *args, **kwargs)
|
self.error(msg, *args, **kwargs)
|
||||||
|
|
||||||
def very_verbose(self, msg, *args, **kwargs):
|
def very_verbose(self, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""Prints if verbosity is > 1.
|
"""Prints if verbosity is > 1.
|
||||||
|
|
||||||
For arguments, see `_format_msg`.
|
For arguments, see `_format_msg`.
|
||||||
|
@ -362,28 +370,28 @@ class _CliLogger():
|
||||||
if self.verbosity > 1:
|
if self.verbosity > 1:
|
||||||
self.print(msg, *args, **kwargs)
|
self.print(msg, *args, **kwargs)
|
||||||
|
|
||||||
def success(self, msg, *args, **kwargs):
|
def success(self, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""Prints a formatted success message.
|
"""Prints a formatted success message.
|
||||||
|
|
||||||
For arguments, see `_format_msg`.
|
For arguments, see `_format_msg`.
|
||||||
"""
|
"""
|
||||||
self.print(cf.green(msg), *args, **kwargs)
|
self.print(cf.green(msg), *args, **kwargs)
|
||||||
|
|
||||||
def warning(self, msg, *args, **kwargs):
|
def warning(self, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""Prints a formatted warning message.
|
"""Prints a formatted warning message.
|
||||||
|
|
||||||
For arguments, see `_format_msg`.
|
For arguments, see `_format_msg`.
|
||||||
"""
|
"""
|
||||||
self.print(cf.yellow(msg), *args, **kwargs)
|
self.print(cf.yellow(msg), *args, **kwargs)
|
||||||
|
|
||||||
def error(self, msg, *args, **kwargs):
|
def error(self, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""Prints a formatted error message.
|
"""Prints a formatted error message.
|
||||||
|
|
||||||
For arguments, see `_format_msg`.
|
For arguments, see `_format_msg`.
|
||||||
"""
|
"""
|
||||||
self.print(cf.red(msg), *args, **kwargs)
|
self.print(cf.red(msg), *args, **kwargs)
|
||||||
|
|
||||||
def print(self, msg, *args, **kwargs):
|
def print(self, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""Prints a message.
|
"""Prints a message.
|
||||||
|
|
||||||
For arguments, see `_format_msg`.
|
For arguments, see `_format_msg`.
|
||||||
|
@ -393,7 +401,7 @@ class _CliLogger():
|
||||||
|
|
||||||
self._print(_format_msg(msg, *args, **kwargs))
|
self._print(_format_msg(msg, *args, **kwargs))
|
||||||
|
|
||||||
def abort(self, msg=None, *args, **kwargs):
|
def abort(self, msg: Optional[str] = None, *args: Any, **kwargs: Any):
|
||||||
"""Prints an error and aborts execution.
|
"""Prints an error and aborts execution.
|
||||||
|
|
||||||
Print an error and throw an exception to terminate the program
|
Print an error and throw an exception to terminate the program
|
||||||
|
@ -407,7 +415,7 @@ class _CliLogger():
|
||||||
|
|
||||||
raise SilentClickException("Exiting due to cli_logger.abort()")
|
raise SilentClickException("Exiting due to cli_logger.abort()")
|
||||||
|
|
||||||
def doassert(self, val, msg, *args, **kwargs):
|
def doassert(self, val: bool, msg: str, *args: Any, **kwargs: Any):
|
||||||
"""Handle assertion without throwing a scary exception.
|
"""Handle assertion without throwing a scary exception.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -421,7 +429,8 @@ class _CliLogger():
|
||||||
if not val:
|
if not val:
|
||||||
self.abort(msg, *args, **kwargs)
|
self.abort(msg, *args, **kwargs)
|
||||||
|
|
||||||
def old_debug(self, logger, msg, *args, **kwargs):
|
def old_debug(self, logger: logging.Logger, msg: str, *args: Any,
|
||||||
|
**kwargs: Any):
|
||||||
"""Old debug logging proxy.
|
"""Old debug logging proxy.
|
||||||
|
|
||||||
Pass along an old debug log iff new logging is disabled.
|
Pass along an old debug log iff new logging is disabled.
|
||||||
|
@ -438,7 +447,8 @@ class _CliLogger():
|
||||||
_format_msg(msg, *args, **kwargs), extra=_parent_frame_info())
|
_format_msg(msg, *args, **kwargs), extra=_parent_frame_info())
|
||||||
return
|
return
|
||||||
|
|
||||||
def old_info(self, logger, msg, *args, **kwargs):
|
def old_info(self, logger: logging.Logger, msg: str, *args: Any,
|
||||||
|
**kwargs: Any):
|
||||||
"""Old info logging proxy.
|
"""Old info logging proxy.
|
||||||
|
|
||||||
Pass along an old info log iff new logging is disabled.
|
Pass along an old info log iff new logging is disabled.
|
||||||
|
@ -455,7 +465,8 @@ class _CliLogger():
|
||||||
_format_msg(msg, *args, **kwargs), extra=_parent_frame_info())
|
_format_msg(msg, *args, **kwargs), extra=_parent_frame_info())
|
||||||
return
|
return
|
||||||
|
|
||||||
def old_warning(self, logger, msg, *args, **kwargs):
|
def old_warning(self, logger: logging.Logger, msg: str, *args: Any,
|
||||||
|
**kwargs: Any):
|
||||||
"""Old warning logging proxy.
|
"""Old warning logging proxy.
|
||||||
|
|
||||||
Pass along an old warning log iff new logging is disabled.
|
Pass along an old warning log iff new logging is disabled.
|
||||||
|
@ -472,7 +483,8 @@ class _CliLogger():
|
||||||
_format_msg(msg, *args, **kwargs), extra=_parent_frame_info())
|
_format_msg(msg, *args, **kwargs), extra=_parent_frame_info())
|
||||||
return
|
return
|
||||||
|
|
||||||
def old_error(self, logger, msg, *args, **kwargs):
|
def old_error(self, logger: logging.Logger, msg: str, *args: Any,
|
||||||
|
**kwargs: Any):
|
||||||
"""Old error logging proxy.
|
"""Old error logging proxy.
|
||||||
|
|
||||||
Pass along an old error log iff new logging is disabled.
|
Pass along an old error log iff new logging is disabled.
|
||||||
|
@ -489,7 +501,8 @@ class _CliLogger():
|
||||||
_format_msg(msg, *args, **kwargs), extra=_parent_frame_info())
|
_format_msg(msg, *args, **kwargs), extra=_parent_frame_info())
|
||||||
return
|
return
|
||||||
|
|
||||||
def old_exception(self, logger, msg, *args, **kwargs):
|
def old_exception(self, logger: logging.Logger, msg: str, *args: Any,
|
||||||
|
**kwargs: Any):
|
||||||
"""Old exception logging proxy.
|
"""Old exception logging proxy.
|
||||||
|
|
||||||
Pass along an old exception log iff new logging is disabled.
|
Pass along an old exception log iff new logging is disabled.
|
||||||
|
@ -506,12 +519,18 @@ class _CliLogger():
|
||||||
_format_msg(msg, *args, **kwargs), extra=_parent_frame_info())
|
_format_msg(msg, *args, **kwargs), extra=_parent_frame_info())
|
||||||
return
|
return
|
||||||
|
|
||||||
def render_list(self, xs, separator=cf.reset(", ")):
|
def render_list(self, xs: List[str], separator: str = cf.reset(", ")):
|
||||||
"""Render a list of bolded values using a non-bolded separator.
|
"""Render a list of bolded values using a non-bolded separator.
|
||||||
"""
|
"""
|
||||||
return separator.join([str(cf.bold(x)) for x in xs])
|
return separator.join([str(cf.bold(x)) for x in xs])
|
||||||
|
|
||||||
def confirm(self, yes, msg, *args, _abort=False, _default=False, **kwargs):
|
def confirm(self,
|
||||||
|
yes: bool,
|
||||||
|
msg: str,
|
||||||
|
*args: Any,
|
||||||
|
_abort: bool = False,
|
||||||
|
_default: bool = False,
|
||||||
|
**kwargs: Any):
|
||||||
"""Display a confirmation dialog.
|
"""Display a confirmation dialog.
|
||||||
|
|
||||||
Valid answers are "y/yes/true/1" and "n/no/false/0".
|
Valid answers are "y/yes/true/1" and "n/no/false/0".
|
||||||
|
@ -592,7 +611,7 @@ class _CliLogger():
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def old_confirm(self, msg, yes):
|
def old_confirm(self, msg: str, yes: bool):
|
||||||
"""Old confirm dialog proxy.
|
"""Old confirm dialog proxy.
|
||||||
|
|
||||||
Let `click` display a confirm dialog iff new logging is disabled.
|
Let `click` display a confirm dialog iff new logging is disabled.
|
||||||
|
@ -613,7 +632,7 @@ class SilentClickException(click.ClickException):
|
||||||
colors and other formatting.
|
colors and other formatting.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, message):
|
def __init__(self, message: str):
|
||||||
super(SilentClickException, self).__init__(message)
|
super(SilentClickException, self).__init__(message)
|
||||||
|
|
||||||
def show(self, file=None):
|
def show(self, file=None):
|
||||||
|
|
Loading…
Add table
Reference in a new issue