,Qc@s|dZddlZddlZddlmZddlmZmZmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZddlmZmZddlmZddlmZddlmZdd lmZdd l m!Z!m"Z"dd l#m$Z$m%Z%m&Z&m'Z'dd l(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/dd l0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<ddl=m>Z>e*dZ?e@aAdZBdZCdZDdZEdZFdeGfdYZHdeGfdYZIe8deGfdYZJdeGfdYZKe7deGfdYZLeIeH_MdS(s jinja2.environment ~~~~~~~~~~~~~~~~~~ Provides a class that holds runtime and parsing time options. :copyright: (c) 2010 by the Jinja Team. :license: BSD, see LICENSE for more details. iN(tnodes(tBLOCK_START_STRINGtBLOCK_END_STRINGtVARIABLE_START_STRINGtVARIABLE_END_STRINGtCOMMENT_START_STRINGtCOMMENT_END_STRINGtLINE_STATEMENT_PREFIXtLINE_COMMENT_PREFIXt TRIM_BLOCKStNEWLINE_SEQUENCEtDEFAULT_FILTERSt DEFAULT_TESTStDEFAULT_NAMESPACEtKEEP_TRAILING_NEWLINEt LSTRIP_BLOCKS(t get_lexert TokenStream(tParser(t EvalContext(toptimize(tgenerate(t Undefinedt new_context(tTemplateSyntaxErrortTemplateNotFoundtTemplatesNotFoundtTemplateRuntimeError(t import_stringtLRUCachetMarkuptmissingtconcattconsumet internalcode( timaptifiltert string_typest iteritemst text_typetreraisetimplements_iteratortimplements_to_stringtget_nexttencode_filenametPY2tPYPY(treducei cGs_ytj|}Wntk r-t|SX|dk r>|St|t|<}t|_|S(sReturn a new spontaneous environment. A spontaneous environment is an unnamed and unaccessible (in theory) environment that is used for templates generated from a string and not from the file system. N(t_spontaneous_environmentstgett TypeErrort EnvironmenttNonetTruetshared(targstenv((s6/usr/lib/python2.7/site-packages/jinja2/environment.pytget_spontaneous_environment,s    cCs*|dkrdS|dkr iSt|S(s*Return the cache class for the given size.iN(R4R(tsize((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyt create_cache<s   cCs3|dkrdSt|tkr&iSt|jS(s(Create an empty copy of the given cache.N(R4ttypetdictRtcapacity(tcache((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyt copy_cacheEs  cCsLi}x?|D]7}t|tr1t|}n||||j`. `optimized` should the optimizer be enabled? Default is `True`. `undefined` :class:`Undefined` or a subclass of it that is used to represent undefined values in the template. `finalize` A callable that can be used to process the result of a variable expression before it is output. For example one can convert `None` implicitly into an empty string here. `autoescape` If set to true the XML/HTML autoescaping feature is enabled by default. For more details about auto escaping see :class:`~jinja2.utils.Markup`. As of Jinja 2.4 this can also be a callable that is passed the template name and has to return `True` or `False` depending on autoescape should be enabled by default. .. versionchanged:: 2.4 `autoescape` can now be a function `loader` The template loader for this environment. `cache_size` The size of the cache. Per default this is ``50`` which means that if more than 50 templates are loaded the loader will clean out the least recently used template. If the cache size is set to ``0`` templates are recompiled all the time, if the cache size is ``-1`` the cache will not be cleaned. `auto_reload` Some loaders load templates from locations where the template sources may change (ie: file system or database). If `auto_reload` is set to `True` (default) every time a template is requested the loader checks if the source changed and if yes, it will reload the template. For higher performance it's possible to disable that. `bytecode_cache` If set to a bytecode cache object, this object will provide a cache for the internal Jinja bytecode so that templates don't have to be parsed if they were not changed. See :ref:`bytecode-cache` for more information. i2cCs||_||_||_||_||_||_||_||_| |_| |_ | |_ | |_ ||_ ||_ ||_||_tj|_tj|_tj|_||_t||_||_||_t|| |_t|dS(N(tblock_start_stringtblock_end_stringtvariable_start_stringtvariable_end_stringtcomment_start_stringtcomment_end_stringtline_statement_prefixtline_comment_prefixt trim_blockst lstrip_blockstnewline_sequencetkeep_trailing_newlinet undefinedt optimizedtfinalizet autoescapeR tcopytfiltersR ttestsR tglobalstloaderR;R?tbytecode_cachet auto_reloadRGRDRH(tselfRIRJRKRLRMRNRORPRQRRRSRTRDRVRURWRXR]t cache_sizeR_R^((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyt__init__s2"                   cCs |jjt||gdS(s\Adds an extension after the environment was created. .. versionadded:: 2.5 N(RDtupdateRG(R`RF((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyt add_extension3scKsCx<t|D].\}}t||s t|||q q WdS(sAdd the items to the instance of the environment if they do not exist yet. This is used by :ref:`extensions ` to register callbacks and configuration values without breaking inheritance. N(R&thasattrtsetattr(R`t attributestkeytvalue((s6/usr/lib/python2.7/site-packages/jinja2/environment.pytextend:scCs8tt}|d=|d=|d=tj|j}|jj|jt|_||_ x9t |D]+\}}|t k rht |||qhqhW|t k rt ||_nt|j|_i|_x3t |jD]"\}}|j||j|us(titertsortedRDtvalues(R`((s6/usr/lib/python2.7/site-packages/jinja2/environment.pytiter_extensionsrscCsy ||SWnttfk rt|tr}yt|}Wntk rTq}Xyt||SWq}tk ryq}Xn|jd|d|SXdS(s:Get an item or attribute of an object but prefer the item.tobjtnameN( R2t LookupErrorRAR%tstrt ExceptiontgetattrtAttributeErrorRU(R`R}targumenttattr((s6/usr/lib/python2.7/site-packages/jinja2/environment.pytgetitemws   cCseyt||SWntk r$nXy ||SWn-tttfk r`|jd|d|SXdS(sGet an item or attribute of an object but prefer the attribute. Unlike :meth:`getitem` the attribute *must* be a bytestring. R}R~N(RRR2RRU(R`R}t attribute((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyRs  cCs|jj|}|dkr1td|n|gt|pCd}t|dtr|dkrwtdn|jd|n}t|dtr|dkr|dk r|j}qt |}n|jd|n%t|dtr|jd|n|||piS( sfInvokes a filter on a value the same way the compiler does it. .. versionadded:: 2.7 sno filter named %rt contextfilters2Attempted to invoke context filter without contextitevalcontextfiltertenvironmentfilterN(( RZR1R4RtlistRtFalsetinsertteval_ctxR(R`R~RiR7tkwargstcontextRtfunc((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyt call_filters"     cCsM|jj|}|dkr1td|n|||p@d|pIiS(sdInvokes a test on a value the same way the compiler does it. .. versionadded:: 2.7 sno test named %rN((R[R1R4R(R`R~RiR7RR((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyt call_tests cCsNy|j|||SWntk r6tj}nX|j|d|dS(sParse the sourcecode and return the abstract syntax tree. This tree of nodes is used by the compiler to convert the template into executable source- or bytecode. This is useful for debugging or to extract information from templates. If you are :ref:`developing Jinja2 extensions ` this gives you a good overview of the node tree generated. t source_hintN(t_parseRtsystexc_infothandle_exception(R`tsourceR~tfilenameR((s6/usr/lib/python2.7/site-packages/jinja2/environment.pytparses  cCst|||t|jS(s8Internal parsing function used by `parse` and `compile`.(RR,R(R`RR~R((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyRscCs]t|}y|jj|||SWntk rEtj}nX|j|d|dS(sLex the given sourcecode and return a generator that yields tokens as tuples in the form ``(lineno, token_type, value)``. This can be useful for :ref:`extension development ` and debugging templates. This does not perform preprocessing. If you want the preprocessing of the extensions to be applied you have to filter source through the :meth:`preprocess` method. RN(R'tlexert tokeniterRRRR(R`RR~RR((s6/usr/lib/python2.7/site-packages/jinja2/environment.pytlexs  cs(tfd|jt|S(sPreprocesses the source with all extensions. This is automatically called for all parsing and compiling methods but *not* for :meth:`lex` because there you usually only want the actual source tokenized. cs|j|S(N(t preprocess(tste(RR~(s6/usr/lib/python2.7/site-packages/jinja2/environment.pyRxs(R/R|R'(R`RR~R((RR~s6/usr/lib/python2.7/site-packages/jinja2/environment.pyRscCs~|j|||}|jj||||}xG|jD]9}|j|}t|ts=t|||}q=q=W|S(sCalled by the parser to do the preprocessing and filtering for all the extensions. Returns a :class:`~jinja2.lexer.TokenStream`. (RRttokenizeR|t filter_streamRAR(R`RR~Rtstatetstreamtext((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyt _tokenizescCst||||d|S(s|Internal hook that can be overridden to hook a different generate method in. .. versionadded:: 2.5 t defer_init(R(R`RR~RR((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyt _generatescCst||dS(s{Internal hook that can be overridden to hook a different compile method in. .. versionadded:: 2.5 texec(tcompile(R`RR((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyt_compilescCsd}yt|tr6|}|j|||}n|jrQt||}n|j|||d|}|rv|S|dkrd}n t|}|j||SWnt k rt j }nX|j |d|dS(sCompile a node or template source code. The `name` parameter is the load name of the template after it was joined using :meth:`join_path` if necessary, not the filename on the file system. the `filename` parameter is the estimated filename of the template on the file system. If the template came from a database or memory this can be omitted. The return value of this method is a python code object. If the `raw` parameter is `True` the return value will be a string with python code equivalent to the bytecode returned otherwise. This method is mainly used internally. `defer_init` is use internally to aid the module code generator. This causes the generated code to be able to import without the global environment variable to be set. .. versionadded:: 2.4 `defer_init` parameter added. Rs