
    ~h3.                         d 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      Zy)zResource manager functionality.    N)Callable)Any)AnyUrl)settings)NotFoundErrorResourceError)Resource)ResourceTemplatematch_uri_template)DuplicateBehavior)
get_loggerc                      e Zd ZdZ	 	 ddedz  dedz  fdZ	 	 	 	 ddedef   de	d	e	dz  d
e	dz  de	dz  de
e	   dz  deez  fdZ	 	 	 	 ddedef   de	d	e	dz  d
e	dz  de	dz  de
e	   dz  defdZddede	dz  defdZ	 	 	 	 ddedef   de	d	e	dz  d
e	dz  de	dz  de
e	   dz  defdZ	 ddede	dz  defdZdee	z  defdZdee	z  defdZdee	z  de	ez  fdZdee	ef   fdZdee	ef   fdZy)ResourceManagerzManages FastMCP resources.Nduplicate_behaviormask_error_detailsc                     i | _         i | _        |xs t        j                  | _        |d}|t        j
                  vr.t        d| ddj                  t        j
                               || _        y)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   s      ^/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/fastmcp/resources/resource_manager.py__init__zResourceManager.__init__   s     0279"4"S8S8S %!'%6%?%??./A.B C##'99->-G-G#H"IK  #5    fn.urinamedescription	mime_typetagsreturnc                    	 ddl m	 d|v xr d|v }t        	fdt        j                  |      j
                  j                         D              }|s|r| j                  ||||||      S |s|s| j                  ||||||      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   >   K   | ]  }|j                   ur|  y wN)
annotation).0pr%   s     r   	<genexpr>zCResourceManager.add_resource_or_template_from_fn.<locals>.<genexpr>R   s%      
||7* 
s   ziInvalid resource or template definition due to a mismatch between URI parameters and function parameters.)
fastmcp.server.contextr%   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%   s
            @r    add_resource_or_template_from_fnz0ResourceManager.add_resource_or_template_from_fn6   s    . 	3 2s
 
&&r*55<<>
 
 _,,C{It   ,,C{It  K r   c                     t        j                  dt        d       t        j                  ||||||      }| j                  |      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"   )warningsr   DeprecationWarningr	   from_functionadd_resource)r   r   r   r   r    r!   r"   resources           r   r5   z$ResourceManager.add_resource_from_fnf   sP    0 	o	

 ))#
   **r   rA   keyc                    |xs t        |j                        }t        j                  d|j                  |t	        |      j
                  |j                  d       | j                  j                  |      }|r| j                  dk(  r(t        j                  d|        || j                  |<   nM| j                  dk(  r|| j                  |<   n.| j                  dk(  rt        d|       | j                  dk(  r|S || j                  |<   |S )	zAdd a resource to the manager.

        Args:
            resource: A Resource instance to add
            key: Optional URI to use as the storage key (if different from resource.uri)
        zAdding resource)r   storage_keytyperesource_nameextrar   zResource already exists: replaceerrorignore)strr   loggerdebugrE   __name__r   r   getr   warningr   )r   rA   rB   rD   existings        r   r@   zResourceManager.add_resource   s     .S.||*X//!)	 	 	
 ??&&{3&&&0!:;-HI/7,((I5/7,((G3 #<[M!JKK((H4'/$r   uri_templatec                     t        j                  dt        d       t        j                  ||||||      }| j                  |      S )z"Create a template from a function.ziadd_template_from_fn is deprecated. Use ResourceTemplate.from_function() and call add_template() instead.r:   r;   )rS   r   r    r!   r"   )r=   r   r>   r
   r?   add_template)r   r   rS   r   r    r!   r"   templates           r   r4   z$ResourceManager.add_template_from_fn   sP     	w	

 $11%#
   **r   rV   c                    t        |j                        }|xs |}t        j                  d||t	        |      j
                  |j                  d       | j                  j                  |      }|r| j                  dk(  r(t        j                  d|        || j                  |<   nM| j                  dk(  r|| j                  |<   n.| j                  dk(  rt        d|       | j                  dk(  r|S || j                  |<   |S )	ag  Add a template to the manager.

        Args:
            template: A ResourceTemplate instance to add
            key: Optional URI template to use as the storage key (if different from template.uri_template)

        Returns:
            The added template. If a template with the same URI already exists,
            returns the existing template.
        zAdding template)rS   rD   rE   template_namerG   r   zTemplate already exists: rI   rJ   rK   )rL   rS   rM   rN   rE   rO   r   r   rP   r   rQ   r   )r   rV   rB   uri_template_strrD   rR   s         r   rU   zResourceManager.add_template   s     x445-- 0*X//!)	 	 	
 ??&&{3&&&0!:;-HI/7,((I5/7,((G3 #<[M!JKK((H4'/$r   c                     t        |      }|| j                  v ry| j                  j                         D ]  }t	        ||      s y y)zCheck if a resource exists.TF)rL   r   r   keysr   )r   r   uri_strtemplate_keys       r   has_resourcezResourceManager.has_resource   sH    c(doo% OO002 	L!'<8	 r   c                 8  K   t        |      }t        j                  dd|i       | j                  j	                  |      x}r|S | j
                  j                         D ]1  \  }}t        ||      x}s	 |j                  ||       d{   c S  t        d|       7 # t        $ r}t        j                  d|        |d}~wt        $ rD}t        j                  d|        | j                  rt        d      |t        d|       |d}~ww xY ww)	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   rG   )paramsNz'Error creating resource from template: z%Error creating resource from templatezUnknown resource: )rL   rM   rN   r   rP   r   itemsr   create_resourcer   rJ   	Exceptionr   r   r   )r   r   r\   rA   rD   rV   r`   es           r   get_resourcezResourceManager.get_resource   s:     c('w/?@ **73383O &*__%:%:%< 	!!K+G[AAvA!!)!9!9% ": "  		!0 0	:;;)
 % LL#J1#!NOG  	!LL#J1#!NO..()PQWXX )EaSI !	!sH   A2D6B$B"B$D"B$$	D-CD?DDDc                 z  K   | j                  |       d{   }	 |j                          d{   S 7 7 # t        $ r"}t        j	                  d|d|        |d}~wt
        $ rM}t        j	                  d|d|        | j                  rt        d|      |t        d|d|       |d}~ww xY ww)zRead a resource contents.NzError reading resource z: )re   readr   rM   rJ   rc   r   )r   r   rA   rd   s       r   read_resourcezResourceManager.read_resource!  s     **3//	S!(( 0 )  	LL23'A3?@G  	SLL23'A3?@&&#&=cW$EFAM $&=cWBqc$JKQRR	SsG   B;5B;9 79 B;9 	B8AB8+AB33B88B;c                     | j                   S )z+Get all registered resources, keyed by URI.)r   r   s    r   get_resourceszResourceManager.get_resources7      r   c                     | j                   S )z4Get all registered templates, keyed by URI template.)r   rj   s    r   get_templateszResourceManager.get_templates;  rl   r   )NN)NNNNr)   )rO   
__module____qualname____doc__r   boolr   r   r   rL   setr	   r
   r8   r5   r@   r4   rU   r   r^   re   bytesrh   dictrk   rn    r   r   r   r      sR   $ 8<*.5-45 !4K5@  "& $ $.S#X. . Dj	.
 4Z. :. #ho. 
$	$.h  "& $ $%+S#X%+ %+ Dj	%+
 4Z%+ :%+ #ho%+ 
%+NX C$J ( F  "& $ $+S#X+ + Dj	+
 4Z+ :+ #ho+ 
+6 =A$($/2Tz$	$L  )<fsl )<x )<VSv| Se S,tCM2 tC)9$9: r   r   )rq   r0   r=   collections.abcr   typingr   pydanticr   fastmcpr   fastmcp.exceptionsr   r   fastmcp.resources.resourcer	   fastmcp.resources.templater
   r   fastmcp.settingsr   fastmcp.utilities.loggingr   rO   rM   r   rv   r   r   <module>r      sC    %   $    ; / / 0	H	f fr   