File: xsltTemplate.py

package info (click to toggle)
okasha 0.2.4-4
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye, sid
  • size: 856 kB
  • sloc: python: 1,092; sh: 29; makefile: 7
file content (49 lines) | stat: -rw-r--r-- 1,494 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: UTF-8 -*-
"""
Okasha - a very simple WSGI webframe work
Copyright © 2009, Muayyad Alsadi <alsadi@ojuba.org>

    Released under terms of Waqf Public License.
    This program is free software; you can redistribute it and/or modify
    it under the terms of the latest version Waqf Public License as
    published by Ojuba.org.

    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.

    The Latest version of the license can be found on
    "http://waqf.ojuba.org/license"

"""
import sys, os, os.path, threading
from lxml import etree
from .utils import ObjectsCache # kid has its own cache

from .baseWebApp import fileNotFoundException

xsltCache=ObjectsCache(lock=threading.Lock())

def xsltTemplate(rq, o, bfn=None, **kw):
  """
  
  """
  fn=rq.webapp._getTemplateFile(bfn, default="root.xsl")
  # prepare xsl trans
  trans=xsltCache.get(fn)
  if not trans:
    try: xslt_doc=etree.parse(fn)
    except IOError: raise fileNotFoundException()
    except:
      # FIXME: check for valid template # lxml.etree.XMLSyntaxError:
      rq.webapp._logger.debug('template error fn=[%s]' % fn)
      raise
    trans = etree.XSLT(xslt_doc)
    xsltCache.append(fn, trans)
  # prepare object
  if isinstance(o,str):
    doc=etree.fromstring(o)
  else: doc=o
  r = trans(doc, **kw)
  return etree.tostring(r, encoding='UTF-8' , pretty_print=True)