Mercurial > libervia-backend
annotate sat_frontends/jp/output_xml.py @ 2846:58ea675d0f05
quick frontend (app): added AUTO_RESYNC attribute in QuickApp, when set (default) the resync method is called on all widgets automatically:
if AUTO_RESYNC is False, frontend must call itself the resync method.
This is useful to improve performance, resynchronising all widgets at once may be resource intensive, and it may be more efficient to resync a widget only when it is visible.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 10 Mar 2019 18:02:42 +0100 |
parents | 003b8b4b56a7 |
children | ab2696e34d29 |
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 |
2771 | 5 # Copyright (C) 2009-2019 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"] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2601
diff
changeset
|
39 RAW = u"xml_raw" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2601
diff
changeset
|
40 PRETTY = u"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 _( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2601
diff
changeset
|
57 u"Pygments is not available, syntax highlighting is not possible. Please install if from http://pygments.org or with pip install pygments" |
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") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2601
diff
changeset
|
65 formatter = TerminalFormatter(bg=u"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 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2601
diff
changeset
|
80 def pretty_list(self, data, separator=u"\n"): |
2190
d823a0cdbcc2
jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 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
|
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 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2601
diff
changeset
|
87 def list_raw(self, data, separator=u"\n"): |
2190
d823a0cdbcc2
jp (outputs): new xml and list_xml outputs, handling pretty formatting and syntax highlighting
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 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
|
89 self.host.disp(separator.join(list_no_pretty)) |