Skip to content

Commit

Permalink
Use helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
timmo001 committed May 27, 2022
1 parent 2a328f3 commit 89f56ac
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
4 changes: 3 additions & 1 deletion sanic/application/logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from os import environ

from sanic.compat import is_atty


BASE_LOGO = """
Expand Down Expand Up @@ -44,7 +46,7 @@
def get_logo(full=False, coffee=False):
logo = (
(FULL_COLOR_LOGO if full else (COFFEE_LOGO if coffee else COLOR_LOGO))
if sys.stdout and sys.stdout.isatty()
if is_atty()
else BASE_LOGO
)

Expand Down
3 changes: 2 additions & 1 deletion sanic/application/motd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Dict, Optional

from sanic import __version__
from sanic.compat import is_atty
from sanic.log import logger


Expand Down Expand Up @@ -36,7 +37,7 @@ def output(
data: Dict[str, str],
extra: Dict[str, str],
) -> None:
motd_class = MOTDTTY if sys.stdout and sys.stdout.isatty() else MOTDBasic
motd_class = MOTDTTY if is_atty() else MOTDBasic
motd_class(logo, serve_location, data, extra).display()


Expand Down
6 changes: 5 additions & 1 deletion sanic/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import signal

from sys import argv
from sys import argv, stdout

from multidict import CIMultiDict # type: ignore

Expand All @@ -25,6 +25,10 @@ def enable_windows_color_support():
kernel.SetConsoleMode(kernel.GetStdHandle(-11), 7)


def is_atty():
return stdout and stdout.isatty()


class Header(CIMultiDict):
"""
Container used for both request and response headers. It is a subclass of
Expand Down
4 changes: 3 additions & 1 deletion sanic/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from typing import Any, Dict
from warnings import warn

from sanic.compat import is_atty


LOGGING_CONFIG_DEFAULTS: Dict[str, Any] = dict( # no cov
version=1,
Expand Down Expand Up @@ -83,7 +85,7 @@ class Colors(str, Enum): # no cov

def deprecation(message: str, version: float): # no cov
version_info = f"[DEPRECATION v{version}] "
if sys.stdout and sys.stdout.isatty():
if is_atty():
version_info = f"{Colors.RED}{version_info}"
message = f"{Colors.YELLOW}{message}{Colors.END}"
warn(version_info + message, DeprecationWarning)
8 changes: 4 additions & 4 deletions sanic/mixins/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from sanic.application.motd import MOTD
from sanic.application.state import ApplicationServerInfo, Mode, ServerStage
from sanic.base.meta import SanicMeta
from sanic.compat import OS_IS_WINDOWS
from sanic.compat import OS_IS_WINDOWS, is_atty
from sanic.helpers import _default
from sanic.log import Colors, error_logger, logger
from sanic.models.handler_types import ListenerType
Expand Down Expand Up @@ -424,7 +424,7 @@ def _helper(

self.motd(self.serve_location)

if sys.stdout and sys.stdout.isatty() and not self.state.is_debug:
if is_atty() and not self.state.is_debug:
error_logger.warning(
f"{Colors.YELLOW}Sanic is running in PRODUCTION mode. "
"Consider using '--debug' or '--dev' while actively "
Expand Down Expand Up @@ -615,7 +615,7 @@ async def _start_servers(
f"{app.state.workers} worker(s), which will be ignored "
"in favor of the primary application."
)
if sys.stdout and sys.stdout.isatty():
if is_atty():
message = "".join(
[
Colors.YELLOW,
Expand Down Expand Up @@ -656,7 +656,7 @@ async def _start_servers(
"The encountered error was: "
)
second_message = str(e)
if sys.stdout and sys.stdout.isatty():
if is_atty():
message_parts = [
Colors.YELLOW,
first_message,
Expand Down

0 comments on commit 89f56ac

Please sign in to comment.