
    "<i3                        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          ZdS )zResource manager functionality.    )annotationsN)Callable)Any)AnyUrl)settings)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,d0dZ
d1d!Zd2d$Zd3d%Zd4d'ZdS )5ResourceManagerzManages FastMCP resources.Nduplicate_behaviorDuplicateBehavior | Nonemask_error_detailsbool | Nonec                    i | _         i | _        |pt          j        | _        |d}|t          j        vr2t          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   s      /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/fastmcp/resources/resource_manager.py__init__zResourceManager.__init__   s     0279"4"S8S %!'%6%???K/A K K#'99->-G#H#HK K   #5    returndict[str, Resource]c                .   K   t          | j                  S )z+Get all registered resources, keyed by URI.)dictr   r   s    r   get_resourceszResourceManager.get_resources8         DO$$$r   dict[str, ResourceTemplate]c                .   K   t          | j                  S )z4Get all registered templates, keyed by URI template.)r#   r   r$   s    r   get_resource_templatesz&ResourceManager.get_resource_templates<   r&   r   fnCallable[..., Any]uristrname
str | Nonedescription	mime_typetagsset[str] | NoneResource | ResourceTemplatec                >  	 ddl m	 d|v od|v }t          	fdt          j        |          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  .   K   | ]}|j         u|V  d S )N)
annotation).0pr6   s     r   	<genexpr>zCResourceManager.add_resource_or_template_from_fn.<locals>.<genexpr>\   s<       
 
|7** ****
 
r   ziInvalid resource or template definition due to a mismatch between URI parameters and function parameters.)
fastmcp.server.contextr6   anyinspect	signature
parametersvaluesadd_template_from_fnadd_resource_from_fnr   )
r   r*   r,   r.   r0   r1   r2   has_uri_paramshas_func_paramsr6   s
            @r    add_resource_or_template_from_fnz0ResourceManager.add_resource_or_template_from_fn@   s    . 	322222 2s
 
 
 
 
&r**5<<>>
 
 
 
 
  	_ 	,,C{It     	 	,,C{It   K  r   r
   c                    t           j        rt          j        dt          d           t          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.   r0   r1   r2   )r   deprecation_warningswarningsr   DeprecationWarningr
   from_functionadd_resource)r   r*   r,   r.   r0   r1   r2   resources           r   rE   z$ResourceManager.add_resource_from_fnp   sr    0 ( 	Ms"   
 )#
 
 
   ***r   rR   c                t   | j                             |j                  }|r| j        dk    r2t                              d|j                    || j         |j        <   nJ| j        dk    r|| j         |j        <   n/| 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   rR   existings      r   rQ   zResourceManager.add_resource   s     ?&&x|44 		 &&00I8<IIJJJ08--(I5508--(G33 !KX\!K!KLLL(H44(0%r   uri_templater   c                    t           j        rt          j        dt          d           t          j        ||||||          }|                     |          S )z"Create a template from a function.ziadd_template_from_fn is deprecated. Use ResourceTemplate.from_function() and call add_template() instead.rJ   rK   )r\   r.   r0   r1   r2   )r   rM   rN   r   rO   r   rP   add_template)r   r*   r\   r.   r0   r1   r2   templates           r   rD   z$ResourceManager.add_template_from_fn   sr     ( 	M{"   
 $1%#
 
 
   ***r   r_   c                t   | j                             |j                  }|r| j        dk    r2t                              d|j                    || j         |j        <   nJ| j        dk    r|| j         |j        <   n/| 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: rT   rU   rV   )r   rW   rX   r   rY   rZ   r   )r   r_   r[   s      r   r^   zResourceManager.add_template   s     ?&&x|44 		 &&00I8<IIJJJ08--(I5508--(G33 !KX\!K!KLLL(H44(0%r   AnyUrl | strboolc                   K   t          |          }|                                  d{V }||v rdS |                                  d{V }|D ]}t          ||           dS dS )zCheck if a resource exists.NTF)r-   r%   r)   r   )r   r,   uri_str	resources	templatestemplate_keys         r   has_resourcezResourceManager.has_resource   s      c(( ,,........	i4 5577777777	% 	 	L!'<88Dtt E ur   c                  K   t          |          }t                              dd|i           |                                  d{V }|                    |          x}r|S |                                  d{V }|                                D ]\  }}t          ||          x}	 |                    ||           d{V c S # t          $ r$}	t          
                    d|	            |	d}	~	wt          $ rL}	t          
                    d|	            | j        rt          d          |	t          d|	           |	d}	~	ww xY w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-   rY   debugr%   rW   r)   itemsr   create_resourcer	   rU   	Exceptionr   r   r   )
r   r,   rd   re   rR   rf   storage_keyr_   rl   es
             r   get_resourcezResourceManager.get_resource   s      c(('w/?@@@ ,,........	 }}W---8 	O 5577777777	%.__%6%6 	! 	!!K,WkBBBO!!)!9!9% ": " "         
 %   LL!N1!N!NOOOG  	! 	! 	!LL!N1!N!NOOO. !()PQQWXX )IaII  !	! P, :::;;;s%   )C
E
C11E
>AEE
str | bytesc                  K   t          |          }|| j        v r|                     |           d{V }	 |                                 d{V S # t          $ r$}t
                              d|           |d}~wt          $ rR}t
                              d|           | j        rt	          d|          |t	          d|d|           |d}~ww xY w| j	        
                                D ]\  }}t          ||          x}	 |                    ||           d{V }|                                 d{V c S # t          $ r$}t
                              d|           |d}~wt          $ rR}t
                              d|           | j        rt	          d|          |t	          d|d|           |d}~ww xY wt          d|d          )zv
        Internal API for servers: Finds and reads a resource, respecting the
        filtered protocol path.
        NzError reading resource z: rk   z%Error reading resource from template z	Resource z not found.)r-   r   rs   readr	   rY   	exceptionrp   r   r   rn   r   ro   r   )r   r,   rd   rR   rr   rX   r_   rl   s           r   read_resourcezResourceManager.read_resource!  s     
 c(( do%%!..w77777777H%]]__,,,,,,, !     !F7!F!FGGG  	 	 	  !F7!F!FGGG* '(M'(M(MNNTUU (B'BBqBB 	 "_2244 	! 	!MC,Wc:::G!%-%=%=gf%=%U%UUUUUUUH!)000000000$   $$KKK   G  ! ! !$$KKK   . !+OGOO  ! ,TGTTQRTT  !! H, >>>>???sI   A 
CA::CACC6E
GE//G<AG		G)NN)r   r   r   r   )r    r!   )r    r'   )NNNN)r*   r+   r,   r-   r.   r/   r0   r/   r1   r/   r2   r3   r    r4   )r*   r+   r,   r-   r.   r/   r0   r/   r1   r/   r2   r3   r    r
   )rR   r
   r    r
   )r*   r+   r\   r-   r.   r/   r0   r/   r1   r/   r2   r3   r    r   )r_   r   r    r   )r,   ra   r    rb   )r,   ra   r    r
   )r,   ra   r    rt   )__name__
__module____qualname____doc__r   r%   r)   rH   rE   rQ   rD   r^   rh   rs   rx    r   r   r   r      sE       $$ 8<*.5 5 5 5 58% % % %% % % %  "& $ $. . . . .h  "& $ $&+ &+ &+ &+ &+P   4  "& $ $+ + + + +6   4   "+< +< +< +<Z6@ 6@ 6@ 6@ 6@ 6@r   r   )r|   
__future__r   r@   rN   collections.abcr   typingr   pydanticr   fastmcpr   fastmcp.exceptionsr   r	   fastmcp.resources.resourcer
   fastmcp.resources.templater   r   fastmcp.settingsr   fastmcp.utilities.loggingr   ry   rY   r   r}   r   r   <module>r      sD   % % " " " " " "   $ $ $ $ $ $                   ; ; ; ; ; ; ; ; / / / / / /        / . . . . . 0 0 0 0 0 0	H		~@ ~@ ~@ ~@ ~@ ~@ ~@ ~@ ~@ ~@r   