m Cc@swdZdZdZdkTdkZdkZdkZdkZdkZdk Z dk Z dk Z dk Z dk Z dZdZdZdZdZdZdZdZeZy dkZWn*y dkZWn eZnXeZnXeZdZdZdZd fd YZd fd YZd fdYZdfdYZ de!fdYZ"de!fdYZ#dS(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 f1.22sTomas Styblo (tripie@cpan.org)(t*NtinciiitTemplateManagercBsqtZdZdddddddZdZdZdZdZd Zd 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. iiicCsc||_||_||_||_||_ ||_ |ot o tdn|iddS(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(tincludetselft_includet max_includet _max_includet precompilet _precompiletcommentst _commentstgettextt_gettexttdebugt_debugtLOCKTYPEt TemplateErrortDEB(RRRRR R R((tF/home/users/jdub/public_html/bzr/planet/devel/trunk/planet/htmltmpl.pyt__init__]s;       cCs:d}|io |i|oy|i|}WnBtj o6}t i d|IJ|i |}|i |qX|i|i|i|i|i|if}|i|o|id|}q|id|i|}q6|id|i |}|i |n|id|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' removedsPRECOMPILED: UPTODATEsPRECOMPILED: NOT UPTODATEsPRECOMPILED: NOT PRECOMPILEDsPRECOMPILATION DISABLEDN(tNonetcompiledRR tis_precompiledtfiletload_precompiledt precompiledtPrecompiledErrorttemplatetsyststderrtcompiletsave_precompiledRRRRR R tcompile_paramst is_uptodateRtupdate(RRRR!RR((Rtprepares.      cCsA|id|i|i}|io|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. tUPDATEN(RRRRRtupdatedR R (RRR&((RR#s   cCs|ioti|IJndS(sY Print debugging message to stderr if debugging is enabled. @hidden N(RRRRtstr(RR'((RRs cCs.|i}ttjoy|tjoti|tiq*|t joti|ti q*|t joti|ti q*t dntt jo|tjot i|t idq*|t jot i|t idq*|t jot i|t idq*t dn t ddS(sH Provide platform independent file locking. @hidden sBUG: bad lock in lock_fileisBUG: bad locktype in lock_fileN(RtfilenotfdRtLOCKTYPE_FCNTLtlocktLOCK_SHtfcntltflocktLOCK_EXtLOCK_UNRtLOCKTYPE_MSVCRTtmsvcrttlockingtLK_LOCKtLK_UNLCK(RRR+R)((Rt lock_files&           cCs.t|i|i|i|i|ii|S(s3 Compile the template. @hidden N( tTemplateCompilerRRRR R RRR(RR((RR"s cCs-|d}tii|odSndSdS(s Return true if the template is already precompiled on the disk. This method doesn't check whether the compiled template is uptodate. @hidden tciiN(Rtfilenametostpathtisfile(RRR9((RR*s  cCs|d}|idzd}d}y2t|d}|i|tt i |}Wndt j o%\}}td|||fn9t ij od}t|nd}nX|SWd|o|i|t|in|o$tii|oti|nXdS(s Load precompiled template from disk. Remove the precompiled template file and recompile it if the file contains corrupted or unpicklable data. @hidden R8sLOADING PRECOMPILEDitrbs3IO error in load precompiled template '%s': (%d) %siN(RR9RRt remove_badRtopenR6R,tcPickletloadRtIOErrorterrnoterrstrRtUnpicklingErrorRR0tcloseR:R;R<tremove(RRRCR>R9RDR((RR6s2   c Cs|id}tiitii|}ti|ti pt d|nzd}d }yct |d}|i|td}d}|ioti|||nti|||Wnvtj o+\} } d}t d|| | fnNtij o"}d}t d||fnd}nX|idWd |o|i|t|in|o$tii|oti|nXd 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 R8sCCannot save precompiled templates to '%s': write permission denied.itwbis8IO error while saving precompiled template '%s': (%d) %ss9Pickling error while saving precompiled template '%s': %ssSAVING PRECOMPILEDN( RRR9R:R;tdirnametabspatht template_dirtaccesstW_OKRR>RR?RR6R/tBINARYtREADABLERR@tdumpRBRCRDt PicklingErrorterrorRR0RFR<RG( RRR>RRKRNRRROR9RDRC((RR [s@  ( t__name__t __module__t__doc__RR$R#RR6RRRR (((RRSs N :     %tTemplateProcessorcBswtZdZdddddZdZddZedZdZedZ 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. iicCsC||_||_||_||_h|_ d|_ d|_ dS(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. iiN( t html_escapeRt _html_escapet magic_varst _magic_varst global_varst _global_varsRRt_varst _current_partt _current_pos(RRWRYR[R((RRs       cCs|i|o"|iptd|qznIt|tjo(||ijotd|qzntd|||i |<|i dt |dS(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. sInvalid variable name '%s'.sInvalid loop name '%s'.sBValue of toplevel variable '%s' must be either a scalar or a list.s VALUE SET: N( Rtis_ordinary_vartvaluetvartislowerRttypetListTypet capitalizeR]RR'(RRbRa((Rtsets   cCs;d|_d|_|p|iin|iddS(s Reset the template data. This method resets the data contained in the template processor instance. The template processor instance can be used to process any number of templates, but this method must be called after a template is processed to reuse the instance, @header reset(keep_data=0) @return No return value. @param keep_data Do not reset the template data. Use this flag if you do not want the template data to be erased. This way you can reuse the data contained in the instance of the TemplateProcessor. iitRESETN(RR^R_t keep_dataR]tclearR(RRi((Rtresets   cCsi|id|ioti|itin|d2jo*|djp||i jo t dnd}g}d}d}g}g} g}g}|i}t|} d}|i}x{|| joPn|od}|t7}qn||}|idp|ido|djo||t} | p t d n||t}||t!}d}||joRt#|i$| || ||}||i ||7}|id t#| qq.|d jo||t} | p t d nd}|i$| || |}|p d}n|i'||i'|| i'd|i'| |djo(|i'||id t#| q|i'||id| |fq.|djo||t} | p t dn||t!}d}|i$| || ||o(|i'||idt#| q|i'||idt#| q.|djo||t} | p t dn||t!}d}|i$| || ||o(|i'||idt#| q|i'||idt#| q.|djod}|p t dn|ddjo| dcd7Template class created either by the TemplateManager or by the TemplateCompiler. @param part The part of a multipart template to process. This parameter can be used only together with a multipart template. It specifies the number of the part to process. It must be greater than zero, because the parts are numbered from one. The parts must be processed in the right order. You cannot process a part which precedes an already processed part. If this parameter is not specified, then the whole template is processed, or all remaining parts are processed. s APP INPUT:isprocess() - invalid part numberits.sVAR: s .sLOOP: DISABLE: sLOOP: FIRST PASS: %s TOTAL: %ds.s IF: ENABLE: s IF: DISABLE: s .sUNLESS: DISABLE: sUNLESS: ENABLE: s .is LOOP: ENDsLOOP: NEXT PASSs .sIF: ENDs .s UNLESS: ENDs .s ELSE: ENABLEs ELSE: DISABLEsBUG: ELSE: INVALID FLAGs

HTMLTMPL WARNING:
Cannot include template: %s


sCANNOT INCLUDE WARNINGs .sMissing .s$Missing or N(-RRRtpprintR]RRtpartRR^Rt skip_paramstoutput_controlt ENABLE_OUTPUTtDISABLE_OUTPUTt loop_namet loop_passt loop_startt loop_totalRttokenstlent len_tokenstoutR_tit PARAMS_NUMBERttokent startswitht PARAM_NAMERbt PARAM_ESCAPEtescapet PARAM_GLOBALtglobalpR't find_valueRat passtotaltappendtpopR9tPARAM_GETTEXT_STRINGttextR (RRRnRvRRRzRsRqR9RyRRbRtRRwRuRrRpR{RaRoR}((Rtprocesss*  *                                                            cCs|ioti|IJndS(sX Print debugging message to stderr if debugging is enabled. @hidden N(RRRRR'(RR'((RRs c Cs|io6|ido&|o|i||d|dSn|i}g}xt t |D]}|io |djp |djo9|i|o)|i||o|i||n|i||o)|||o|||||}qbdSqbW|i|o5t||tjot ||Sq||SnW|o2|io |djp |djo|iSn|diodSndSdS(s  Search the self._vars data structure to find variable var located in currently processed pass of a loop which is currently being processed. If the variable is an ordinary variable, then return it. If the variable is an identificator of a loop, then return the total number of times this loop will be executed. Return an empty string, if the variable is not found at all. @hidden t__it0t1RliN(RRZRbR~Rst magic_varRtRvR]tscopetglobalstrangeRxR{R\tglobal_overridethas_keyR`RRdReRtisupper( RRbRsRtRvRR{RR((RR s*! H# +cCs|id|||f|djo|djodSqdSn|djo!||djodSqdSnb|djo.|djo||djodSqdSn'|djo |dSn|djo|Sn|d jo%|dd djodSqdSn|id o|djo||djoyt|d }Wntj ot d qX|p t dq|d|djo|idt |dSqdSqdSnt d|dS(s Resolve and return value of a magic variable. Raise an exception if the magic variable is not recognized. @hidden s MAGIC: '%s', PASS: %d, TOTAL: %dt __FIRST__iit__LAST__t __INNER__t__PASS__t __PASSTOTAL__t__ODD__it __EVERY__i s/Magic variable __EVERY__x: Invalid pass number.s6Magic variable __EVERY__x: Pass number cannot be zero.sMAGIC: EVERY: sInvalid magic variable '%s'.N( RRRbRtRvR~tintteveryt ValueErrorRR'(RRbRtRvR((RRHsH         RlcCsd}|io'|djo|djo |djp|djp |djoti||Sn#|djoti|Sn|SdS(sY Escape a string either by HTML escaping or by URL escaping. @hidden itNONERtURLtHTMLRN( t ESCAPE_QUOTESRRXtoverridetcgiRR'turllibt quote_plus(RR'RR((RRsK cCs\t|tjp9t|tjp&t|tjpt|tjodSndSdS(sW Return true if var is a scalar. (not a reference to loop) @hidden iiN(RdRbt StringTypetIntTypetLongTypet FloatType(RRb((RR`sL( RSRTRURRgRkRRRRRRR`(((RRVs 0 )   = = R7cBstZdZddddddZdZdZdZdZd Zd Z d Z d Z d Z dZ dZdZdZdZRS(s Preprocess, parse, tokenize and compile the template. This class parses the template and produces a 'compiled' form of it. This compiled form is an instance of the Template class. The compiled form is used as input for the TemplateProcessor which uses it to actually process the template. This class should be used direcly only when you need to compile a template from a string. If your template is in a file, then you should use the TemplateManager class which provides a higher level interface to this class and also can save the compiled template to disk in a precompiled form. iiicCsC||_||_||_||_||_ g|_ d|_ dS(s Constructor. @header __init__(include=1, max_include=5, comments=1, gettext=0, debug=0) @param include Enable or disable included templates. @param max_include Maximum depth of nested inclusions. @param comments Enable or disable template comments. @param gettext Enable or disable gettext support. @param debug Enable or disable debugging messages. iN( RRRRRR R R R RRt_include_filest_include_level(RRRR R R((RRs       cCs|id|tiitii|t|_|i |i |}|i |i |i|if}tt||i|||iS(s Compile template from a file. @header compile(file) @return Compiled template. The return value is an instance of the Template class. @param file Filename of the template. See the prepare() method of the TemplateManager class for exaplanation of this parameter. sCOMPILING FROM FILE: N(RRRR:R;tjoinRIt INCLUDE_DIRt _include_pathtparsetreadRwRRR R R!tTemplatet __version__RR(RRR!Rw((RRs $cCs_|idd|_|i|}|i|i|i|if}t t dd|||i S(s Compile template from a string. This method compiles a template from a string. The template cannot include any templates. TMPL_INCLUDE statements are turned into warnings. @header compile_string(data) @return Compiled template. The return value is an instance of the Template class. @param data String containing the template data. sCOMPILING FROM STRINGiN(RRRRtdataRwRR R R!RRRR(RRR!Rw((Rtcompile_strings   cCs|ioti|IJndS(sX Print debugging message to stderr if debugging is enabled. @hidden N(RRRRR'(RR'((RRs cCs|id|zad}yt|d}|i}Wn1tj o%\}}t d|||fnX|SWd|o|i nXdS(sq Read content of file and return it. Raise an error if a problem occurs. @hidden s READING: trs-IO error while reading template '%s': (%d) %sN( RRR9RtfR?RRRBRCRDRRF(RR9RRDRCR((RRscCse|io |id|i|}n|i|}|io|id|i|n|S(s Parse the template. This method is recursively called from within the include_templates() method. @return List of processing tokens. @hidden sPREPROCESS: COMMENTSsPREPROCESS: INCLUDESN( RR Rtremove_commentst template_datattokenizeRwRtinclude_templates(RRRw((RRs    cCsd}ti|d|S(sE Remove comments from the template data. @hidden s### .*RlN(tpatterntretsubR(RRR((RR$sc Csd}d} d}xD|t|joPn|od}|t7}qn||}|djo||t}|p t dn|i d7_ |i |i jod}|i d|qJd}tii|i|}|ii||i|}|i|}||||td+|t|}|i d|qn|d7}qW|i djo|i d8_ n| S( s Process TMPL_INCLUDE statements. Use the include_level counter to prevent infinite recursion. Record paths to all included templates to self._include_files. @hidden iRlis .sINCLUDE: LIMIT REACHED: s INCLUDED: N(R{RzRoRxRwR|R}RR9RRRRRR:R;RRt include_fileRRRt include_dataRtinclude_tokens( RRwRR{RR9RoR}RRz((RR+sD    cCs[|iddti}ti|titiB}|i |}g}x |D]}|idp0|idp |idp|ido|i|}ti d|}|i|i||i|i||i|id||i|id |qR|io!|id |i||qR|i|qRW|S( s  Split the template into tokens separated by template statements. The statements itself and associated parameters are also separately included in the resulting list of tokens. Return list of the tokens. @hidden sTOKENIZING TEMPLATEs (?:^[ \t]+)? # eat spaces, tabs (opt.) (< (?:!--[ ])? # comment start + space (opt.) /?TMPL_[A-Z]+ # closing slash / (opt.) + statement [ a-zA-Z0-9""/.=:_\\-]* # this spans also comments ending (--) >) [%s]? # eat trailing newline (opt.) s|idp|ido|dd!Sn |dd!SdS(s Strip HTML brackets (with optional HTML comments) from the beggining and from the end of a statement. @hidden s