Skip to content

Commit

Permalink
feat: move Logger into a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
pivoshenko committed May 6, 2024
1 parent decaf2b commit cef85e3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 49 deletions.
57 changes: 57 additions & 0 deletions src/poetry_plugin_dotenv/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Module that contains plugin logger."""

from __future__ import annotations

import enum

from typing import TYPE_CHECKING


if TYPE_CHECKING: # pragma: no cover
from cleo.events.console_command_event import ConsoleCommandEvent


class Verbosity(enum.Enum): # pragma: no cover
"""Levels of verbosity."""

info = "<info>{0!s}</info>"
debug = "<debug>{0!s}</debug>"
warning = "<warning>{0!s}</warning>"
error = "<error>{0!s}</error>"


class Logger: # pragma: no cover
"""Logger of the ``poetry`` events.
Because this logger is used for the plugin that are running before the main command,
all the messages will be logged only in the debug mode.
"""

def __init__(self, event: ConsoleCommandEvent) -> None:
"""Initialize."""

self.event = event

def info(self, msg: str) -> None:
"""Log a info message."""

if self.event.io.is_debug():
self.event.io.write_line(Verbosity.info.value.format(msg))

def debug(self, msg: str) -> None:
"""Log a debug message."""

if self.event.io.is_debug():
self.event.io.write_line(Verbosity.debug.value.format(msg))

def warning(self, msg: str) -> None:
"""Log a warning message."""

if self.event.io.is_debug():
self.event.io.write_line(Verbosity.warning.value.format(msg))

def error(self, msg: str) -> None:
"""Log a error message."""

if self.event.io.is_debug():
self.event.io.write_error_line(Verbosity.error.value.format(msg))
52 changes: 3 additions & 49 deletions src/poetry_plugin_dotenv/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,23 @@
from __future__ import annotations

import os
import enum

from typing import TYPE_CHECKING

from cleo.events.console_events import COMMAND
from poetry.console.commands.env_command import EnvCommand
from poetry.plugins.application_plugin import ApplicationPlugin

from poetry_plugin_dotenv import config
from poetry_plugin_dotenv import dotenv
from poetry_plugin_dotenv.config import Config
from poetry_plugin_dotenv.logger import Logger


if TYPE_CHECKING: # pragma: no cover
from cleo.events.console_command_event import ConsoleCommandEvent
from poetry.console.application import Application


class Verbosity(enum.Enum): # pragma: no cover
"""Levels of verbosity."""

info = "<info>{0!s}</info>"
debug = "<debug>{0!s}</debug>"
warning = "<warning>{0!s}</warning>"
error = "<error>{0!s}</error>"


class Logger: # pragma: no cover
"""Logger of the ``poetry`` events.
Because this logger is used for the plugin that are running before the main command,
all the messages will be logged only in the debug mode.
"""

def __init__(self, event: ConsoleCommandEvent) -> None:
"""Initialize."""

self.event = event

def info(self, msg: str) -> None:
"""Log a info message."""

if self.event.io.is_debug():
self.event.io.write_line(Verbosity.info.value.format(msg))

def debug(self, msg: str) -> None:
"""Log a debug message."""

if self.event.io.is_debug():
self.event.io.write_line(Verbosity.debug.value.format(msg))

def warning(self, msg: str) -> None:
"""Log a warning message."""

if self.event.io.is_debug():
self.event.io.write_line(Verbosity.warning.value.format(msg))

def error(self, msg: str) -> None:
"""Log a error message."""

if self.event.io.is_debug():
self.event.io.write_error_line(Verbosity.error.value.format(msg))


class DotenvPlugin(ApplicationPlugin):
"""Plugin that automatically loads environment variables from a dotenv file.
Expand All @@ -82,7 +36,7 @@ def load(self, event: ConsoleCommandEvent, *args, **kwargs) -> None:
"""Load a dotenv file."""

logger = Logger(event)
configuration = config.Config()
configuration = Config()

is_env_command = isinstance(event.command, EnvCommand)

Expand Down

0 comments on commit cef85e3

Please sign in to comment.