comparison sat_frontends/jp/output_template.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/jp/output_template.py@0cb32e503aff
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #! /usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # jp: a SàT command line tool
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 """Standard outputs"""
20
21
22 from sat_frontends.jp.constants import Const as C
23 from sat.core.i18n import _
24 from sat.tools.common import template
25 import webbrowser
26 import tempfile
27 import os.path
28
29 __outputs__ = ["Template"]
30 TEMPLATE = u'template'
31 OPTIONS = {u'template', u'browser', u'inline-css'}
32
33
34 class Template(object):
35 """outputs data using SàT templates"""
36
37 def __init__(self, jp):
38 self.host = jp
39 jp.register_output(C.OUTPUT_COMPLEX, TEMPLATE, self.render)
40
41 def render(self, data):
42 """render output data using requested template
43
44 template to render the data can be either command's TEMPLATE or
45 template output_option requested by user.
46 @param data(dict): data is a dict which map from variable name to use in template
47 to the variable itself.
48 command's template_data_mapping attribute will be used if it exists to convert
49 data to a dict usable by the template.
50 """
51 # media_dir is needed for the template
52 self.host.media_dir = self.host.bridge.getConfig('', 'media_dir')
53 cmd = self.host.command
54 try:
55 template_path = cmd.TEMPLATE
56 except AttributeError:
57 if not 'template' in cmd.args.output_opts:
58 self.host.disp(u'no default template set for this command, '
59 u'you need to specify a template using --oo template=[path/to/template.html]',
60 error=True)
61 self.host.quit(C.EXIT_BAD_ARG)
62
63 options = self.host.parse_output_options()
64 self.host.check_output_options(OPTIONS, options)
65 self.renderer = template.Renderer(self.host)
66 try:
67 template_path = options['template']
68 except KeyError:
69 # template is not specified, we use default one
70 pass
71 if template_path is None:
72 self.host.disp(u"Can't parse template, please check its syntax",
73 error=True)
74 self.host.quit(C.EXIT_BAD_ARG)
75
76 try:
77 mapping_cb = cmd.template_data_mapping
78 except AttributeError:
79 kwargs = data
80 else:
81 kwargs = mapping_cb(data)
82
83 css_inline = u'inline-css' in options
84 rendered = self.renderer.render(template_path, css_inline=css_inline, **kwargs)
85
86 if 'browser' in options:
87 template_name = os.path.basename(template_path)
88 tmp_dir = tempfile.mkdtemp()
89 self.host.disp(_(u"Browser opening requested.\nTemporary files are put in the following directory, "
90 u"you'll have to delete it yourself once finished viewing: {}").format(tmp_dir))
91 tmp_file = os.path.join(tmp_dir, template_name)
92 with open(tmp_file, 'w') as f:
93 f.write(rendered.encode('utf-8'))
94 theme, theme_root_path = self.renderer.getThemeAndRoot(template_path)
95 static_dir = os.path.join(theme_root_path, C.TEMPLATE_STATIC_DIR)
96 if os.path.exists(static_dir):
97 import shutil
98 shutil.copytree(static_dir, os.path.join(tmp_dir, theme, C.TEMPLATE_STATIC_DIR))
99 webbrowser.open(tmp_file)
100 else:
101 self.host.disp(rendered)