Coverage for /Users/OORDCOR/Documents/code/bump-my-version/bumpversion/ui.py: 68%

1"""Utilities for user interface."""

2 import logging

3

4 import click

5 from click import UsageError, secho

6 from rich.logging import RichHandler

7

8 from bumpversion.indented_logger import IndentedLoggerAdapter

9

10 logger = logging.getLogger("bumpversion")

11

12 VERBOSITY = {

13 0: logging.WARNING,

14 1: logging.INFO,

15 2: logging.DEBUG,

16}

17

18

19 def get_indented_logger(name: str) -> "IndentedLoggerAdapter":

20 """Get a logger with indentation."""

21 return IndentedLoggerAdapter(logging.getLogger(name))

22

23

24 def setup_logging(verbose: int = 0) -> None:

25 """Configure the logging."""

26 logging.basicConfig(

27 level=VERBOSITY.get(verbose, logging.DEBUG),

28 format="%(message)s",

29 datefmt="[%X]",

30 handlers=[

31 RichHandler(

32 rich_tracebacks=True, show_level=False, show_path=False, show_time=False, tracebacks_suppress=[click]

33 )

34 ],

35 )

36 root_logger = get_indented_logger("")

37 root_logger.setLevel(VERBOSITY.get(verbose, logging.DEBUG))

38

39

40 def print_info(msg: str) -> None:

41 """Echo a message to the console."""

42 secho(msg)

43

44

45 def print_error(msg: str) -> None:

46 """Raise an error and exit."""

47 raise UsageError(msg)

48

49

50 def print_warning(msg: str) -> None:

51 """Echo a warning to the console."""

52 secho(f"\nWARNING:\n\n{msg}\n", fg="yellow")