annotate sat_frontends/jp/output_xml.py @ 3513:753d151da886

XEP-0277: new preview/mbPreview method: This method does more or less the same thing as sending, but without actually sending the item, and parse the generated element just after. This way, the triggers are run, and a preview of the item can be made for the resulting microblog data.
author Goffi <goffi@goffi.org>
date Thu, 29 Apr 2021 15:49:59 +0200
parents be6d91572633
children f4c02bdb2d91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #! /usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
2190
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
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2190
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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
26
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
27 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
28 import sys
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
29
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
30 try:
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
31 import pygments
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
32 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
33 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
34 except ImportError:
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
35 pygments = None
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
36
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
37
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
38 __outputs__ = ["XML"]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
39 RAW = "xml_raw"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
40 PRETTY = "xml_pretty"
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
41
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
42
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
43 class XML(object):
2601
12bf089f0bf3 jp (output): new OUPUT_XMLUI and OUTPUT_LIST_XMLUI outputs
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
44 """Outputs for XML"""
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
45
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
46 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
47 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
48 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
49 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
50 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
51 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
52
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
53 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
54 if pygments is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
55 self.host.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
56 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
57 "Pygments is not available, syntax highlighting is not possible. Please install if from http://pygments.org or with pip install pygments"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
58 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
59 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
60 )
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
61 return xml
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
62 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
63 return xml
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
64 lexer = XmlLexer(encoding="utf-8")
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
65 formatter = TerminalFormatter(bg="dark")
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
66 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
67
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
68 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
69 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
70 tree = etree.fromstring(data, parser)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
71 xml = etree.tostring(tree, encoding="unicode", pretty_print=pretty)
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
72 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
73
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
74 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
75 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
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 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
78 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
79
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
80 def pretty_list(self, data, separator="\n"):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
81 list_pretty = list(map(self.format, data))
2190
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_pretty))
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
83
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
84 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
85 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
86
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
87 def list_raw(self, data, separator="\n"):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
88 list_no_pretty = list(map(self.format_no_pretty, data))
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.host.disp(separator.join(list_no_pretty))