comparison sat_frontends/jp/output_xml.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 12bf089f0bf3
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
21 21
22 from sat_frontends.jp.constants import Const as C 22 from sat_frontends.jp.constants import Const as C
23 from sat.core.i18n import _ 23 from sat.core.i18n import _
24 from lxml import etree 24 from lxml import etree
25 from sat.core.log import getLogger 25 from sat.core.log import getLogger
26
26 log = getLogger(__name__) 27 log = getLogger(__name__)
27 import sys 28 import sys
29
28 try: 30 try:
29 import pygments 31 import pygments
30 from pygments.lexers.html import XmlLexer 32 from pygments.lexers.html import XmlLexer
31 from pygments.formatters import TerminalFormatter 33 from pygments.formatters import TerminalFormatter
32 except ImportError: 34 except ImportError:
33 pygments = None 35 pygments = None
34 36
35 37
36 __outputs__ = ["XML"] 38 __outputs__ = ["XML"]
37 RAW = u'xml_raw' 39 RAW = u"xml_raw"
38 PRETTY = u'xml_pretty' 40 PRETTY = u"xml_pretty"
39 41
40 42
41 class XML(object): 43 class XML(object):
42 """Outputs for XML""" 44 """Outputs for XML"""
43 45
48 host.register_output(C.OUTPUT_XML, RAW, self.raw) 50 host.register_output(C.OUTPUT_XML, RAW, self.raw)
49 host.register_output(C.OUTPUT_LIST_XML, RAW, self.list_raw) 51 host.register_output(C.OUTPUT_LIST_XML, RAW, self.list_raw)
50 52
51 def colorize(self, xml): 53 def colorize(self, xml):
52 if pygments is None: 54 if pygments is None:
53 self.host.disp(_(u'Pygments is not available, syntax highlighting is not possible. Please install if from http://pygments.org or with pip install pygments'), error=True) 55 self.host.disp(
56 _(
57 u"Pygments is not available, syntax highlighting is not possible. Please install if from http://pygments.org or with pip install pygments"
58 ),
59 error=True,
60 )
54 return xml 61 return xml
55 if not sys.stdout.isatty(): 62 if not sys.stdout.isatty():
56 return xml 63 return xml
57 lexer = XmlLexer(encoding='utf-8') 64 lexer = XmlLexer(encoding="utf-8")
58 formatter = TerminalFormatter(bg=u'dark') 65 formatter = TerminalFormatter(bg=u"dark")
59 return pygments.highlight(xml, lexer, formatter) 66 return pygments.highlight(xml, lexer, formatter)
60 67
61 def format(self, data, pretty=True): 68 def format(self, data, pretty=True):
62 parser = etree.XMLParser(remove_blank_text=True) 69 parser = etree.XMLParser(remove_blank_text=True)
63 tree = etree.fromstring(data, parser) 70 tree = etree.fromstring(data, parser)
64 xml = etree.tostring(tree, encoding='unicode', pretty_print=pretty) 71 xml = etree.tostring(tree, encoding="unicode", pretty_print=pretty)
65 return self.colorize(xml) 72 return self.colorize(xml)
66 73
67 def format_no_pretty(self, data): 74 def format_no_pretty(self, data):
68 return self.format(data, pretty=False) 75 return self.format(data, pretty=False)
69 76
70 def pretty(self, data): 77 def pretty(self, data):
71 self.host.disp(self.format(data)) 78 self.host.disp(self.format(data))
72 79
73 def pretty_list(self, data, separator=u'\n'): 80 def pretty_list(self, data, separator=u"\n"):
74 list_pretty = map(self.format, data) 81 list_pretty = map(self.format, data)
75 self.host.disp(separator.join(list_pretty)) 82 self.host.disp(separator.join(list_pretty))
76 83
77 def raw(self, data): 84 def raw(self, data):
78 self.host.disp(self.format_no_pretty(data)) 85 self.host.disp(self.format_no_pretty(data))
79 86
80 def list_raw(self, data, separator=u'\n'): 87 def list_raw(self, data, separator=u"\n"):
81 list_no_pretty = map(self.format_no_pretty, data) 88 list_no_pretty = map(self.format_no_pretty, data)
82 self.host.disp(separator.join(list_no_pretty)) 89 self.host.disp(separator.join(list_no_pretty))