Mercurial > libervia-backend
annotate frontends/src/jp/output_template.py @ 2282:d8e48c850ad2
jp (input): log improvments + empty filter:
- more colors in debug logs
- read values are printed if verbosity is >= 2
- stdout and stderr of each command can be written in files using --log and --log-err
- --empty option allow to ignore arguments if its value is empty, or skip the whole row
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 28 Jun 2017 23:49:55 +0200 |
parents | a81261cee29b |
children | 18d71226b3a8 |
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 |
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 """ |
2248
a81261cee29b
jp (output/template): initialize template rendered only if a rendering is actually needed
Goffi <goffi@goffi.org>
parents:
2187
diff
changeset
|
51 self.renderer = template.Renderer(self.host) |
2163
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 cmd = self.host.command |
2187
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
53 options = self.host.parse_output_options() |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
54 self.host.check_output_options(OPTIONS, options) |
2163
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 try: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 template_path = cmd.TEMPLATE |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 except AttributeError: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 if not 'template' in cmd.args.output_opts: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 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
|
60 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
|
61 error=True) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 self.host.quit(C.EXIT_BAD_ARG) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 try: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 template_path = options['template'] |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 except KeyError: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 # template is not specified, we use default one |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 pass |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 if template_path is None: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 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
|
70 error=True) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 self.host.quit(C.EXIT_BAD_ARG) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 try: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 mapping_cb = cmd.template_data_mapping |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 except AttributeError: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 kwargs = data |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 else: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 kwargs = mapping_cb(data) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 css_inline = u'inline-css' in options |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 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
|
82 |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 if 'browser' in options: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 template_name = os.path.basename(template_path) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 tmp_dir = tempfile.mkdtemp() |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 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
|
87 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
|
88 tmp_file = os.path.join(tmp_dir, template_name) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 with open(tmp_file, 'w') as f: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 f.write(rendered.encode('utf-8')) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2163
diff
changeset
|
91 theme, theme_root_path = self.renderer.getThemeAndRoot(template_path) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2163
diff
changeset
|
92 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
|
93 if os.path.exists(static_dir): |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 import shutil |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2163
diff
changeset
|
95 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
|
96 webbrowser.open(tmp_file) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 else: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 self.host.disp(rendered) |