File: //var/opt/nydus/ops/referencing/__pycache__/retrieval.cpython-39.pyc
a
�,�h�
� @ s� d Z ddlmZ ddlmZ ddlmZmZ ddlZzddl m
Z
W n eyb ddlm
Z
Y n0 ddlm
Z
er�ddlmZmZmZ e
d ed
�Zdeje
jfddd
dd�dd�ZdS )z2
Helpers related to (dynamic) resource retrieval.
� )�annotations�� lru_cache)�
TYPE_CHECKING�CallableN)�TypeVar)�Resource)�URI�D�Retrieve�_T)�defaultz+Callable[[Retrieve[D]], Retrieve[D]] | NonezCallable[[_T], D]zCallable[[D], Resource[D]]z,Callable[[Callable[[URI], _T]], Retrieve[D]])�cache�loads�
from_contents�returnc s, � du rt dd�� dd�� ��fdd�}|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)�maxsizezCallable[[URI], _T]��retrievec s �dd���� fdd��}|S )Nr )�uric s �| �}�|�}� |�S )N� )r �response�contents)r r r r �F/opt/nydus/tmp/pip-target-wkfpz8uv/lib/python/referencing/retrieval.py�cached_retrieveT s z>to_cached_resource.<locals>.decorator.<locals>.cached_retriever )r r �r r r r r � decoratorS s z%to_cached_resource.<locals>.decoratorr )r r r r r r r �to_cached_resource s 7
r )�__doc__�
__future__r � functoolsr �typingr r �jsonZtyping_extensionsr �ImportErrorZreferencingr Zreferencing.typingr r
r �strr r r r r r r r �<module> s �