tools (common/template): list themes and parse their browser data, available through new getThemesData method
This commit is contained in:
parent
794b5be810
commit
3f735d406e
1 changed files with 43 additions and 11 deletions
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
# SAT: a jabber client
|
||||
# SAT: an XMPP client
|
||||
# Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
|
|
@ -20,22 +19,24 @@
|
|||
"""Template generation"""
|
||||
|
||||
import os.path
|
||||
from collections import namedtuple
|
||||
from sat.core.constants import Const as C
|
||||
from sat.core.i18n import _
|
||||
from sat.core import exceptions
|
||||
from sat.tools import config
|
||||
from sat.tools.common import date_utils
|
||||
from sat.core.log import getLogger
|
||||
from xml.sax.saxutils import quoteattr
|
||||
import time
|
||||
import re
|
||||
import json
|
||||
from pathlib import Path
|
||||
from collections import namedtuple
|
||||
from xml.sax.saxutils import quoteattr
|
||||
from babel import support
|
||||
from babel import Locale
|
||||
from babel.core import UnknownLocaleError
|
||||
import pygments
|
||||
from pygments import lexers
|
||||
from pygments import formatters
|
||||
from sat.core.constants import Const as C
|
||||
from sat.core.i18n import _
|
||||
from sat.core import exceptions
|
||||
from sat.tools import config
|
||||
from sat.tools.common import date_utils
|
||||
from sat.core.log import getLogger
|
||||
|
||||
log = getLogger(__name__)
|
||||
|
||||
|
|
@ -57,19 +58,21 @@ except:
|
|||
"pip install jinja2"
|
||||
)
|
||||
|
||||
from lxml import etree
|
||||
from jinja2 import Markup as safe
|
||||
from jinja2 import is_undefined
|
||||
from jinja2 import utils
|
||||
from jinja2 import TemplateNotFound
|
||||
from jinja2 import contextfilter
|
||||
from jinja2.loaders import split_template_path
|
||||
from lxml import etree
|
||||
|
||||
HTML_EXT = ("html", "xhtml")
|
||||
RE_ATTR_ESCAPE = re.compile(r"[^a-z_-]")
|
||||
SITE_RESERVED_NAMES = ("sat",)
|
||||
TPL_RESERVED_CHARS = r"()/."
|
||||
RE_TPL_RESERVED_CHARS = re.compile("[" + TPL_RESERVED_CHARS + "]")
|
||||
BROWSER_DIR = "_browser"
|
||||
BROWSER_META_FILE = "browser_meta.json"
|
||||
|
||||
TemplateData = namedtuple("TemplateData", ['site', 'theme', 'path'])
|
||||
|
||||
|
|
@ -350,6 +353,8 @@ class Renderer(object):
|
|||
self.sites_paths = {
|
||||
"": os.path.dirname(sat_templates.__file__),
|
||||
}
|
||||
self.sites_themes = {
|
||||
}
|
||||
conf = config.parseMainConf()
|
||||
public_sites = config.getConfig(conf, None, "sites_path_public_dict", {})
|
||||
sites_data = [public_sites]
|
||||
|
|
@ -372,6 +377,27 @@ class Renderer(object):
|
|||
normalised[name] = path
|
||||
self.sites_paths.update(normalised)
|
||||
|
||||
for site, site_path in self.sites_paths.items():
|
||||
tpl_path = Path(site_path) / C.TEMPLATE_TPL_DIR
|
||||
for p in tpl_path.iterdir():
|
||||
if not p.is_dir():
|
||||
continue
|
||||
log.debug(f"theme found for {site or 'default site'}: {p.name}")
|
||||
theme_data = self.sites_themes.setdefault(site, {})[p.name] = {'path': p}
|
||||
browser_path = p / BROWSER_DIR
|
||||
if browser_path.is_dir():
|
||||
theme_data['browser_path'] = browser_path
|
||||
browser_meta_path = browser_path / BROWSER_META_FILE
|
||||
if browser_meta_path.is_file():
|
||||
try:
|
||||
with browser_meta_path.open() as f:
|
||||
theme_data['browser_meta'] = json.load(f)
|
||||
except Exception as e:
|
||||
log.error(
|
||||
f"Can't parse browser metadata at {browser_meta_path}: {e}"
|
||||
)
|
||||
continue
|
||||
|
||||
self.env = Environment(
|
||||
loader=TemplateLoader(sites_paths=self.sites_paths, trusted=trusted),
|
||||
autoescape=jinja2.select_autoescape(["html", "xhtml", "xml"]),
|
||||
|
|
@ -516,6 +542,12 @@ class Renderer(object):
|
|||
raise exceptions.NotFound
|
||||
return theme, os.path.join(site_root_dir, C.TEMPLATE_TPL_DIR, theme)
|
||||
|
||||
def getThemesData(self, site_name):
|
||||
try:
|
||||
return self.sites_themes[site_name]
|
||||
except KeyError:
|
||||
raise exceptions.NotFound(f"no theme found for {site_name}")
|
||||
|
||||
def getStaticPath(self, template_data, filename):
|
||||
"""Retrieve path of a static file if it exists with current theme or default
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue