reverse pype.lib.terminal

This commit is contained in:
Jakub Jezek 2020-12-28 14:12:47 +01:00
parent 4d7ae2cc12
commit b3798cf950
No known key found for this signature in database
GPG key ID: C4B96E101D2A47F3

View file

@ -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 +