diff --git a/sanic/application/logo.py b/sanic/application/logo.py index 56b8c0b1..3c16a442 100644 --- a/sanic/application/logo.py +++ b/sanic/application/logo.py @@ -3,6 +3,8 @@ import sys from os import environ +from sanic.compat import is_atty + BASE_LOGO = """ @@ -44,7 +46,7 @@ ansi_pattern = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])") def get_logo(full=False, coffee=False): logo = ( (FULL_COLOR_LOGO if full else (COFFEE_LOGO if coffee else COLOR_LOGO)) - if sys.stdout.isatty() + if is_atty() else BASE_LOGO ) diff --git a/sanic/application/motd.py b/sanic/application/motd.py index 4de046a5..df1f1338 100644 --- a/sanic/application/motd.py +++ b/sanic/application/motd.py @@ -1,11 +1,10 @@ -import sys - from abc import ABC, abstractmethod from shutil import get_terminal_size from textwrap import indent, wrap from typing import Dict, Optional from sanic import __version__ +from sanic.compat import is_atty from sanic.log import logger @@ -36,7 +35,7 @@ class MOTD(ABC): data: Dict[str, str], extra: Dict[str, str], ) -> None: - motd_class = MOTDTTY if sys.stdout.isatty() else MOTDBasic + motd_class = MOTDTTY if is_atty() else MOTDBasic motd_class(logo, serve_location, data, extra).display() diff --git a/sanic/compat.py b/sanic/compat.py index d8e0bea1..880ca681 100644 --- a/sanic/compat.py +++ b/sanic/compat.py @@ -1,8 +1,7 @@ import asyncio import os import signal - -from sys import argv +import sys from multidict import CIMultiDict # type: ignore @@ -47,7 +46,7 @@ class Header(CIMultiDict): return self.getall(key, default=[]) -use_trio = argv[0].endswith("hypercorn") and "trio" in argv +use_trio = sys.argv[0].endswith("hypercorn") and "trio" in sys.argv if use_trio: # pragma: no cover import trio # type: ignore @@ -89,3 +88,7 @@ def ctrlc_workaround_for_windows(app): die = False signal.signal(signal.SIGINT, ctrlc_handler) app.add_task(stay_active) + + +def is_atty() -> bool: + return bool(sys.stdout and sys.stdout.isatty()) diff --git a/sanic/log.py b/sanic/log.py index 5911e832..0c3ff826 100644 --- a/sanic/log.py +++ b/sanic/log.py @@ -5,6 +5,8 @@ from enum import Enum 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, @@ -98,7 +100,7 @@ Logger used by Sanic for access logging def deprecation(message: str, version: float): # no cov version_info = f"[DEPRECATION v{version}] " - if 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) diff --git a/sanic/mixins/runner.py b/sanic/mixins/runner.py index 1df77e55..d2d1e66c 100644 --- a/sanic/mixins/runner.py +++ b/sanic/mixins/runner.py @@ -2,7 +2,6 @@ from __future__ import annotations import os import platform -import sys from asyncio import ( AbstractEventLoop, @@ -26,7 +25,7 @@ from sanic.application.logo import get_logo 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 @@ -424,7 +423,7 @@ class RunnerMixin(metaclass=SanicMeta): self.motd(self.serve_location) - if 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 " @@ -615,7 +614,7 @@ class RunnerMixin(metaclass=SanicMeta): f"{app.state.workers} worker(s), which will be ignored " "in favor of the primary application." ) - if sys.stdout.isatty(): + if is_atty(): message = "".join( [ Colors.YELLOW, @@ -656,7 +655,7 @@ class RunnerMixin(metaclass=SanicMeta): "The encountered error was: " ) second_message = str(e) - if sys.stdout.isatty(): + if is_atty(): message_parts = [ Colors.YELLOW, first_message,