
    Hoh%                    ^   d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZmZmZmZmZmZ ddlmZmZmZ g dZ G d	 d
e      Z G d d      Zej0                   G d d             Z G d d      Zej0                   G d d             Z G d d      Z G d d      Z G d d      Zy)    )annotationsN)OrderedDictdeque)
ContextVar)CallableDequeDictOptionalProtocolUnion   )DomainPartialResultResolver)CacheCachingResolverLruS3FifoSievec                  \    e Zd ZdZej
                  dd       Zej
                  dd       Zy)r   zCache()

    Cache abstract protocol. The :class:`CachingResolver` will look
    values up, merge what was returned (possibly nothing) with what it
    got from its actual parser, and *re-set the result*.
    c                     y)z6Adds or replace ``value`` to the cache at key ``key``.N selfkeyvalues      m/var/www/html/wine-match-dev/backend/winematch-backend/venv/lib/python3.12/site-packages/ua_parser/caching.py__setitem__zCache.__setitem__$        	    c                     y)z5Returns a partial result for ``key`` if there is any.Nr   r   r   s     r   __getitem__zCache.__getitem__)   r   r    Nr   strr   r   returnNoner   r%   r&   zOptional[PartialResult])__name__
__module____qualname____doc__abcabstractmethodr   r#   r   r    r   r   r      s;     	  	 r    r   c                  (    e Zd ZdZddZddZddZy)	r   a   Cache following a least-recently used replacement policy: when
    there is no more room in the cache, whichever entry was last seen
    the least recently is removed.

    Simple LRUs are generally outdated and to avoid as they have
    relatively low hit rates for modern caches (at lower sizes). The
    main use case here is if the workload can lead to the cache being
    full of popular items then all of them being replaced at once:
    :class:`S3Fifo` and :class:`Sieve` are FIFO-based caches and have
    worst-case O(n) eviction.
    c                b    || _         t               | _        t        j                         | _        y N)maxsizer   cache	threadingLocklockr   r2   s     r   __init__zLru.__init__<   s     6Am
NN$	r    c                    | j                   5  | j                  j                  |      }|r| j                  j                  |       |cd d d        S # 1 sw Y   y xY wr1   )r6   r3   getmove_to_endr   r   es      r   r#   zLru.__getitem__A   sH    YY 	

s#A

&&s+		 	 	s   :AAc                    | j                   5  t        | j                        | j                  k\  r*|| j                  vr| j                  j	                  d       || j                  |<   d d d        y # 1 sw Y   y xY w)NF)last)r6   lenr3   r2   popitemr   s      r   r   zLru.__setitem__H   s_    YY 	$4::$,,.3djj3H

""".#DJJsO	$ 	$ 	$s   AA22A;Nr2   intr(   r$   )r)   r*   r+   r,   r8   r#   r   r   r    r   r   r   /   s    
%
$r    r   c                  4    e Zd ZU g dZded<   ded<   ded<   y)	
CacheEntry)freqr   r   r%   r   r   r   rC   rF   Nr)   r*   r+   	__slots____annotations__r   r    r   rE   rE   O   s    (I	H
Ir    rE   c                  8    e Zd ZdZddZd	dZd
dZddZddZy)r   aB  FIFO-based quick-demotion lazy-promotion cache [S3-FIFO]_.

    Experimentally provides excellent hit rate at lower cache sizes,
    for a relatively simple and efficient implementation. Notably
    excellent at handling "one hit wonders", aka entries seen only
    once during a work-set (or reasonable work window).
    c                   || _         i | _        t        dt        |dz              | _        t               | _        || j                  z
  | _        t               | _        t               | _	        t        j                         | _        y )Nr   
   )r2   indexmaxrC   small_targetr   smallmain_targetmainghostr4   r5   r6   r7   s     r   r8   zS3Fifo.__init__`   sd    8:
3w|#45(-
"T%6%66',w	!&
NN$	r    c                    | j                   j                  |      x}r;t        |      t        u r*t	        |j
                  dz   d      |_        |j                  S y )Nr      )rM   r:   typerE   minrF   r   r<   s      r   r#   zS3Fifo.__getitem__j   sG    $$A$$q'Z*?!Q'AF77Nr    c                "   | j                   5  | j                  j                  |      x}r"t        |      t        u r||_        	 d d d        y t        | j                        t        | j                        z   | j                  k\  rzt        | j                        | j                  k  r| j                          t        | j                        t        | j                        z   | j                  k\  r| j                          t	        ||d      }t        | j                  j                  |            t        u r| j                  j                  |       n| j                  j                  |       || j                  |<   d d d        y # 1 sw Y   y xY wNr   )r6   rM   r:   rV   rE   r   r@   rP   rR   r2   rQ   _evict_small_evict_mainr%   
appendleft)r   r   rr=   entrys        r   r   zS3Fifo.__setitem__r   s"   YY 	$ZZ^^C(((d1g.C	$ 	$
 4::TYY/4<<?tyy>D$4$44%%' tzz?S^3t||C$$&sAq)EDJJNN3'(C/		$$U+

%%e,#DJJsO'	$ 	$ 	$s   7FD/FFc                    	 | j                   j                         }|j                  r1|xj                  dz  c_        | j                   j                  |       n| j                  |j
                  = y p)Nr   )rR   poprF   r\   rM   r   )r   r=   s     r   r[   zS3Fifo._evict_main   sR    		Avv!		$$Q'JJquu% r    c                b   | j                   r"| j                   j                         }|j                  r#d|_        | j                  j	                  |       n|j
                  x}| j                  |j
                  <   | j                  j	                  |       t        | j                        | j                  kD  rg| j                  j                         }| j                  j                  |      |u r| j                  |= t        | j                        | j                  kD  rgy | j                   r!y y rY   )rP   r`   rF   rR   r\   r   rM   rS   r@   rQ   r:   )r   r=   gs      r   rZ   zS3Fifo._evict_small   s    jj

 Avv		$$Q'()-DJJquu%

%%a($**o(8(88

(Azz~~a(A- JJqM $**o(8(88  jjr    NrB   r(   )r   r%   r]   r   r&   r'   r&   r'   )	r)   r*   r+   r,   r8   r#   r   r[   rZ   r   r    r   r   r   W   s     %$,r    r   c                  :    e Zd ZU dZded<   ded<   ded<   ded	<   y
)	SieveNode)r   nextr   visitedr%   r   r   r   boolrg   zOptional[SieveNode]rf   NrG   r   r    r   re   re      s    3I	HM
r    re   c                  0    e Zd ZdZddZddZd	dZd
dZy)r   a  FIFO-based quick-demotion cache [SIEVE]_.

    Simpler FIFO-based cache, cousin of :class:`S3Fifo`.
    Experimentally slightly lower hit rates than :class:`S3Fifo` (if
    way superior to LRU still), but much more compact (~50% lower
    memory overhead at larger cache sizes, up to 100% at very small
    cache sizes).

    Can be an interesting candidate when trying to save on memory,
    although the contained entries will generally be much larger than
    the cache itself.
    c                    || _         i | _        d | _        d | _        d | _        d | _        t        j                         | _        y r1   )	r2   r3   headtailhandprevr4   r5   r6   r7   s     r   r8   zSieve.__init__   s:    +-
)-	)-	)-	)-	NN$	r    c                d    | j                   j                  |      x}rd|_        |j                  S y )NT)r3   r:   rg   r   )r   r   r^   s      r   r#   zSieve.__getitem__   s.    JJNN3''5' EM;;r    c                   | j                   5  | j                  j                  |      x}r||_        	 d d d        y t	        | j                        | j
                  k\  r| j                          t        ||dd       x}| j                  |<   | j                  r|| j                  _	        || _        | j                  || _
        d d d        y # 1 sw Y   y xY wNF)r6   r3   r:   r   r@   r2   _evictre   rk   rf   rl   )r   r   r   r=   nodes        r   r   zSieve.__setitem__   s    YY 	!JJNN3''q'	! 	!
 4::$,,.%.sE5$%GGD4::c?yy!%		DIyy  		! 	! 	!s   &CBCCc                   | j                   r| j                   | j                  }}n| j                  d }}|rK|j                  r?d|_        |j                  r|j                  |}}n| j                  d }}|r|j                  r?|sy |j                  | _         || _        | j
                  |j                  = |j                  s|| _        |r|j                  |_        y |j                  | _        y rq   )rm   rn   rl   rg   rf   r3   r   rk   )r   objpobjs      r   rr   zSieve._evict   s    99		499C		4CckkCKxxHHcT IItT ckk HH		JJswwxxDIDIDIr    N)r2   rC   r&   r'   r(   r$   rc   )r)   r*   r+   r,   r8   r#   r   rr   r   r    r   r   r      s    %! !r    r   c                  :    e Zd ZdZddZedd       Zd	dZd
dZy)Localad  Thread local cache decorator. Takes a cache factory and lazily
    instantiates a cache for each thread it's accessed from.

    This means the cache capacity and memory consumption is
    figuratively multiplied by however many threads the cache is used
    from, but those threads don't share their caching, and thus don't
    contend on cache use.

    c                2    t        d      | _        || _        y )Nzlocal-cache)r   cvfactory)r   r{   s     r   r8   zLocal.__init__  s    %/%>r    c                    | j                   j                  d       }|+| j                         }| j                   j                  |       |S r1   )rz   r:   r{   set)r   cs     r   r3   zLocal.cache  s8    GGKK9AGGKKNr    c                     | j                   |   S r1   r3   r"   s     r   r#   zLocal.__getitem__  s    zz#r    c                "    || j                   |<   y r1   r   r   s      r   r   zLocal.__setitem__  s    

3r    N)r{   zCallable[[], Cache]r&   r'   )r&   r   r(   r$   )	r)   r*   r+   r,   r8   propertyr3   r#   r   r   r    r   rx   rx      s*       r    rx   c                       e Zd ZdZddZddZy)r   a  A wrapper resolver which takes an underlying concrete
    :class:`Cache` for the actual caching and cache strategy.

    This resolver only interacts with the :class:`Cache` and delegates
    to the wrapped resolver in case of lookup failure.

    :class:`CachingParser` will set entries back in the cache when
    filling them up, it does not update results in place (and can't
    really, they're immutable).

    c                     || _         || _        y r1   )parserr3   )r   resolverr3   s      r   r8   zCachingResolver.__init__"  s     (!
r    c                  | j                   |   }|r ||j                  v r|S ||j                   z  }| j                  ||      }|rot        ||j                  |j                  z  |j                  xs |j                  |j
                  xs |j
                  |j                  xs |j                        }|| j                   |<   |S )N)stringdomains
user_agentosdevice)r3   r   r   r   r   r   r   )r   uar   r^   r]   s        r   __call__zCachingResolver.__call__&  s    

2%--'~%GKKG$		1 ++;q||88#qtt||/qxxA 

2r    N)r   r   r3   r   )r   r%   r   r   r&   r   )r)   r*   r+   r,   r8   r   r   r    r   r   r     s    
"r    r   )
__future__r   r-   dataclassesr4   collectionsr   r   contextvarsr   typingr   r   r	   r
   r   r   corer   r   r   __all__r   r   	dataclassrE   r   re   r   rx   r   r   r    r   <module>r      s    " 
   * "  2 1H &$ $@   H HV   I! I!X   <# #r    