
    k7i3                        S r SSKJr  SSKrSSKrSSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJrJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  \" \5      r " S S5      rg)zResource manager functionality.    )annotationsN)Callable)Any)AnyUrl)settings)NotFoundErrorResourceError)Resource)ResourceTemplatematch_uri_template)DuplicateBehavior)
get_loggerc                     \ rS rSrSr  S   SS jjrSS jrSS jr    S             SS jjr    S             SS jjr	SS	 jr
    S             SS
 jjrSS jrSS jrSS jrSS jrSrg)ResourceManager   zManages FastMCP resources.Nc                    0 U l         0 U l        U=(       d    [        R                  U l        Uc  SnU[        R
                  ;  a.  [        SU SSR                  [        R
                  5       35      eXl        g)zInitialize 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      \/home/james-whalen/.local/lib/python3.13/site-packages/fastmcp/resources/resource_manager.py__init__ResourceManager.__init__   s     0279"4"S8S8S %!'%6%?%??./A.B C##'99->-G-G#H"IK  #5    c                4   #    [        U R                  5      $ 7f)z+Get all registered resources, keyed by URI.)dictr   r   s    r   get_resourcesResourceManager.get_resources8        DOO$$   c                4   #    [        U R                  5      $ 7f)z4Get all registered templates, keyed by URI template.)r!   r   r"   s    r   get_resource_templates&ResourceManager.get_resource_templates<   r%   r&   c                N  ^	 SSK Jm	  SU;   =(       a    SU;   n[        U	4S j[        R                  " U5      R
                  R                  5        5       5      nU(       d  U(       a  U R                  XX4XV5      $ U(       d  U(       d  U R                  XX4XV5      $ [        S5      e)aC  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  J   >#    U  H  nUR                   TLd  M  Uv   M     g 7f)N)
annotation).0pr+   s     r   	<genexpr>CResourceManager.add_resource_or_template_from_fn.<locals>.<genexpr>\   s&      
>||7* A>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_fn0ResourceManager.add_resource_or_template_from_fn@   s    . 	3 2s
 
&&r*55<<>
 
 _,,I   ,,I  K r   c           	         [         R                  (       a  [        R                  " S[        SS9  [
        R                  " UUUUUUS9nU R                  U5      $ )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@   rA   )r   deprecation_warningswarningsr   DeprecationWarningr
   from_functionadd_resource)r   r<   r=   r>   r?   r@   rA   resources           r   r;   $ResourceManager.add_resource_from_fnp   sY    0 ((MMs"
 ))#
   **r   c                   U R                   R                  UR                  5      nU(       a  U R                  S:X  a;  [        R                  SUR                   35        XR                   UR                  '   OcU R                  S:X  a  XR                   UR                  '   O:U R                  S:X  a  [        SUR                   35      eU R                  S:X  a  U$ XR                   UR                  '   U$ )zAdd 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   rO   existings      r   rN   ResourceManager.add_resource   s     ??&&x||4&&&0!:8<<.IJ08-((I508-((G3 #<X\\N!KLL((H4(0%r   c           	         [         R                  (       a  [        R                  " S[        SS9  [
        R                  " UUUUUUS9nU R                  U5      $ )z"Create a template from a function.ziadd_template_from_fn is deprecated. Use ResourceTemplate.from_function() and call add_template() instead.rG   rH   )uri_templater>   r?   r@   rA   )r   rJ   rK   r   rL   r   rM   add_template)r   r<   r\   r>   r?   r@   rA   templates           r   r:   $ResourceManager.add_template_from_fn   sY     ((MM{"
 $11%#
   **r   c                   U R                   R                  UR                  5      nU(       a  U R                  S:X  a;  [        R                  SUR                   35        XR                   UR                  '   OcU R                  S:X  a  XR                   UR                  '   O:U R                  S:X  a  [        SUR                   35      eU R                  S:X  a  U$ XR                   UR                  '   U$ )aw  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: rR   rS   rT   )r   rU   rV   r   rW   rX   r   )r   r^   rY   s      r   r]   ResourceManager.add_template   s     ??&&x||4&&&0!:8<<.IJ08-((I508-((G3 #<X\\N!KLL((H4(0%r   c                   #    [        U5      nU R                  5       I Sh  vN nX#;   a  gU R                  5       I Sh  vN nU H  n[        X%5      (       d  M    g   g N> N"7f)zCheck if a resource exists.NTF)strr#   r(   r   )r   r=   uri_str	resources	templatestemplate_keys         r   has_resourceResourceManager.has_resource   sd     c( ,,..	 5577	%L!'88 &  /
 8s&   A$A A$A" A$A$"A$c                  #    [        U5      n[        R                  SSU0S9  U R                  5       I Sh  vN nUR	                  U5      =n(       a  U$ U R                  5       I Sh  vN nUR                  5        H2  u  pg[        X&5      =n(       d  M   UR                  UUS9I Sh  vN s  $    [        SU 35      e N NZ N! [         a  n	[        R                  SU	 35        U	eSn	A	f[         aI  n	[        R                  SU	 35        U R                  (       a  [        S5      U	e[        SU	 35      U	eSn	A	ff = f7f)	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: )rc   rW   debugr#   rU   r(   itemsr   create_resourcer	   rS   	Exceptionr   r   r   )
r   r=   rd   re   rO   rf   storage_keyr^   rm   es
             r   get_resourceResourceManager.get_resource   sP     c('w/?@ ,,..	 }}W--8-O 5577	%.__%6!K+GAAvA!!)!9!9% ": "  	 &70 0	:;;= /
 8

 % LL#J1#!NOG  	!LL#J1#!NO..()PQWXX )EaSI !	!sd   5EC1E)C*)EC+C,C/EEC
EC,,E9AD==EEc                  #    [        U5      nX R                  ;   a2  U R                  U5      I Sh  vN n UR                  5       I Sh  vN $ U R                  R                  5        HI  u  pV[        X%5      =n(       d  M   UR                  X'S9I Sh  vN nUR                  5       I Sh  vN s  $    [        SU< S35      e N N}! [         a   n[
        R                  SU< 35        UeSnAf[         aR  n[
        R                  SU< 35        U R                  (       a  [	        SU< 35      Ue[	        SU< SU 35      UeSnAff = f N N! [         a   n[
        R                  SU< 35        UeSnAf[         aR  n[
        R                  SU< 35        U R                  (       a  [	        SU< 35      Ue[	        SU< SU 35      UeSnAff = f7f)z^
Internal API for servers: Finds and reads a resource, respecting the
filtered protocol path.
NzError reading resource z: rl   z%Error reading resource from template z	Resource z not found.)rc   r   rt   readr	   rW   	exceptionrq   r   r   ro   r   rp   r   )r   r=   rd   rO   rs   rV   r^   rm   s           r   read_resourceResourceManager.read_resource!  s    
 c( oo%!..w77H%]]_,,( "__224MC+G99v9!%-%=%=g%=%UUH!)00	 5. i{+>??[ 8, !   #:7+!FG  	  #:7+!FG**'*A'(MNTUU (1'BqcB	  V0$ $$?{K G  !$$?{K ..+CG;O ! ,CG;bQRPST !!s   /GCGC
 
CC
 0GEEE-E.E1GC
 

EC//E<AE		EGEE
GE::GAGGG)r   r   r   r   )NN)r   zDuplicateBehavior | Noner   zbool | None)returnzdict[str, Resource])r{   zdict[str, ResourceTemplate])NNNN)r<   Callable[..., Any]r=   rc   r>   
str | Noner?   r}   r@   r}   rA   set[str] | Noner{   zResource | ResourceTemplate)r<   r|   r=   rc   r>   r}   r?   r}   r@   r}   rA   r~   r{   r
   )rO   r
   r{   r
   )r<   r|   r\   rc   r>   r}   r?   r}   r@   r}   rA   r~   r{   r   )r^   r   r{   r   )r=   AnyUrl | strr{   bool)r=   r   r{   r
   )r=   r   r{   zstr | bytes)__name__
__module____qualname____firstlineno____doc__r   r#   r(   rD   r;   rN   r:   r]   rh   rt   ry   __static_attributes__ r   r   r   r      sP   $ 8<*.545 (58%%  "& $ $.. . 	.
  . . . 
%.h  "& $ $&+&+ &+ 	&+
  &+ &+ &+ 
&+P4  "& $ $++ + 	+
  + + + 
+64"+<Z6@r   r   )r   
__future__r   r6   rK   collections.abcr   typingr   pydanticr   fastmcpr   fastmcp.exceptionsr   r	   fastmcp.resources.resourcer
   fastmcp.resources.templater   r   fastmcp.settingsr   fastmcp.utilities.loggingr   r   rW   r   r   r   r   <module>r      sH    % "   $    ; / / 0	H	~@ ~@r   