o
    e	                     @  sv   d Z ddlmZ ddlmZ ddlmZmZ ddlZddl	m
Z
 ddlmZmZmZ edZdeje
jfdddZdS )z2
Helpers related to (dynamic) resource retrieval.
    )annotations	lru_cache)CallableTypeVarN)Resource)URIDRetrieve_Tcache+Callable[[Retrieve[D]], Retrieve[D]] | NoneloadsCallable[[_T], D]from_contentsCallable[[D], Resource[D]]return,Callable[[Callable[[URI], _T]], Retrieve[D]]c                   s(    du r	t dd d fdd}|S )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
        import referencing.retrieval


        @referencing.retrieval.to_cached_resource()
        def retrieve(uri: str):
            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)maxsizeretrieveCallable[[URI], _T]c                   s   d fdd}|S )Nurir   c                   s   | }|} |S )N )r   responsecontents)r   r   r   r   ED:\Projects\ConvertPro\env\Lib\site-packages\referencing/retrieval.pycached_retrieveK   s   z>to_cached_resource.<locals>.decorator.<locals>.cached_retrieve)r   r   r   )r   r   r   r   r   )r   r   	decoratorJ   s   z%to_cached_resource.<locals>.decorator)r   r   r   )r   r   r   r   r   r   r   to_cached_resource   s   6
	r   )r   r   r   r   r   r   r   r   )__doc__
__future__r   	functoolsr   typingr   r   jsonZreferencingr   Zreferencing.typingr   r	   r
   r   r   r   r   r   r   r   r   <module>   s    