~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to turbozpt/zptsupport.py

tidyings, and efforts to not recompile some of the zpts each time they're used

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# This program is free software; you can redistribute it and/or modify
3
 
# it under the terms of the GNU General Public License as published by
4
 
# the Free Software Foundation; either version 2 of the License, or
5
 
# (at your option) any later version.
6
 
#
7
 
# This program is distributed in the hope that it will be useful,
8
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
# GNU General Public License for more details.
11
 
#
12
 
# You should have received a copy of the GNU General Public License
13
 
# along with this program; if not, write to the Free Software
14
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 
#
16
 
"""Support for Zope Page Templates using the simpletal library."""
 
1
"TurboGears support for Zope Page Templates"
 
2
 
 
3
import sys, os
 
4
 
 
5
from template import PageTemplate
 
6
import pkg_resources
17
7
 
18
8
import logging
19
 
import os
20
 
import pkg_resources
21
 
import re
22
 
import StringIO
23
 
 
24
 
from simpletal import simpleTAL, simpleTALES
25
 
 
26
 
_zpt_cache = {}
27
 
 
28
 
 
29
 
def zpt(tfile):
30
 
    tinstance = _zpt_cache.get(tfile)
31
 
    stat = os.stat(tfile)
32
 
    if tinstance is None or tinstance.stat != stat:
33
 
        text = open(tfile).read()
34
 
        text = re.sub(r'\s*\n\s*', '\n', text)
35
 
        text = re.sub(r'[ \t]+', ' ', text)
36
 
        tinstance = _zpt_cache[tfile] = TemplateWrapper(
37
 
            simpleTAL.compileXMLTemplate(text), tfile, stat)
38
 
    return tinstance
39
 
 
40
 
 
41
 
class TemplateWrapper(object):
42
 
 
43
 
    def __init__(self, template, filename, stat):
44
 
        self.template = template
45
 
        self.filename = filename
46
 
        self.stat = stat
47
 
 
48
 
    def expand(self, **info):
49
 
        context = simpleTALES.Context(allowPythonPath=1)
50
 
        for k, v in info.iteritems():
51
 
            context.addGlobal(k, v)
52
 
        s = StringIO.StringIO()
53
 
        self.template.expandInline(context, s)
54
 
        return s.getvalue()
55
 
 
56
 
    def expand_into(self, f, **info):
57
 
        context = simpleTALES.Context(allowPythonPath=1)
58
 
        for k, v in info.iteritems():
59
 
            context.addGlobal(k, v)
60
 
        self.template.expand(context, f, 'utf-8')
61
 
 
62
 
    @property
63
 
    def macros(self):
64
 
        return self.template.macros
65
 
 
66
 
 
67
 
def load_template(classname):
68
 
    """Searches for a template along the Python path.
69
 
 
70
 
    Template files must end in ".pt" and be in legitimate packages.
71
 
    Templates are automatically checked for changes and reloaded as
72
 
    neccessary.
73
 
    """
74
 
    divider = classname.rfind(".")
75
 
    if divider > -1:
76
 
        package = classname[0:divider]
77
 
        basename = classname[divider+1:]
78
 
    else:
79
 
        raise ValueError("All templates must be in a package")
80
 
 
81
 
    tfile = pkg_resources.resource_filename(
82
 
        package, "%s.%s" % (basename, "pt"))
83
 
    return zpt(tfile)
 
9
log = logging.getLogger("turbogears.zptsupport")
 
10
 
 
11
def _recompile_template(package, basename, tfile, classname):
 
12
    log.debug("Recompiling template for %s" % classname)
 
13
    mod = PageTemplate(tfile)
 
14
    mtime = os.stat(tfile).st_mtime
 
15
    mod.__mtime__ = mtime
 
16
    return mod
 
17
 
 
18
class TurboZpt:
 
19
    extension = "pt"
 
20
    
 
21
    def __init__(self, extra_vars_func=None, options={}):
 
22
        self.options = options
 
23
        self.get_extra_vars = extra_vars_func
 
24
        self.compiledTemplates = {}
 
25
    
 
26
    def load_template(self, classname, loadingSite=False):
 
27
        """Searches for a template along the Python path.
 
28
 
 
29
        Template files must end in ".pt" and be in legitimate packages.
 
30
        U can set "zpt.cache_templates" option to cache a loaded template
 
31
        class and only check for updates. Templates are automatically
 
32
        checked for changes and reloaded as neccessary.
 
33
        """
 
34
        ct = self.compiledTemplates
 
35
 
 
36
        divider = classname.rfind(".")
 
37
        if divider > -1:
 
38
            package = classname[0:divider]
 
39
            basename = classname[divider+1:]
 
40
        else:
 
41
            raise ValueError, "All templates must be in a package"
 
42
 
 
43
        cache_templates = self.options.get("zpt.cache_templates", True)
 
44
        tfile = pkg_resources.resource_filename(package, 
 
45
                                                "%s.%s" % 
 
46
                                                (basename,
 
47
                                                self.extension))
 
48
                                                
 
49
        if cache_templates:
 
50
            if ct.has_key(classname):
 
51
                mtime = os.stat(tfile).st_mtime
 
52
                mod = ct[classname]
 
53
                if mod.__mtime__ != mtime:
 
54
                    # Recompile template
 
55
                    mod = _recompile_template(package, basename, tfile, classname)
 
56
                    ct[classname] = mod
 
57
                #else:
 
58
                    # use template from cache    
 
59
                    # pass 
 
60
            else:
 
61
                # First time compile template
 
62
                mod = PageTemplate(tfile)
 
63
                mod.__mtime__ = os.stat(tfile).st_mtime
 
64
                ct[classname] = mod
 
65
        else:
 
66
            mod = PageTemplate(tfile)
 
67
 
 
68
        return mod
 
69
 
 
70
    def render(self, info, format="html", fragment=False, template=None):
 
71
        """Renders data in the desired format.
 
72
        
 
73
        @param info: the data / context itself
 
74
        @type info: dict
 
75
        @para format: "html"
 
76
        @type format: "string"
 
77
        @para template: name of the template to use
 
78
        @type template: string
 
79
        """
 
80
        #if isinstance(template, type):
 
81
        #    tclass = template
 
82
        #else:
 
83
        
 
84
        tinstance = self.load_template(template)
 
85
        log.debug("Applying template %s" % (tinstance.filename))
 
86
        data = dict()
 
87
        if self.get_extra_vars:
 
88
            data.update(self.get_extra_vars())
 
89
        data.update(info)
 
90
        return tinstance(**data).encode('utf-8')
 
91
 
 
92
    def transform(self, info, template):
 
93
        "Render the output to Elements"
 
94
        pass