o
    .df                     @  s  d Z ddlmZ ddlZddlZddlZddlZddlZddl	Z	ddl
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl'm)Z) ddl'm*Z* ddl'm+Z+ ddl'm,Z, ddl'm-Z- dd l.m/Z/ dd!l0m1Z1 dd"l0m2Z2 dd#l0m3Z3 dd$l0m4Z4 dd%l0m5Z5 dd&l0m6Z6 erdd'l#m7Z7 dd(l8m9Z9 dd)l:m;Z; d*Z<e"e=e e"e=e>f  f Z?ee=e@f ZAG d+d, d,ee=e@f ZBeB ZCefdOd3d4ZDefdOd5d6ZEefdOd7d8ZFdPd:d;ZGdPd<d=ZHdPd>d?ZIG d@dA dAZJG dBdC dCeJZKG dDdE dEeJZLG dFdG dGeKeLZMe1dHdQdMdNZNdS )RzLiquid template render context.    )annotationsN)contextmanager)partial)reduce)StringIO)cycle)getitem)mul)TYPE_CHECKING)Any)	Awaitable)Callable)Dict)Iterator)List)Mapping)MutableMapping)Optional)Sequence)TextIO)Union)Mode)ReadOnlyChainMap)ContextDepthError)Error)LocalNamespaceLimitError)LoopIterationLimitError)NoSuchFilterFunc)lookup_warning)LimitedStringIO)	UNDEFINED)DebugUndefined)StrictDefaultUndefined)StrictUndefined)	Undefined)is_undefined)Environment)ForLoop)BoundTemplate)	Contextr!   get_itemr%   r   r"   r#   VariableCaptureContextr$   c                   @  s8   e Zd ZdZdddZdd
dZdddZdddZdS )BuiltInz<Mapping-like object for resolving built-in, dynamic objects.itemobjectreturnboolc                 C  s   |dv rdS dS )N)nowtodayTF )selfr-   r3   r3   F/var/www/Matress/matenv/lib/python3.10/site-packages/liquid/context.py__contains__J   s   zBuiltIn.__contains__keystrc                 C  s0   |dkr	t j  S |dkrt j S tt|Nr1   r2   )datetimer1   dater2   KeyErrorr8   )r4   r7   r3   r3   r5   __getitem__O   s
   

zBuiltIn.__getitem__intc                 C  s   dS )N   r3   r4   r3   r3   r5   __len__V   s   zBuiltIn.__len__Iterator[str]c                 C  s   t ddgS r9   )iterr@   r3   r3   r5   __iter__Y   s   zBuiltIn.__iter__N)r-   r.   r/   r0   )r7   r8   r/   r.   r/   r>   )r/   rB   )__name__
__module____qualname____doc__r6   r=   rA   rD   r3   r3   r3   r5   r,   G   s    


r,   objr   item_getterCallable[[Any, str], object]r/   r.   c              
   C  s>   z|| dW S  t ttfy   t| tjjrt|  Y S  w Nsizer<   
IndexError	TypeError
isinstancecollectionsabcSizedlenrJ   rK   r3   r3   r5   _liquid_size`   s   rX   c              
   C  sx   z|| dW S  t ttfy;   t| tr t| tjjr-| r-tt	
|  dd  Y S t| tjjr:| d  Y S  w Nfirst   r   r<   rP   rQ   rR   r8   rS   rT   r   list	itertoolsisliceitemsr   rW   r3   r3   r5   _liquid_firstk   s   
ra   c              
   C  sJ   z|| dW S  t ttfy$   t| tr t| tjjr#| d  Y S  w Nlastr<   rP   rQ   rR   r8   rS   rT   r   rW   r3   r3   r5   _liquid_lastz   s   
rf   'Callable[[Any, str], Awaitable[object]]c              
     sF   z	|| dI d H W S  t ttfy"   t| tjjr!t|  Y S  w rM   rO   rW   r3   r3   r5   _liquid_size_async   s   rh   c              
     s   z	|| dI d H W S  t ttfy?   t| tr t| tjjr1| r1tt	
|  dd  Y S t| tjjr>| d  Y S  w rY   r\   rW   r3   r3   r5   _liquid_first_async   s   
ri   c              
     sR   z	|| dI d H W S  t ttfy(   t| tr t| tjjr'| d  Y S  w rb   re   rW   r3   r3   r5   _liquid_last_async   s   
rj   c                   @  s\  e Zd ZdZdZ							dldmddZdnddZdoddZefdpd$d%Z	efdpd&d'Z
efdqd)d*Zefdqd+d,Zdrd.d/Zdsd1d2Zdsd3d4Zdtd6d7Zdtd8d9Zdud:d;Zdud<d=ZdvdAdBZdwdDdEZdxdydHdIZe	dxdzdMdNZed{dQdRZd|dTdUZd}d~dWdXZ		Y		Yddd\d]Zdd`daZdxddedfZeddhdiZ eddjdkZ!dS )r)   zA template render context.

    A new render context is created automatically each time `BoundTemplate.render`
    is called, which includes `globals` set on the bound `liquid.Environment` and
    `liquid.template.BoundTemplate`.
    )_copy_depth
autoescapecountersdisabled_tagsenvglobalslocal_namespace_size_carrylocalsloop_iteration_carryloopsparent_contextscopetag_namespacetemplateNr   r[   ro   r&   rp   Optional[Namespace]rn   Optional[List[str]]
copy_depthr>   ru   Optional[Context]rs   rq   rx   Optional[BoundTemplate]c	           	      C  s|   || _ || _i | _|pi | _i | _t| j| jt| j| _i di d| _g | _	|p)g | _
| j j| _|| _|| _|| _|| _d S )N )cycles	ifchanged	stopindex)ro   rx   rr   rp   rm   r   builtinrv   rw   rt   rn   rl   rk   ru   rs   rq   )	r4   ro   rp   rn   r{   ru   rs   rq   rx   r3   r3   r5   __init__   s"   



zContext.__init__r7   r8   valr   r/   Nonec                 C  s2   || j |< | jjr|  | jjkrtddS dS )z(Add `val` to the context with key `key`.zlocal namespace limit reachedN)rr   ro   local_namespace_limitget_size_of_localsr   r4   r7   r   r3   r3   r5   assign  s   
zContext.assignc                 C  s*   | j jsdS tdd | j D | j S )a  Return the "size" or a "score" for the current local namespace.

        This is used by the optional local namespace resource limit. Override
        `get_size_of_locals` to customize how the limit is calculated. Be sure
        to consider `self.local_namespace_size_carry` when writing a custom
        implementation of `get_size_of_locals`.

        The default implementation uses `sys.getsizeof()` on each of the local
        namespace's values. It is not a reliable measure of size in bytes.
        r   c                 s  s    | ]
}t j|d dV  qdS )r[   defaultN)sys	getsizeof).0rJ   r3   r3   r5   	<genexpr>&  s    z-Context.get_size_of_locals.<locals>.<genexpr>)ro   r   sumrr   valuesrq   r@   r3   r3   r5   r     s   zContext.get_size_of_localspathContextPathr   r.   c                 C  s  t |tr| ||S |d |dd }}t |tsJ | ||}|rzt| j||W S  tttfy } zCt |trPd| d| ddd |D  }n| d	| dd
d |D  }|t	krs| j
j||dW  Y d}~S |W  Y d}~S d}~ww |S )@Return the value at path `path` if it is in scope, else default.r   r[   Nzkey error: z, r~   c                 S     g | ]}d | dqS []r3   r   ir3   r3   r5   
<listcomp>9      zContext.get.<locals>.<listcomp>: c                 S  r   r   r3   r   r3   r3   r5   r   <  r   namehint)rR   r8   _resolver   r   r<   rP   rQ   joinr    ro   	undefined)r4   r   r   r   r`   rJ   errr   r3   r3   r5   get*  s,   

""zContext.getc           	        s   t |tr| ||S |d |dd }}t |tsJ | ||}|rq| j}z|D ]
}|||I dH }q,W |S  tttfyp } z(|tkre| jj	|| d| d
dd |D  dW  Y d}~S W Y d}~|S d}~ww |S )	r   r   r[   Nr   r~   c                 S  r   r   r3   r   r3   r3   r5   r   Y  r   z%Context.get_async.<locals>.<listcomp>r   )rR   r8   r   getitem_asyncr<   rP   rQ   r    ro   r   r   )	r4   r   r   r   r`   rJ   _gir-   r   r3   r3   r5   	get_asyncG  s0   
	 
zContext.get_asyncr   c                 C  s   |  ||S )zReturn the object/value at `name` in the current scope.

        This is like `get`, but does a single, top-level lookup rather than a
        chained lookup from a sequence of keys.
        )r   r4   r   r   r3   r3   r5   resolve^  s   zContext.resolvec                 C  s<   z| j | W S  ty   |tkr| j| Y S | Y S w N)rv   r<   r    ro   r   r   r3   r3   r5   r   f  s   zContext._resolveCallable[..., object]c              
   C  s   z| j j| }W n ty } z	td| d|d}~ww i }t|ddr*| |d< t|ddr5| j |d< |rXt|d	rPt|fi |}t|jfi ||_|S t|fi |S |S )
z+Return the filter function with given name.zunknown filter ''Nwith_contextFcontextwith_environmentenvironmentfilter_async)ro   filtersr<   r   getattrhasattrr   r   )r4   r   filter_funcr   kwargs_filter_funcr3   r3   r5   filtern  s,   

zContext.filterr(   c                 C  s   | j |S )z%Load a template from the environment.)ro   get_templater4   r   r3   r3   r5   r     s   zContext.get_templatec                   s   | j |I dH S )z4Load a template from the environment asynchronously.N)ro   get_template_asyncr   r3   r3   r5   r     s   zContext.get_template_asyncr   c                 K  s   | j j| |fi |S )z7Load a template referencing the current render context.)ro   get_template_with_contextr4   r   r   r3   r3   r5   r     s   z!Context.get_template_with_contextc                   s   | j j| |fi |I dH S )z0An async version of `get_template_with_context`.N)ro   get_template_with_context_asyncr   r3   r3   r5   r     s   z'Context.get_template_with_context_asyncc                 C  s    | j |d}|d | j |< |S )z1Increment the named counter and return its value.r   r[   rm   r   r4   r   r   r3   r3   r5   	increment  s   zContext.incrementc                 C  s    | j |dd }|| j |< |S )z1Decrement the named counter and return its value.r   r[   r   r   r3   r3   r5   	decrement  s   
zContext.decrement
group_nameargsSequence[object]c                 C  sB   |t |f}| jd }||vrttt|||< |t||  S )z9Return the next item in the cycle of the given arguments.r   )tuplerw   r   rangerV   next)r4   r   r   r7   	namespacer3   r3   r5   r     s
   
zContext.cycler0   c                 C  s    || j d kr|| j d< dS dS )z1Return True if the `ifchanged` value has changed.r   TF)rw   )r4   r   r3   r3   r5   r     s   
zContext.ifchangedindexOptional[int]c                 C  s0   |dur|| j d |< |S | j d |d}|S )z+Set or return the stop index of a for loop.Nr   r   )rw   r   )r4   r7   r   idxr3   r3   r5   r     s
   zContext.stopindexr   	NamespaceIterator[Context]c                 c  sp    | j  | jjkrtd| j}|r|| _| j | z| V  W |r&|| _| j   dS |r2|| _| j   w )z7Extend this context with the given read-only namespace.z9maximum context depth reached, possible recursive includeN)rv   rN   ro   context_depth_limitr   rx   pushpop)r4   r   rx   	_templater3   r3   r5   extend  s"   zContext.extendforloopr'   c              	   c  sl    |  |j | j| | |}z
|V  W | j  n| j  w W d   dS 1 s/w   Y  dS )zCJust like `Context.extend`, but keeps track of ForLoop objects too.N)raise_for_loop_limitlengthrt   appendr   r   )r4   r   r   r   r3   r3   r5   loop  s   "zContext.loopUnion[Undefined, object]c                 C  s,   z| j d W S  ty   | jd Y S w )z3Return the last ForLoop object from the loop stack.rd   
parentloop)rt   rP   ro   r   r@   r3   r3   r5   r     s
   zContext.parentloopr   c                 C  s>   | j jrttdd | jD || j | j jkrtddS dS )zIRaise a `LoopIterationLimitError` if loop stack is bigger than the limit.c                 s      | ]}|j V  qd S r   r   r   r   r3   r3   r5   r         z/Context.raise_for_loop_limit.<locals>.<genexpr>zloop iteration limit reachedN)ro   loop_iteration_limitr   r	   rt   rs   r   )r4   r   r3   r3   r5   r     s   zContext.raise_for_loop_limitFcarry_loop_iterationsblock_scopec              	   C  s   | j | jjkrtd|rttdd | jD | j}nd}|r>| j| jt	|| j
|| j d | ||  d}| jd |jd< n| j| jt	|| j|| j d | ||  d}|pX| j|_|S )zyReturn a copy of this render context.

        Local variables and other state for stateful tags are not copied.
        z8maximum context depth reached, possible recursive renderc                 s  r   r   r   r   r3   r3   r5   r     r   zContext.copy.<locals>.<genexpr>r[   rp   rn   r{   ru   rs   rq   extends)rk   ro   r   r   r   r	   rt   rs   	__class__r   rv   r   rw   rp   rx   )r4   r   rn   r   rx   r   rs   ctxr3   r3   r5   copy  sB   


zContext.copyexcr   c                 C  sB   | j jtjkr	|| j jtjkrtjt|t|j	dd dS dS )z:Ignore, raise or convert the given exception to a warning.r?   )category
stacklevelN)
ro   moder   STRICTWARNwarningswarnr8   r   r   )r4   r   r3   r3   r5   error-  s   
zContext.errorbufOptional[TextIO]r   c                 C  s8   | j jdu r	t S t|tr|jnd}t| j j| dS )zGReturn a new StringIO object that respects the configured stream limit.Nr   )limit)ro   output_stream_limitr   rR   r   rN   )r4   r   carryr3   r3   r5   
get_buffer6  s   zContext.get_bufferrJ   c                 C  sL   t |dr	| }|dkrt|S |dkrt|S |dkr!t|S t||S )CItem getter with special methods for arrays/lists and hashes/dicts.
__liquid__rN   rZ   rc   )r   r   rX   ra   rf   r   clsrJ   r7   r3   r3   r5   r   >  s   

zContext.getitemc                   sv   ddd}t |dr| }|d	krt||I d
H S |dkr't||I d
H S |dkr3t||I d
H S |||I d
H S )r   rJ   r   r7   r/   r.   c                   s&   t | dr| |I d H S t| |S )N__getitem_async__)r   r   r   )rJ   r7   r3   r3   r5   	_get_itemQ  s   

z(Context.getitem_async.<locals>._get_itemr   rN   NrZ   rc   rJ   r   r7   r   r/   r.   )r   r   rh   ri   rj   )r   rJ   r7   r   r3   r3   r5   r   M  s   

zContext.getitem_async)NNr   Nr[   r   N)ro   r&   rp   ry   rn   rz   r{   r>   ru   r|   rs   r>   rq   r>   rx   r}   r7   r8   r   r   r/   r   rE   r   r   r   r.   r/   r.   r   r8   r   r.   r/   r   r   r8   r/   r   )r   r8   r/   r(   )r   r8   r   r8   r/   r(   r   r8   r/   r>   r   r8   r   r   r/   r.   )r   r8   r/   r0   r   )r7   r8   r   r   r/   r>   )r   r   rx   r}   r/   r   )r   r   r   r'   r/   r   )r/   r   )r[   )r   r>   r/   r   )NFNF)r   r   rn   rz   r   r0   rx   r}   r   r0   r/   r)   )r   r   r/   r   )r   r   r/   r   rJ   r   r7   r   r/   r   r   )"rF   rG   rH   rI   	__slots__r   r   r   r    r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   classmethodr   r   r3   r3   r3   r5   r)      sZ    
A
	








	


5	r)   c                      s   e Zd ZdZdZedZ						d2d3 fddZd4 fddZ	e
fd5 fd!d"Ze
fd5 fd#d$Ze
fd6 fd&d'Zd7 fd(d)Zd7 fd*d+Zd8 fd-d.Zd9d0d1Z  ZS ):r+   zBA render context that captures template variable and filter names.)local_referencesall_referencesundefined_referencesroot_contextr   z^[\w_][\w_\-]*$Nr   r[   ro   r&   rp   ry   rn   rz   r{   r>   ru    Optional[VariableCaptureContext]rs   rq   c           	   	     sj   t  j|||||||d g | _g | _g | _g | _| }|jr0t|jtr0|j}|jr0t|jts$|| _	d S )Nr   )
superr   r  r  r	  r   ru   rR   r+   r
  )	r4   ro   rp   rn   r{   ru   rs   rq   r
  r   r3   r5   r   u  s,   
	
zVariableCaptureContext.__init__r7   r8   r   r   r/   r   c                   s   | j j| t ||S r   )r
  r  r   r  r   r   r  r3   r5   r     s   zVariableCaptureContext.assignr   r   r   r.   c                      t  ||}| || |S r   )r  r   _count_referencer4   r   r   resultr  r3   r5   r        zVariableCaptureContext.getc                   s&   t  ||I d H }| || |S r   )r  r   r  r  r  r3   r5   r     s   z VariableCaptureContext.get_asyncr   c                   r  r   )r  r   r  )r4   r   r   r  r  r3   r5   r     r  zVariableCaptureContext.resolvec                      | j j| t |S r   )r
  r  r   r  r   r   r  r3   r5   r        z VariableCaptureContext.incrementc                   r  r   )r
  r  r   r  r   r   r  r3   r5   r     r  z VariableCaptureContext.decrementr   c                   r  r   )r
  r   r   r  r   r   r  r3   r5   r     r  zVariableCaptureContext.filterr  c                 C  s   t |tr|}n<g }|D ]2}t |tr|d| d q| j|r5|r.|d|  q||  q|d| d qd|}t|rO| jj	| | jj
| d S )Nr   r   .z["z"]r~   )rR   r8   r>   r   re_identmatchr   r%   r
  r	  r  )r4   r   r  ref_pathelemr3   r3   r5   r    s   


z'VariableCaptureContext._count_reference)NNr   Nr[   r   )ro   r&   rp   ry   rn   rz   r{   r>   ru   r  rs   r>   rq   r>   r   r   r   r  r  )r   r   r  r.   r/   r   )rF   rG   rH   rI   r  recompiler  r   r   r    r   r   r   r   r   r   r  __classcell__r3   r3   r  r5   r+   g  s&    
	r+   c                      sB   e Zd ZdZed fddZed fd	d
ZdddZ  ZS )FutureContextam  A render context configured for maximum compatibility with the Ruby liquid.

    These "fixes" have not been implemented in the default `Context` for the benefit of
    existing Python Liquid users that rely on past behavior.

    This render context currently fixes https://github.com/jg-rp/liquid/issues/43 and
    https://github.com/jg-rp/liquid/issues/90.

    rJ   r   r7   r/   c                   s*   t |trt |trtdt ||S Nzstring indices are not allowed)rR   r8   r>   rP   r  r   r   r  r3   r5   r     s   zFutureContext.getitemr.   c                   s2   t |trt |trtdt ||I d H S r  )rR   r8   r>   rP   r  r   r   r  r3   r5   r     s   zFutureContext.getitem_asyncr   r8   r   r   c                 C  sn   |r|nt |}| jd }||d}z|| }W n ty$   d }Y nw |d7 }|t|kr1d}|||< |S )Nr   r   r[   )r8   rw   
setdefaultrP   rV   )r4   r   r   r7   r   r   rvr3   r3   r5   r     s   
zFutureContext.cycler  r   r  )	rF   rG   rH   rI   r  r   r   r   r  r3   r3   r  r5   r    s    
r  c                   @  s   e Zd ZdZdS )FutureVariableCaptureContextzIA context that captures information about template variables and filters.N)rF   rG   rH   rI   r3   r3   r3   r5   r"    s    r"  r   Sequence[Any]r`   r   Optional[object]c             
   G  s4   z
t tj|| }W |S  tttfy   |}Y |S w )zChained item getter.)r   r)   r   r<   rP   rQ   )rJ   r   r`   itmr3   r3   r5   r*     s   r*   )rJ   r   rK   rL   r/   r.   )rJ   r   rK   rg   r/   r.   )rJ   r#  r`   r   r   r$  r/   r   )OrI   
__future__r   collections.abcrS   r:   r^   r  r   r   
contextlibr   	functoolsr   r   ior   r   operatorr   r	   typingr
   r   r   r   r   r   r   r   r   r   r   r   r   liquidr   liquid.chain_mapr   liquid.exceptionsr   r   r   r   r   r   liquid.outputr   liquid.undefinedr    r!   r"   r#   r$   r%   r&   liquid.builtin.tags.for_tagr'   liquid.templater(   __all__r8   r>   r   r.   r   r,   r   rX   ra   rf   rh   ri   rj   r)   r+   r  r"  r*   r3   r3   r3   r5   <module>   s    


   <b(