Newer
Older
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
""" A templating engine for separation of code and HTML.
The documentation of this templating engine is separated to two parts:
1. Description of the templating language.
2. Documentation of classes and API of this module that provides
a Python implementation of the templating language.
All the documentation can be found in 'doc' directory of the
distribution tarball or at the homepage of the engine.
Latest versions of this module are also available at that website.
You can use and redistribute this module under conditions of the
GNU General Public License that can be found either at
[ http://www.gnu.org/ ] or in file "LICENSE" contained in the
distribution tarball of this module.
Copyright (c) 2001 Tomas Styblo, tripie@cpan.org
@name htmltmpl
@version 1.22
@author-name Tomas Styblo
@author-email tripie@cpan.org
@website http://htmltmpl.sourceforge.net/
@license-name GNU GPL
@license-url http://www.gnu.org/licenses/gpl.html
"""
__version__ = 1.22
__author__ = "Tomas Styblo (tripie@cpan.org)"
# All imported modules are part of the standard Python library.
from types import *
import re
import os
import os.path
import pprint # only for debugging
import sys
import copy
import cgi # for HTML escaping of variables
import urllib # for URL escaping of variables
import cPickle # for template compilation
import gettext
import portalocker # for locking
INCLUDE_DIR = "inc"
# Total number of possible parameters.
# Increment if adding a parameter to any statement.
PARAMS_NUMBER = 3
# Relative positions of parameters in TemplateCompiler.tokenize().
PARAM_NAME = 1
PARAM_ESCAPE = 2
PARAM_GLOBAL = 3
PARAM_GETTEXT_STRING = 1
##############################################
# CLASS: TemplateManager #
##############################################
class TemplateManager:
""" Class that manages compilation and precompilation of templates.
You should use this class whenever you work with templates
that are stored in a file. The class can create a compiled
template and transparently manage its precompilation. It also
keeps the precompiled templates up-to-date by modification times
comparisons.
"""
def __init__(self, include=1, max_include=5, precompile=1, comments=1,
gettext=0, debug=0):
""" Constructor.
@header
__init__(include=1, max_include=5, precompile=1, comments=1,
gettext=0, debug=0)
@param include Enable or disable included templates.
This optional parameter can be used to enable or disable
<em>TMPL_INCLUDE</em> inclusion of templates. Disabling of
inclusion can improve performance a bit. The inclusion is
enabled by default.
@param max_include Maximum depth of nested inclusions.
This optional parameter can be used to specify maximum depth of
nested <em>TMPL_INCLUDE</em> inclusions. It defaults to 5.
This setting prevents infinite recursive inclusions.
@param precompile Enable or disable precompilation of templates.
This optional parameter can be used to enable or disable
creation and usage of precompiled templates.
A precompiled template is saved to the same directory in
which the main template file is located. You need write
permissions to that directory.
Precompilation provides a significant performance boost because
it's not necessary to parse the templates over and over again.
The boost is especially noticeable when templates that include
other templates are used.
Comparison of modification times of the main template and all
included templates is used to ensure that the precompiled
templates are up-to-date. Templates are also recompiled if the
htmltmpl module is updated.
The <em>TemplateError</em>exception is raised when the precompiled
template cannot be saved. Precompilation is enabled by default.
@param comments Enable or disable template comments.
This optional parameter can be used to enable or disable
template comments.
Disabling of the comments can improve performance a bit.
Comments are enabled by default.
@param gettext Enable or disable gettext support.
@param debug Enable or disable debugging messages.
This optional parameter is a flag that can be used to enable
or disable debugging messages which are printed to the standard
error output. The debugging messages are disabled by default.
"""
# Save the optional parameters.
# These values are not modified by any method.
self._include = include
self._max_include = max_include
self._precompile = precompile
self._comments = comments
self._gettext = gettext
self._debug = debug
self.DEB("INIT DONE")
def prepare(self, file):
""" Preprocess, parse, tokenize and compile the template.
If precompilation is enabled then this method tries to load
a precompiled form of the template from the same directory
in which the template source file is located. If it succeeds,
then it compares modification times stored in the precompiled
form to modification times of source files of the template,
including source files of all templates included via the
<em>TMPL_INCLUDE</em> statements. If any of the modification times
differs, then the template is recompiled and the precompiled
form updated.
If precompilation is disabled, then this method parses and
compiles the template.
@header prepare(file)
@return Compiled template.
The methods returns an instance of the <em>Template</em> class
which is a compiled form of the template. This instance can be
used as input for the <em>TemplateProcessor</em>.
@param file Path to the template file to prepare.
The method looks for the template file in current directory
if the parameter is a relative path. All included templates must
be placed in subdirectory <strong>'inc'</strong> of the
directory in which the main template file is located.
"""
compiled = None
if self._precompile:
if self.is_precompiled(file):
try:
precompiled = self.load_precompiled(file)
except PrecompiledError, template:
print >> sys.stderr, "Htmltmpl: bad precompiled "\
"template '%s' removed" % template
compiled = self.compile(file)
self.save_precompiled(compiled)
else:
precompiled.debug(self._debug)
compile_params = (self._include, self._max_include,
self._comments, self._gettext)
if precompiled.is_uptodate(compile_params):
self.DEB("PRECOMPILED: UPTODATE")
compiled = precompiled
else:
self.DEB("PRECOMPILED: NOT UPTODATE")
compiled = self.update(precompiled)
else:
self.DEB("PRECOMPILED: NOT PRECOMPILED")
compiled = self.compile(file)
self.save_precompiled(compiled)
else:
self.DEB("PRECOMPILATION DISABLED")
compiled = self.compile(file)
return compiled
def update(self, template):
""" Update (recompile) a compiled template.
This method recompiles a template compiled from a file.
Loading
Loading full blame...