comparison sat_frontends/jp/output_xml.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/jp/output_xml.py@0046283a285d
children 12bf089f0bf3
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #! /usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # jp: a SàT command line tool
5 # Copyright (C) 2009-2018 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 parser = etree.XMLParser(remove_blank_text=True)
63 tree = etree.fromstring(data, parser)
64 xml = etree.tostring(tree, encoding='unicode', pretty_print=pretty)
65 return self.colorize(xml)
66
67 def format_no_pretty(self, data):
68 return self.format(data, pretty=False)
69
70 def pretty(self, data):
71 self.host.disp(self.format(data))
72
73 def pretty_list(self, data, separator=u'\n'):
74 list_pretty = map(self.format, data)
75 self.host.disp(separator.join(list_pretty))
76
77 def raw(self, data):
78 self.host.disp(self.format_no_pretty(data))
79
80 def list_raw(self, data, separator=u'\n'):
81 list_no_pretty = map(self.format_no_pretty, data)
82 self.host.disp(separator.join(list_no_pretty))