Skip to content

Commit

Permalink
Merge branch 'main' into fix/vvv
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Sep 19, 2024
2 parents 0148f36 + 99df05f commit fb78f5f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/ape/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import logging
import sys
import traceback
from collections.abc import Callable, Sequence
from collections.abc import Callable, Iterator, Sequence
from contextlib import contextmanager
from enum import IntEnum
from pathlib import Path
from typing import IO, Any, Optional, Union
Expand Down Expand Up @@ -198,6 +199,23 @@ def set_level(self, level: Union[str, int, LogLevel]):
for _logger in self._extra_loggers.values():
_logger.setLevel(level)

@contextmanager
def at_level(self, level: Union[str, int, LogLevel]) -> Iterator:
"""
Change the log-level in a context.
Args:
level (Union[str, int, LogLevel]): The level to use.
Returns:
Iterator
"""

initial_level = self.level
self.set_level(level)
yield
self.set_level(initial_level)

def log_error(self, err: Exception):
"""
Avoids logging empty messages.
Expand Down
5 changes: 4 additions & 1 deletion src/ape_pm/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def _list(cli_ctx, list_all):
packages.append(item)

if not packages:
click.echo("No packages installed.")
if list_all:
click.echo("No packages installed.")
else:
click.echo("No packages installed. Use `--all` to list all installed dependencies.")
return

# Output gathered packages.
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,12 @@ def cmd(cli_ctx):
def test_set_level(level):
logger.set_level(level)
assert logger.level == LogLevel.INFO.value


def test_at_level():
level_to_set = next(lvl for lvl in LogLevel if lvl != logger.level)
initial_level = logger.level
with logger.at_level(level_to_set):
assert logger.level == level_to_set

assert logger.level == initial_level

0 comments on commit fb78f5f

Please sign in to comment.