sets up the terminal color scheme avoid initializing colorama in nonwindows platforms if justfixwindowsconsole accesses sys stdout with wsgirestrictedstdout return true if the running system s terminal supports color and false otherwise check the windows registry to see if vt code handling has been enabled by default see https superuser coma1300251447564 winreg is only available on windows isatty is not always implemented 6223 windows terminal supports vt codes microsoft visual studio code s builtin terminal supports colors create a style object from the given configstring if configstring is empty django utils termcolors defaultpalette is used the nocolor palette has all available roles use that palette as the basis for populating the palette as defined in the environment for backwards compatibility set style for erroroutput error return a style object with no color scheme return a style object from the django color scheme avoid initializing colorama in non windows platforms colorama 0 4 6 colorama is not installed if just_fix_windows_console accesses sys stdout with wsgirestrictedstdout return true if the running system s terminal supports color and false otherwise check the windows registry to see if vt code handling has been enabled by default see https superuser com a 1300251 447564 winreg is only available on windows isatty is not always implemented 6223 windows terminal supports vt codes microsoft visual studio code s built in terminal supports colors create a style object from the given config_string if config_string is empty django utils termcolors default_palette is used the nocolor palette has all available roles use that palette as the basis for populating the palette as defined in the environment for backwards compatibility set style for error_output error return a style object with no color scheme return a style object from the django color scheme
import functools
import os
import sys

from django.utils import termcolors

try:
    import colorama

    
    colorama.just_fix_windows_console()
except (
    AttributeError,  
    ImportError,  
    
    
    OSError,
):
    HAS_COLORAMA = False
else:
    HAS_COLORAMA = True


def supports_color():
    

    def vt_codes_enabled_in_windows_registry():
        
        try:
            
            import winreg
        except ImportError:
            return False
        else:
            try:
                reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Console")
                reg_key_value, _ = winreg.QueryValueEx(reg_key, "VirtualTerminalLevel")
            except FileNotFoundError:
                return False
            else:
                return reg_key_value == 1

    
    is_a_tty = hasattr(sys.stdout, "isatty") and sys.stdout.isatty()

    return is_a_tty and (
        sys.platform != "win32"
        or (HAS_COLORAMA and getattr(colorama, "fixed_windows_console", False))
        or "ANSICON" in os.environ
        or
        
        "WT_SESSION" in os.environ
        or
        
        os.environ.get("TERM_PROGRAM") == "vscode"
        or vt_codes_enabled_in_windows_registry()
    )


class Style:
    pass


def make_style(config_string=""):
    

    style = Style()

    color_settings = termcolors.parse_color_setting(config_string)

    
    
    
    for role in termcolors.PALETTES[termcolors.NOCOLOR_PALETTE]:
        if color_settings:
            format = color_settings.get(role, {})
            style_func = termcolors.make_style(**format)
        else:

            def style_func(x):
                return x

        setattr(style, role, style_func)

    
    
    style.ERROR_OUTPUT = style.ERROR

    return style


@functools.cache
def no_style():
    
    return make_style("nocolor")


def color_style(force_color=False):
    
    if not force_color and not supports_color():
        return no_style()
    return make_style(os.environ.get("DJANGO_COLORS", ""))