
    ci
                        d Z ddlmZ ddlmZ ddlmZ ddlZ	 ddlm	Z	 ddlmZ erddlmZ dd	lmZmZmZ  e	d
e      Zdej*                  ej,                  f	 	 	 	 	 	 	 ddZy# e
$ r	 ddlm	Z	 Y Sw xY w)z2
Helpers related to (dynamic) resource retrieval.
    )annotations	lru_cache)TYPE_CHECKINGN)TypeVar)Resource)Callable)URIDRetrieve_T)defaultc                8      t        d       d fd}|S )aG  
    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                $     d fd       }|S )Nc                4     |       } |      } |      S )N )uriresponsecontentsfrom_contentsloadsretrieves      </tmp/pip-target-z3e9_cxr/lib/python/referencing/retrieval.pycached_retrievez>to_cached_resource.<locals>.decorator.<locals>.cached_retrieveV   s     }HXH **    )r   r
   r   )r   r   cacher   r   s   ` r   	decoratorz%to_cached_resource.<locals>.decoratorU   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   jsontyping_extensionsr   ImportErrorreferencingr   collections.abcr	   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& &A43A4