annotate sat_frontends/jp/output_template.py @ 2637:534b264d63df 0.7.0a1

install: fixed DBUS_FILE path
author Goffi <goffi@goffi.org>
date Wed, 04 Jul 2018 07:58:24 +0200
parents 56f94936df1e
children 0fa217fafabf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #! /usr/bin/python
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # jp: a SàT command line tool
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 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
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat_frontends.jp.constants import Const as C
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.i18n import _
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.tools.common import template
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import webbrowser
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import tempfile
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import os.path
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 __outputs__ = ["Template"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
30 TEMPLATE = u"template"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
31 OPTIONS = {u"template", u"browser", u"inline-css"}
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 class Template(object):
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 """outputs data using SàT templates"""
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 def __init__(self, jp):
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self.host = jp
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 jp.register_output(C.OUTPUT_COMPLEX, TEMPLATE, self.render)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def render(self, data):
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 """render output data using requested template
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 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
45 template output_option requested by user.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 @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
47 to the variable itself.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 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
49 data to a dict usable by the template.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 """
2530
0cb32e503aff jp (output/template): fixed template output (media_dir was missing)
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
51 # media_dir is needed for the template
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 self.host.media_dir = self.host.bridge.getConfig("", "media_dir")
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 cmd = self.host.command
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 try:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 template_path = cmd.TEMPLATE
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 except AttributeError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 if not "template" in cmd.args.output_opts:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 self.host.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 u"no default template set for this command, "
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 u"you need to specify a template using --oo template=[path/to/template.html]",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 )
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 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
64
18d71226b3a8 jp (output/template): check if there is a template before initializing Renderer
Goffi <goffi@goffi.org>
parents: 2248
diff changeset
65 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
66 self.host.check_output_options(OPTIONS, options)
18d71226b3a8 jp (output/template): check if there is a template before initializing Renderer
Goffi <goffi@goffi.org>
parents: 2248
diff changeset
67 self.renderer = template.Renderer(self.host)
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 template_path = options["template"]
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 except KeyError:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 # template is not specified, we use default one
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 pass
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 if template_path is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
74 self.host.disp(u"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
75 self.host.quit(C.EXIT_BAD_ARG)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 try:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 mapping_cb = cmd.template_data_mapping
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 except AttributeError:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 kwargs = data
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 else:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 kwargs = mapping_cb(data)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 css_inline = u"inline-css" in options
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 rendered = self.renderer.render(template_path, css_inline=css_inline, **kwargs)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 if "browser" in options:
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 template_name = os.path.basename(template_path)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 tmp_dir = tempfile.mkdtemp()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 self.host.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 u"Browser opening requested.\nTemporary files are put in the following directory, "
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 u"you'll have to delete it yourself once finished viewing: {}"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
94 ).format(tmp_dir)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
95 )
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 tmp_file = os.path.join(tmp_dir, template_name)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
97 with open(tmp_file, "w") as f:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
98 f.write(rendered.encode("utf-8"))
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2163
diff changeset
99 theme, theme_root_path = self.renderer.getThemeAndRoot(template_path)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2163
diff changeset
100 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
101 if os.path.exists(static_dir):
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 import shutil
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 shutil.copytree(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
105 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
106 )
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 webbrowser.open(tmp_file)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 else:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 self.host.disp(rendered)