
    i"                    R   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
  ej        d          ZdZd	Zd
Z ed           G d d                      Ze G d d                      Ze G d d                      Zd0dZ	 	 d1d2d Zd3d!Z	 d4d5d$Zd3d%Z	 d6d7d*Zd8d.Zd8d/ZdS )9a   
DAVObject operations - Sans-I/O business logic for DAV objects.

This module contains pure functions for DAVObject operations like
getting/setting properties, listing children, and deleting resources.
Both sync and async clients use these same functions.
    )annotationsN)	dataclass)Any)quoteunquotecaldavz{DAV:}displaynamez{DAV:}resourcetypez'{urn:ietf:params:xml:ns:caldav}calendarT)frozenc                  >    e Zd ZU dZded<   dZded<   eefZded<   d	S )
ChildrenQueryz)Query specification for listing children.strurl   intdepthztuple[str, ...]propsN)	__name__
__module____qualname____doc____annotations__r   DAV_DISPLAYNAMEDAV_RESOURCETYPEr        Z/root/projects/butler/venv/lib/python3.11/site-packages/caldav/operations/davobject_ops.pyr   r      sE         33HHHENNNN-/?@E@@@@@@r   r   c                  2    e Zd ZU dZded<   ded<   ded<   dS )		ChildDatazData for a child resource.r   r   z	list[str]resource_types
str | Nonedisplay_nameNr   r   r   r   r   r   r   r   r   r   "   s9         $$HHHr   r   c                  (    e Zd ZU dZded<   ded<   dS )PropertiesResultz6Result of extracting properties for a specific object.zdict[str, Any]
propertiesr   matched_pathNr!   r   r   r   r#   r#   +   s0         @@r   r#   r   r   returnc                "    t          |           S )z
    Build query for listing children of a collection.

    Args:
        url: URL of the parent collection

    Returns:
        ChildrenQuery specification
    r   )r   r(   s    r   _build_children_queryr)   3   s     S!!!!r   Fproperties_by_hrefdict[str, dict[str, Any]]
parent_urlfilter_typer   is_calendar_setboollist[ChildData]c                4   g }t          |          }|                                 D ]\  }}|                    t          g           }t	          |t
                    r|g}n|g }|                    t                    }	|||vr^|}
|                    d          st          |          }
t          |          }|r1|t          k    r&|
                    t          |
||	                     ||k    r%|
                    t          |
||	                     |S )a  
    Process PROPFIND response into list of children.

    This is Sans-I/O - works on already-parsed response data.

    Args:
        properties_by_href: Dict mapping href -> properties dict
        parent_url: URL of the parent collection (to exclude from results)
        filter_type: Optional resource type to filter by (e.g., CALDAV_CALENDAR)
        is_calendar_set: True if parent is a CalendarSet (affects filtering logic)

    Returns:
        List of ChildData for matching children
    Nhttp)r   r   r    )_canonical_pathitemsgetr   
isinstancer   r   
startswithr   CALDAV_CALENDARappendr   )r*   r,   r-   r.   childrenparent_canonicalpathr   r   r    url_obj_pathchild_canonicals               r   _process_children_responser?   @   s^   ( H 'z22)//11 ' 'e#3R88nc** 	 ,-NN#Nyy11 "{.'H'H v&& 	' ;;L *$//  	{o==OO$#1!-      00OO$#1!-     Or   c                j    d| v rddl m}  ||           }|j        }n| }|                    d          S )z>Get canonical path for comparison, stripping trailing slashes.://r   urlparse/)urllib.parserC   r<   rstrip)r   rC   parsedr<   s       r   r3   r3      sN     ||))))))#{ ;;sr   
object_urlis_principalc           	        d|vrt          |          nt          t          |                    }|                    d          r|dd         }n|dz   }|| v rt          | |         |          S || v r:|s!t                              d| d| d           t          | |         |          S || v rt          | |         |          S d	| v rF|                    d	          r1t                              d
           t          | d	         d	          S d|v rQ|                    dd          }|| v r7t                              d| d|            t          | |         |          S t          |           dk    r_t          | 	                                          d         }t                              d| d| d           t          | |         |          S t          d| dt          | 	                                                     )a  
    Find properties for a specific object from a PROPFIND response.

    Handles various server quirks like trailing slash mismatches,
    iCloud path issues, and double slashes.

    Args:
        properties_by_href: Dict mapping href -> properties dict
        object_url: URL of the object we're looking for
        is_principal: True if object is a Principal (affects warning behavior)

    Returns:
        PropertiesResult with the found properties

    Raises:
        ValueError: If no matching properties found
    rA   rD   N)r$   r%   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/z8Applying iCloud workaround for /principal/ path mismatchz//zPath contained double slashes: z -> r   r   zfPossibly the server has a path handling problem, possibly the URL configured is wrong. Path expected: z, path found: z.. Continuing, probably everything will be finezCould not find properties for z. Available paths: )r   _extract_pathendswithr#   logwarningreplacelenlistkeys
ValueError)r*   rH   rI   r<   exchange_path
normalized	only_paths          r   _find_object_propertiesrX      s   , #(z"9"97:w}U_G`G`?a?aD }}S #SbS	s
 !!!+=d+CRVWWWW *** 	KKLD L L L L L    )-8}
 
 
 	

 '''+=j+IXbcccc ***t}}]/K/K*NOOO)-8}
 
 
 	

 t||\\$,,
+++KKP$PPJPPQQQ#-j9
   
 !##+002233A6	;"; ;2;; ; ;	
 	
 	

  +=i+HW`aaaa ccc$GYG^G^G`G`BaBacc  r   c                :    d| vr| S ddl m}  ||           j        S )z"Extract path component from a URL.rA   r   rB   )rE   rC   r<   )r   rC   s     r   rL   rL      s5    C
%%%%%%8C==r   results	list[Any]requested_propslist[str] | Nonec                x    i }| D ]4}i }|r
|D ]}d||<   |                     |j                   |||j        <   5|S )a]  
    Convert protocol layer results to the {href: {tag: value}} format.

    Args:
        results: List of PropfindResult from protocol layer
        requested_props: Optional list of property tags that were requested
                        (used to initialize missing props to None)

    Returns:
        Dict mapping href -> properties dict
    N)updater$   href)rZ   r\   r$   resultresult_propsprops         r   '_convert_protocol_results_to_propertiesrd      sm     J / / 	*' * *%)T""F-...".
6;r   statusr   Nonec                2    | dvrt          d|            dS )z
    Validate DELETE response status.

    Args:
        status: HTTP status code

    Raises:
        ValueError: If status indicates failure
    )      i  zDelete failed with status NrT   re   s    r   _validate_delete_responserl     s.     _$$>f>>??? %$r   c                6    | dk    rt          d|            dS )z
    Validate PROPPATCH response status.

    Args:
        status: HTTP status code

    Raises:
        ValueError: If status indicates failure
    i  zPROPPATCH failed with status Nrj   rk   s    r   _validate_proppatch_responsern     s+     }}AAABBB }r   )r   r   r&   r   )NF)
r*   r+   r,   r   r-   r   r.   r/   r&   r0   )r   r   r&   r   )F)r*   r+   rH   r   rI   r/   r&   r#   )N)rZ   r[   r\   r]   r&   r+   )re   r   r&   rf   )r   
__future__r   loggingdataclassesr   typingr   rE   r   r   	getLoggerrN   r   r   r8   r   r   r#   r)   r?   r3   rX   rL   rd   rl   rn   r   r   r   <module>rt      s    # " " " " "  ! ! ! ! ! !       ' ' ' ' ' ' ' 'g!! &' ; $A A A A A A A A                
" 
" 
" 
"  #!	B B B B BJ   & N N N N Nb    )-    8@ @ @ @C C C C C Cr   