annotate frontends/src/jp/output_template.py @ 2166:1b3fbb76984b

core (memory/cache): escape profile when creating cache dir
author Goffi <goffi@goffi.org>
date Sun, 26 Feb 2017 18:20:27 +0100
parents 75667727c500
children f472179305a1
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
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
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 self.renderer = template.Renderer(jp)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 def parse_options(self, options):
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 options_dict = {}
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 for option in options:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 try:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 key, value = option.split(u'=', 1)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 except ValueError:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 key, value = option, None
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 options_dict[key.strip()] = value.strip() if value is not None else None
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 return options_dict
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 def render(self, data):
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 """render output data using requested template
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 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
56 template output_option requested by user.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 @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
58 to the variable itself.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 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
60 data to a dict usable by the template.
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 """
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 cmd = self.host.command
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 options = self.parse_options(cmd.args.output_opts)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 if not OPTIONS.issuperset(options):
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self.host.disp(u"The following output options are invalid: {invalid_options}".format(
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 invalid_options = u', '.join(set(options).difference(OPTIONS))),
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 error=True)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.host.quit(C.EXIT_BAD_ARG)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 try:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 template_path = cmd.TEMPLATE
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 except AttributeError:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 if not 'template' in cmd.args.output_opts:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self.host.disp(u'no default template set for this command, '
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 u'you need to specify a template using --oo template=[path/to/template.html',
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 error=True)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 self.host.quit(C.EXIT_BAD_ARG)
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 template_path = options['template']
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 except KeyError:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 # template is not specified, we use default one
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 pass
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 if template_path is None:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 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
84 error=True)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.host.quit(C.EXIT_BAD_ARG)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 try:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 mapping_cb = cmd.template_data_mapping
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 except AttributeError:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 kwargs = data
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 else:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 kwargs = mapping_cb(data)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 css_inline = u'inline-css' in options
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 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
96
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 if 'browser' in options:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 template_name = os.path.basename(template_path)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 tmp_dir = tempfile.mkdtemp()
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 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
101 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
102 tmp_file = os.path.join(tmp_dir, template_name)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 with open(tmp_file, 'w') as f:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 f.write(rendered.encode('utf-8'))
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 static_dir = self.renderer.getStaticDir(template_path)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 if os.path.exists(static_dir):
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 import shutil
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 shutil.copytree(static_dir, os.path.join(tmp_dir, u'static'))
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 webbrowser.open(tmp_file)
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 else:
75667727c500 jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 self.host.disp(rendered)