Mercurial > libervia-backend
view frontends/src/jp/output_template.py @ 2169:f472179305a1
tools(templates): workflow improvments:
- template theme can be specified in parenthesis: (some_theme)path/to/template.html. Withtout parenthesis, "default" is used
- static content are supposed to be in [theme]/static, error pages in [theme]/error/[err_code].html
- default page are used in some case (2 for now): if error page with specified code doesn't exists, a base page is used, and if a page doesn't exist for a theme, the same one for default theme is looked for
- CSS files are automatically found for HTML pages
- CSS files can be split, the'll be added in the template according to the page requested.
- theme CSS file is looked for, and if not found the default theme equivalent is looked for.
- each element of a path can be associated to a CSS file, and styles.css is always there. For instance if blog/articles.html is requested, the following CSS can be included: "styles.css", "blog.css", "blog_article.css". They all must be in /static
- if the automatic finding of CSS files is not wanted, css_files arguments can be used instead, with full relative path (i.e. including theme)
- CSS files can be merged and included inline with css_inline argument
- root_path can be specified, it will be used as a prefix for static files
- requested theme (which may differ from actual theme, e.g. if the template is not found and default one is used instead) is available in template with "theme" variable
- added getThemeAndRoot method to retrieve theme and theme root path from template
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 05 Mar 2017 23:41:10 +0100 |
parents | 75667727c500 |
children | 4ec72927a222 |
line wrap: on
line source
#! /usr/bin/python # -*- coding: utf-8 -*- # jp: a SàT command line tool # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """Standard outputs""" from sat_frontends.jp.constants import Const as C from sat.core.i18n import _ from sat.tools.common import template import webbrowser import tempfile import os.path __outputs__ = ["Template"] TEMPLATE = u'template' OPTIONS = {u'template', u'browser', u'inline-css'} class Template(object): """outputs data using SàT templates""" def __init__(self, jp): self.host = jp jp.register_output(C.OUTPUT_COMPLEX, TEMPLATE, self.render) self.renderer = template.Renderer(jp) def parse_options(self, options): options_dict = {} for option in options: try: key, value = option.split(u'=', 1) except ValueError: key, value = option, None options_dict[key.strip()] = value.strip() if value is not None else None return options_dict def render(self, data): """render output data using requested template template to render the data can be either command's TEMPLATE or template output_option requested by user. @param data(dict): data is a dict which map from variable name to use in template to the variable itself. command's template_data_mapping attribute will be used if it exists to convert data to a dict usable by the template. """ cmd = self.host.command options = self.parse_options(cmd.args.output_opts) if not OPTIONS.issuperset(options): self.host.disp(u"The following output options are invalid: {invalid_options}".format( invalid_options = u', '.join(set(options).difference(OPTIONS))), error=True) self.host.quit(C.EXIT_BAD_ARG) try: template_path = cmd.TEMPLATE except AttributeError: if not 'template' in cmd.args.output_opts: self.host.disp(u'no default template set for this command, ' u'you need to specify a template using --oo template=[path/to/template.html', error=True) self.host.quit(C.EXIT_BAD_ARG) try: template_path = options['template'] except KeyError: # template is not specified, we use default one pass if template_path is None: self.host.disp(u"Can't parse template, please check its syntax", error=True) self.host.quit(C.EXIT_BAD_ARG) try: mapping_cb = cmd.template_data_mapping except AttributeError: kwargs = data else: kwargs = mapping_cb(data) css_inline = u'inline-css' in options rendered = self.renderer.render(template_path, css_inline=css_inline, **kwargs) if 'browser' in options: template_name = os.path.basename(template_path) tmp_dir = tempfile.mkdtemp() self.host.disp(_(u"Browser opening requested.\nTemporary files are put in the following directory, " u"you'll have to delete it yourself once finished viewing: {}").format(tmp_dir)) tmp_file = os.path.join(tmp_dir, template_name) with open(tmp_file, 'w') as f: f.write(rendered.encode('utf-8')) theme, theme_root_path = self.renderer.getThemeAndRoot(template_path) static_dir = os.path.join(theme_root_path, C.TEMPLATE_STATIC_DIR) if os.path.exists(static_dir): import shutil shutil.copytree(static_dir, os.path.join(tmp_dir, theme, C.TEMPLATE_STATIC_DIR)) webbrowser.open(tmp_file) else: self.host.disp(rendered)