Mercurial > libervia-backend
comparison libervia/cli/output_xml.py @ 4075:47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 14:54:26 +0200 |
parents | libervia/frontends/jp/output_xml.py@26b7ed2817da |
children |
comparison
equal
deleted
inserted
replaced
4074:26b7ed2817da | 4075:47401850dec6 |
---|---|
1 #! /usr/bin/env python3 | |
2 | |
3 # Libervia CLI frontend | |
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
5 | |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 """Standard outputs""" | |
19 | |
20 | |
21 from libervia.cli.constants import Const as C | |
22 from libervia.backend.core.i18n import _ | |
23 from lxml import etree | |
24 from libervia.backend.core.log import getLogger | |
25 | |
26 log = getLogger(__name__) | |
27 import sys | |
28 | |
29 try: | |
30 import pygments | |
31 from pygments.lexers.html import XmlLexer | |
32 from pygments.formatters import TerminalFormatter | |
33 except ImportError: | |
34 pygments = None | |
35 | |
36 | |
37 __outputs__ = ["XML"] | |
38 | |
39 | |
40 class XML(object): | |
41 """Outputs for XML""" | |
42 | |
43 def __init__(self, host): | |
44 self.host = host | |
45 host.register_output(C.OUTPUT_XML, C.OUTPUT_NAME_XML, self.pretty, default=True) | |
46 host.register_output( | |
47 C.OUTPUT_LIST_XML, C.OUTPUT_NAME_XML, self.pretty_list, default=True | |
48 ) | |
49 host.register_output(C.OUTPUT_XML, C.OUTPUT_NAME_XML_RAW, self.raw) | |
50 host.register_output(C.OUTPUT_LIST_XML, C.OUTPUT_NAME_XML_RAW, self.list_raw) | |
51 | |
52 def colorize(self, xml): | |
53 if pygments is None: | |
54 self.host.disp( | |
55 _( | |
56 "Pygments is not available, syntax highlighting is not possible. " | |
57 "Please install if from http://pygments.org or with pip install " | |
58 "pygments" | |
59 ), | |
60 error=True, | |
61 ) | |
62 return xml | |
63 if not sys.stdout.isatty(): | |
64 return xml | |
65 lexer = XmlLexer(encoding="utf-8") | |
66 formatter = TerminalFormatter(bg="dark") | |
67 return pygments.highlight(xml, lexer, formatter) | |
68 | |
69 def format(self, data, pretty=True): | |
70 parser = etree.XMLParser(remove_blank_text=True) | |
71 tree = etree.fromstring(data, parser) | |
72 xml = etree.tostring(tree, encoding="unicode", pretty_print=pretty) | |
73 return self.colorize(xml) | |
74 | |
75 def format_no_pretty(self, data): | |
76 return self.format(data, pretty=False) | |
77 | |
78 def pretty(self, data): | |
79 self.host.disp(self.format(data)) | |
80 | |
81 def pretty_list(self, data, separator="\n"): | |
82 list_pretty = list(map(self.format, data)) | |
83 self.host.disp(separator.join(list_pretty)) | |
84 | |
85 def raw(self, data): | |
86 self.host.disp(self.format_no_pretty(data)) | |
87 | |
88 def list_raw(self, data, separator="\n"): | |
89 list_no_pretty = list(map(self.format_no_pretty, data)) | |
90 self.host.disp(separator.join(list_no_pretty)) |