some translations

This commit is contained in:
Anthony Scopatz 2019-02-09 13:50:47 -05:00
parent 50101ce9a7
commit c111811bab
2 changed files with 40 additions and 12 deletions

View file

@ -125,9 +125,17 @@ def ansi_reverse_style(style='default', return_style=False):
# add keys to make this more useful
updates = {
'1': 'BOLD_',
'2': 'FAINT_',
'4': 'UNDERLINE_',
'5': 'SLOWBLINK_',
'1;4': 'BOLD_UNDERLINE_',
'4;1': 'BOLD_UNDERLINE_',
'38': 'SET_FOREGROUND_',
'48': 'SET_BACKGROUND_',
'38;2': 'SET_FOREGROUND_RGB_',
'48;2': 'SET_BACKGROUND_RGB_',
'38;5': 'SET_FOREGROUND_SHORT_',
'48;5': 'SET_BACKGROUND_SHORT_',
}
for ec, name in reversed_style.items():
no_left_zero = ec.lstrip('0')
@ -148,6 +156,17 @@ def ANSI_ESCAPE_CODE_RE():
return re.compile(r'\001?(\033\[)?([0-9;]+)m?\002?')
@lazyobject
def ANSI_REVERSE_COLOR_NAME_TRANSLATIONS():
return {
'UNDERLINE_BOLD_': 'BOLD_UNDERLINE_',
'SET_FOREGROUND_FAINT_': 'SET_FOREGROUND_RGB_',
'SET_FOREGROUND_FAINT_': 'SET_FOREGROUND_RGB_',
'SET_FOREGROUND_SLOWBLINK_': 'SET_FOREGROUND_SHORT_',
'SET_BACKGROUND_SLOWBLINK_': 'SET_BACKGROUND_SHORT_',
}
def ansi_color_escape_code_to_name(escape_code, style='default', reversed_style=None):
"""Converts an ASNI color code escape sequence to a tuple of color names
in the provided style ('default', by default). For example,
@ -170,8 +189,7 @@ def ansi_color_escape_code_to_name(escape_code, style='default', reversed_style=
# skip '0' entries
continue
n = n + name if n else name
if n == 'UNDERLINE_BOLD_':
n = 'BOLD_UNDERLINE_'
n = ANSI_REVERSE_COLOR_NAME_TRANSLATIONS.get(n, n)
if n.endswith('_'):
continue
norm_names.append(n)

View file

@ -1297,22 +1297,19 @@ class Env(cabc.MutableMapping):
self._d["PATH"] = list(PATH_DEFAULT)
self._detyped = None
@staticmethod
def detypeable(val):
return not (callable(val) or isinstance(val, cabc.MutableMapping))
def detype(self):
if self._detyped is not None:
return self._detyped
ctx = {}
for key, val in self._d.items():
if key not in self._ensurers and not self.detypeable(val):
continue
if not isinstance(key, str):
key = str(key)
ensurer = self.get_ensurer(key)
val = ensurer.detype(val)
ctx[key] = val
if not ensurer.detype or not callable(ensurer.detype):
# cannot actually detype this var.
continue
deval = ensurer.detype(val)
ctx[key] = deval
self._detyped = ctx
return ctx
@ -1334,7 +1331,20 @@ class Env(cabc.MutableMapping):
os_environ.update(self._orig_env)
self._orig_env = None
def get_ensurer(self, key, default=Ensurer(always_true, None, ensure_string)):
@staticmethod
def detypeable(val):
return not (callable(val) or isinstance(val, cabc.MutableMapping))
def _get_default_ensurer(self, val, default=None):
if default is not None:
return default
if self.detypeable(val):
default = Ensurer(always_true, None, ensure_string)
else:
default = Ensurer(always_true, None, False)
return default
def get_ensurer(self, key, val=None, default=None):
"""Gets an ensurer for the given key."""
if key in self._ensurers:
return self._ensurers[key]
@ -1344,7 +1354,7 @@ class Env(cabc.MutableMapping):
if k.match(key) is not None:
break
else:
ensurer = default
ensurer = self._get_default_ensurer(val=val, default=default)
self._ensurers[key] = ensurer
return ensurer