mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2025-01-07 22:34:18 +08:00
695e260308
E.g. for subprocess.Popen, it can be opened in `text=True` mode where it returns strings, or `text=False` where it returns bytes. To differentiate, you can annotate types as `Popen[str]` or `Popen[bytes]`. This patch should add subscripts in all the places we were missing them. Reported-by: Johannes Berg <johannes.berg@intel.com> Link: https://lore.kernel.org/linux-kselftest/20230315105055.9b2be0153625.I7a2cb99b95dff216c0feed4604255275e0b156a7@changeid/ Signed-off-by: Daniel Latypov <dlatypov@google.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
#
|
|
# Utilities for printing and coloring output.
|
|
#
|
|
# Copyright (C) 2022, Google LLC.
|
|
# Author: Daniel Latypov <dlatypov@google.com>
|
|
|
|
import datetime
|
|
import sys
|
|
import typing
|
|
|
|
_RESET = '\033[0;0m'
|
|
|
|
class Printer:
|
|
"""Wraps a file object, providing utilities for coloring output, etc."""
|
|
|
|
def __init__(self, output: typing.IO[str]):
|
|
self._output = output
|
|
self._use_color = output.isatty()
|
|
|
|
def print(self, message: str) -> None:
|
|
print(message, file=self._output)
|
|
|
|
def print_with_timestamp(self, message: str) -> None:
|
|
ts = datetime.datetime.now().strftime('%H:%M:%S')
|
|
self.print(f'[{ts}] {message}')
|
|
|
|
def _color(self, code: str, text: str) -> str:
|
|
if not self._use_color:
|
|
return text
|
|
return code + text + _RESET
|
|
|
|
def red(self, text: str) -> str:
|
|
return self._color('\033[1;31m', text)
|
|
|
|
def yellow(self, text: str) -> str:
|
|
return self._color('\033[1;33m', text)
|
|
|
|
def green(self, text: str) -> str:
|
|
return self._color('\033[1;32m', text)
|
|
|
|
def color_len(self) -> int:
|
|
"""Returns the length of the color escape codes."""
|
|
return len(self.red(''))
|
|
|
|
# Provides a default instance that prints to stdout
|
|
stdout = Printer(sys.stdout)
|