,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|jknsOtd|jdksjtd|S(s*Perform a sanity check on the environment.sGundefined must be a subclass of undefined because filters depend on it.s;block, variable and comment start strings must be differents s s s3newline_sequence set to unknown line ending string.(s s s (t issubclasst undefinedRtAssertionErrortblock_start_stringtvariable_start_stringtcomment_start_stringtnewline_sequence(RC((s6/usr/lib/python2.7/site-packages/jinja2/environment.pyt_environment_sanity_checkZs R3cBsQeZdZeZeZd!ZeZd!Z d!Z e e e eeeeeeeeed"eed!ed!ded!dZdZdZeeeeeeeeeeeeeeeeeeedZeeddZ dZ!d Z"d Z#d!d!d!d!d Z$d!d!d Z%e&d!d!d Z'dZ(d!d!dZ)d!d!dZ*d!d!dZ+edZ,dZ-e&d!d!eedZ.edZ/d!d!dd!eedZ0d!d!dZ1d!ed!dZ2dZ3e&dZ4e&d!d!dZ5e&d!d!dZ6e&d!d!dZ7d!d!dZ8d Z9RS(#sThe core component of Jinja is the `Environment`. It contains important shared variables like configuration, filters, tests, globals and others. Instances of this class may be modified if they are not shared and if no template was loaded so far. Modifications on environments after the first template was loaded will lead to surprising effects and undefined behavior. Here the possible initialization parameters: `block_start_string` The string marking the begin of a block. Defaults to ``'{%'``. `block_end_string` The string marking the end of a block. Defaults to ``'%}'``. `variable_start_string` The string marking the begin of a print statement. Defaults to ``'{{'``. `variable_end_string` The string marking the end of a print statement. Defaults to ``'}}'``. `comment_start_string` The string marking the begin of a comment. Defaults to ``'{#'``. `comment_end_string` The string marking the end of a comment. Defaults to ``'#}'``. `line_statement_prefix` If given and a string, this will be used as prefix for line based statements. See also :ref:`line-statements`. `line_comment_prefix` If given and a string, this will be used as prefix for line based based comments. See also :ref:`line-statements`. .. versionadded:: 2.2 `trim_blocks` If this is set to ``True`` the first newline after a block is removed (block, not variable tag!). Defaults to `False`. `lstrip_blocks` If this is set to ``True`` leading spaces and tabs are stripped from the start of a line to a block. Defaults to `False`. `newline_sequence` The sequence that starts a newline. Must be one of ``'\r'``, ``'\n'`` or ``'\r\n'``. The default is ``'\n'`` which is a useful default for Linux and OS X systems as well as web applications. `keep_trailing_newline` Preserve the trailing newline when rendering templates. The default is ``False``, which causes a single newline, if present, to be stripped from the end of the template. .. versionadded:: 2.7 `extensions` List of Jinja extensions to use. This can either be import paths as strings or extension classes. For more information have a look at :ref:`the extensions documentation `. `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(RKtblock_end_stringRLtvariable_end_stringRMtcomment_end_stringtline_statement_prefixtline_comment_prefixt trim_blockst lstrip_blocksRNtkeep_trailing_newlineRIt optimizedtfinalizet autoescapeR tcopytfiltersR ttestsR tglobalstloaderR;R?tbytecode_cachet auto_reloadRGRDRO(tselfRKRPRLRQRMRRRSRTRURVRNRWRDRXRIRYRZR_t cache_sizeRaR`((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(RbRF((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(Rbt 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(Rb((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 ExceptiontgetattrtAttributeErrorRI(RbRtargumenttattr((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. RRN(RRR2RRI(RbRt 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(( R\R1R4RtlistRtFalsetinsertteval_ctxR(RbRRkR7tkwargstcontextRtfunc((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(RbRRkR7RR((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(RbtsourceRtfilenameR((s6/usr/lib/python2.7/site-packages/jinja2/environment.pytparses  cCst|||t|jS(s8Internal parsing function used by `parse` and `compile`.(RR,R(RbRRR((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(RbRRRR((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.pyRzs(R/R~R'(RbRRR((RRs6/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(RbRRRtstatetstreamtext((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(RbRRRR((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(RbRR((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