
    iT                     |   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 	 d dlmZ eeez           Zn#  Y nxY werddlmZ d dl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)r      )	DAVClient)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<   	 	 	 	 	 	 d.d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	eeeeef                  fdZd	eez  fdZd Zd0dee         dz  defdZ	 d0dee         dz  defdZ	 	 	 	 	 d1dZ	 	 	 	 	 d1dZ 	 d2deded	edz  fdZ!	 d2deded	edz  fdZ"de#d	e#fdZ$	 	 	 	 d3dee         dz  ded!ed"efd#Z%	 	 	 	 d3dee         dz  ded!ed"efd$Z&d/dedz  d	e'fd%Z(d/dedz  d	e'fd&Z)d	e'fd'Z*d4d(Z+d4d)Z,d* Z-ed	edz  fd+            Z.d	efd,Z/d	efd-Z0dS )5	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           K/root/projects/butler/venv/lib/python3.11/site-packages/caldav/davobject.py__init__zDAVObject.__init__;   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    r/   canonical_urlzDAVObject.canonical_urlf   s5    8ABBB48%%''(((r1   c                 N    | j         dS t          | j                   j        dk    S )zCheck if this object is connected to an async client.

        Returns:
            True if the client is an AsyncDAVClient, False otherwise.
        NFAsyncDAVClient)r!   type__name__r5   s    r/   is_async_clientzDAVObject.is_async_clientl   s*     ;5DK  )-===r1   r9   c                 R   ddl m} g }d}| j        t          d          t	          j                    g}t	          j                    g}||z   }|                     ||          }|                    ||          }	|	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 )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   )CalendarSetNr3   )r'   multi_value_props)
collectionr=   r    r,   r   DisplayNameResourceType_query_propertiesexpand_simple_propstagr   hostnamer	   
isinstancer   Calendarr4   strip_trailing_slashr)   append)r-   r9   r=   cdepthr'   
multipropsprops_multipropsresponse
propertiespathresource_typesresource_namer    s                 r/   childrenzDAVObject.childrenx   s    	,+++++8ABBB""#&(()
 :-))*:EBB11Q[1\\
 	S 	SD'-c.>.BCN&t,S_-@AM|t~55$ii<' ;;D t[11 Sddm>O6O6OH&&((==??x}}T**4466KKMMN N HHdhmmD11>=QRRR r1   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   tostringrU   r   debug_dump_communication)r-   roots     r/   _build_xml_bodyzDAVObject._build_xml_body   sZ     	t\** ~OO%%$$(!&!?	    Krr1   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      r/   _build_propfind_rootzDAVObject._build_propfind_root   s<    Ua8::%D<>>D((tr1   r   r'   rK   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_queryrd   r-   r'   rK   s      r/   rB   zDAVObject._query_properties   sG      	>//u==={{444U;;UCCCr1   c                 d   K   |                      |                     |          |           d{V S )z*Async implementation of _query_properties.N)_async_queryrd   rh   s      r/   rf   z!DAVObject._async_query_properties   s>       &&t'@'@'G'GOOOOOOOOOr1   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;   rj   r^   r    getattrr!   statusr   NotFoundErrorr   r   replacerg   exception_by_methodr-   r]   rK   query_methodr    expected_return_valuebodyrets           r/   rg   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
r1   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.Nrm   rn   ro   rp   rq   rr   )r^   r    rs   r!   rt   r   ru   r   r   rv   rj   rw   rx   s           r/   rj   zDAVObject._async_query   sD      ##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!..%s4I         +L9&++FFF
r1   Frc   
use_cachedc                     | j         r | j        ||fi |S |r |j        | j        v r| j        |j                 S  | j        |gfi |}|                    |j        d          S )az  
        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

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

        For async clients, returns a coroutine that must be awaited.
        N)r;   _async_get_propertyrD   r'   get_propertiesgetr-   rc   r~   passthroughfoos        r/   get_propertyzDAVObject.get_property  s      	M+4+D*LLLLL  	,x4:%%z$(++!d!4&88K88wwtx&&&r1   c                    K   |r |j         | j        v r| j        |j                  S  | j        |gfi | d{V }|                    |j         d          S )z%Async implementation of get_property.N)rD   r'   _async_get_propertiesr   r   s        r/   r   zDAVObject._async_get_property.  sr        	,x4:%%z$(++.D.vEEEEEEEEEEwwtx&&&r1   rO   c           	         ddl m} | j        t          d          t	          | j        j                  }|                    d          r|dd         }n|dz   }||v r
||         }n?||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         }nSt          	                    d| dt          |                                                      t          j        d           | 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   )	PrincipalNr3   /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)r?   r   r    r,   r
   rP   endswithrF   logwarningrv   r`   listvalueskeysr   assert_r'   update)r-   rO   r   rP   exchange_pathrcs         r/   _resolve_propertieszDAVObject._resolve_properties8  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%   
"	r1   Tparse_response_xmlparse_propsc                    | j         r|                     ||||          S d}|                     ||          }|s|S |rR|j        rKi }|j        D ]@}i }	|r|D ]}
|
j        r
d|	|
j        <   |	                    |j                   |	||j        <   An,|s|                                }n|	                    |          }t          j        |           |                     |          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.
        N)r;   r   rB   resultsrD   r   rO   href_find_objects_and_propsrC   r   r   r   r-   r'   rK   r   r   r   rN   rO   resultresult_propsrc   s              r/   r   zDAVObject.get_propertiesq  s2   4  	]--eU<NP[\\\))%77! 	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3r1   c                   K   d}|                      ||           d{V }|s|S |rR|j        rKi }|j        D ]@}i }	|r|D ]}
|
j        r
d|	|
j        <   |	                    |j                   |	||j        <   An,|s|                                }n|                    |          }t          j	        |           | 
                    |          S )z'Async implementation of get_properties.N)rf   r   rD   r   rO   r   r   rC   r   r   r   r   s              r/   r   zDAVObject._async_get_properties  s       55eUCCCCCCCC! 	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3r1   c                    | j         r|                     |          S |g n|}t          j                    |z   }t          j                    |z   }t          j                    |z   }|                     |          }| j        t          d          | j	        t          d          | j	        
                    t          | j                  |          }|j        dk    r!t          j        t          |                    | S )z
        Set properties (PROPPATCH) for this object.

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

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

        Returns:
         * self
        Nr3   %Unexpected value None for self.clientrn   )r;   _async_set_propertiesr   ra   SetPropertyUpdater^   r    r,   r!   	proppatchr+   rt   r   PropsetErrorr   r-   r'   rc   set_elemr]   r{   rs          r/   set_propertieszDAVObject.set_properties  s      	5--e444mxzzE!799t#!##h.##D))8ABBB;DEEEK!!#dh--668s??$VAYY///r1   c                   K   |g n|}t          j                    |z   }t          j                    |z   }t          j                    |z   }|                     |          }| j        t          d          | j        t          d          | j                            t          | j                  |           d{V }|j
        dk    r!t          j        t          |                    | S )z'Async implementation of set_properties.Nr3   r   rn   )r   ra   r   r   r^   r    r,   r!   r   r+   rt   r   r   r   r   s          r/   r   zDAVObject._async_set_properties  s      mxzzE!799t#!##h.##D))8ABBB;DEEE+''DHt<<<<<<<<8s??$VAYY///r1   c                     t                      )z
        Save the object. This is an abstract method, that all classes
        derived from DAVObject implement.

        Returns:
         * self
        )NotImplementedErrorr5   s    r/   savezDAVObject.save  s     "###r1   c                 &   | j         | j        t          d          | j        r|                                 S | j                            t          | j                             }|j        dvr#t          j	        t          |                    dS 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         rm   )r    r!   r,   r;   _async_deletedeleter+   rt   r   DeleteErrorr   r-   r   s     r/   r   zDAVObject.delete  s     8{" !HIII # ,))+++""3tx==11A x..'q		222   /.r1   c                    K   | j         c| j        ^| j                            t          | j                              d{V }|j        dvr%t          j        t          |                    dS dS dS )zAsync implementation of delete.Nr   )r    r!   r   r+   rt   r   r   r   r   s     r/   r   zDAVObject._async_delete+  s|      8DK$;k((TX77777777Ax..'q		222  $;$;..r1   c                 R    |                      t          j                    d          S )zB
        Get display name (calendar, principal, ...more?)
        Tr~   )r   r   r@   r5   s    r/   get_display_namezDAVObject.get_display_name2  s%       !2!2t DDDr1   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   r5   s    r/   r#   zDAVObject.name8  s:     	Q	
 	
 	
 	

 $$&&&r1   c                     	 t          |                     t          j                    d                    p| j        S #  t          | j                  cY S xY w)NTr   )r+   r   r   r@   r    r5   s    r/   __str__zDAVObject.__str__G  sV    	!t(():):t(LLMMYQUQYY	!tx==   s	   ;> Ac                 0    | j         j        d| j        dS )N())	__class__r:   r    r5   s    r/   __repr__zDAVObject.__repr__M  s    >222DHHH==r1   )NNNNNN)N)Nr   )Nr   rk   NN)F)Nr   TT)r$   N)1r:   
__module____qualname____doc__r   r+   __annotations__r    r   r!   r   r"   r   r   r0   propertyr6   boolr;   r   tupler   rS   bytesr^   rd   r   r   intrB   rf   rg   rj   r   r   dictr   r   r   r   r   r   r   r   r   r   r#   r   r    r1   r/   r   r   0   s         
 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)
 	> 	> 	> 	> X	>4 4S4Z 44c3m8L3M 4 4 4 4lus{      D Dx'<t'C DSV D D D D HIP Pk*T1PADP P P P "" " " "L "   : 5:' ''-1'	t' ' ' '4 5:' ''-1'	t' ' ' '7d 7t 7 7 7 7v /3#' 94 94$t+94 94 !	94
 94 94 94 94z /3#' $4 $4$t+$4 $4 !	$4
 $4 $4 $4 $4L C$J $    @ t t    ($d $ $ $ $3 3 3 363 3 3 3E E E 'cDj ' ' ' X'! ! ! ! !># > > > > > >r1   r   )*loggingsysr   typingr   r   r   r   urllib.parser   r   r	   r
   lxmlr   datedatetime	TimeStamp	davclientr   collections.abcr   version_infotyping_extensionsr   elementsr   r   elements.baser   libr   	lib.errorr   lib.python_utilitiesr   lib.urlr   r   	getLoggerr   r   r   r1   r/   <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      	)II	D %$$$$$$ $ $ $ $ $ $g&&&&&&&         & & & & & &             ) ) ) ) ) )      ge3444g!!^> ^> ^> ^> ^> ^> ^> ^> ^> ^>s	   > A