o
    i3                     @  s   d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ eeZG dd dZdS )zResource manager functionality.    )annotationsN)Callable)Any)AnyUrl)settings)NotFoundErrorResourceError)Resource)ResourceTemplatematch_uri_template)DuplicateBehavior)
get_loggerc                   @  s   e Zd ZdZ		d3d4ddZd5ddZd6ddZ				d7d8ddZ				d7d9ddZd:d!d"Z					d7d;d%d&Z
d<d(d)Zd=d,d-Zd>d.d/Zd?d1d2ZdS )@ResourceManagerzManages FastMCP resources.Nduplicate_behaviorDuplicateBehavior | Nonemask_error_detailsbool | Nonec                 C  sT   i | _ i | _|p
tj| _|du rd}|tjvr%td| ddtj || _dS )a  Initialize the ResourceManager.

        Args:
            duplicate_behavior: How to handle duplicate resources
                (warn, error, replace, ignore)
            mask_error_details: Whether to mask error details from exceptions
                other than ResourceError
        NwarnzInvalid duplicate_behavior: z. Must be one of: z, )	
_resources
_templatesr   r   r   __args__
ValueErrorjoinr   )selfr   r    r   h/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/resources/resource_manager.py__init__   s   


zResourceManager.__init__returndict[str, Resource]c                      t | jS )z+Get all registered resources, keyed by URI.)dictr   r   r   r   r   get_resources8      
zResourceManager.get_resourcesdict[str, ResourceTemplate]c                   r   )z4Get all registered templates, keyed by URI template.)r    r   r!   r   r   r   get_resource_templates<   r#   z&ResourceManager.get_resource_templatesfnCallable[..., Any]uristrname
str | Nonedescription	mime_typetagsset[str] | NoneResource | ResourceTemplatec           	        s~   ddl m  d|v od|v }t fddt|j D }|s#|r-| ||||||S |s;|s;| ||||||S t	d)a  Add a resource or template to the manager from a function.

        Args:
            fn: The function to register as a resource or template
            uri: The URI for the resource or template
            name: Optional name for the resource or template
            description: Optional description of the resource or template
            mime_type: Optional MIME type for the resource or template
            tags: Optional set of tags for categorizing the resource or template

        Returns:
            The added resource or template. If a resource or template with the same URI already exists,
            returns the existing resource or template.
        r   Context{}c                 3  s    | ]
}|j  ur|V  qd S )N)
annotation).0pr1   r   r   	<genexpr>\   s    
zCResourceManager.add_resource_or_template_from_fn.<locals>.<genexpr>ziInvalid resource or template definition due to a mismatch between URI parameters and function parameters.)
fastmcp.server.contextr2   anyinspect	signature
parametersvaluesadd_template_from_fnadd_resource_from_fnr   )	r   r&   r(   r*   r,   r-   r.   has_uri_paramshas_func_paramsr   r1   r    add_resource_or_template_from_fn@   s    z0ResourceManager.add_resource_or_template_from_fnr	   c                 C  6   t jrtjdtdd tj||||||d}| |S )a#  Add a resource to the manager from a function.

        Args:
            fn: The function to register as a resource
            uri: The URI for the resource
            name: Optional name for the resource
            description: Optional description of the resource
            mime_type: Optional MIME type for the resource
            tags: Optional set of tags for categorizing the resource

        Returns:
            The added resource. If a resource with the same URI already exists,
            returns the existing resource.
        zaadd_resource_from_fn is deprecated. Use Resource.from_function() and call add_resource() instead.   
stacklevel)r&   r(   r*   r,   r-   r.   )r   deprecation_warningswarningsr   DeprecationWarningr	   from_functionadd_resource)r   r&   r(   r*   r,   r-   r.   resourcer   r   r   r@   p   s   
z$ResourceManager.add_resource_from_fnrM   c                 C     | j |j}|r>| jdkrtd|j  || j |j< n | jdkr*|| j |j< n| jdkr7td|j | jdkr>|S || j |j< |S )a  Add a resource to the manager.

        Args:
            resource: A Resource instance to add. The resource's .key attribute
                will be used as the storage key. To overwrite it, call
                Resource.model_copy(key=new_key) before calling this method.
        r   zResource already exists: replaceerrorignore)r   getkeyr   loggerwarningr   )r   rM   existingr   r   r   rL      s   



zResourceManager.add_resourceuri_templater
   c                 C  rD   )z"Create a template from a function.ziadd_template_from_fn is deprecated. Use ResourceTemplate.from_function() and call add_template() instead.rE   rF   )rW   r*   r,   r-   r.   )r   rH   rI   r   rJ   r
   rK   add_template)r   r&   rW   r*   r,   r-   r.   templater   r   r   r?      s   
z$ResourceManager.add_template_from_fnrY   c                 C  rN   )a  Add a template to the manager.

        Args:
            template: A ResourceTemplate instance to add. The template's .key attribute
                will be used as the storage key. To overwrite it, call
                ResourceTemplate.model_copy(key=new_key) before calling this method.

        Returns:
            The added template. If a template with the same URI already exists,
            returns the existing template.
        r   zTemplate already exists: rO   rP   rQ   )r   rR   rS   r   rT   rU   r   )r   rY   rV   r   r   r   rX      s   



zResourceManager.add_templateAnyUrl | strboolc                   sT   t |}|  I dH }||v rdS |  I dH }|D ]}t||dur' dS qdS )zCheck if a resource exists.NTF)r)   r"   r%   r   )r   r(   uri_str	resources	templatestemplate_keyr   r   r   has_resource   s   zResourceManager.has_resourcec           
        s
  t |}tjdd|id |  I dH }|| }r|S |  I dH }| D ]T\}}t|| }dur}z|j||dI dH W   S  t	yY }	 z
t
d|	  |	d}	~	w ty| }	 zt
d|	  | jrptd|	td|	 |	d}	~	ww q)td| )	zGet resource by URI, checking concrete resources first, then templates.

        Args:
            uri: The URI of the resource to get

        Raises:
            NotFoundError: If no resource or template matching the URI is found.
        zGetting resourcer(   )extraNparamsz'Error creating resource from template: z%Error creating resource from templatezUnknown resource: )r)   rT   debugr"   rR   r%   itemsr   create_resourcer   rP   	Exceptionr   r   r   )
r   r(   r\   r]   rM   r^   storage_keyrY   rc   er   r   r   get_resource   s>   	
zResourceManager.get_resourcestr | bytesc                   s  t |}|| jv rZ| |I dH }z| I dH W S  ty0 } z
td| |d}~w tyY } ztd| | jrJtd||td|d| |d}~ww | j	
 D ]a\}}t|| }durz|j||dI dH }| I dH W   S  ty } z
td| |d}~w ty } ztd| | jrtd||td|d| |d}~ww q_td|d)zv
        Internal API for servers: Finds and reads a resource, respecting the
        filtered protocol path.
        NzError reading resource z: rb   z%Error reading resource from template z	Resource z not found.)r)   r   rj   readr   rT   	exceptionrg   r   r   re   r   rf   r   )r   r(   r\   rM   ri   rS   rY   rc   r   r   r   read_resource!  sf   
zResourceManager.read_resource)NN)r   r   r   r   )r   r   )r   r$   )NNNN)r&   r'   r(   r)   r*   r+   r,   r+   r-   r+   r.   r/   r   r0   )r&   r'   r(   r)   r*   r+   r,   r+   r-   r+   r.   r/   r   r	   )rM   r	   r   r	   )r&   r'   rW   r)   r*   r+   r,   r+   r-   r+   r.   r/   r   r
   )rY   r
   r   r
   )r(   rZ   r   r[   )r(   rZ   r   r	   )r(   rZ   r   rk   )__name__
__module____qualname____doc__r   r"   r%   rC   r@   rL   r?   rX   r`   rj   rn   r   r   r   r   r      s6    

4
(


-r   )rr   
__future__r   r;   rI   collections.abcr   typingr   pydanticr   fastmcpr   fastmcp.exceptionsr   r   fastmcp.resources.resourcer	   fastmcp.resources.templater
   r   fastmcp.settingsr   fastmcp.utilities.loggingr   ro   rT   r   r   r   r   r   <module>   s    