~ Dc @ s d Z d Z d Z d d k Td d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k
Z
d d k Z d d k Z d d k
Z
d Z d Z d Z d Z d Z d Z d Z d Z e Z y d d k Z Wn- y d d k Z Wn
e Z n Xe Z n Xe Z d Z d Z d Z d
f d YZ d f d
YZ d f d YZ d f d YZ d e! f d YZ" d e! f d YZ# d S( s> 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
gQ?s Tomas Styblo (tripie@cpan.org)i( t *Nt inci i i t TemplateManagerc B sq e Z d Z d d d d d d d Z d Z d Z d Z d Z d Z d
Z d Z
d Z RS(
s{ 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.
i i i c C sc | | _ | | _ | | _ | | _ | | _ | | _ | o t o
t d n | i d d S( sg 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
TMPL_INCLUDE 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 TMPL_INCLUDE 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 TemplateErrorexception is raised when the precompiled
template cannot be saved. Precompilation is enabled by default.
Precompilation is available only on UNIX and Windows platforms,
because proper file locking which is necessary to ensure
multitask safe behaviour is platform specific and is not
implemented for other platforms. Attempts to enable precompilation
on the other platforms result in raise of the
TemplateError exception.
@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.
s: Template precompilation is not available on this platform.s INIT DONEN( t _includet _max_includet _precompilet _commentst _gettextt _debugt LOCKTYPEt
TemplateErrort DEB( t selft includet max_includet
precompilet commentst gettextt debug( ( s6 /home/mrben/misterben.org.uk/planet/planet/htmltmpl.pyt __init__] s >
c C s: d } | i o
| i | o y | i | } WnB t j
o6 } t i d | IJ| i | } | i | qX| i | i
| i | i | i
| i f } | i | o | i d | } q| i d | i | } q6| i d | i | } | i | n | i d | i | } | S( s 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
TMPL_INCLUDE 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 Template class
which is a compiled form of the template. This instance can be
used as input for the TemplateProcessor.
@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 'inc' of the
directory in which the main template file is located.
s/ Htmltmpl: bad precompiled template '%s' removeds PRECOMPILED: UPTODATEs PRECOMPILED: NOT UPTODATEs PRECOMPILED: NOT PRECOMPILEDs PRECOMPILATION DISABLEDN( t NoneR t is_precompiledt load_precompiledt PrecompiledErrort syst stderrt compilet save_precompiledR R R R R R t is_uptodateR t update( R t filet compiledt precompiledt templatet compile_params( ( s6 /home/mrben/misterben.org.uk/planet/planet/htmltmpl.pyt prepare s0
c C sA | i d | i | i } | i o | i | n | S( s Update (recompile) a compiled template.
This method recompiles a template compiled from a file.
If precompilation is enabled then the precompiled form saved on
disk is also updated.
@header update(template)
@return Recompiled template.
It's ensured that the returned template is up-to-date.
@param template A compiled template.
This parameter should be an instance of the Template
class, created either by the TemplateManager or by the
TemplateCompiler. The instance must represent a template
compiled from a file on disk.
t UPDATE( R R R R R ( R R! t updated( ( s6 /home/mrben/misterben.org.uk/planet/planet/htmltmpl.pyR s
c C s | i o t i | IJn d S( sY Print debugging message to stderr if debugging is enabled.
@hidden
N( R R R ( R t str( ( s6 /home/mrben/misterben.org.uk/planet/planet/htmltmpl.pyR s
c C s. | i } t t j oy | t j o t i | t i q*| t j o t i | t i q*| t j o t i | t i q*t d n t t j o | t j o t
i | t
i d q*| t j o t
i | t
i d q*| t j o t
i | t
i
d q*t d n
t d d S( sH Provide platform independent file locking.
@hidden
s BUG: bad lock in lock_filei s BUG: bad locktype in lock_fileN( t filenoR t LOCKTYPE_FCNTLt LOCK_SHt fcntlt flockt LOCK_EXt LOCK_UNR
t LOCKTYPE_MSVCRTt msvcrtt lockingt LK_LOCKt LK_UNLCK( R R t lockt fd( ( s6 /home/mrben/misterben.org.uk/planet/planet/htmltmpl.pyt lock_file s$
c C s. t | i | i | i | i | i i | S( s3 Compile the template.
@hidden
( t TemplateCompilerR R R R R R ( R R ( ( s6 /home/mrben/misterben.org.uk/planet/planet/htmltmpl.pyR " s c C s- | d } t i i | o d Sn d Sd S( s Return true if the template is already precompiled on the disk.
This method doesn't check whether the compiled template is
uptodate.
@hidden
t ci i N( t ost patht isfile( R R t filename( ( s6 /home/mrben/misterben.org.uk/planet/planet/htmltmpl.pyR * s
c
C s | d } | i d z d } d } y2 t | d } | i | t t i | } Wnd t j
o% \ } } t d | | | f n9 t i j
o d } t
| n d } n X| SWd | o | i | t | i n | o$ t
i i | o t
i | n Xd S( s Load precompiled template from disk.
Remove the precompiled template file and recompile it
if the file contains corrupted or unpicklable data.
@hidden
R7 s LOADING PRECOMPILEDi t rbs3 IO error in load precompiled template '%s': (%d) %si N( R R t openR5 R) t cPicklet loadt IOErrorR
t UnpicklingErrorR R- t closeR8 R9 R: t remove( R R R; t
remove_badR t errnot errstr( ( s6 /home/mrben/misterben.org.uk/planet/planet/htmltmpl.pyR 6 s0
c
C s | i d } t i i t i i | } t i | t i p t d | n z d } d } yc t | d } | i
| t d } d } | i o t
i | | | n t
i | | | Wnv t j
o+ \ } } d } t d | | | f nN t
i j
o" }
d } t d | |
f n d } n X| i d Wd | o | i
| t | i n | o$ t i i | o t i | n Xd S(
s Save compiled template to disk in precompiled form.
Associated metadata is also saved. It includes: filename of the
main template file, modification time of the main template file,
modification times of all included templates and version of the
htmltmpl module which compiled the template.
The method removes a file which is saved only partially because
of some error.
@hidden
R7 sC Cannot save precompiled templates to '%s': write permission denied.i t wbi s8 IO error while saving precompiled template '%s': (%d) %ss9 Pickling error while saving precompiled template '%s': %ss SAVING PRECOMPILEDN( R R8 R9 t dirnamet abspatht accesst W_OKR
R R= R5 R, R R> t dumpR@ t
PicklingErrorR R- RB R: RC ( R R! R; t template_dirRD R t BINARYt READABLERE RF t error( ( s6 /home/mrben/misterben.org.uk/planet/planet/htmltmpl.pyR [ sB
( t __name__t
__module__t __doc__R R# R R R5 R R R R ( ( ( s6 /home/mrben/misterben.org.uk/planet/planet/htmltmpl.pyR S s M : %t TemplateProcessorc B sw e Z d Z d d d d d Z d Z d d Z d
d Z d Z d
d Z d Z
d
d Z d Z RS( s Fill the template with data and process it.
This class provides actual processing of a compiled template.
Use it to set template variables and loops and then obtain
result of the processing.
i i c C sC | | _ | | _ | | _ | | _ h | _ d | _ d | _ d S( s Constructor.
@header __init__(html_escape=1, magic_vars=1, global_vars=0,
debug=0)
@param html_escape Enable or disable HTML escaping of variables.
This optional parameter is a flag that can be used to enable or
disable automatic HTML escaping of variables.
All variables are by default automatically HTML escaped.
The escaping process substitutes HTML brackets, ampersands and
double quotes with appropriate HTML entities.
@param magic_vars Enable or disable loop magic variables.
This parameter can be used to enable or disable
"magic" context variables, that are automatically defined inside
loops. Magic variables are enabled by default.
Refer to the language specification for description of these
magic variables.
@param global_vars Globally activate global lookup of variables.
This optional parameter is a flag that can be used to specify
whether variables which cannot be found in the current scope
should be automatically looked up in enclosing scopes.
Automatic global lookup is disabled by default. Global lookup
can be overriden on a per-variable basis by the
GLOBAL parameter of a TMPL_VAR
statement.
@param debug Enable or disable debugging messages.
i i N( t _html_escapet _magic_varst _global_varsR t _varst
_current_partt _current_pos( R t html_escapet
magic_varst global_varsR ( ( s6 /home/mrben/misterben.org.uk/planet/planet/htmltmpl.pyR s ! c C s | i | o" | i p t d | qz nI t | t j o( | | i j o t d | qz n t d | | | i | <| i d t | d S( s Associate a value with top-level template variable or loop.
A template identifier can represent either an ordinary variable
(string) or a loop.
To assign a value to a string identifier pass a scalar
as the 'value' parameter. This scalar will be automatically
converted to string.
To assign a value to a loop identifier pass a list of mappings as
the 'value' parameter. The engine iterates over this list and
assigns values from the mappings to variables in a template loop
block if a key in the mapping corresponds to a name of a variable
in the loop block. The number of mappings contained in this list
is equal to number of times the loop block is repeated in the
output.
@header set(var, value)
@return No return value.
@param var Name of template variable or loop.
@param value The value to associate.
s Invalid variable name '%s'.s Invalid loop name '%s'.sB Value of toplevel variable '%s' must be either a scalar or a list.s VALUE SET: N( t is_ordinary_vart islowerR
t typet ListTypet
capitalizeRY R R&