Skip to content

Commit

Permalink
python encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Mrázek committed Oct 3, 2022
1 parent 5f1c662 commit 511c9a8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
1 change: 1 addition & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ jobs:
poetry install
- name: Build
run: |
PYTHONUTF8=1
poetry run task build
54 changes: 22 additions & 32 deletions src/wanna/core/loggers/wanna_logger.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
import logging
import sys
from typing import cast

import typer
from halo import Halo
from rich.console import Console
from rich.live import Live


class Spinner(Halo):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.is_tty = sys.stdout.isatty()

def __enter__(self):
"""Starts the spinner on a separate thread. For use in context managers.
The spinner is actually started only in the interactive terminal (tty),
if the environment is output only, we only print the start and end of the process.
class Spinner(Live):
def __init__(self, text: str, **kwargs):
self.text = text
super().__init__(text, **kwargs)

Returns
-------
self
"""
return self.start() if self.is_tty else self.info()
def __enter__(self) -> Live:
self.update(f":hourglass_flowing_sand: {self.text}")
self.start(refresh=self._renderable is not None)
return self

def __exit__(self, exception_type, exception_value, traceback):
"""Stops the spinner. For use in context managers."""
if exception_value:
self.text_color = typer.colors.RED
self.fail()
self.update(f":x: {self.text}")
else:
self.text_color = typer.colors.GREEN
self.succeed()
self.update(f":white_check_mark: {self.text}")
self.stop()


class WannaLogger(logging.Logger):
Expand All @@ -44,22 +36,20 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Set the logging config here if needed
logging.basicConfig()
self.console = Console()

@staticmethod
def user_error(text, fg: str = typer.colors.RED, *args, **kwargs) -> None:
typer.secho(f"✖ {text}", fg=fg, *args, **kwargs)
def user_error(self, text) -> None:
self.console.print(f":x: {text}", style="bold red")

@staticmethod
def user_info(text, *args, **kwargs) -> None:
typer.secho(f"ℹ {text}", *args, **kwargs)
def user_info(self, text) -> None:
self.console.print(f":information_source: {text}")

@staticmethod
def user_success(text, fg: str = typer.colors.GREEN, *args, **kwargs) -> None:
typer.secho(f"✔ {text}", fg=fg, *args, **kwargs)
def user_success(self, text) -> None:
self.console.print(f":white_check_mark: {text}")

@staticmethod
def user_spinner(text, *args, **kwargs) -> Spinner:
return Spinner(text=text, *args, **kwargs)
def user_spinner(text, **kwargs) -> Spinner:
return Spinner(text=text, **kwargs)


def get_logger(name: str) -> WannaLogger:
Expand Down

0 comments on commit 511c9a8

Please sign in to comment.