def colorize(text, level_or_color): """ Colorize a text to be displayed on the console. The following color names may be used: - Blue - Cyan - Green - Grey (or gray) - Magenta - Red - Yellow - White The following risk levels may be used in lieu of colors: - Informational (0) - Low (1) - Middle (2) - High (3) - Critical (4) :param text: Text to colorize. :type text: str :param level_or_color: Color name or risk level name. :type level_or_color: str :returns: Colorized text. :rtype: str """ # Check if colors are enabled. if Console.use_colors: # Parse the color name or level into # a color value that colored() expects. try: level_or_color = level_or_color.lower() except AttributeError: pass color = m_colors[level_or_color] # Colorize the text. if color: if color in m_make_brighter: text = colored(text, color, attrs=["bold"]) else: text = colored(text, color) # Return the text. return text
def _atexit_restore_console(cls): """ This method is called automatically by an exit hook to restore the console colors before quitting. """ if cls.use_colors: sys.stdout.write( colored("") )
def _atexit_restore_console(cls): """ This method is called automatically by an exit hook to restore the console colors before quitting. """ if cls.use_colors: sys.stdout.write(colored(""))
def colorize(text, level_or_color): """ Colorize a text depends of type of alert: - Information - Low - Middle - High - Critical :param text: text to colorize. :type text: int with level (0-4) or string with values: info, low, middle, high, critical. :param level_or_color: color name or integer with level selected. :type level_or_color: str or integer (0-4). :returns: str -- string with information to print. """ if Console.use_colors: try: level_or_color = level_or_color.lower() except AttributeError: pass return colored(text, m_colors[level_or_color]) else: return text
def colorize(text, level_or_color): """ Colorize a text depends of type of alert: - Informational - Low - Middle - High - Critical :param text: text to colorize. :type text: int with level (0-4) or string with values: info, low, middle, high, critical. :param level_or_color: color name or integer with level selected. :type level_or_color: str or integer (0-4). :returns: str -- string with information to print. """ # Check if colors are enabled. if Console.use_colors: # Parse the color name or level into # a color value that colored() expects. try: level_or_color = level_or_color.lower() except AttributeError: pass color = m_colors[level_or_color] # Colorize the text. if color: if color in m_make_brighter: text = colored(text, color, attrs=["bold"]) else: text = colored(text, color) # Return the text. return text
def colorize_substring(text, substring, level_or_color): """ Colorize a substring in a text depending of the type of alert: - Information - Low - Middle - Hight - Critical :param text: Full text. :type text: str :param substring: Text to find and colorize. :type substring: str :param level_or_color: May be an integer with level (0-4) or string with values: info, low, middle, high, critical. :type level_or_color: int | str :returns: Colorized text. :rtype: str """ # # XXX TODO: # # We also probably need to parse existing ANSI escape codes # to know what's the color of the surrounding text, otherwise # we'll only properly colorize substrings in non colored text. # # Maybe we can settle with this: indicate a color for the text # and a color for the substring. Should work in all situations # we _currently_ need to handle. # # Check for trivial cases. if text and substring and Console.use_colors: # Parse the color name or level into # a color value that colored() expects. try: level_or_color = level_or_color.lower() except AttributeError: pass color = m_colors[level_or_color] # Loop for each occurrence of the substring. m_pos = 0 while 1: # Find the substring in the text. m_pos = text.find(substring, m_pos) # If not found, break out of the loop. if m_pos < 0: break # Split the text where the substring was found. m_prefix = text[:m_pos] m_content = text[m_pos: m_pos + len(substring)] m_suffix = text[m_pos + len(substring):] # Patch the text to colorize the substring. m_content = colored(m_content, color) text = "%s%s%s" % (m_prefix, m_content, m_suffix) # Update the current position and keep searching. m_pos = len(m_prefix) + len(m_content) # Return the patched text. return text