Mercurial > libervia-backend
annotate frontends/src/jp/output_template.py @ 2484:785b6a1cef0a
plugin forums: first draft:
this plugin handle forums hierarchy, where forums link to topics which themselves link to blog nodes.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 30 Jan 2018 08:17:08 +0100 |
parents | 0046283a285d |
children | 0cb32e503aff |
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 | 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 """ |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 cmd = self.host.command |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 try: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 template_path = cmd.TEMPLATE |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 except AttributeError: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 if not 'template' in cmd.args.output_opts: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 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
|
57 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
|
58 error=True) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 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
|
60 |
18d71226b3a8
jp (output/template): check if there is a template before initializing Renderer
Goffi <goffi@goffi.org>
parents:
2248
diff
changeset
|
61 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
|
62 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
|
63 self.renderer = template.Renderer(self.host) |
2163
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 try: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 template_path = options['template'] |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 except KeyError: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 # template is not specified, we use default one |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 pass |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 if template_path is None: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 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
|
71 error=True) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 self.host.quit(C.EXIT_BAD_ARG) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 try: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 mapping_cb = cmd.template_data_mapping |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 except AttributeError: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 kwargs = data |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 else: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 kwargs = mapping_cb(data) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 css_inline = u'inline-css' in options |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 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
|
83 |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 if 'browser' in options: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 template_name = os.path.basename(template_path) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 tmp_dir = tempfile.mkdtemp() |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 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
|
88 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
|
89 tmp_file = os.path.join(tmp_dir, template_name) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 with open(tmp_file, 'w') as f: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 f.write(rendered.encode('utf-8')) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2163
diff
changeset
|
92 theme, theme_root_path = self.renderer.getThemeAndRoot(template_path) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2163
diff
changeset
|
93 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
|
94 if os.path.exists(static_dir): |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 import shutil |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2163
diff
changeset
|
96 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
|
97 webbrowser.open(tmp_file) |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 else: |
75667727c500
jp (output): template output first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 self.host.disp(rendered) |