
    
3jG                     8   S SK r S SKrS SKrS SKrS SKrS SKJrJr  SSKJ	r	J
r
  \R                  " S5      r\R                  " S5      r\R                  " S5      r\R                  " S\R                  S	\R                   4   S
9r " S S\	\\4   5      r " S S5      r\\\4S4S\S\S\4S jjr\\44S\S\4S jjrS\S\4S jrS\S\4S jr\" S/ SQ5      rSrSr S\RB                  \	\R                  4   S\R                  \\/\RD                  4   S\#S\RH                  \R                  \\R                   \R                   /\R                   4      S\S \#4S! jr%S\RB                  \	\R                  4   S\R                  \\/\RD                  4   S\#S\RH                  \R                  \\R                   \R                   /\R                   4      S\S \#4S" jr&\S#SS4S\RB                  \	\\R                  S	\	4   S4   S\R                  \\/\RD                  4   S\#S\RH                  \R                  \\R                   \R                   /\R                   4      S\S\R                  \/\4   4S$ jjr'\S#SS4S\RB                  \	\S4   S\R                  \\/\RD                  4   S\#S\RH                  \R                  \\R                   \R                   /\R                   4      S\S\R                  \/\4   4S% jjr(S&\)S\#4S' jr*g)(    N)defaultdict
namedtuple   )BaseCacheImpl	FIFOCacheKTVTDTFT.)boundc            
       `   \ rS rSrSrSrS0S\\\4   S\	SS4S	 jjr
\S\\\4   4S
 j5       r\S\4S j5       r\S\4S j5       rS\4S jrS\4S jrS\	4S jrS\S\	4S jrS\S\SS4S jrS\S\4S jrS\SS4S jrS\4S jrS\R4                  \   4S jrS\R8                  S\S\	4S jrS\4S jrS\4S jrS\	4S jr S\	4S jr!S\S\S\RD                  \   4S jr#S1S\S \RD                  \$   S\RJ                  \\$4   4S! jjr&S1S\S \RD                  \$   S\RJ                  \\$4   4S" jjr' S1S\S \RD                  \$   S\RD                  \RJ                  \\$4      4S# jjr(S\RR                  \\4   4S$ jr*S%\S\4S& jr+SS'.S(\	SS4S) jjr,S2S* jr-S+\RJ                  \R\                  \RR                  \\4      \R^                  \\4   4   SS4S, jr0S\R\                  \   4S- jr1S\R\                  \   4S. jr2S\R\                  \RR                  \\4      4S/ jr3Sr4g)3Frozen   z
A wrapper class that prevents modifications to an underlying cache implementation.

This class provides a read-only view of a cache, optionally allowing silent
suppression of modification attempts instead of raising exceptions.
)__cacheignoreFclsr   returnNc                 r    [        U[        5      (       d   e[        U5      [        Ld   eXl        X l        g)a  
Initialize a frozen cache wrapper.

:param cls: The underlying cache implementation to be frozen
:type cls: BaseCacheImpl[KT, VT]
:param ignore: If True, silently ignores modification attempts; if False, raises TypeError when modification is attempted
:type ignore: bool, optional
N)
isinstancer   typer   _Frozen__cacher   )selfr   r   s      H/home/wildlama/miniconda3/lib/python3.13/site-packages/cachebox/utils.py__init__Frozen.__init__   s3     #}----Cy&&&    c                     U R                   $ Nr   r   s    r   cacheFrozen.cache)   s    ||r   c                 .    U R                   R                  $ r   )r   maxsizer    s    r   r$   Frozen.maxsize-   s    ||###r   c                 .    U R                   R                  $ r   )r   	maxmemoryr    s    r   r'   Frozen.maxmemory1   s    ||%%%r   c                 ,    [        U R                  5      $ r   )lenr   r    s    r   __len__Frozen.__len__5   s    4<<  r   c                 6    U R                   R                  5       $ r   )r   
__sizeof__r    s    r   r.   Frozen.__sizeof__8   s    ||&&((r   c                 ,    [        U R                  5      $ r   )boolr   r    s    r   __bool__Frozen.__bool__;       DLL!!r   keyc                     XR                   ;   $ r   r   r   r5   s     r   __contains__Frozen.__contains__>   s    ll""r   valuec                 <    U R                   (       a  g [        S5      eNzThis cache is frozen.r   	TypeError)r   r5   r:   s      r   __setitem__Frozen.__setitem__A       ;;/00r   c                      U R                   U   $ r   r   r7   s     r   __getitem__Frozen.__getitem__G   s    ||C  r   c                 <    U R                   (       a  g [        S5      er<   r=   r7   s     r   __delitem__Frozen.__delitem__J       ;;/00r   c                 "    SU R                    S3$ )Nz	<Frozen: >r   r    s    r   __repr__Frozen.__repr__P   s    4<<.**r   c                 ,    [        U R                  5      $ r   )iterr   r    s    r   __iter__Frozen.__iter__S   r4   r   otheropc                 8    U R                   R                  X5      $ r   )r   __richcmp__)r   rQ   rR   s      r   rT   Frozen.__richcmp__V   s    ||''22r   c                 6    U R                   R                  5       $ r   )r   capacityr    s    r   rW   Frozen.capacityY       ||$$&&r   c                 6    U R                   R                  5       $ r   )r   memoryr    s    r   r[   Frozen.memory\       ||""$$r   c                 6    U R                   R                  5       $ r   )r   is_fullr    s    r   r_   Frozen.is_full_   s    ||##%%r   c                 6    U R                   R                  5       $ r   )r   is_emptyr    s    r   rb   Frozen.is_emptyb   rY   r   c                 <    U R                   (       a  g [        S5      er<   r=   )r   r5   r:   argskwargss        r   insertFrozen.inserte   rH   r   defaultc                 8    U R                   R                  X5      $ r   )r   getr   r5   ri   s      r   rk   
Frozen.getk   s    ||--r   c                 <    U R                   (       a  g [        S5      er<   r=   rl   s      r   pop
Frozen.popn   rH   r   c                 <    U R                   (       a  g [        S5      er<   r=   )r   r5   ri   re   rf   s        r   
setdefaultFrozen.setdefaultt   s     ;;/00r   c                 <    U R                   (       a  g [        S5      er<   r=   r    s    r   popitemFrozen.popitem|   rA   r   nc                 <    U R                   (       a  g [        S5      er<   r=   )r   rw   s     r   drainFrozen.drain   rA   r   reuser|   c                <    U R                   (       a  g [        S5      er<   r=   )r   r|   s     r   clearFrozen.clear   rA   r   c                 <    U R                   (       a  g [        S5      er<   r=   r    s    r   shrink_to_fitFrozen.shrink_to_fit   rA   r   iterablec                 <    U R                   (       a  g [        S5      er<   r=   )r   r   re   rf   s       r   updateFrozen.update   s     ;;/00r   c                 6    U R                   R                  5       $ r   )r   keysr    s    r   r   Frozen.keys   s    ||  ""r   c                 6    U R                   R                  5       $ r   )r   valuesr    s    r   r   Frozen.values   r]   r   c                 6    U R                   R                  5       $ r   )r   itemsr    s    r   r   Frozen.items   s    ||!!##r   Fr   r   N)5__name__
__module____qualname____firstlineno____doc__	__slots__r   r   r	   r1   r   propertyr!   intr$   r'   r+   r.   r2   r8   r?   rC   rF   strrK   typingIteratorrO   AnyrT   rW   r[   r_   rb   Optionalrg   r
   Unionrk   ro   rr   Tupleru   ry   r~   r   IterableDictr   r   r   r   __static_attributes__ r   r   r   r      s    &IM"b&1 4 D  }RV,   $ $ $ &3 & &! !)C )"$ "# #t #1r 1" 1 1!r !b !1r 1d 1+# +"&//"- "3 3 3 3'# '% %& &'$ '1" 1R 1V__R=P 1.r .FOOB$7 .6<<PRTVPVCW .1r 1FOOB$7 16<<PRTVPVCW 1 7;11 & 31	b"f-	.11b"f- 11s 1s 1 &+ 1d 1t 11	1,,vv||BF/CDfkkRTVXRXFYYZ	1
 
	1#foob) #%+ %$vv||BF';< $r   r   c                   V    \ rS rSrSrSrSS\4S jjrSS jrSS jr	SS	 jr
SS
 jrSrg)_LockWithCounter   z
A lock with a counter to track the number of waiters.

This class provides a lock mechanism that supports both synchronous and asynchronous contexts,
with the ability to track the number of threads or coroutines waiting to acquire the lock.
)lockwaitersis_asyncc                 ~    U(       d  [         R                  " 5       O[        R                  " 5       U l        SU l        g )Nr   )_threadallocate_lockasyncioLockr   r   )r   r   s     r   r   _LockWithCounter.__init__   s#    3;G))+	r   Nc                 ~   #    U =R                   S-  sl         U R                  R                  5       I S h  vN   g  N7fNr   r   r   acquirer    s    r   
__aenter___LockWithCounter.__aenter__   s)     ii!!!s   3=;=c                 j   #    U =R                   S-  sl         U R                  R                  5         g 7fr   r   r   releaser   re   kwdss      r   	__aexit___LockWithCounter.__aexit__   s#     		s   13c                 b    U =R                   S-  sl         U R                  R                  5         g r   r   r    s    r   	__enter___LockWithCounter.__enter__        		r   c                 b    U =R                   S-  sl         U R                  R                  5         g r   r   r   s      r   __exit___LockWithCounter.__exit__   r   r   r   r   )r   r   r   r   r   r   r1   r   r   r   r   r   r   r   r   r   r   r      s.     $I "r   r   objlevelr   c                 l    SSK J n  US:X  a  U $ US:X  a  U" U 5      $ [        U 5      U;   a  U" U 5      $ U $ )Nr   )copy   )r   r   )r   tocopyr   r   s       r   _copy_if_needr      s<    z
zCycf,49636r   re   r   c                     U nU(       a%  U[         4-  nUR                  5        H  nX4-  nM	     U(       a&  [        U5      S:X  a  [        US   5      U;   a  US   $ U$ )a  
Create a hashable key from function arguments for caching purposes.

Args:
    args (tuple): Positional arguments to be used in key generation.
    kwds (dict): Keyword arguments to be used in key generation.
    fasttype (tuple, optional): Types that can be directly used as keys. Defaults to (int, str).

Returns:
    A hashable key representing the function arguments, optimized for simple single-argument cases.
r   r   )objectr   r*   r   )re   r   fasttyper5   items        r   make_keyr      s[     CyJJLDKC ! CHMd3q6lh&>1vJr   c                 *    [        [        X5      5      $ )a  
Create a hashable hash key from function arguments for caching purposes.

Args:
    args (tuple): Positional arguments to be used in key generation.
    kwds (dict): Keyword arguments to be used in key generation.

Returns:
    int: A hash value representing the function arguments.
)hashr   )re   r   s     r   make_hash_keyr      s     $%%r   c                     [        XSS9nU[        S U  5       5      -  nU(       a#  U[        S UR                  5        5       5      -  nU$ )aD  
Create a hashable key from function arguments that includes type information.

Args:
    args (tuple): Positional arguments to be used in key generation.
    kwds (dict): Keyword arguments to be used in key generation.

Returns:
    A hashable key representing the function arguments, including the types of the arguments.
r   )r   c              3   8   #    U  H  n[        U5      v   M     g 7fr   r   .0vs     r   	<genexpr>!make_typed_key.<locals>.<genexpr>  s     '$Qa$   c              3   8   #    U  H  n[        U5      v   M     g 7fr   r   r   s     r   r   r   	  s     4mT!WWmr   )r   tupler   )re   r   r5   s      r   make_typed_keyr      sH     4
+C5'$'''Cu4dkkm444Jr   	CacheInfo)hitsmissesr$   lengthr[   r   r!   	key_makerclear_reusecallback
copy_level	is_methodc           
      ,  ^ ^^^^^^	^
^^^^ [         R                  " T5      =nm
U(       a  U4S jOTm	SmSm[        [        5      m0 mU	UU
UUUU UUU4
S jnT
(       d  TUl        UUU4S jUl        TUl        T
(       d  SUUUUUU4S jjnXl        U$ )Nc                    > T" U SS  U5      $ r   r   re   r   r   s     r   <lambda>!_cached_wrapper.<locals>.<lambda>      YtABx%>r   r   c                    >
 UR                  SS5      (       a  T" U 0 UD6$ T
(       a  T	" U S   5      OT	nT" X5      n X#   nTS-  mTb  T" [        X45        [        UTS9$ ! [         a     Of = fTU      TR	                  US 5      b-  TU   R
                  S:  a  TU   nUeTR                  U5      nUe X#   nTS-  m[        nOT! [         aG     T" U 0 UD6nXBU'   TS-  m[        n O0! [         a  nTU   R
                  S:  a  UTU'   UeS nAff = ff = fS S S 5        O! , (       d  f       O= fTb
  T" WUW5        [        WTS9$ Ncachebox__ignoreFr   r   )r   )ro   	EVENT_HITr   KeyErrorrk   r   
EVENT_MISS	Exception)re   r   _cacher5   resultcached_errorevente
_key_makerr!   cache_is_functionr   r   
exceptionsfuncr   locksr   s           r   _wrapped!_cached_wrapper.<locals>._wrapped$  s    88&..&&&#4tAw%$	;[F AID#C0 z::  		 3Z~~c4(427*2D2Dq2Hz#"" OYnn]`Na""'	! ''!4040F #)3KaKF&E ! Sz))A-*+
3G	' ZZ. UC(V:66s`   A! !
A.-A.6AD%8CD%
DC,D*D%,
D6DDDD%%
D3c                  d   > [        TTT R                  [        T 5      T R                  5       5      $ r   r   r$   r*   r[   r!   r   r   s   r   r   r   Z  #    i&%--UU\\^'
r   c                  l   > T R                  TS9  SmSmTR                  5         TR                  5         g Nr{   r   r~   r!   r   r   r   r   r   s   r   cache_clear$_cached_wrapper.<locals>.cache_clearb  0    KKkK*FDKKMr   r   )inspect
isfunctionr   r   r!   
cache_infor   r
  r   r!   r   r   r   r   r   r   r
  r   r   r   r   r   r   s   ``````   @@@@@@r   _cached_wrapperr    s     %,$6$6u$==I!CL>R[JDF<GHX<YE>@J27 27h 
 !H	 	  +Or   c           
      &  ^ ^^^^^^	^
^^^^ [         R                  " T5      =nm
U(       a  U4S jOTm	SmSm[        S 5      m0 mU	UU
UUUU UUU4
S jnT
(       d  TUl        UUU4S jUl        TUl        T
(       d  SUUUUUU4S jjnXl        U$ )Nc                    > T" U SS  U5      $ r   r   r   s     r   r   '_async_cached_wrapper.<locals>.<lambda>y  r   r   r   c                      [        S5      $ NT)r   r   r   r   r   r  ~  s
     &r   c                    >
#    UR                  SS5      (       a  T" U 0 UD6I S h  vN $ T(       a  T
" U S   5      OT
nT	" X5      n X#   nTS-  mTb2  T" [        X45      n[        R                  " U5      (       a
  UI S h  vN   [	        UTS9$  Ni N! [
         a     Of = fTU    IS h  vN    TR                  US 5      b-  TU   R                  S:  a  TU   nUeTR                  U5      nUe X#   nTS-  m[        nO]! [
         aP     T" U 0 UD6I S h  vN  nXBU'   TS-  m[        n O0! [         a  nTU   R                  S:  a  UTU'   UeS nAff = ff = fS S S 5      IS h  vN    O! , IS h  vN  (       d  f       O= fTb0  T" WUW5      n[        R                  " U5      (       a  UI S h  vN    [	        WTS9$ 7fr   )
ro   r   r  isawaitabler   r   rk   r   r   r   )re   r   r   r5   r   	awaitabler   r   r   r   r!   r   r   r   r   r   r   r   r   s            r   r   '_async_cached_wrapper.<locals>._wrapped  s     88&..t,t,,,#4tAw%$	;[F AID#$Y<	&&y11#OO z::) -$ $  		 ::~~c4(427*2D2Dq2Hz#"" OYnn]`Na""'	! ''#'#6#666F #)3KaKF&E ! Sz))A-*+
3G	' :::::.  V4I""9--V:66s   #G
B G
B 5G
BG
G

B!G
 B!!G
-B0.G
2AE34DE3
ED1DD1 E/E31
E;EEEE3!G
,E/-G
3F
9E<:F
4G
:F=;G
c                  d   > [        TTT R                  [        T 5      T R                  5       5      $ r   r  r  s   r   r   r    r  r   c                  l   > T R                  TS9  SmSmTR                  5         TR                  5         g r  r  r	  s   r   r
  *_async_cached_wrapper.<locals>.cache_clear  r  r   r   )r  r  r   r!   r  r   r
  r  s   ``````   @@@@@@r   _async_cached_wrapperr  o  s     %,$6$6u$==I!CL>R[JDF<G&=E ?AJ67 67p 
 !H	 	  +Or   Fc                   ^ ^^^^ T c  [        S5      m [        T 5      [        L a  [        ST 5      m [        T [        5      (       d*  [
        R                  " T 5      (       d  [        ST < 35      eS[        S[        4U UUUU4S jjnU$ )aw  
Decorator to create a memoized cache for function results.

Wraps a function to automatically cache and retrieve its results based on input parameters.

Args:
    cache (BaseCacheImpl, dict, callable): Cache implementation to store results. Defaults to FIFOCache.
                                           Can be a function that got `self` and should return cache.
    key_maker (Callable, optional): Function to generate cache keys from function arguments. Defaults to make_key.
    clear_reuse (bool, optional): Whether to reuse cache during clearing. Defaults to False.
    callback (Callable, optional): Function called on cache hit/miss events. Defaults to None.
    copy_level (int, optional): Level of result copying. Defaults to 1.

Returns:
    Callable: Decorated function with caching capabilities.

Example for functions::

    @cachebox.cached(cachebox.LRUCache(128))
    def sum_as_string(a, b):
        return str(a+b)

    assert sum_as_string(1, 2) == "3"

    assert len(sum_as_string.cache) == 1
    sum_as_string.cache_clear()
    assert len(sum_as_string.cache) == 0

Example for methods::

    class A:
        def __init__(self, num):
            self.num = num
            self._cache = cachebox.FIFOCache(0)

        @cachebox.cached(lambda self: self._cache)
        def method(self, n):
            return self.num * n

    instance = A(10)
    assert A.method(2) == 20
r   z-we expected cachebox caches or function, got r   r   c           	         > [         R                  " U 5      (       a  [        U TTTTTS5      nO[        U TTTTTS5      n[        R
                  " X5      $ )NFr  iscoroutinefunctionr  r  	functoolsupdate_wrapperr   wrapperr!   r   r   r   r   s     r   	decoratorcached.<locals>.decorator  s[    &&t,,+eYXz5G &eYXz5G ''66r   )	r   r   dictr   r   r  r  r>   r   )r!   r   r   r   r   r'  s   ````` r   cachedr*    sw    b }!E{d!U#e]++G4F4Fu4M4MUTUU
7 
7r 
7 
7 r   c                   ^ ^^^^ SSK nUR                  S[        SS9  T c  [        S5      m [	        T 5      [
        L a  [        ST 5      m [        T [        5      (       d  [        ST < 35      eS[        S[        4U UUUU4S	 jjnU$ )
a[  
**This function is deperecated due to issue [#35](https://github.com/awolverp/cachebox/issues/35)**.
Use `cached` method instead.

Decorator to create a method-specific memoized cache for function results.

Similar to `cached()`, but ignores `self` parameter when generating cache keys.

Args:
    cache (BaseCacheImpl, dict, optional): Cache implementation to store results. Defaults to FIFOCache.
    key_maker (Callable, optional): Function to generate cache keys from function arguments. Defaults to make_key.
    clear_reuse (bool, optional): Whether to reuse cache during clearing. Defaults to False.
    callback (Callable, optional): Function called on cache hit/miss events. Defaults to None.
    copy_level (int, optional): Level of result copying. Defaults to 1.

Returns:
    Callable: Decorated method with method-specific caching capabilities.
r   Nzhcachedmethod is deprecated, use cached instead. see issue https://github.com/awolverp/cachebox/issues/35r   )
stacklevelz!we expected cachebox caches, got r   r   c           	         > [         R                  " U 5      (       a  [        U TTTTTS5      nO[        U TTTTTS5      n[        R
                  " X5      $ r  r!  r%  s     r   r'  cachedmethod.<locals>.decoratorD  s[    &&t,,+eYXz4G &eYXz4G ''66r   )
warningswarnDeprecationWarningr   r   r)  r   r   r>   r   )r!   r   r   r   r   r/  r'  s   `````  r   cachedmethodr2    s    2 MMr   }!E{d!U#e]++HII
7 
7r 
7 
7 r   r   c                 \    [        U S5      =(       a    [        U R                  [        5      $ )z6
Check if a function/method cached by cachebox or not
r!   )hasattrr   r!   r   )r   s    r   	is_cachedr5  S  s!     4!Kj]&KKr   )+r   r   r#  r  r   collectionsr   r   	_cacheboxr   r   TypeVarr   r	   r
   Callabler   r   r   r   r)  listsetr   r   r   r   r   r   r   r   r   r   r   Hashabler1   r   r  r  r*  r2  r   r5  r   r   r   <module>r=     s        / /^^D^^D^^D^^DVZZ @AV$]2r6" V$r > $(s"3! 	7r 	7C 	7 	7 14Sz 5  0& &T & d ( {$UV	
	Y<<v67Y t}foo=>Y 	Y
 oofoosFJJ

.KVZZ.WXYY Y Yx_<<v67_ t}foo=>_ 	_
 oofoosFJJ

.KVZZ.WXY_ _ _H BJ\`F<<tV__S-=O-PRVVWFt}foo=>F F oofoosFJJ

.KVZZ.WXY	F
 F __bT2XFV BJ\`6<<tT126t}foo=>6 6 oofoosFJJ

.KVZZ.WXY	6
 6 __bT2X6rLF Lt Lr   