Mercurial > libervia-backend
view sat_frontends/jp/output_template.py @ 3254:6cf4bd6972c2
core, frontends: avatar refactoring:
/!\ huge commit
Avatar logic has been reworked around the IDENTITY plugin: plugins able to handle avatar
or other identity related metadata (like nicknames) register to IDENTITY plugin in the
same way as for other features like download/upload. Once registered, IDENTITY plugin will
call them when suitable in order of priority, and handle caching.
Methods to manage those metadata from frontend now use serialised data.
For now `avatar` and `nicknames` are handled:
- `avatar` is now a dict with `path` + metadata like `media_type`, instead of just a string
path
- `nicknames` is now a list of nicknames in order of priority. This list is never empty,
and `nicknames[0]` should be the preferred nickname to use by frontends in most cases.
In addition to contact specified nicknames, user set nickname (the one set in roster) is
used in priority when available.
Among the side changes done with this commit, there are:
- a new `contactGet` bridge method to get roster metadata for a single contact
- SatPresenceProtocol.send returns a Deferred to check when it has actually been sent
- memory's methods to handle entities data now use `client` as first argument
- metadata filter can be specified with `getIdentity`
- `getAvatar` and `setAvatar` are now part of the IDENTITY plugin instead of XEP-0054 (and
there signature has changed)
- `isRoom` and `getBareOrFull` are now part of XEP-0045 plugin
- jp avatar/get command uses `xdg-open` first when available for `--show` flag
- `--no-cache` has been added to jp avatar/get and identity/get
- jp identity/set has been simplified, explicit options (`--nickname` only for now) are
used instead of `--field`. `--field` may come back in the future if necessary for extra
data.
- QuickContactList `SetContact` now handle None as a value, and doesn't use it to delete the
metadata anymore
- improved cache handling for `metadata` and `nicknames` in quick frontend
- new `default` argument in QuickContactList `getCache`
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 14 Apr 2020 21:00:33 +0200 |
parents | 559a625a236b |
children | be6d91572633 |
line wrap: on
line source
#! /usr/bin/env python3 # jp: a SàT command line tool # Copyright (C) 2009-2020 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.core import log from sat.tools.common import template from functools import partial import logging import webbrowser import tempfile import os.path __outputs__ = ["Template"] TEMPLATE = "template" OPTIONS = {"template", "browser", "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) def _front_url_tmp_dir(self, ctx, relative_url, tmp_dir): """Get front URL for temporary directory""" template_data = ctx['template_data'] return "file://" + os.path.join(tmp_dir, template_data.theme, relative_url) def _do_render(self, template_path, css_inline, **kwargs): try: return self.renderer.render(template_path, css_inline=css_inline, **kwargs) except template.TemplateNotFound: self.host.disp(_("Can't find requested template: {template_path}") .format(template_path=template_path), error=True) self.host.quit(C.EXIT_NOT_FOUND) 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. """ # media_dir is needed for the template self.host.media_dir = self.host.bridge.getConfig("", "media_dir") cmd = self.host.command try: template_path = cmd.TEMPLATE except AttributeError: if not "template" in cmd.args.output_opts: self.host.disp(_( "no default template set for this command, you need to specify a " "template using --oo template=[path/to/template.html]"), error=True, ) self.host.quit(C.EXIT_BAD_ARG) options = self.host.parse_output_options() self.host.check_output_options(OPTIONS, options) try: template_path = options["template"] except KeyError: # template is not specified, we use default one pass if template_path is None: self.host.disp(_("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 = "inline-css" in options if "browser" in options: template_name = os.path.basename(template_path) tmp_dir = tempfile.mkdtemp() front_url_filter = partial(self._front_url_tmp_dir, tmp_dir=tmp_dir) self.renderer = template.Renderer( self.host, front_url_filter=front_url_filter, trusted=True) rendered = self._do_render(template_path, css_inline=css_inline, **kwargs) self.host.disp(_( "Browser opening requested.\n" "Temporary files are put in the following directory, 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) if theme is None: # we have an absolute path webbrowser static_dir = os.path.join(theme_root_path, C.TEMPLATE_STATIC_DIR) if os.path.exists(static_dir): # we have to copy static files in a subdirectory, to avoid file download # to be blocked by same origin policy import shutil shutil.copytree( static_dir, os.path.join(tmp_dir, theme, C.TEMPLATE_STATIC_DIR) ) webbrowser.open(tmp_file) else: # FIXME: Q&D way to disable template logging # logs are overcomplicated, and need to be reworked template_logger = log.getLogger("sat.tools.common.template") template_logger.log = lambda *args: None logging.disable(logging.WARNING) self.renderer = template.Renderer(self.host, trusted=True) rendered = self._do_render(template_path, css_inline=css_inline, **kwargs) self.host.disp(rendered)