| 1 |
""" |
| 2 |
Sets up the terminal color scheme. |
| 3 |
""" |
| 4 |
|
| 5 |
import functools |
| 6 |
import os |
| 7 |
import sys |
| 8 |
|
| 9 |
from django.utils import termcolors |
| 10 |
|
| 11 |
try: |
| 12 |
import colorama |
| 13 |
|
| 14 |
# Avoid initializing colorama in non-Windows platforms. |
| 15 |
colorama.just_fix_windows_console() |
| 16 |
except ( |
| 17 |
AttributeError, # colorama <= 0.4.6. |
| 18 |
ImportError, # colorama is not installed. |
| 19 |
# If just_fix_windows_console() accesses sys.stdout with |
| 20 |
# WSGIRestrictedStdout. |
| 21 |
OSError, |
| 22 |
): |
| 23 |
HAS_COLORAMA = False |
| 24 |
else: |
| 25 |
HAS_COLORAMA = True |
| 26 |
|
| 27 |
|
| 28 |
def supports_color(): |
| 29 |
""" |
| 30 |
Return True if the running system's terminal supports color, |
| 31 |
and False otherwise. |
| 32 |
""" |
| 33 |
|
| 34 |
def vt_codes_enabled_in_windows_registry(): |
| 35 |
""" |
| 36 |
Check the Windows Registry to see if VT code handling has been enabled |
| 37 |
by default, see https://superuser.com/a/1300251/447564. |
| 38 |
""" |
| 39 |
try: |
| 40 |
# winreg is only available on Windows. |
| 41 |
import winreg |
| 42 |
except ImportError: |
| 43 |
return False |
| 44 |
else: |
| 45 |
try: |
| 46 |
reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Console") |
| 47 |
reg_key_value, _ = winreg.QueryValueEx(reg_key, "VirtualTerminalLevel") |
| 48 |
except FileNotFoundError: |
| 49 |
return False |
| 50 |
else: |
| 51 |
return reg_key_value == 1 |
| 52 |
|
| 53 |
# isatty is not always implemented, #6223. |
| 54 |
is_a_tty = hasattr(sys.stdout, "isatty") and sys.stdout.isatty() |
| 55 |
|
| 56 |
return is_a_tty and ( |
| 57 |
sys.platform != "win32" |
| 58 |
or (HAS_COLORAMA and getattr(colorama, "fixed_windows_console", False)) |
| 59 |
or "ANSICON" in os.environ |
| 60 |
or |
| 61 |
# Windows Terminal supports VT codes. |
| 62 |
"WT_SESSION" in os.environ |
| 63 |
or |
| 64 |
# Microsoft Visual Studio Code's built-in terminal supports colors. |
| 65 |
os.environ.get("TERM_PROGRAM") == "vscode" |
| 66 |
or vt_codes_enabled_in_windows_registry() |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
class Style: |
| 71 |
pass |
| 72 |
|
| 73 |
|
| 74 |
def make_style(config_string=""): |
| 75 |
""" |
| 76 |
Create a Style object from the given config_string. |
| 77 |
|
| 78 |
If config_string is empty django.utils.termcolors.DEFAULT_PALETTE is used. |
| 79 |
""" |
| 80 |
|
| 81 |
style = Style() |
| 82 |
|
| 83 |
color_settings = termcolors.parse_color_setting(config_string) |
| 84 |
|
| 85 |
# The nocolor palette has all available roles. |
| 86 |
# Use that palette as the basis for populating |
| 87 |
# the palette as defined in the environment. |
| 88 |
for role in termcolors.PALETTES[termcolors.NOCOLOR_PALETTE]: |
| 89 |
if color_settings: |
| 90 |
format = color_settings.get(role, {}) |
| 91 |
style_func = termcolors.make_style(**format) |
| 92 |
else: |
| 93 |
|
| 94 |
def style_func(x): |
| 95 |
return x |
| 96 |
|
| 97 |
setattr(style, role, style_func) |
| 98 |
|
| 99 |
# For backwards compatibility, |
| 100 |
# set style for ERROR_OUTPUT == ERROR |
| 101 |
style.ERROR_OUTPUT = style.ERROR |
| 102 |
|
| 103 |
return style |
| 104 |
|
| 105 |
|
| 106 |
@functools.cache |
| 107 |
def no_style(): |
| 108 |
""" |
| 109 |
Return a Style object with no color scheme. |
| 110 |
""" |
| 111 |
return make_style("nocolor") |
| 112 |
|
| 113 |
|
| 114 |
def color_style(force_color=False): |
| 115 |
""" |
| 116 |
Return a Style object from the Django color scheme. |
| 117 |
""" |
| 118 |
if not force_color and not supports_color(): |
| 119 |
return no_style() |
| 120 |
return make_style(os.environ.get("DJANGO_COLORS", "")) |