Ë
    C¨6i©
  ã                  óî   — 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      €€€úX/home/azureuser/techstart-app/venv/lib/python3.12/site-packages/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ñ ˆT˜3Ô€ð :>Ø#Ÿz™zØ08×0FÑ0FðCØ6ðCàðCð .ðCð 2ô	Cøð ò ßðús   šA& Á&A4Á3A4