#!/data_systems/opt/bin/python3 """ aim_html_formatter.py Utilities for formatting AIM HTML messages. HTML is Hyper Text Markup Language Created 2019-05 @author: Bill Barrett """ # The following two colors are used in the various status messages to # either indicate that all is well or that there are problems AOK = 'DarkGreen' NOT_OK = 'DarkRed' # HTML code to insert four non-breaking spaces FOUR_SPACES_HTML = 4 * ' ' class AIMHTMLFormatter: """ Produce the HTML text for an AIM processing summary email """ @staticmethod def get_html_ok_not_ok_color(aok): """ Get the HTML string that determines a lines color Parameters ---------- aok : bool true sets output to the AOK color, false NOT_OK Returns ------- : str html text to set the color to be printed """ if aok: color = AOK else: color = NOT_OK return AIMHTMLFormatter.set_html_text_color(color) @staticmethod def set_html_text_color(color, terminator=True): """ Set the color for a block of HTML text Parameters ---------- color : str the color for the HTML line such 'black', 'blue', etc terminator : bool true if the html is to be terminated with '

' rather than just '>' Returns ------- : str html text to set the color to be printed """ color_string = '

' @staticmethod def html_underline(message): """ Format text for html underlining Parameters ---------- message : str the text of the message to be underlined Returns ------- : str the html to underline the message """ return AIMHTMLFormatter.html_closed_tag_creator('u', message) @staticmethod def html_header(message, level): """ Format text for as a header at a specified level Parameters ---------- message : str the tesxt of the message level : int the header level Returns ------- : str the html to print the message at the specified header level """ assert isinstance(level, int), 'header level must be an int' header_level = 'h{}'.format(level) return AIMHTMLFormatter.html_closed_tag_creator(header_level, message) @staticmethod def html_preserve_formatting(message): """ Preserve original text formatting Parameters ---------- message : str the tesxt of the message Returns ------- : str the html to leave the formatting of the text unchanged from its original format """ return AIMHTMLFormatter.html_closed_tag_creator('pre', message) @staticmethod def html_center_text(message): """ Center html text Parameters ---------- message : str the text of the message Returns ------- : str the html to center the message """ return AIMHTMLFormatter.html_closed_tag_creator('center', message) @staticmethod def html_closed_tag_creator(tag_type, message): """ Create an HTML closed tag. I.e if the tag type is 'x' and the body is 'body' the closed tag would be 'body' Parameters ---------- tag_type : str a valid html tag type such as 'u' for underline pr 'pre' for preserve formatting message : str the text of the message """ return '<{0}>{1}'.format(tag_type, message)