From b3798cf9508657cf6a6041c942613dec2f57d59c Mon Sep 17 00:00:00 2001 From: Jakub Jezek Date: Mon, 28 Dec 2020 14:12:47 +0100 Subject: [PATCH] reverse pype.lib.terminal --- pype/lib/terminal.py | 69 ++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 48 deletions(-) diff --git a/pype/lib/terminal.py b/pype/lib/terminal.py index d54d52e9be..a47d58ec3b 100644 --- a/pype/lib/terminal.py +++ b/pype/lib/terminal.py @@ -11,14 +11,8 @@ # ..---===[[ PyP3 Setup ]]===---... # import re -import os -import sys -try: - import blessed - term = blessed.Terminal() - -except Exception: - term = None +import time +import threading class Terminal: @@ -30,49 +24,28 @@ class Terminal: Using **PYPE_LOG_NO_COLORS** environment variable. """ - # shortcuts for colorama codes + # 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 = {} - if term: - _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 - _sdict = { + @staticmethod + def _initialize(): + """Initialize Terminal class as object. - 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 - } + First check if colorized output is disabled by environment variable + `PYPE_LOG_NO_COLORS` value. By default is colorized output turned on. - def __init__(self): - pass + 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 pype.lib import env_value_to_bool use_colors = env_value_to_bool( @@ -218,7 +191,7 @@ class Terminal: time.sleep(0.1) # if we dont want colors, just print raw message - if not T._sdict or os.environ.get('PYPE_LOG_NO_COLORS'): + if not T.use_colors: return message message = re.sub(r'\[(.*)\]', '[ ' + T._SB + T._W +