comparison frontends/src/jp/output_xml.py @ 2190:d823a0cdbcc2

jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
author Goffi <goffi@goffi.org>
date Sun, 12 Mar 2017 23:34:19 +0100
parents
children 577e19724744
comparison
equal deleted inserted replaced
2189:a25a256688e2 2190:d823a0cdbcc2
1 #! /usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # jp: a SàT command line tool
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 """Standard outputs"""
20
21
22 from sat_frontends.jp.constants import Const as C
23 from sat.core.i18n import _
24 from lxml import etree
25 from sat.core.log import getLogger
26 log = getLogger(__name__)
27 import sys
28 try:
29 import pygments
30 from pygments.lexers.html import XmlLexer
31 from pygments.formatters import TerminalFormatter
32 except ImportError:
33 pygments = None
34
35
36 __outputs__ = ["XML"]
37 RAW = u'xml_raw'
38 PRETTY = u'xml_pretty'
39
40
41 class XML(object):
42 """Default outputs"""
43
44 def __init__(self, host):
45 self.host = host
46 host.register_output(C.OUTPUT_XML, PRETTY, self.pretty, default=True)
47 host.register_output(C.OUTPUT_LIST_XML, PRETTY, self.pretty_list, default=True)
48 host.register_output(C.OUTPUT_XML, RAW, self.raw)
49 host.register_output(C.OUTPUT_LIST_XML, RAW, self.list_raw)
50
51 def colorize(self, xml):
52 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)
54 return xml
55 if not sys.stdout.isatty():
56 return xml
57 lexer = XmlLexer(encoding='utf-8')
58 formatter = TerminalFormatter(bg=u'dark')
59 return pygments.highlight(xml, lexer, formatter)
60
61 def format(self, data, pretty=True):
62 tree = etree.fromstring(data)
63 xml = etree.tostring(tree, encoding='unicode', pretty_print=pretty)
64 return self.colorize(xml)
65
66 def format_no_pretty(self, data):
67 return self.format(data, pretty=False)
68
69 def pretty(self, data):
70 self.host.disp(self.format(data))
71
72 def pretty_list(self, data, separator=u'\n'):
73 list_pretty = map(self.format, data)
74 self.host.disp(separator.join(list_pretty))
75
76 def raw(self, data):
77 self.host.disp(self.format_no_pretty(data))
78
79 def list_raw(self, data, separator=u'\n'):
80 list_no_pretty = map(self.format_no_pretty, data)
81 self.host.disp(separator.join(list_no_pretty))