
    ȇh
                        S r SSKJr  SSKJr  SSKJrJr  SSKr SSK	J
r
  SSKJr  \(       a
  SSKJrJrJr  \
" S	\S
9rS\R(                  \R*                  4       SS jjrg! \ a	    SSKJ
r
   NRf = f)z2
Helpers related to (dynamic) resource retrieval.
    )annotations	lru_cache)TYPE_CHECKINGCallableN)TypeVar)Resource)URIDRetrieve_T)defaultc                8   ^ ^^ T c	  [        SS9m SU UU4S jjnU$ )a  
Create a retriever which caches its return values from a simpler callable.

Takes a function which returns things like serialized JSON (strings) and
returns something suitable for passing to `Registry` as a retrieve
function.

This decorator both reduces a small bit of boilerplate for a common case
(deserializing JSON from strings and creating `Resource` objects from the
result) as well as makes the probable need for caching a bit easier.
Retrievers which otherwise do expensive operations (like hitting the
network) might otherwise be called repeatedly.

Examples
--------

.. testcode::

    from referencing import Registry
    from referencing.typing import URI
    import referencing.retrieval


    @referencing.retrieval.to_cached_resource()
    def retrieve(uri: URI):
        print(f"Retrieved {uri}")

        # Normally, go get some expensive JSON from the network, a file ...
        return '''
            {
                "$schema": "https://json-schema.org/draft/2020-12/schema",
                "foo": "bar"
            }
        '''

    one = Registry(retrieve=retrieve).get_or_retrieve("urn:example:foo")
    print(one.value.contents["foo"])

    # Retrieving the same URI again reuses the same value (and thus doesn't
    # print another retrieval message here)
    two = Registry(retrieve=retrieve).get_or_retrieve("urn:example:foo")
    print(two.value.contents["foo"])

.. testoutput::

    Retrieved urn:example:foo
    bar
    bar

N)maxsizec                (   >^  TSUUU 4S jj5       nU$ )Nc                4   > T" U 5      nT" U5      nT" U5      $ )N )uriresponsecontentsfrom_contentsloadsretrieves      mC:\Users\julio\OneDrive\Documentos\Trabajo\IdeasFrscas\Cabanna\env\Lib\site-packages\referencing/retrieval.pycached_retrieve>to_cached_resource.<locals>.decorator.<locals>.cached_retrieveT   s     }HXH **    )r   r
   r   )r   r   cacher   r   s   ` r   	decorator%to_cached_resource.<locals>.decoratorS   s"    		+ 	+ 
	+
 r   )r   zCallable[[URI], _T]r   )r   r   r   r   s   ``` r   to_cached_resourcer!      s'    n }$'  r   )r   z+Callable[[Retrieve[D]], Retrieve[D]] | Noner   zCallable[[_T], D]r   zCallable[[D], Resource[D]]returnz,Callable[[Callable[[URI], _T]], Retrieve[D]])__doc__
__future__r   	functoolsr   typingr   r   jsontyping_extensionsr   ImportErrorreferencingr	   referencing.typingr
   r   r   strr   r   r   r!   r   r   r   <module>r-      s    #  * ) !33 T3 :>#zz080F0FC6CC .C 2	C  s   A& &A54A5