Skip to content

Commit

Permalink
Enable multiple section wrappers (#2984)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Nov 19, 2020
1 parent e4b2bc8 commit 3f640d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
21 changes: 2 additions & 19 deletions src/molecule/command/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,6 @@
MOLECULE_DEFAULT_SCENARIO_NAME = "default"


def section_logger(func: Callable) -> Callable:
"""Wrap effective execution of a method."""

def wrapper(*args, **kwargs):
self = args[0]
LOG.info(
"[info]Running [scenario]%s[/] > [action]%s[/][/]",
self._config.scenario.name,
text.underscore(self.__class__.__name__),
extra={"markup": True},
)
rt = func(*args, **kwargs)
# section close code goes here
return rt

return wrapper


class Base(object, metaclass=abc.ABCMeta):
"""An abstract base class used to define the command interface."""

Expand All @@ -72,7 +54,8 @@ def __init__(self, c):
def __init_subclass__(cls) -> None:
"""Decorate execute from all subclasses."""
super().__init_subclass__()
setattr(cls, "execute", section_logger(cls.execute))
for wrapper in logger.get_section_loggers():
setattr(cls, "execute", wrapper(cls.execute))

@abc.abstractmethod
def execute(self): # pragma: no cover
Expand Down
25 changes: 25 additions & 0 deletions src/molecule/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import logging
import sys
from functools import lru_cache
from typing import Callable, Iterable

from enrich.console import Console
from enrich.logging import RichHandler

from molecule.console import should_do_markup, theme
from molecule.text import underscore

SUCCESS = 100
OUT = 101
Expand Down Expand Up @@ -74,6 +76,29 @@ def get_logger(name=None) -> logging.Logger:
return logger


def section_logger(func: Callable) -> Callable:
"""Wrap effective execution of a method."""

def wrapper(*args, **kwargs):
self = args[0]
get_logger().info(
"[info]Running [scenario]%s[/] > [action]%s[/][/]",
self._config.scenario.name,
underscore(self.__class__.__name__),
extra={"markup": True},
)
rt = func(*args, **kwargs)
# section close code goes here
return rt

return wrapper


def get_section_loggers() -> Iterable[Callable]:
"""Return a list of section wrappers to be added."""
return [section_logger]


LOGGING_CONSOLE = Console(
file=sys.stderr, force_terminal=should_do_markup(), theme=theme
)

0 comments on commit 3f640d7

Please sign in to comment.