
    F2jQ                     L   d dl Z d dlZd dlZd dlmZmZmZmZ d dlm	Z	m
Z
mZmZ d dlmZ erddlmZ d dlmZmZ ej        dk     rd d	lmZ nd d	lmZ dd
lmZm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#  edd          Z$ e j%        d          Z&	  G d d          Z'dS )    N)TYPE_CHECKINGAnyOptionalTypeVar)ParseResultSplitResultquoteunquote)etree   )	DAVClient)	CoroutineSequence)      )Self)cdavdav)BaseElement)error)errmsg)to_wire)URL_CCCalendarObjectResource)boundcaldavc                      e Zd ZU dZdZedz  ed<   dZedz  ed<   dZ	e
d         ed<   dZe
d          ed<   	 	 	 	 	 	 d9de
d         deez  ez  ez  dz  de
d          dedz  dedz  d	dfd
Zed	efd            Zed	efd            Z	 d:dedz  d	dfdZd Zd Zd	eez  fdZd Z	 d;dee         dz  ded	dfdZ	 d;dee         dz  defdZ	 	 	 	 	 d<	 d=dZ	 	 	 	 	 d<dZ	 d>deded	d fd!Z ded	edz  fd"Z!d#e"d	e"fd$Z#	 	 	 	 d?dee         dz  ded&ed'ed	d(f
d)Z$d* Z%	 	 	 	 d?dee         dz  ded&ed'efd+Z&d:de'dz  d	d,fd-Z(d	e)fd.Z*d	e)fd/Z+d	e)fd0Z,d@d2Z-dAd3Z.dAd4Z/dBd5Z0ed	edz  fd6            Z1d	efd7Z2d	efd8Z3dS )C	DAVObjectz
    Base class for all DAV objects.  Can be instantiated by a client
    and an absolute or relative URL, or from the parent object.
    Nidurlr   clientparentnamereturnc                 x   |	||j         }|| _         || _        || _        |pi | _        || _        |
|| j        d<   |r"|r |j                            |          | _        n#|d| _        nt          j        |          | _        | j        -dt          | j                  v rt          d| j                  dS dS )a  
        Default constructor.

        Args:
          client: A DAVClient instance
          url: The url for this object.  May be a full URL or a relative URL.
          parent: The parent object - used when creating objects
          id: The resource id (UID for an Event)
          props: a dict with known properties for this object
          name: Display name (deprecated, use props={'{DAV:}displayname': name})
        Nz{DAV:}displayname zURL must not contain spaces: )r"   r#   r    propsextra_init_optionsr!   joinr   	objectifystr
ValueError)selfr"   r!   r#   r    r(   r$   extras           X/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/caldav/davobject.py__init__zDAVObject.__init__4   s    , >f0]F[b
"' .2DJ*+ 	*c 	*zs++DHH[DHH}S))DH8C3tx==$8$8ITXIIJJJ  $8$8    c                 z    | j         t          d          t          | j                                                   S )N"Unexpected value None for self.url)r!   r-   r,   	canonicalr.   s    r0   canonical_urlzDAVObject.canonical_url_   s5    8ABBB48%%''(((r2   c                 J    | j         dS ddlm} t          | j         |          S )zCheck if this object is connected to an async client.

        Returns:
            True if the client is an AsyncDAVClient, False otherwise.
        NFr   )AsyncDAVClient)r"   caldav.async_davclientr9   
isinstance)r.   r9   s     r0   is_async_clientzDAVObject.is_async_cliente   s5     ;5999999$+~666r2   typezLlist[tuple[URL, Any, Any]] | Coroutine[Any, Any, list[tuple[URL, Any, Any]]]c                 ,   ddl m} d}| j        t          d          t	          j                    g}t	          j                    g}||z   }| j        r|                     |||          S | 	                    ||          }| 
                    ||          S )a  List children, using a propfind (resourcetype) on the parent object,
        at depth = 1.

        TODO: This is old code, it's querying for DisplayName and
        ResourceTypes prop and returning a tuple of those.  Those two
        are relatively arbitrary.  I think it's mostly only calendars
        having DisplayName, but it may make sense to ask for the
        children of a calendar also as an alternative way to get all
        events?  It should be redone into a more generic method, and
        it should probably return a dict rather than a tuple.  We
        should also look over to see if there is any code duplication.
        r   CalendarSetNr4   )
collectionr@   r!   r-   r   DisplayNameResourceTyper<   _async_children_query_properties_children_post_process)r.   r=   r@   depthr(   
multipropsprops_multipropsresponses           r0   childrenzDAVObject.childrenr   s      	,+++++8ABBB""#&(()
 :- 	G''.>FFF))*:EBB**4:::r2   c                    ddl m} g }t          j                    g}t          j                    g}|                    ||          }|D ](}||         t          j        j                 }	||         t          j        j                 }
|||	v rt          |          }|j        t          |          }t          | |          r|t          j        j        k    sk| j                                                                        | j                            |                                                                          k    r0|                    | j                            |          |	|
f           *|S )Nr   r?   )r(   multi_value_props)rA   r@   r   rB   rC   expand_simple_propstagr   hostnamer	   r;   r   Calendarr!   r5   strip_trailing_slashr*   append)r.   type_rJ   r@   cr(   rH   
propertiespathresource_typesresource_namer!   s               r0   rF   z DAVObject._children_post_process   sa   ++++++""#&(()
11Q[1\\
 	S 	SD'-c.>.BCN&t,S_-@AM} 7 7$ii<' ;;D t[11 Set}?P6P6PH&&((==??x}}T**4466KKMMN N HHdhmmD11>=QRRR r2   c                 j   K   |                      ||           d {V }|                     ||          S N)rE   rF   )r.   r=   rI   rG   rJ   s        r0   rD   zDAVObject._async_children   sD      //0@%HHHHHHHH**4:::r2   c                     |rFt          |d          r4t          j        |                                ddt          j                  S |S dS )z=Serialize a DAV element (or raw bytes/str) to a request body.
xmlelementzutf-8T)encodingxml_declarationpretty_print )hasattrr   tostringr]   r   debug_dump_communication)r.   roots     r0   _build_xml_bodyzDAVObject._build_xml_body   sZ     	t\** ~OO%%$$(!&!?	    Krr2   c                     |?t          |          dk    r,t          j                    |z   }t          j                    |z   S dS )zBBuild a Propfind XML element from a list of props, or return None.Nr   )lenr   PropPropfind)r.   r(   props      r0   _build_propfind_rootzDAVObject._build_propfind_root   s<    Ua8::%D<>>D((tr2   r   r(   rG   Any | Coroutine[Any, Any, Any]c                     | j         r|                     ||          S |                     |                     |          |          S )a  
        This is an internal method for doing a propfind query.  It's a
        result of code-refactoring work, attempting to consolidate
        similar-looking code into a common method.

        For async clients, returns a coroutine that must be awaited.
        )r<   _async_query_properties_queryrl   r.   r(   rG   s      r0   rE   zDAVObject._query_properties   sG      	>//u==={{444U;;UCCCr2   c                 d   K   |                      |                     |          |           d{V S )z*Async implementation of _query_properties.N)rp   rl   rq   s      r0   ro   z!DAVObject._async_query_properties   s<       [[!:!:5!A!A5IIIIIIIIIr2   propfindc                 :   | j         r|                     |||||          S |                     |          }|| j        } t	          | j        |          |||          }|j        dk    r!t          j        t          |                    ||j        |k    s|j        dk    rxt          |          }|j        dk    r7d|vr3d|v r/|                    dd          }|                     |||||          S t          j        |         t          |                    |S )a  
        This is an internal method for doing a query.  It's a
        result of code-refactoring work, attempting to consolidate
        similar-looking code into a common method.

        For async clients, returns a coroutine that must be awaited.
        N      	   D:getetag   <C:calendar-data   <D:getetag/><C:calendar-data)r<   _async_queryrf   r!   getattrr"   statusr   NotFoundErrorr   r   replacerp   exception_by_methodr.   re   rG   query_methodr!   expected_return_valuebodyrets           r0   rp   zDAVObject._query   s0     	\$$T5,EZ[[[##D));(C0gdk<00dEBB:%fSkk222!-#*@U2U2UZ3 4==DzS  \%=%=BUY]B]B]||$79XYY{{4cCXYYY+L9&++FFF
r2   c                   K   |                      |          }|| j        } t          | j        |          |||           d{V }|j        dk    r!t          j        t          |                    ||j        |k    s|j        dk    r~t          |          }|j        dk    r=d|vr9d|v r5|	                    dd          }| 
                    |||||           d{V S t          j        |         t          |                    |S )zAsync implementation of _query.Nru   rv   rw   rx   ry   rz   )rf   r!   r|   r"   r}   r   r~   r   r   r   rp   r   r   s           r0   r{   zDAVObject._async_query  s-      ##D));(C6GDK66sD%HHHHHHHH:%fSkk222!-#*@U2U2UZ34==DzS  \%=%=BUY]B]B]||$79XYY![[ulCI^_________+L9&++FFF
r2   Frk   
use_cached,str | None | Coroutine[Any, Any, str | None]c                     |r8|j         | j        v r*| j                            | j        |j                            S | j        r | j        |fi |S  | j        |gfi |                    |j         d          S )a  
        Wrapper for the :class:`get_properties`, when only one property is wanted

        Args:

         prop: the property to search for
         use_cached: don't send anything to the server if we've asked before

        TODO: use_cached default is False, should probably be True?

        Other parameters are sent directly to the :class:`get_properties` method

        For async clients, returns a coroutine that must be awaited.
        N)rO   r(   r"   _value_or_coroutiner<   _async_get_propertyget_propertiesget)r.   rk   r   passthroughs       r0   get_propertyzDAVObject.get_property   s    "  	I$(dj00;224:dh3GHHH 	A+4+D@@K@@@"t"D699[99==dhMMMr2   c                 b   K    | j         |gfi | d{V                     |j        d          S )z%Async implementation of get_property.N)r   r   rO   )r.   rk   r   s      r0   r   zDAVObject._async_get_property7  sH      )d)4&@@K@@@@@@@@EEdhPTUUUr2   rV   c           	         ddl m} | j        t          d          t	          | j        j                  }|                    d          r|dd         }n|dz   }||v r
||         }nA||v r;t          | |          s!t          	                    d| d| d	           ||         }n| j        |v r|| j                 }nd
|v r|                    d
          r	|d
         }nd|v r5|
                    dd          |v r||
                    dd                   }nt          |          dk    r(t          |                                          d         }nUt          	                    d| dt          |                                                      t          j        d           i S | j                            |           |S )a  Resolve the correct property dict from a PROPFIND response.

        Servers may return hrefs that don't exactly match the request-URI.
        RFC 4918, Section 9.1: "Clients MUST be able to handle the case
        where the href in the response does not match the request-URI."

        This method handles various known mismatches (trailing slashes,
        double slashes, iCloud quirks) and returns the resolved property
        dict, also updating self.props as a side effect.
        r   )	PrincipalNr4   /z	The path z& was not found in the properties, but z? was. This may indicate a server bug or a trailing slash issue.z/principal/z//r   z&Path handling problem. Path expected: z, paths found: F)rA   r   r!   r-   r
   rW   endswithr;   logwarningr   rh   listvalueskeysr   assert_r(   update)r.   rV   r   rW   exchange_pathrcs         r0   _resolve_propertieszDAVObject._resolve_properties;  s!   	
 	
 	
 	
 	
 	
 8ABBBtx}%%== 	' "IMM 3JM:D!BBj((dI.. P P PM P P P   M*BBX##DH%BBj((T]]=-I-I( M*BBT\\dll455CCDLLs334BB__!!
 j''))**1-BBKK: : : $Z__%6%6 7 7: :   M%   I
"	r2   Tparse_response_xmlparse_propsz,dict | Any | Coroutine[Any, Any, dict | Any]c                     | j         r|                     ||||          S |                     ||          }|                     ||||          S )a  Get properties (PROPFIND) for this object.

        With parse_response_xml and parse_props set to True a
        best-attempt will be done on decoding the XML we get from the
        server - but this works only for properties that don't have
        complex types.  With parse_response_xml set to False, a
        DAVResponse object will be returned, and it's up to the caller
        to decode.  With parse_props set to false but
        parse_response_xml set to true, xml elements will be returned
        rather than values.

        Args:
         props: ``[dav.ResourceType(), dav.DisplayName(), ...]``

        Returns:
          ``{proptag: value, ...}``

        For async clients, returns a coroutine that must be awaited.
        )r<   _async_get_propertiesrE   _post_get_propertiesr.   r(   rG   r   r   rJ   s         r0   r   zDAVObject.get_propertiesu  s\    4  	]--eU<NP[\\\))%77((5:LkZZZr2   c                 \   |s|S |rR|j         rKi }|j         D ]@}i }|r|D ]}|j        r
d||j        <   |                    |j                   |||j        <   An,|s|                                }n|                    |          }t          j        |           | 	                    |          S )zDShared post-processing for get_properties and _async_get_properties.N)
resultsrO   r   rV   href_find_objects_and_propsrN   r   r   r   )	r.   rJ   r(   r   r   rV   resultresult_propsrk   s	            r0   r   zDAVObject._post_get_properties  s    ! 	O  	=8+ 	=J"* 	7 	7! : % : :8 :59L2##F$5666*6
6;''	7  	=!99;;JJ "55e<<Jj!!!''
333r2   c                 n   K   |                      ||           d{V }|                     ||||          S )z'Async implementation of get_properties.N)rE   r   r   s         r0   r   zDAVObject._async_get_properties  sJ       //u========((5:LkZZZr2   z Self | Coroutine[Any, Any, Self]c                    |g n|}t          j                    |z   }t          j                    |z   }t          j                    |z   }|                     |          }| j        t          d          | j        t          d          | j        r| 	                    |          S | 
                    | j                            t          | j                  |                    S )z
        Set properties (PROPPATCH) for this object.

         * props = [dav.DisplayName('name'), ...]

        For async clients, returns a coroutine that must be awaited.

        Returns:
         * self
        Nr4   %Unexpected value None for self.client)r   ri   SetPropertyUpdaterf   r!   r-   r"   r<   _async_set_properties_post_set_properties	proppatchr,   )r.   r(   rk   set_elemre   r   s         r0   set_propertieszDAVObject.set_properties  s     mxzzE!799t#!##h.##D))8ABBB;DEEE 	4--d333(()>)>s48}}d)S)STTTr2   c                 ^    |j         dk    r!t          j        t          |                    | S )Nrv   )r}   r   PropsetErrorr   r.   rs     r0   r   zDAVObject._post_set_properties  s)    8s??$VAYY///r2   c                    K   |                      | j                            t          | j                  |           d{V           S )z'Async implementation of set_properties.N)r   r"   r   r,   r!   )r.   r   s     r0   r   zDAVObject._async_set_properties  sG      ((t{/D/DS]]TX/Y/Y)Y)Y)Y)Y)Y)YZZZr2   c                     t                      )z
        Save the object. This is an abstract method, that all classes
        derived from DAVObject implement.

        Returns:
         * self
        )NotImplementedErrorr6   s    r0   savezDAVObject.save  s     "###r2    None | Coroutine[Any, Any, None]c                     | j         dS | j        t          d          | j        r|                                 S |                     | j                            t          | j                                        dS )a	  
        Delete the object.

        For sync clients, deletes and returns None.
        For async clients, returns a coroutine that must be awaited.

        Example (sync):
            obj.delete()

        Example (async):
            await obj.delete()
        Nr   )r!   r"   r-   r<   _async_delete_post_deletedeleter,   r6   s    r0   r   zDAVObject.delete  sw     8F;DEEE 	(%%'''$+,,S]];;<<<<<r2   c                 Z    |j         dvr!t          j        t          |                    d S )N)      ru   )r}   r   DeleteErrorr   r   s     r0   r   zDAVObject._post_delete  s.    8?**#F1II... +*r2   c                    K   |                      | j                            t          | j                             d{V            dS )zAsync implementation of delete.N)r   r"   r   r,   r!   r6   s    r0   r   zDAVObject._async_delete  sJ       2 23tx== A AAAAAAABBBBBr2   c                 R    |                      t          j                    d          S )zB
        Get display name (calendar, principal, ...more?)
        Tr   )r   r   rB   r6   s    r0   get_display_namezDAVObject.get_display_name	  s%       !2!2t DDDr2   c                 b    t          j        dt          d           |                                 S )z
        Display name of this DAV object.

        .. deprecated:: 3.0
            Use :meth:`get_display_name` instead.
        zCThe 'name' attribute is deprecated. Use get_display_name() instead.   )
stacklevel)warningswarnDeprecationWarningr   r6   s    r0   r$   zDAVObject.name  s:     	Q	
 	
 	
 	

 $$&&&r2   c                     	 t          |                     t          j                    d                    p| j        S # t
          $ r t          | j                  cY S w xY w)NTr   )r,   r   r   rB   r!   	Exceptionr6   s    r0   __str__zDAVObject.__str__  sh    	!t(():):t(LLMMYQUQYY 	! 	! 	!tx==   	!s   ;> AAc                 0    | j         j        d| j        dS )N())	__class____name__r!   r6   s    r0   __repr__zDAVObject.__repr__$  s    >222DHHH==r2   )NNNNNNr[   )Nr   )Nr   rs   NN)r%   rm   )F)Nr   TT)r%   r   )r%   N)r%   r   )4r   
__module____qualname____doc__r    r,   __annotations__r!   r   r"   r   r#   r   r   r1   propertyr7   boolr<   rK   rF   rD   bytesrf   rl   r   r   intrE   ro   rp   r{   r   r   dictr   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r   r    r2   r0   r   r   )   sf         
 Bd
Ct$(FH[!((($(FH[!((( )-<@(,)K )K%)K ;,s2T9)K %	)K
 $J)K Dj)K 
)K )K )K )KV )s ) ) ) X)
 
7 
7 
7 
7 X
7 "&; ;$J;	W; ; ; ;B  D; ; ;us{       HID Dk*T1DADD	)D D D D  HIJ Jk*T1JADJ J J J "" 
*" " " "L "   6 5:N NN-1N	7N N N N.Vk VSSWZ V V V V8d 8t 8 8 8 8x /3#' [ [$t+[ [ !	[
 [ 
8[ [ [ [@4 4 4> /3#' 	[ 	[$t+	[ 	[ !		[
 	[ 	[ 	[ 	[U UC$J U:\ U U U U6    
[4 [ [ [ [$d $ $ $ $= = = =,/ / / /C C C CE E E E 'cDj ' ' ' X'! ! ! ! !># > > > > > >r2   r   )(loggingsysr   typingr   r   r   r   urllib.parser   r   r	   r
   lxmlr   	davclientr   collections.abcr   r   version_infotyping_extensionsr   elementsr   r   elements.baser   libr   	lib.errorr   lib.python_utilitiesr   lib.urlr   r   	getLoggerr   r   r   r2   r0   <module>r      s    



  8 8 8 8 8 8 8 8 8 8 8 8 A A A A A A A A A A A A       %$$$$$$ / / / / / / / /g&&&&&&&         & & & & & &             ) ) ) ) ) )      ge3444g!!|> |> |> |> |> |> |> |> |> |>r2   