Ë
    Öªg
  ã                  ó¶   — d Z ddlmZ ddlmZ ddlmZmZmZ ddl	Z	ddl
mZ er
ddlmZmZmZ  ed«      Zde	j"                  ej$                  f	 	 	 	 	 	 	 d
d	„Zy)z2
Helpers related to (dynamic) resource retrieval.
é    )Úannotations©Ú	lru_cache)ÚTYPE_CHECKINGÚCallableÚTypeVarN)ÚResource)ÚURIÚDÚRetrieveÚ_Tc                ó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      €€€úJ/var/www/openai/venv/lib/python3.12/site-packages/referencing/retrieval.pyÚcached_retrievez>to_cached_resource.<locals>.decorator.<locals>.cached_retrieveO   s    ø€ á “}ˆHÙ˜X“ˆHÙ  Ó*Ð*ó    )r   r
   r   )r   r   Úcacher   r   s   ` €€€r   Ú	decoratorz%to_cached_resource.<locals>.decoratorN   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   r   ÚjsonÚreferencingr	   Úreferencing.typingr
   r   r   r   r   r   r   r   r   r   Ú<module>r'      su   ðñõ #å ß 3Ñ 3Û å  áß3Ñ3ñ ˆTƒ]€ð :>Ø#Ÿz™zØ08×0FÑ0FðCØ6ðCàðCð .ðCð 2ô	Cr   