Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_strip():
assert s("<b>1</b>23") == "123"
assert s("1") == "1"
assert s("1") == "1"
assert s("1") == "1"
assert s("1") == "1"
markups = {"red": "", "b,g,r": "", "fg 1,2,3": ""}
am = AnsiMarkup(custom_markups=markups, strip=True)
assert am.feed("123", strict=True) == "123"
def parse(text, colorize=True):
if colorize:
return AnsiMarkup(strip=False).feed(text, strict=True)
else:
return AnsiMarkup(strip=True).feed(text, strict=True)
def parse(text):
return AnsiMarkup(strip=False).feed(text, strict=True)
def s(text, *, strict=False):
return AnsiMarkup(strip=True).feed(text, strict=strict)
def parse(text):
return AnsiMarkup(strip=False).feed(text, strict=True)
elif isinstance(level, int):
levelno = level
else:
raise TypeError(
"Invalid level, it should be an integer or a string, not: '%s'"
% type(level).__name__
)
if levelno < 0:
raise ValueError(
"Invalid level value, it should be a positive integer, not: %d" % levelno
)
if isinstance(format, str):
try:
AnsiMarkup.verify(format, ["level", "lvl"])
except ValueError as e:
raise ValueError(
"Invalid format, color markups could not be parsed correctly"
) from e
formatter = format + terminator + "{exception}"
is_formatter_dynamic = False
elif callable(format):
if format == builtins.format:
raise ValueError(
"The built-in 'format()' function cannot be used as a 'format' parameter, "
"this is most likely a mistake (please double-check the arguments passed "
"to 'logger.add()')."
)
formatter = format
is_formatter_dynamic = True
else:
def _decolorize_format(format_):
markups = {"level": "", "lvl": ""}
return AnsiMarkup(custom_markups=markups, strip=True).feed(format_, strict=True)
def _memoize_ansi_messages(format_, ansi_code, message):
markups = {"level": ansi_code, "lvl": ansi_code}
ansimarkup = AnsiMarkup(custom_markups=markups, strip=False)
def parse(string_, *, recursive=True):
for text, name, spec, _ in string.Formatter().parse(string_):
ansimarkup.feed(text, strict=False)
if spec and recursive:
yield from parse(spec, recursive=False)
if name and name[:8] in ("message", "message.", "message["):
yield ansimarkup.feed(message, strict=True)
messages = list(parse(format_))
class AnsiDict:
def __init__(self, record):
self._record = record
self._messages = iter(messages)