PL iljdZddlZddlZddlZddlmZddlmZddlmZGddZGdd e Z Gd d Z e Z d Z Gd dZGddZGddZGddZGddeZGddeZdZdZdZdZdZdZdZd'd Zd(d!Zd"Zd#Zd$Zd)d%Z d&Z!y)*zdprovides runtime services for templates, including Context, Namespace, and various helper functions.N)compat) exceptions)utilceZdZdZdZdZedZedZdZ dZ dZ d Z d Z d Zd Zd ZddZdZdZdZdZdZy)ContextzProvides runtime namespace, output buffer, and various callstacks for templates. See :ref:`runtime_toplevel` for detail on the usage of :class:`.Context`. c |g|_||_|j|_d|_d|_i|_tjt||jd<tx|_ |jd<y)Ncapturecaller) _buffer_stack_datacopy_kwargs_with_template_outputting_as_unicode namespaces functoolspartialr CallerStack caller_stack)selfbufferdatas R/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/mako/runtime.py__init__zContext.__init__sn$X yy{ "&*#!* 1 1'4 @ 94?=@DJJx0c||_|jj|j}|r't j ddj |zy)Nz%Reserved words passed to render(): %sz, )rreserved_names intersectionr rNameConflictErrorjoin)rt illegal_namess r_set_with_templatezContext._set_with_template.sS((55djjA ..7))M*+  rc.|jjS)z]Return the :class:`.TemplateLookup` associated with this :class:`.Context`. )rlookuprs rr%zContext.lookup7s "")))rc6|jjS)a]Return the dictionary of top level keyword arguments associated with this :class:`.Context`. This dictionary only includes the top-level arguments passed to :meth:`.Template.render`. It does not include names produced within the template execution such as local variable names or special names such as ``self``, ``next``, etc. The purpose of this dictionary is primarily for the case that a :class:`.Template` accepts arguments via its ``<%page>`` tag, which are normally expected to be passed via :meth:`.Template.render`, except the template is being called in an inheritance context, using the ``body()`` method. :attr:`.Context.kwargs` can then be used to propagate these arguments to the inheriting template:: ${next.body(**context.kwargs)} )rr r&s rkwargszContext.kwargs?s(||  ""rc:|jj|y)zQPush a ``caller`` callable onto the callstack for this :class:`.Context`.N)rappend)rr s r push_callerzContext.push_callerUs   (rc|jd=y)zPPop a ``caller`` callable onto the callstack for this :class:`.Context`.N)rr&s r pop_callerzContext.pop_caller[s   b !rcHt|jjS)zAReturn a list of all names established in this :class:`.Context`.)listr keysr&s rr1z Context.keysasDJJOO%&&rcb||jvr|j|Stj|SN)r builtins__dict__rkeys r __getitem__zContext.__getitem__fs- $** ::c? "$$S) )rcxtj}|jj||jS)zUpush a capturing buffer onto this Context and return the new writer function.)rFastEncodingBufferr r*writerbufs r _push_writerzContext._push_writerls0%%' !!#&yyrcl|jj}||jdjfS)zupop the most recent capturing buffer from this Context and return the current writer after the pop. r-)r popr;r<s r_pop_buffer_and_writerzContext._pop_buffer_and_writerts4   $$&D&&r*0000rc$|jy)z*push a capturing buffer onto this Context.N)r>r&s r _push_bufferzContext._push_buffer}s rc6|jjS)z7pop the most recent capturing buffer from this Context.)r r@r&s r _pop_bufferzContext._pop_buffers!!%%''rNcv|jj|tjj||S)z+Return a value from this :class:`.Context`.)r getr4r5)rr7defaults rrGz Context.gets,zz~~c8#4#4#8#8g#FGGrc@|jdj|y)zSWrite a string to this :class:`.Context` object's underlying output buffer.r-Nr r;)rstrings rr;z Context.writes 2$$V,rc4|jdjS)z#Return the current writer function.r-rJr&s rwriterzContext.writers!!"%+++rcBtjt}|j|_|jj |_|j |_|j |_|j|_|j|_|j|_ |Sr3) r__new__r r r rrrrr)rcs r_copyz Context._copyss OOG $,,**//#LL ..#'#>#>  **rcd|s|S|j}|jj||S)zCreate a new :class:`.Context` with a copy of this :class:`.Context`'s current state, updated with the given dictionary. The :attr:`.Context.kwargs` collection remains unaffected. )rQr update)rdrPs r_localszContext._localss,K JJL qrc|j}|j}|jdd|jdd|jdd|S)zfcreate a new copy of this :class:`.Context`. with tokens related to inheritance state removed.rNparentnext)rQr r@)rrPxs r_clean_inheritance_tokensz!Context._clean_inheritance_tokenssG JJL GG fd h fdrr3)__name__ __module__ __qualname____doc__rr#propertyr%r(r+r.r1r8r>rArCrErGr;rMrQrUrZrrrrsA"**##*) " ' * 1 ( H - , " rrc6eZdZdZdZdZdZdZdZdZ y) rcd|_yr3) nextcallerr&s rrzCallerStack.__init__s rc"|jSr3__bool__r&s r __nonzero__zCallerStack.__nonzero__}}rcLt|xr|jxrdxsdS)NTF)len _get_callerr&s rrfzCallerStack.__bool__s$4y8T--/8DAEArc |dSNr-r`r&s rrkzCallerStack._get_callers Bxrc6t|j|Sr3)getattrrkr6s r __getattr__zCallerStack.__getattr__st'')3//rcV|jxsd}|j|d|_|Sr3)rcr*)rframes r _push_framezCallerStack._push_frames('4 E rc.|j|_yr3)r@rcr&s r _pop_framezCallerStack._pop_frames((*rN) r[r\r]rrgrfrkrprsrur`rrrrs&B 0 %rrc"eZdZdZdZdZdZy) UndefinedzRepresents an undefined value in a template. All template modules have a constant value ``UNDEFINED`` present which is an instance of this object. ctd)Nrw) NameErrorr&s r__str__zUndefined.__str__s  $$rc"|jSr3rer&s rrgzUndefined.__nonzero__rhrcy)NFr`r&s rrfzUndefined.__bool__srN)r[r\r]r^rzrgrfr`rrrwrws%rrwcJeZdZdZdZdZdZedZdZ dZ dZ d Z y ) LoopStackza stack for LoopContexts that implements the context manager protocol to automatically pop off the top of the stack on context exit cg|_yr3stackr&s rrzLoopStack.__init__s  rc<|j||jSr3)_push_topriterables r_enterzLoopStack._enters 8yyrc:|j|jSr3)_poprr&s r_exitzLoopStack._exits yyrc<|jr|jdS|Srmrr&s rrzLoopStack._tops ::::b> !Krc6|jjSr3)rr@r&s rrzLoopStack._pop szz~~rct|}|jr|jd|_|jj|Srm) LoopContextrrWr*)rrnews rrzLoopStack._pushs7(# ::BCJzz  %%rc,tjd)NzNo loop context is established)rRuntimeExceptionr6s rrpzLoopStack.__getattr__s))*JKKrc,t|jSr3)iterrr&s r__iter__zLoopStack.__iter__sDIIrN) r[r\r]r^rrrr_rrrrprr`rrrrs?  & LrrceZdZdZdZdZejdZe dZ e dZ e dZ e dZ e d Zd Zy ) raA magic loop variable. Automatically accessible in any ``% for`` block. See the section :ref:`loop_context` for usage notes. :attr:`parent` -> :class:`.LoopContext` or ``None`` The parent loop, if one exists. :attr:`index` -> `int` The 0-based iteration count. :attr:`reverse_index` -> `int` The number of iterations remaining. :attr:`first` -> `bool` ``True`` on the first iteration, ``False`` otherwise. :attr:`last` -> `bool` ``True`` on the last iteration, ``False`` otherwise. :attr:`even` -> `bool` ``True`` when ``index`` is even. :attr:`odd` -> `bool` ``True`` when ``index`` is odd. c.||_d|_d|_yNr) _iterableindexrWrs rrzLoopContext.__init__3s!  rc#`K|jD]}||xjdz c_ywN)rr)ris rrzLoopContext.__iter__8s+ AG JJ!OJ s,.c,t|jSr3)rjrr&s r__len__zLoopContext.__len__=s4>>""rc8t||jz dz Sr)rjrr&s r reverse_indexzLoopContext.reverse_indexAs4y4::%))rc |jdk(Sr)rr&s rfirstzLoopContext.firstEszzQrc8|jt|dz k(Sr)rrjr&s rlastzLoopContext.lastIszzSY]**rc|j Sr3)oddr&s revenzLoopContext.evenMs88|rc2t|jdzS)N)boolrr&s rrzLoopContext.oddQsDJJN##rcR|s td||jt|zS)z,Cycle through values as the loop progresses.z(You must provide values to cycle through) ValueErrorrrj)rvaluess rcyclezLoopContext.cycleUs)GH Hdjj3v;.//rN)r[r\r]r^rrrmemoized_instancemethodrr_rrrrrrr`rrrrs,   !!#"#**++$$0rrceZdZdZdZy)_NSAttrc||_yr3)_NSAttr__parent)rrWs rrz_NSAttr.__init__]s  rc|j}|r;t|j|rt|j|S|j}|r;t |r3)rhasattrmoduleroinheritsAttributeError)rr7nss rrpz_NSAttr.__getattr__`sH ]]ryy#&ryy#..[[  S!!rN)r[r\r]rrpr`rrrr\s "rrceZdZdZ ddZdZdZ dZ dZ dZ dZ dZ e jdZdZdZdZed Zd Zd Zd Zd Zy) NamespaceaProvides access to collections of rendering methods, which can be local, from other templates, or from imported modules. To access a particular rendering method referenced by a :class:`.Namespace`, use plain attribute access: .. sourcecode:: mako ${some_namespace.foo(x, y, z)} :class:`.Namespace` also contains several built-in attributes described here. Nc~||_||_||_|!|Dcic]}|j|c}|_yycc}wr3)namecontextrr[ callables)rrrrr populate_self calling_urirPs rrzNamespace.__init__{sA     5>?ajj!m?DN !?s:r`ct|S)a-Access module level attributes by name. This accessor allows templates to supply "scalar" attributes which are particularly handy in inheritance relationships. .. seealso:: :ref:`inheritance_attr` :ref:`namespace_attr_for_includes` )rr&s rattrzNamespace.attrst}rc||f}||jjvr|jj|St||jj||j}||jj|<|S)a]Return a :class:`.Namespace` corresponding to the given ``uri``. If the given ``uri`` is a relative URI (i.e. it does not contain a leading slash ``/``), the ``uri`` is adjusted to be relative to the ``uri`` of the namespace itself. This method is therefore mostly useful off of the built-in ``local`` namespace, described in :ref:`namespace_local`. In most cases, a template wouldn't need this function, and should instead use the ``<%namespace>`` tag to load namespaces. However, since all ``<%namespace>`` tags are evaluated before the body of a template ever runs, this method can be used to locate namespaces using expressions that were generated within the body code of the template, or to conditionally use a particular namespace. ) templateurir)rrTemplateNamespacerQ _templateuri)rurir7rs r get_namespacezNamespace.get_namespacesx(Sk $,,)) )<<**3/ /   LL   ))   (* $ rcDt|j||jS)zReturn a :class:`.Template` from the given ``uri``. The ``uri`` resolution is relative to the ``uri`` of this :class:`.Namespace` object's :class:`.Template`. )_lookup_templaterr)rrs r get_templatezNamespace.get_templates  c43D3DEErc <|jj|fi|S)aReturn a value from the :class:`.Cache` referenced by this :class:`.Namespace` object's :class:`.Template`. The advantage to this method versus direct access to the :class:`.Cache` is that the configuration parameters declared in ``<%page>`` take effect here, thereby calling up the same configured backend as that configured by ``<%page>``. )cacherG)rr7r(s r get_cachedzNamespace.get_cachedstzz~~c,V,,rc.|jjS)zReturn the :class:`.Cache` object referenced by this :class:`.Namespace` object's :class:`.Template`. )templaterr&s rrzNamespace.caches}}"""rc Ht|j||jfi|y)z$Include a file at the given ``uri``.N) _include_filerr)rrr(s r include_filezNamespace.include_file s dllC):):EfErcv|D]4}|dk(r|jD] \}}|||< &t||||<6y)N*) _get_starro)rrTlidentkvs r _populatezNamespace._populatesM 0E| NN,DAqAaD#4/%  0rc#nK|jr%|jD]}||j|fyywr3)rr6s rrzNamespace._get_stars9 >>~~ 1DNN3/00 1 s35c||jvr|j|}n?|jrt|j|}ntd|jd|dt ||||SNz Namespace 'z' has no member '')rrrorrsetattr)rr7vals rrpzNamespace.__getattr__s_ $.. ..%C ]]$---C 7;yy#F  c3 rNNTN)r[r\r]r^rrrrrfilenamerrrmemoized_propertyrrrrr_rrrrrpr`rrrrjs & @I FH GH CL  @F -##F 01 rrc`eZdZdZ d dZedZedZedZdZ dZ y) rz@A :class:`.Namespace` specific to a :class:`.Template` instance.Nc ||_||_||_| |D cic]} | j| c} |_|8t ||||_|j jj|_ n0|#||_|jj|_ n td|rt||j |\} } yycc} w)Nz 'template' argument is required.)self_ns) rrrr[rrrr _template_urir TypeError_populate_self_namespace) rrrrrrrrrrP lclcallable lclcontexts rrzTemplateNamespace.__init__0s     5>?ajj!m?DN  ",Wk;ODM $ 4 4 B BD   !$DM ( = =D >? ? &>' #K @sCc.|jjS)zThe Python module referenced by this :class:`.Namespace`. If the namespace references a :class:`.Template`, then this module is the equivalent of ``template.module``, i.e. the generated module for the template. )rrr&s rrzTemplateNamespace.moduleOs}}###rc.|jjSzhThe path of the filesystem file used for this :class:`.Namespace`'s module or template. )rrr&s rrzTemplateNamespace.filenameZs }}%%%rc.|jjS)zThe URI for this :class:`.Namespace`'s template. I.e. whatever was sent to :meth:`.TemplateLookup.get_template()`. This is the equivalent of :attr:`.Template.uri`. )rrr&s rrzTemplateNamespace.urias}}   rc#Kjr$jD]}|j|ffd}jjjD]}|||fyw)Nczjj|}tj|jSr3)r_get_def_callablerrr)r7 callable_rs rrGz(TemplateNamespace._get_star..getqs. 77>~~ 1DNN3/00 1 >%%.. Ac!f+  sA(A+c||jvr|j|}n|jj|r<|jj|}t j ||j }n?|jrt|j|}ntd|jd|dt||||Sr) rrhas_defrrrrrrorrrrr7rrs rrpzTemplateNamespace.__getattr__xs $.. ..%C ]] " "3 ' 77$$&& !! rrc<eZdZdZ ddZedZdZdZy)ModuleNamespacez;A :class:`.Namespace` specific to a Python module instance.Nc||_||_||_| |Dcic]}|j|c}|_t |} |j dddD]} t| | } | |_ycc}w)N.r) rrrr[r __import__splitror) rrrrrrrrrPmodtokens rrzModuleNamespace.__init__s{     5>?ajj!m?DN \\#&qr* &E#u%C & @sA0c.|jjSr)r__file__r&s rrzModuleNamespace.filenames {{###rc#>K|jr$|jD]}||j|ft|jD]Q}|ddk7s t|j|}t |s.|t j ||jfSyw)Nr_)rdirrrocallablerrr)rr7rs rrzModuleNamespace._get_stars >>~~ 1DNN3/00 1t{{# JC1v}#DKK5 I&y00DLLIII  JsAB!B6'Bct||jvr|j|}nt|j|r7t|j|}t j ||j }n?|jrt|j|}ntd|jd|dt||||Sr) rrrrorrrrrrrrs rrpzModuleNamespace.__getattr__s $.. ..%C T[[# & S1I##It||.wrap_stackframesQ((* .1$1&1  + + -G + + -s AAr`)rrs` rsupports_callerrs. rct|stjd|j ||i||j }|j S#|j }wxYw)zExecute the given template def, capturing the output into a buffer. See the example in :ref:`namespaces_python_modules`. z[capture() function expects a callable as its argument (i.e. capture(func, *args, **kwargs)))r rrrCrEgetvalue)rrr r(r=s rr r sq I )) A   $4"6"!!# <<>!!#s AA,cfd}|S)Ncfd}|S)Nc~fd} jdd|_|g|i|S#t$rYwxYw)Ncg|i|Sr3r`)r kwr render_fns ryzB_decorate_toplevel..decorate_render..go..ys 646266r)r[r)rr rrfnrs` rgoz7_decorate_toplevel..decorate_render..gosR 7 &//3 2a5.4.2. .  s 0 <<r`)rrrs` rdecorate_renderz+_decorate_toplevel..decorate_renders / rr`)rrs` r_decorate_toplevelrs  rcfd}|S)Nc&|fd}|S)Ncg|i|Sr3r`)r rrdecs rrz5_decorate_inline..decorate_render..gosw,,, ,rr`)rrr"rrs @rrz)_decorate_inline..decorate_rendersm - rr`)rrrs`` r_decorate_inliner#s rc 2t|||}t|j|\}}t||jfi|}|j r ||fi|y||fi|y#t $r*|j |tj}|sYywxYw)zPlocate the template from the given uri and include it in the current output.N) rrrZ_kwargs_for_includer include_error_handler Exceptionr exception_as)rrrr(rrctxresults rrrs k:H/))+XY!GMM DV DF%%  c $V $ #   33C9L9L9NOF s A##0BBc|yt|||}|d}|}|j|j}|j|jd|i}td|jz||d|_|jx|j d<|j d<t |jd d}| |||}|r|St |jd d} | | ||j|fS) zcalled by the _inherit method in template modules to set up the inheritance chain at the start of a template's execution.NrrXself:%sFrrrWlocal _mako_inherit_mako_generate_namespaces) rrrUrrr rorr) rrrrrihrrretgen_nss r _inherit_fromr4s  {k:HfoG B ++ ! [[ ++ !&".J#HLL  BK ;=++EGMM(j..w7$?I*- J X__&A4 HF w    ++rcj|jj}|,tjd|jjz|j ||} |j |S#tj$r6}tjttj|d}~wwxYw)Nz.Template '%s' has no TemplateLookup associated) rr%rTemplateLookupExceptionr adjust_urirTopLevelLookupExceptionstrrr()rr relativetor%es rrr<s  # # * *F ~00 <$$(( )     C ,C""3''  - -00 ##% &  sA))B2<1B--B2c|td|jz||d}|x|jd<|jd<t|jdr |jj ||}|r|S|j |fS)Nr,Fr-rr.r/)rrr rrr/r)rrrr2s rrrLs#  $    6= J    ((rc@|rtj}n+tj|j|j}t |fi|}||_|j |t|||g|it|||jjS)z^create a Context and return the string output of the given template and template callable.)encodingerrors) rr:output_encodingencoding_errorsrrr#_render_context_kwargs_for_callablerEr)rrr r as_unicoder=rs r_renderrE\s%%'%%--h6N6N c"T"G%/G" x(   y$ /     ) ) ++rctj|}|dr|S|d|ddDcgc]}|| c}z}i}|D]}|dk7s ||vs||vs||||<|Scc}w)Nrrrrrinspect_getargspec)rrargspecr namedargsr(args rrCrCts'' 2Gqz  1GAaGGI F$ ) t 60As)F3K$ M Hs AAc tj|}|d|ddDcgc]}|| c}z}|D]}|dk7s ||vs||vs||||<|Scc}w)NrrrGrrH)rrr(rJrrKrLs rr%r%ss'' 2G 1GAaGGI$ ) t 60As)F3K$ M Hs AAcddlm}t||jst ||\}}t ||||yt ||j \}}t ||||y)Nr)r r() mako.templater isinstance DefTemplater_exec_templaterW)tmplrrr r(rinheritrs rrBrBs[$ dH00 1 8$ G*w fE!9$++ N*y'VDrc,|j}|&|js |jr ||g|i|y||g|i|y#t$r"t ||t j Yytjd}t |||YyxYw)zexecute a rendering callable given the callable, a Context, and optional explicit arguments the contextual Template will be located if it exists, and the error handling options specified on that Template will be interpreted here. Nr) rformat_exceptions error_handlerr' _render_errorrr(sysexc_info)rrr r(rr;s rrRrRs%%H""h&<&< 0 g / / / '+D+F+  D (GV-@-@-B C 0 q!A (GQ /s A(B,%Bc|jrD|j||}|s/tj\}}}|r|r|j||yt j }|j r#tjg|jddn8tj|j|jg|jdd|j||j||y)N)error)rWrYrZwith_tracebackrhtml_error_templaterrr:r r@rAr#render_context)rrr\r*tpvaluetberror_templates rrXrXs''7LLNMBr**2.. $779  ) )(,(?(?(A'BG ! !! $''"22"22(G ! !! $ "">2%%gU%;rr3)F)NN)"r^r4rrYmakorrrrr0rrw UNDEFINEDSTOP_RENDERINGrrrrrrrr rr#rr4rrrErCr%rBrRrXr`rrrgs, iiX%$%8( K %%P>0>0B " "DZ Zz7i7t$*$ !(,@  ) ,0  E,.