Skip to content

terminal

Package helping with colorizing and formatting terminal output.

Terminal

Class formatting messages using colorama to specific visual tokens.

If :mod:Colorama is not found, it will still work, but without colors.

Depends on :mod:Colorama Using AYON_LOG_NO_COLORS environment variable.

Source code in client/ayon_core/lib/terminal.py
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
class Terminal:
    """Class formatting messages using colorama to specific visual tokens.

    If :mod:`Colorama` is not found, it will still work, but without colors.

    Depends on :mod:`Colorama`
    Using **AYON_LOG_NO_COLORS** environment variable.
    """

    # Is Terminal initialized
    _initialized = False
    # Thread lock for initialization to avoid race conditions
    _init_lock = threading.Lock()
    # Use colorized output
    use_colors = True
    # Output message replacements mapping - set on initialization
    _sdict = {}

    @staticmethod
    def _initialize():
        """Initialize Terminal class as object.

        First check if colorized output is disabled by environment variable
        `AYON_LOG_NO_COLORS` value. By default is colorized output turned
        on.

        Then tries to import python module that do the colors magic and create
        it's terminal object. Colorized output is not used if import of python
        module or terminal object creation fails.

        Set `_initialized` attribute to `True` when is done.
        """

        from ayon_core.lib import env_value_to_bool
        log_no_colors = env_value_to_bool(
            "AYON_LOG_NO_COLORS", default=None
        )
        if log_no_colors is not None:
            Terminal.use_colors = not log_no_colors

        if not Terminal.use_colors:
            Terminal._initialized = True
            return

        try:
            # Try to import `blessed` module and create `Terminal` object
            import blessed
            term = blessed.Terminal()

        except Exception:
            # Do not use colors if crashed
            Terminal.use_colors = False
            print(
                "Module `blessed` failed on import or terminal creation."
                " AYON terminal won't use colors."
            )
            Terminal._initialized = True
            return

        # shortcuts for blessed codes
        _SB = term.bold
        _RST = ""
        _LR = term.tomato2
        _LG = term.aquamarine3
        _LB = term.turquoise2
        _LM = term.slateblue2
        _LY = term.gold
        _R = term.red
        _G = term.green
        _B = term.blue
        _C = term.cyan
        _Y = term.yellow
        _W = term.white

        # dictionary replacing string sequences with colorized one
        Terminal._sdict = {
            r">>> ": _SB + _LG + r">>> " + _RST,
            r"!!!(?!\sCRI|\sERR)": _SB + _R + r"!!! " + _RST,
            r"\-\-\- ": _SB + _C + r"--- " + _RST,
            r"\*\*\*(?!\sWRN)": _SB + _LY + r"***" + _RST,
            r"\*\*\* WRN": _SB + _LY + r"*** WRN" + _RST,
            r"  \- ": _SB + _LY + r"  - " + _RST,
            r"\[ ": _SB + _LG + r"[ " + _RST,
            r" \]": _SB + _LG + r" ]" + _RST,
            r"{": _LG + r"{",
            r"}": r"}" + _RST,
            r"\(": _LY + r"(",
            r"\)": r")" + _RST,
            r"^\.\.\. ": _SB + _LR + r"... " + _RST,
            r"!!! ERR: ":
                _SB + _LR + r"!!! ERR: " + _RST,
            r"!!! CRI: ":
                _SB + _R + r"!!! CRI: " + _RST,
            r"(?i)failed": _SB + _LR + "FAILED" + _RST,
            r"(?i)error": _SB + _LR + "ERROR" + _RST
        }

        Terminal._SB = _SB
        Terminal._RST = _RST
        Terminal._LR = _LR
        Terminal._LG = _LG
        Terminal._LB = _LB
        Terminal._LM = _LM
        Terminal._LY = _LY
        Terminal._R = _R
        Terminal._G = _G
        Terminal._B = _B
        Terminal._C = _C
        Terminal._Y = _Y
        Terminal._W = _W

        Terminal._initialized = True

    @staticmethod
    def _multiple_replace(text, adict):
        """Replace multiple tokens defined in dict.

        Find and replace all occurrences of strings defined in dict is
        supplied string.

        Args:
            text (str): string to be searched
            adict (dict): dictionary with `{'search': 'replace'}`

        Returns:
            str: string with replaced tokens

        """
        for r, v in adict.items():
            text = re.sub(r, v, text)

        return text

    @staticmethod
    def echo(message):
        """Print colorized message to stdout.

        Args:
            message (str): Message to be colorized.
            debug (bool):

        Returns:
            str: Colorized message.

        """
        colorized = Terminal.log(message)
        print(colorized)

        return colorized

    @staticmethod
    def log(message):
        """Return color formatted message.

        If environment variable `AYON_LOG_NO_COLORS` is set to
        whatever value, message will be formatted but not colorized.

        Args:
            message (str): Message to be colorized.

        Returns:
            str: Colorized message.

        """
        T = Terminal
        # Initialize if not yet initialized and use thread lock to avoid race
        # condition issues
        if not T._initialized:
            # Check if lock is already locked to be sure `_initialize` is not
            # executed multiple times
            if not T._init_lock.locked():
                with T._init_lock:
                    T._initialize()
            else:
                # If lock is locked wait until is finished
                while T._init_lock.locked():
                    time.sleep(0.1)

        # if we dont want colors, just print raw message
        if not T.use_colors:
            return message

        message = re.sub(r'\[(.*)\]', '[ ' + T._SB + T._W +
                         r'\1' + T._RST + ' ]', message)
        message = T._multiple_replace(message + T._RST, T._sdict)

        return message

echo(message) staticmethod

Print colorized message to stdout.

Parameters:

Name Type Description Default
message str

Message to be colorized.

required
debug bool
required

Returns:

Name Type Description
str

Colorized message.

Source code in client/ayon_core/lib/terminal.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
@staticmethod
def echo(message):
    """Print colorized message to stdout.

    Args:
        message (str): Message to be colorized.
        debug (bool):

    Returns:
        str: Colorized message.

    """
    colorized = Terminal.log(message)
    print(colorized)

    return colorized

log(message) staticmethod

Return color formatted message.

If environment variable AYON_LOG_NO_COLORS is set to whatever value, message will be formatted but not colorized.

Parameters:

Name Type Description Default
message str

Message to be colorized.

required

Returns:

Name Type Description
str

Colorized message.

Source code in client/ayon_core/lib/terminal.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
@staticmethod
def log(message):
    """Return color formatted message.

    If environment variable `AYON_LOG_NO_COLORS` is set to
    whatever value, message will be formatted but not colorized.

    Args:
        message (str): Message to be colorized.

    Returns:
        str: Colorized message.

    """
    T = Terminal
    # Initialize if not yet initialized and use thread lock to avoid race
    # condition issues
    if not T._initialized:
        # Check if lock is already locked to be sure `_initialize` is not
        # executed multiple times
        if not T._init_lock.locked():
            with T._init_lock:
                T._initialize()
        else:
            # If lock is locked wait until is finished
            while T._init_lock.locked():
                time.sleep(0.1)

    # if we dont want colors, just print raw message
    if not T.use_colors:
        return message

    message = re.sub(r'\[(.*)\]', '[ ' + T._SB + T._W +
                     r'\1' + T._RST + ' ]', message)
    message = T._multiple_replace(message + T._RST, T._sdict)

    return message