From 879bc422638325263f3786a58138d4bd8c4a8018 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 18 Nov 2020 17:33:07 +0000 Subject: [PATCH] Enable multiple section wrappers --- src/molecule/command/base.py | 21 ++------------------- src/molecule/logger.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/molecule/command/base.py b/src/molecule/command/base.py index 754abc149..2acdac79d 100644 --- a/src/molecule/command/base.py +++ b/src/molecule/command/base.py @@ -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.""" @@ -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 diff --git a/src/molecule/logger.py b/src/molecule/logger.py index 2470c13d9..bd0424aa6 100644 --- a/src/molecule/logger.py +++ b/src/molecule/logger.py @@ -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 @@ -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 )