annotate frontends/src/jp/output_xml.py @ 2213:1010054cf771

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