def stdout_xml(the_xml, is_colorized=True): message = str(etree.tostring(the_xml, pretty_print=True), 'utf-8') if is_colorized: print( highlight(message, lexers.XmlLexer(), formatters.TerminalFormatter())) else: print(message)
class XmlFormatter: formatter = formatters.TerminalFormatter() lexer = lexers.XmlLexer() def pformat(self, doc): string = lxml.etree.tostring(doc, pretty_print=True).decode('utf8') return highlight(string, self.lexer, self.formatter) def pprint(self): print(self.pformat())
def stdout_xml(the_xml, is_colorized=True): """Prints an lxml.objectify.ObjectifiedElement to console. :param lxml.objectify.ObjectifiedElement the_xml: the object we want to print. :param bool is_colorized: if True, will print highlight xml tags and attributes else will print b&w output to the console. """ message = str(etree.tostring(the_xml, pretty_print=True), 'utf-8') if is_colorized: print( highlight(message, lexers.XmlLexer(), formatters.TerminalFormatter())) else: print(message)
def colorizer(content): return highlight(content, lexers.XmlLexer(), formatters.TerminalFormatter())
else: logger.info("MPD OK | time={}s size={}B".format(t.elapsed_secs, r.headers['content-length'])) else: logger.info("MPD status.") table = [["Descriptions", "Values"], ["http code", r.status_code], ["time spend (sec)", round(t.elapsed_secs, 2)], ["content-length", r.headers['content-length']], ["url", str(r.url)], ["content-type", r.headers['content-type']]] # Output table print tabulate(table, headers="firstrow") # Get Lexer base on content-type if r.headers['content-type'].startswith('application/xml'): lexer = lexers.XmlLexer() elif r.headers['content-type'].startswith('application/json'): lexer = lexers.JsonLexer() else: lexer = lexers.HtmlLexer() # Try to print JSON content try: # Format Json with indentation formatted_http_response = json.dumps(json.loads(r.text), sort_keys=True, indent=2) except Exception: formatted_http_response = r.text print("content:\n{}".format(highlight(formatted_http_response,lexer, formatters.TerminalFormatter()))) except ValueError as e:
def stdout_xml(the_xml): print( highlight(str(etree.tostring(the_xml, pretty_print=True), 'utf-8'), lexers.XmlLexer(), formatters.TerminalFormatter()))