Adjusted print functions

* Logging is now always done at the DEBUG level
This commit is contained in:
2Shirt 2019-07-16 16:42:28 -06:00
parent 6928485965
commit 9da283f7fc
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -33,12 +33,16 @@ def ask(prompt='Kotaero!'):
"""Prompt the user with a Y/N question, returns bool.""" """Prompt the user with a Y/N question, returns bool."""
answer = None answer = None
prompt = '{} [Y/N]: '.format(prompt) prompt = '{} [Y/N]: '.format(prompt)
# Loop until acceptable answer is given
while answer is None: while answer is None:
tmp = input_text(prompt) tmp = input_text(prompt)
if re.search(r'^y(es|)$', tmp, re.IGNORECASE): if re.search(r'^y(es|up|)$', tmp, re.IGNORECASE):
answer = True answer = True
elif re.search(r'^n(o|ope|)$', tmp, re.IGNORECASE): elif re.search(r'^n(o|ope|)$', tmp, re.IGNORECASE):
answer = False answer = False
# Done
LOG.info('%s%s', prompt, 'Yes' if answer else 'No') LOG.info('%s%s', prompt, 'Yes' if answer else 'No')
return answer return answer
@ -67,11 +71,13 @@ def input_text(prompt='Enter text'):
def pause(prompt='Press Enter to continue... '): def pause(prompt='Press Enter to continue... '):
"""Simple pause implementation.""" """Simple pause implementation."""
LOG.debug('prompt: %s', prompt)
input_text(prompt) input_text(prompt)
def print_colored(strings, colors, **kwargs): def print_colored(strings, colors, **kwargs):
"""Prints strings in the colors specified and adds to log.""" """Prints strings in the colors specified."""
LOG.debug('strings: %s, colors: %s, kwargs: %s', strings, colors, kwargs)
msg = '' msg = ''
print_options = { print_options = {
'end': kwargs.get('end', '\n'), 'end': kwargs.get('end', '\n'),
@ -88,35 +94,50 @@ def print_colored(strings, colors, **kwargs):
) )
print(msg, **print_options) print(msg, **print_options)
LOG.log(
level=logging.getLevelName(kwargs.get('level', 'INFO')),
msg=''.join(strings),
)
def print_error(msg, **kwargs): def print_error(msg, **kwargs):
"""Prints message in RED and adds to log.""" """Prints message in RED."""
print_colored([mgs], ['RED'], level='ERROR', **kwargs) LOG.debug('msg: %s, kwargs: %s', msg, kwargs)
if 'file' not in kwargs:
# Only set if not specified
kwargs['file'] = sys.stderr
print_colored([msg], ['RED'], **kwargs)
def print_info(msg, **kwargs): def print_info(msg, **kwargs):
"""Prints message in BLUE and adds to log.""" """Prints message in BLUE."""
print_colored([mgs], ['BLUE'], **kwargs) LOG.debug('msg: %s, kwargs: %s', msg, kwargs)
print_colored([msg], ['BLUE'], **kwargs)
def print_standard(msg, **kwargs): def print_standard(msg, **kwargs):
"""Prints message and adds to log.""" """Prints message."""
print_colored([mgs], [None], **kwargs) LOG.debug('msg: %s, kwargs: %s', msg, kwargs)
print_colored([msg], [None], **kwargs)
def print_success(msg, **kwargs): def print_success(msg, **kwargs):
"""Prints message in GREEN and adds to log.""" """Prints message in GREEN."""
print_colored([mgs], ['GREEN'], **kwargs) LOG.debug('msg: %s, kwargs: %s', msg, kwargs)
print_colored([msg], ['GREEN'], **kwargs)
def print_warning(msg, **kwargs): def print_warning(msg, **kwargs):
"""Prints message in YELLOW and adds to log.""" """Prints message in YELLOW."""
print_colored([mgs], ['YELLOW'], level='WARNING', **kwargs) LOG.debug('msg: %s, kwargs: %s', msg, kwargs)
if 'file' not in kwargs:
# Only set if not specified
kwargs['file'] = sys.stderr
print_colored([msg], ['YELLOW'], **kwargs)
def strip_colors(string):
"""Strip known ANSI color escapes from string, returns str."""
LOG.debug('string: %s', string)
for color in COLORS.values():
string = string.replace(color, '')
return string
if __name__ == '__main__': if __name__ == '__main__':