
    Ki2                        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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.    )annotationsN)Callable)Any)AnyUrl)settings)FastMCPErrorNotFoundErrorResourceError)Resource)ResourceTemplatematch_uri_template)DuplicateBehavior)
get_loggerc                      e Zd ZdZ	 	 d	 	 	 ddZddZddZ	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZ		 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Z
dd
ZddZddZddZy)ResourceManagerzManages FastMCP resources.Nc                    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   mask_error_detailsr   __args__
ValueErrorjoinduplicate_behavior)selfr   r   s      n/home/jay/workspace/scripts/.codegraph-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    c                4   K   t        | j                        S w)z+Get all registered resources, keyed by URI.)dictr   r   s    r   get_resourceszResourceManager.get_resources8        DOO$$   c                4   K   t        | j                        S w)z4Get all registered templates, keyed by URI template.)r    r   r!   s    r   get_resource_templatesz&ResourceManager.get_resource_templates<   r#   r$   c                   	 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>\   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   fnurinamedescription	mime_typetagshas_uri_paramshas_func_paramsr(   s
            @r    add_resource_or_template_from_fnz0ResourceManager.add_resource_or_template_from_fn@   s    . 	3 2s
 
&&r*55<<>
 
 _,,C{It   ,,C{It  K r   c                    t         j                  r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)r8   r9   r:   r;   r<   r=   )r   deprecation_warningswarningsr   DeprecationWarningr   from_functionadd_resource)r   r8   r9   r:   r;   r<   r=   resources           r   r7   z$ResourceManager.add_resource_from_fnp   sY    0 ((MMs"
 ))#
   **r   c                   | j                   j                  |j                        }|r| j                  dk(  r<t        j                  d|j                          || j                   |j                  <   na| j                  dk(  r|| j                   |j                  <   n8| j                  dk(  rt        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   rJ   existings      r   rI   zResourceManager.add_resource   s     ??&&x||4&&&0!:8<<.IJ08-((I508-((G3 #<X\\N!KLL((H4(0%r   c                    t         j                  r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.rB   rC   )uri_templater:   r;   r<   r=   )r   rE   rF   r   rG   r   rH   add_template)r   r8   rU   r:   r;   r<   r=   templates           r   r6   z$ResourceManager.add_template_from_fn   sY     ((MM{"
 $11%#
   **r   c                   | j                   j                  |j                        }|r| j                  dk(  r<t        j                  d|j                          || j                   |j                  <   na| j                  dk(  r|| j                   |j                  <   n8| j                  dk(  rt        d|j                         | j                  dk(  r|S || j                   |j                  <   |S )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: rL   rM   rN   )r   rO   rP   r   rQ   rR   r   )r   rW   rS   s      r   rV   zResourceManager.add_template   s     ??&&x||4&&&0!:8<<.IJ08-((I508-((G3 #<X\\N!KLL((H4(0%r   c                   K   t        |      }| j                          d{   }||v ry| j                          d{   }|D ]  }t        ||       y y7 77 w)zCheck if a resource exists.NTF)strr"   r&   r   )r   r9   uri_str	resources	templatestemplate_keys         r   has_resourcezResourceManager.has_resource   so     c( ,,..	i 5577	% 	L!'<8D	  /
 8s%   AAAAAAAc                >  K   t        |      }t        j                  dd|i       | j                          d{   }|j	                  |      x}r|S | j                          d{   }|j                         D ]1  \  }}t        ||      x}	 |j                  ||       d{   c S  t        d|       7 7 X7 # t        $ r  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 resourcer9   )extraNparamsz'Error creating resource from template: z%Error creating resource from templatezUnknown resource: )rZ   rQ   debugr"   rO   r&   itemsr   create_resourcer   	ExceptionrM   r   r   r	   )
r   r9   r[   r\   rJ   r]   storage_keyrW   rc   es
             r   get_resourcezResourceManager.get_resource   sE     c('w/?@ ,,..	 }}W--8-O 5577	%.__%6 	!!K,WkBBO!!)!9!9% ": "  		!. 0	:;;; /
 8

 $   	!LL#J1#!NO..()PQWXX )EaSI !	!sW   8DB>,D'C ((DC(C)C,D DCD?DDDc                  K   t        |      }|| j                  v r2| j                  |       d{   }	 |j                          d{   S | j                  j                         D ]I  \  }}t        ||      x}	 |j                  ||       d{   }|j                          d{   c S  t        d|d      7 7 {# t        $ r  t
        $ rJ}t        j                  d|       | j                  rt        d|      |t        d|d|       |d}~ww xY w7 7 {# t        $ r  t
        $ rJ}t        j                  d|       | j                  rt        d|      |t        d|d|       |d}~ww xY ww)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.)rZ   r   rj   readr   rg   rQ   	exceptionr   r
   r   re   r   rf   r	   )r   r9   r[   rJ   ri   rP   rW   rc   s           r   read_resourcezResourceManager.read_resource   s    
 c( doo%!..w77H%]]_,,& "__224 	!MC,Wc::G!%-%=%=gf%=%UUH!)00		!( i{+>??S 8,     	  #:7+!FG**'*A'(MNTUU (1'BqcB	  V0#   !$$?{K ..+CG;O ! ,CG;bQRPST !!s   .F	CF	C 	C
C /F	>D*D&D*,D(-D*0F	C D#ADD##F	&D*(D**F<AFFF	)NN)r   zDuplicateBehavior | Noner   zbool | None)returnzdict[str, Resource])ro   zdict[str, ResourceTemplate])NNNN)r8   Callable[..., Any]r9   rZ   r:   
str | Noner;   rq   r<   rq   r=   set[str] | Nonero   zResource | ResourceTemplate)r8   rp   r9   rZ   r:   rq   r;   rq   r<   rq   r=   rr   ro   r   )rJ   r   ro   r   )r8   rp   rU   rZ   r:   rq   r;   rq   r<   rq   r=   rr   ro   r   )rW   r   ro   r   )r9   AnyUrl | strro   bool)r9   rs   ro   r   )r9   rs   ro   zstr | bytes)__name__
__module____qualname____doc__r   r"   r&   r@   r7   rI   r6   rV   r_   rj   rn    r   r   r   r      sP   $ 8<*.545 (58%%  "& $ $.. . 	.
  . . . 
%.h  "& $ $&+&+ &+ 	&+
  &+ &+ &+ 
&+P4  "& $ $++ + 	+
  + + + 
+64"*<X2@r   r   )rx   
__future__r   r2   rF   collections.abcr   typingr   pydanticr   fastmcpr   fastmcp.exceptionsr   r	   r
   fastmcp.resources.resourcer   fastmcp.resources.templater   r   fastmcp.settingsr   fastmcp.utilities.loggingr   ru   rQ   r   ry   r   r   <module>r      sK    % "   $    I I / / 0	H	y@ y@r   