annotate sat_frontends/jp/output_template.py @ 2609:75d2ac872965

jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
author Goffi <goffi@goffi.org>
date Wed, 06 Jun 2018 19:24:15 +0200
parents 26edcf3a30eb
children 56f94936df1e
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"]
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 TEMPLATE = u'template'
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 OPTIONS = {u'template', u'browser', u'inline-css'}
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
0cb32e503aff jp (output/template): fixed template output (media_dir was missing)
Goffi <goffi@goffi.org>
parents: 2483
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:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 if not 'template' in cmd.args.output_opts:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.host.disp(u'no default template set for this command, '
2348
18d71226b3a8 jp (output/template): check if there is a template before initializing Renderer
Goffi <goffi@goffi.org>
parents: 2248
diff changeset
59 u'you need to specify a template using --oo template=[path/to/template.html]',
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 error=True)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 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
62
18d71226b3a8 jp (output/template): check if there is a template before initializing Renderer
Goffi <goffi@goffi.org>
parents: 2248
diff changeset
63 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
64 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
65 self.renderer = template.Renderer(self.host)
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 try:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 template_path = options['template']
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 except KeyError:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 # template is not specified, we use default one
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 pass
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 if template_path is None:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self.host.disp(u"Can't parse template, please check its syntax",
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 error=True)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self.host.quit(C.EXIT_BAD_ARG)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 try:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 mapping_cb = cmd.template_data_mapping
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 except AttributeError:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 kwargs = data
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 else:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 kwargs = mapping_cb(data)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 css_inline = u'inline-css' in options
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 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
85
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 if 'browser' in options:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 template_name = os.path.basename(template_path)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 tmp_dir = tempfile.mkdtemp()
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.host.disp(_(u"Browser opening requested.\nTemporary files are put in the following directory, "
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 u"you'll have to delete it yourself once finished viewing: {}").format(tmp_dir))
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 tmp_file = os.path.join(tmp_dir, template_name)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 with open(tmp_file, 'w') as f:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 f.write(rendered.encode('utf-8'))
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2163
diff changeset
94 theme, theme_root_path = self.renderer.getThemeAndRoot(template_path)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2163
diff changeset
95 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
96 if os.path.exists(static_dir):
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 import shutil
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2163
diff changeset
98 shutil.copytree(static_dir, os.path.join(tmp_dir, theme, C.TEMPLATE_STATIC_DIR))
2163
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 webbrowser.open(tmp_file)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 else:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 self.host.disp(rendered)