gecko-dev/third_party/python/pip_tools/piptools/logging.py
ahochheiden bc34a9d56d Bug 1857470 - Vendor pip-tools at version 7.4.1 r=mach-reviewers,ahal
While nothing explicitly prompted updating this, letting `pip` and
`pip-tools` diverage too greatly in version release dates seems like
a bad idea, especially with the various deprecations in
`pip` and `python3.12`.

This also vendors the implicit dependencies `build`, `tomli`, and
`pyproject_hooks`.

Differential Revision: https://phabricator.services.mozilla.com/D210526
2024-05-27 23:06:19 +00:00

68 lines
1.9 KiB
Python

from __future__ import annotations
import contextlib
import logging
import sys
from typing import Any, Iterator
import click
# Initialise the builtin logging module for other component using it.
# Ex: pip
logging.basicConfig()
class LogContext:
stream = sys.stderr
def __init__(self, verbosity: int = 0, indent_width: int = 2):
self.verbosity = self._initial_verbosity = verbosity
self.current_indent = self._initial_indent = 0
self._indent_width = self._initial_indent_width = indent_width
def log(self, message: str, *args: Any, **kwargs: Any) -> None:
kwargs.setdefault("err", True)
prefix = " " * self.current_indent
click.secho(prefix + message, *args, **kwargs)
def debug(self, message: str, *args: Any, **kwargs: Any) -> None:
if self.verbosity >= 1:
self.log(message, *args, **kwargs)
def info(self, message: str, *args: Any, **kwargs: Any) -> None:
if self.verbosity >= 0:
self.log(message, *args, **kwargs)
def warning(self, message: str, *args: Any, **kwargs: Any) -> None:
kwargs.setdefault("fg", "yellow")
self.log(message, *args, **kwargs)
def error(self, message: str, *args: Any, **kwargs: Any) -> None:
kwargs.setdefault("fg", "red")
self.log(message, *args, **kwargs)
def _indent(self) -> None:
self.current_indent += self._indent_width
def _dedent(self) -> None:
self.current_indent -= self._indent_width
@contextlib.contextmanager
def indentation(self) -> Iterator[None]:
"""
Increase indentation.
"""
self._indent()
try:
yield
finally:
self._dedent()
def reset(self) -> None:
"""Reset logger to initial state."""
self.verbosity = self._initial_verbosity
self.current_indent = self._initial_indent
self._indent_width = self._initial_indent_width
log = LogContext()