annotate libervia/cli/output_xml.py @ 4292:dd0891d0b22b

plugin XEP-0298: Delivering Conference Information to Jingle Participants (Coin) implementation: This is the first draft of XEP-0298 implementation. The focus is to implement elements needed for A/V Conferences protoXEP. rel 447
author Goffi <goffi@goffi.org>
date Mon, 29 Jul 2024 03:31:13 +0200
parents 47401850dec6
children
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
3640
f4c02bdb2d91 CLI: use global constants for output names
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
3 # Libervia CLI frontend
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
4 # 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
5
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # 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
7 # 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
8 # 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
9 # (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
10
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # 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
12 # 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
13 # 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
14 # 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
15
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # 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
17 # 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
18 """Standard outputs"""
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
19
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
20
4075
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
21 from libervia.cli.constants import Const as C
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 3640
diff changeset
22 from libervia.backend.core.i18n import _
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from lxml import etree
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 3640
diff changeset
24 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
25
2190
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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
28
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
29 try:
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import pygments
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.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
32 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
33 except ImportError:
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
34 pygments = None
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
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
37 __outputs__ = ["XML"]
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
38
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 class XML(object):
2601
12bf089f0bf3 jp (output): new OUPUT_XMLUI and OUTPUT_LIST_XMLUI outputs
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
41 """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
42
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
43 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
44 self.host = host
3640
f4c02bdb2d91 CLI: use global constants for output names
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
45 host.register_output(C.OUTPUT_XML, C.OUTPUT_NAME_XML, self.pretty, default=True)
f4c02bdb2d91 CLI: use global constants for output names
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
46 host.register_output(
f4c02bdb2d91 CLI: use global constants for output names
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
47 C.OUTPUT_LIST_XML, C.OUTPUT_NAME_XML, self.pretty_list, default=True
f4c02bdb2d91 CLI: use global constants for output names
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
48 )
f4c02bdb2d91 CLI: use global constants for output names
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
49 host.register_output(C.OUTPUT_XML, C.OUTPUT_NAME_XML_RAW, self.raw)
f4c02bdb2d91 CLI: use global constants for output names
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
50 host.register_output(C.OUTPUT_LIST_XML, C.OUTPUT_NAME_XML_RAW, self.list_raw)
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
51
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
52 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
53 if pygments is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
54 self.host.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
55 _(
3640
f4c02bdb2d91 CLI: use global constants for output names
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
56 "Pygments is not available, syntax highlighting is not possible. "
f4c02bdb2d91 CLI: use global constants for output names
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
57 "Please install if from http://pygments.org or with pip install "
f4c02bdb2d91 CLI: use global constants for output names
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
58 "pygments"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
59 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
60 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
61 )
2190
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
62 return xml
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
63 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
64 return xml
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
65 lexer = XmlLexer(encoding="utf-8")
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
66 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
67 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
68
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
69 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
70 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
71 tree = etree.fromstring(data, parser)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2601
diff changeset
72 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
73 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
74
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
75 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
76 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
77
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
78 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
79 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
80
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
81 def pretty_list(self, data, separator="\n"):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
82 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
83 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
84
d823a0cdbcc2 jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff changeset
85 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
86 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
87
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
88 def list_raw(self, data, separator="\n"):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
89 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
90 self.host.disp(separator.join(list_no_pretty))