Mercurial > libervia-backend
annotate src/tools/common/template.py @ 2160:e67e8cd24141
core (tools/common): data objects first draft:
this module aims is to help manipulate complex data from bridge, mainly for the template system.
It is in common and not only in frontends as it may be used in some case by backend, if it needs to use template system in the future.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 21 Feb 2017 21:01:40 +0100 |
parents | 5734b0994cf0 |
children | f472179305a1 |
rev | line source |
---|---|
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT: a jabber client |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ template generation """ |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.constants import Const as C |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.core import exceptions |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from sat.core.log import getLogger |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 log = getLogger(__name__) |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 import os.path |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 try: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 import sat_templates |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 except ImportError: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 raise exceptions.MissingModule(u'sat_templates module is not available, please install it or check your path to use template engine') |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 else: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 sat_templates # to avoid pyflakes warning |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 try: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 import jinja2 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 except: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 raise exceptions.MissingModule(u'Missing module jinja2, please install it from http://jinja.pocoo.org or with pip install jinja2') |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 class Renderer(object): |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 def __init__(self, host): # , template_dir=None): |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 self.host = host |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 self.base_dir = os.path.dirname(sat_templates.__file__) |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 self.theme = u'default' # FIXME: temporary, template should be selected in render() |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 self.env = jinja2.Environment( |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 loader=jinja2.PackageLoader('sat_templates', self.theme), |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 autoescape=jinja2.select_autoescape(['html', 'xhtml', 'xml']), |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 trim_blocks=True, |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 lstrip_blocks=True, |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 ) |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 # we want to have access to SàT constants in templates |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 self.env.globals[u'C'] = C |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 def render(self, template_path, theme=u"default", css_file=u"style.css", css_inline=False, **kwargs): |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 """render a template |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 @param template_path(unicode): path of the template to render (e.g. blog/articles.html) |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 @param theme(unicode): template theme |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 @param css_file(unicode): path to CSS file (relative to template dir, or absolute) |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 @param css_inline(bool): if True, CSS will be embedded in the HTML page |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 @param **kwargs: variable to transmit to the template |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 """ |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 # TODO: handle theme |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 template = self.env.get_template(template_path) |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 if css_inline: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 css_file_path = os.path.join(self.getStaticDir(template_path), css_file) |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 with open(css_file_path, 'r') as f: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 kwargs[u"css_content"] = f.read() |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 return template.render(theme=theme, css_file=css_file, css_inline=css_inline, **kwargs) |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 def getStaticDir(self, template_path): |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 template_base = template_path.split(u'/')[0] |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 return os.path.join(self.base_dir, self.theme, template_base, "static") |