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

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

2

3 import logging

4

5 import click

6 from click import UsageError, secho

7 from rich.logging import RichHandler

8

9 from bumpversion.indented_logger import IndentedLoggerAdapter

10

11 logger = logging.getLogger("bumpversion")

12

13 VERBOSITY = {

14 0: logging.WARNING,

15 1: logging.INFO,

16 2: logging.DEBUG,

17}

18

19

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

21 """Get a logger with indentation."""

22 return IndentedLoggerAdapter(logging.getLogger(name))

23

24

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

26 """Configure the logging."""

27 logging.basicConfig(

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

29 format="%(message)s",

30 datefmt="[%X]",

31 handlers=[

32 RichHandler(

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

34 )

35 ],

36 )

37 root_logger = get_indented_logger("")

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

39

40

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

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

43 secho(msg)

44

45

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

47 """Raise an error and exit."""

48 raise UsageError(msg)

49

50

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

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

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