o 3a2@sddlZddlmZddlmZmZddlmZddlm Z ddl m Z m Z m Z ddlmZGd d d eZGd d d ZGd dde ZGdddeZGdddeZddZddZdS)N) import_module)getfullargspecunwrap)conditional_escape) is_iterable)NodeTemplate token_kwargs)TemplateSyntaxErrorc@s eZdZdS)InvalidTemplateLibraryN)__name__ __module__ __qualname__rr9/usr/lib/python3/dist-packages/django/template/library.pyr sr c@sPeZdZdZddZdddZddZdd d Zd d Zdd dZ dddZ dS)Librarya  A class for registering template tags and filters. Compiled filter and template tag functions are stored in the filters and tags attributes. The filter, simple_tag, and inclusion_tag methods provide a convenient way to register callables as tags. cCsi|_i|_dSN)filterstags)selfrrr__init__s zLibrary.__init__Ncsxdur |dur jSdur%|dur%trSfdd}|Sdur4|dur4|j<|Std|f)Ncs |Sr)tagfuncnamerrrdec%s zLibrary.tag..decz.Unsupported arguments to Library.tag: (%r, %r)) tag_functioncallabler ValueError)rrcompile_functionrrrrrs  z Library.tagcCs||jt|d|j<|SN_decorated_function)rgetattrr )rrrrrr2szLibrary.tag_functionc sdur|durfdd}|Sdur0|dur0tr&jfiSfdd}|Sdura|dura|j<dD]}|vr[|}t|||t|dr[t|j||q?|_|Std|f)z Register a callable as a template filter. Example: @register.filter def lower(value): return value.lower() Ncsj|fiSr)filter_functionr)flagsrrrr@szLibrary.filter..deccsj|fiSr)filterrr&rrrrrIs)expects_localtimeis_safeneeds_autoescaper#z1Unsupported arguments to Library.filter: (%r, %r))rr%rsetattrhasattrr# _filter_namer )rr filter_funcr&rattrvaluerr(rr'6s0   zLibrary.filtercKs"t|d|j}|j||fi|Sr")r$r r')rrr&rrrrr%`szLibrary.filter_functioncs4fdd}|dur|St|r||Std)z Register a callable as a compiled template tag. Example: @register.simple_tag def hello(*args, **kwargs): return 'world' c sbtt\}ptdjt f dd} |S)Nr#c sn|dd}d}t|dkr |ddkr |d}|dd}t|| \}}t|||S)Nras)split_contentslen parse_bits SimpleNode)parsertokenbits target_varargskwargs) defaultsr function_namekwonlykwonly_defaultsparams takes_contextvarargsvarkwrr compile_funcps  z5Library.simple_tag..dec..compile_funcrrr$r functoolswrapsrr_rHrrrEr@rrArBrCrDrFrGrrls  zLibrary.simple_tag..decNz(Invalid arguments provided to simple_tag)rr )rrrErrrrNr simple_tagds zLibrary.simple_tagcsfdd}|S)z Register a callable as an inclusion tag: @register.inclusion_tag('results.html') def show_results(poll): choices = poll.choice_set.all() return {'choices': choices} c sdtt\} ptdjt f dd} |S)Nr#c s>|dd}t||  \}}t||S)Nr)r6r8 InclusionNode)r:r;r<r>r?) r@filenamerrArBrCrDrErFrGrrrHs  z8Library.inclusion_tag..dec..compile_funcrIrLrRrrrErOrrs   z"Library.inclusion_tag..decr)rrRrrErrrrSr inclusion_tags zLibrary.inclusion_tag)NN)NNN) r rr__doc__rrrr'r%rPrTrrrrrs  * $rc@s eZdZdZddZddZdS) TagHelperNodez Base class for tag helper nodes such as SimpleNode and InclusionNode. Manages the positional and keyword arguments to be passed to the decorated function. cCs||_||_||_||_dSr)rrEr>r?)rrrEr>r?rrrrs zTagHelperNode.__init__csDfdd|jD}|jrg|}fdd|jD}||fS)Ncsg|]}|qSrresolve).0varcontextrr sz8TagHelperNode.get_resolved_arguments..csi|] \}}||qSrrW)rYkvr[rr sz8TagHelperNode.get_resolved_arguments..)r>rEr?items)rr\ resolved_argsresolved_kwargsrr[rget_resolved_argumentss  z$TagHelperNode.get_resolved_argumentsN)r rrrUrrdrrrrrVs rVc$eZdZfddZddZZS)r9ct||||||_dSr)superrr=)rrrEr>r?r= __class__rrr zSimpleNode.__init__cCsH||\}}|j|i|}|jdur|||j<dS|jr"t|}|S)N)rdrr= autoescaper)rr\rbrcoutputrrrrenders  zSimpleNode.renderr rrrrn __classcell__rrrhrr9 r9cre)rQcrfr)rgrrR)rrrEr>r?rRrhrrrrjzInclusionNode.__init__cCs||\}}|j|i|}|j|}|durSt|jtr#|j}n+tt|jddtr2|jj}nt|jt sFt |jrF|jj |j}n|jj |j}||j|<||}|d}|dure||d<||S)z Render the specified template and context. Cache the template object in render_context to avoid reparsing and loading when used in a for loop. Ntemplate csrf_token)rdrrender_contextget isinstancerRr r$rrstrrengineselect_template get_templatenewrn)rr\rbrc_dictt new_contextrsrrrrns"       zInclusionNode.renderrorrrhrrQrqrQc  s|r|ddkr|dd}ntd| g} i} t|} fdd|D} |D]t}t|g|}|rr|\}}||vrL||vrL|durLtd| |f|| vrXtd | |f|| t|<|| vrh| |q(|| vrq| |q(| rztd | | ||z| dWq(t y|durtd | Yq(w|dur| dt | } | s| rtd | d dd| | Df| | fS)z Parse bits for template tag helpers simple_tag and inclusion_tag, in particular by detecting syntax errors and by extracting positional and keyword arguments. rr\rNzW'%s' is decorated with takes_context=True so it must have a first argument of 'context'csg|] }r |vr|qSrr)rYkwargrCrrr]szparse_bits..z.'%s' received unexpected keyword argument '%s'z7'%s' received multiple values for keyword argument '%s'zH'%s' received some positional argument(s) after some keyword argument(s)z+'%s' received too many positional argumentsz5'%s' did not receive value(s) for the argument(s): %sz, css|]}d|VqdS)z'%s'Nr)rYprrr 4szparse_bits..) r listr popitemrwremoveappendcompile_filterpop IndexErrorr7join)r:r<rDrFrGr@rBrCrErr>r?unhandled_paramsunhandled_kwargsbitrparamr1rrrr8s~        r8c CsZzt|}Wnty}ztd||fd}~wwz|jWSty,td|w)z; Load a Library object from a template tag module. zSInvalid template library specified. ImportError raised when trying to load '%s': %sNz4Module %s does not have a variable named 'register')r ImportErrorr registerAttributeError)rmoduleerrrimport_library8s"  r)rJ importlibrinspectrrdjango.utils.htmlrdjango.utils.itercompatrbaserr r exceptionsr Exceptionr rrVr9rQr8rrrrrs    $ K