annotate libervia/cli/output_template.py @ 4340:ea72364131d5 default tip @

doc (components): Update Email Gateway documentation: A section has been added to explain how attachments are handled. fix 453
author Goffi <goffi@goffi.org>
date Tue, 03 Dec 2024 00:53:18 +0100
parents 0d7bb4df2343
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
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
4075
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
4 # Libervia CLI
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 """Standard outputs"""
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
4075
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
22 from libervia.cli.constants import Const as C
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
23 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
24 from libervia.backend.core import log
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
25 from libervia.backend.tools.common import template
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
26 from functools import partial
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
27 import logging
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 import webbrowser
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import tempfile
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import os.path
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 __outputs__ = ["Template"]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
33 TEMPLATE = "template"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
34 OPTIONS = {"template", "browser", "inline-css"}
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 class Template(object):
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 """outputs data using SàT templates"""
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
4075
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
40 def __init__(self, libervia_cli):
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
41 self.host = libervia_cli
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
42 libervia_cli.register_output(C.OUTPUT_COMPLEX, TEMPLATE, self.render)
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
44 def _front_url_tmp_dir(self, ctx, relative_url, tmp_dir):
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
45 """Get front URL for temporary directory"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
46 template_data = ctx["template_data"]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
47 return "file://" + os.path.join(tmp_dir, template_data.theme, relative_url)
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
48
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
49 def _do_render(self, template_path, css_inline, **kwargs):
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
50 try:
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
51 return self.renderer.render(template_path, css_inline=css_inline, **kwargs)
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
52 except template.TemplateNotFound:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
53 self.host.disp(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
54 _("Can't find requested template: {template_path}").format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
55 template_path=template_path
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
56 ),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
57 error=True,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
58 )
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
59 self.host.quit(C.EXIT_NOT_FOUND)
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
60
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 def render(self, data):
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 """render output data using requested template
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 template to render the data can be either command's TEMPLATE or
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 template output_option requested by user.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 @param data(dict): data is a dict which map from variable name to use in template
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 to the variable itself.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 command's template_data_mapping attribute will be used if it exists to convert
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 data to a dict usable by the template.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 """
2530
0cb32e503aff jp (output/template): fixed template output (media_dir was missing)
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
71 # media_dir is needed for the template
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
72 self.host.media_dir = self.host.bridge.config_get("", "media_dir")
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 cmd = self.host.command
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 try:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 template_path = cmd.TEMPLATE
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 except AttributeError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
77 if not "template" in cmd.args.output_opts:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
78 self.host.disp(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
79 _(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
80 "no default template set for this command, you need to specify a "
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
81 "template using --oo template=[path/to/template.html]"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
82 ),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 )
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.host.quit(C.EXIT_BAD_ARG)
2348
18d71226b3a8 jp (output/template): check if there is a template before initializing Renderer
Goffi <goffi@goffi.org>
parents: 2248
diff changeset
86
18d71226b3a8 jp (output/template): check if there is a template before initializing Renderer
Goffi <goffi@goffi.org>
parents: 2248
diff changeset
87 options = self.host.parse_output_options()
18d71226b3a8 jp (output/template): check if there is a template before initializing Renderer
Goffi <goffi@goffi.org>
parents: 2248
diff changeset
88 self.host.check_output_options(OPTIONS, options)
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 template_path = options["template"]
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 except KeyError:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 # template is not specified, we use default one
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 pass
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 if template_path is None:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
95 self.host.disp(_("Can't parse template, please check its syntax"), error=True)
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 self.host.quit(C.EXIT_BAD_ARG)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 try:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 mapping_cb = cmd.template_data_mapping
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 except AttributeError:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 kwargs = data
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 else:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 kwargs = mapping_cb(data)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
105 css_inline = "inline-css" in options
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
107 if "browser" in options:
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 template_name = os.path.basename(template_path)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 tmp_dir = tempfile.mkdtemp()
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
110 front_url_filter = partial(self._front_url_tmp_dir, tmp_dir=tmp_dir)
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
111 self.renderer = template.Renderer(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
112 self.host, front_url_filter=front_url_filter, trusted=True
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
113 )
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
114 rendered = self._do_render(template_path, css_inline=css_inline, **kwargs)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
115 self.host.disp(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
116 _(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
117 "Browser opening requested.\n"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
118 "Temporary files are put in the following directory, you'll have to "
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
119 "delete it yourself once finished viewing: {}"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
120 ).format(tmp_dir)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
121 )
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 tmp_file = os.path.join(tmp_dir, template_name)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
123 with open(tmp_file, "w") as f:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
124 f.write(rendered.encode("utf-8"))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
125 theme, theme_root_path = self.renderer.get_theme_and_root(template_path)
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
126 if theme is None:
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
127 # we have an absolute path
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
128 webbrowser
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2163
diff changeset
129 static_dir = os.path.join(theme_root_path, C.TEMPLATE_STATIC_DIR)
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 if os.path.exists(static_dir):
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
131 # we have to copy static files in a subdirectory, to avoid file download
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
132 # to be blocked by same origin policy
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 import shutil
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
134
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
135 shutil.copytree(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
136 static_dir, os.path.join(tmp_dir, theme, C.TEMPLATE_STATIC_DIR)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 )
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 webbrowser.open(tmp_file)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 else:
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
140 # FIXME: Q&D way to disable template logging
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
141 # logs are overcomplicated, and need to be reworked
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
142 template_logger = log.getLogger("sat.tools.common.template")
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
143 template_logger.log = lambda *args: None
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
144
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
145 logging.disable(logging.WARNING)
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
146 self.renderer = template.Renderer(self.host, trusted=True)
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
147 rendered = self._do_render(template_path, css_inline=css_inline, **kwargs)
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 self.host.disp(rendered)