
    i                        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
mZmZ  ej        d          Ze G d d	                      Zd#dZd$dZd%dZd&dZd'dZd(dZd)dZd*d"ZdS )+z
CalendarSet operations - Sans-I/O business logic for CalendarSet objects.

This module contains pure functions for CalendarSet operations like
extracting calendar IDs and building calendar URLs. Both sync and async
clients use these same functions.
    )annotationsN)	dataclass)Any)quoteunquoteurlparse
urlunparsecaldavc                  <    e Zd ZU dZded<   ded<   ded<   ded<   d	S )
CalendarInfoz5Data for a calendar extracted from PROPFIND response.strurl
str | Nonecal_idnamez	list[str]resource_typesN)__name__
__module____qualname____doc____annotations__     \/root/projects/butler/venv/lib/python3.11/site-packages/caldav/operations/calendarset_ops.pyr   r      sE         ??HHHr   r   r   r   returnr   c                   	 t          |                               d                              d          }t          |          dk    r|d         }|r|S n-# t          $ r  t
                              d|             Y nw xY wdS )a  
    Extract calendar ID from a calendar URL.

    Calendar URLs typically look like: /calendars/user/calendar-id/
    The calendar ID is the second-to-last path segment.

    Args:
        url: Calendar URL

    Returns:
        Calendar ID, or None if extraction fails
    /   zCalendar has unexpected url N)r   rstripsplitlen	Exceptionlogerror)r   partsr   s      r   _extract_calendar_id_from_urlr'      s    8C$$**3//u::??2YF  8 8 8		6667777784s   AA 'B Bchildren_data'list[tuple[str, list[str], str | None]]list[CalendarInfo]c           	         g }| D ]>\  }}}t          |          }|s|                    t          ||||                     ?|S )a	  
    Process children data into CalendarInfo objects.

    Args:
        children_data: List of (url, resource_types, display_name) tuples
                      from children() call

    Returns:
        List of CalendarInfo objects with extracted calendar IDs
    r   r   r   r   )r'   appendr   )r(   	calendarsc_urlc_typesc_namer   s         r   _process_calendar_listr2   6   sz     I"/ 
 
w.u55 	&	  	
 	
 	
 	
 r   r   
parent_urlclient_base_urlc                   t          |                              d          }t          |           }|                    |          rt          ||           S |                    d          s|                    d          rt          ||           S t	          |           }|                    d          s|dz  }t          ||          S )az  
    Resolve a calendar URL from a calendar ID.

    Handles different formats:
    - Full URLs (https://...)
    - Absolute paths (/calendars/...)
    - Relative IDs (just the calendar name)

    Args:
        cal_id: Calendar ID or URL
        parent_url: URL of the calendar set
        client_base_url: Base URL of the client

    Returns:
        Resolved calendar URL
    r   zhttps://zhttp://)r   r    
startswith	_join_urlr   endswith)r   r3   r4   client_canonical
cal_id_str	quoted_ids         r   _resolve_calendar_urlr<   S   s    , ?++22377VJ -.. 2&111 Z(( -J,A,A),L,L -V,,, fIc"" S	Z+++r   basepathc                    t          |                               d          } t          |                              d          }|  d| S )a  
    Simple URL join - concatenates base and path.

    This is a placeholder that the actual URL class will handle.
    Returns a string representation for the operations layer.

    Args:
        base: Base URL
        path: Path to join

    Returns:
        Joined URL string
    r   )r   r    lstrip)r=   r>   s     r   r7   r7   ~   sJ     t99C  Dt99C  DTr   r.   r   CalendarInfo | Nonec                .    | D ]}|j         |k    r|c S dS )z
    Find a calendar by display name.

    Args:
        calendars: List of CalendarInfo objects
        name: Display name to search for

    Returns:
        CalendarInfo if found, None otherwise
    N)r   )r.   r   cals      r   _find_calendar_by_namerD      s3       8tJJJ 4r   c                .    | D ]}|j         |k    r|c S dS )z
    Find a calendar by ID.

    Args:
        calendars: List of CalendarInfo objects
        cal_id: Calendar ID to search for

    Returns:
        CalendarInfo if found, None otherwise
    N)r   )r.   r   rC   s      r   _find_calendar_by_idrF      s3       :JJJ  4r   c                    t          |           }t          t          |j                  d          }t	          |                    |                    S )aj  
    Quote the path component of a URL to handle spaces and special characters.

    Some servers (e.g., Zimbra) return URLs with unencoded spaces in the path.
    This function ensures the path is properly percent-encoded.

    Args:
        url: URL string that may contain unencoded characters in path

    Returns:
        URL with properly encoded path
    z/@)safe)r>   )r   r   r   r>   r	   _replace)r   parsedquoted_paths      r   _quote_url_pathrL      sG     c]]F,,4888Kfoo;o77888r   resultslist[Any] | Nonec                D   ddl m} g }| pg D ]} ||j                  st          |j                  }|j                            d          }t          |          }|sS|                    t          ||||j                            dg                                |S )aW  
    Extract calendar information from PROPFIND results.

    This pure function processes propfind results to identify calendar
    resources and extract their metadata.

    Args:
        results: List of PropfindResult objects from parse_propfind_response

    Returns:
        List of CalendarInfo objects for calendar resources found
    r   )_is_calendar_resourcez{DAV:}displaynamez{DAV:}resourcetyper,   )	caldav.operations.baserP   
propertiesrL   hrefgetr'   r-   r   )rM   is_calendar_resourcer.   resultr   r   r   s          r   (_extract_calendars_from_propfind_resultsrW      s     UTTTTTI-R 
 
##F$566 	 fk** $$%899.s33 	%0445I2NN	  	
 	
 	
 	
 r   )r   r   r   r   )r(   r)   r   r*   )r   r   r3   r   r4   r   r   r   )r=   r   r>   r   r   r   )r.   r*   r   r   r   rA   )r.   r*   r   r   r   rA   )r   r   r   r   )rM   rN   r   r*   )r   
__future__r   loggingdataclassesr   typingr   urllib.parser   r   r   r	   	getLoggerr$   r   r'   r2   r<   r7   rD   rF   rL   rW   r   r   r   <module>r^      se    # " " " " "  ! ! ! ! ! !       = = = = = = = = = = = =g!!           2   :(, (, (, (,V   (   (   (9 9 9 9&( ( ( ( ( (r   