Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable multiple section wrappers #2984

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
Copy link
Contributor

@cognifloyd cognifloyd Nov 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you imagining a separate wrapper function for each of the output types?

return [github_actions_grouper, gitlab_sectioner, travis_ci_folder, section_logger]

Then the environment var checks would happen during each of those?
or something more like what I had before:

return [ci_section_wrapper, section_logger]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add them to the logger module. I was considering console initially but due to some circular import issues I decided to go for logger. logger can import console but console does not, this being the main reason.

Yep, I did fancy adding these to enrich but I am afraid that it may not be so easy. If I am correct, there wrappers may require some parameters like section name.

For the moment just extend get_section_loggers to make it return additional wrappens when needed, add them inside the same file and also add lru_cache() wrapper on it for performance reasons, as we do not want it to run each time it wraps a class.



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