
    i                        d Z ddlmZ ddlmZ ddlmZ ddlmZ e G d d                      Z	ddZ
d dZd!dZ	 d"d#dZd$dZd%dZdS )&z
Principal operations - Sans-I/O business logic for Principal objects.

This module contains pure functions for Principal operations like
URL sanitization and vCalAddress creation. Both sync and async clients
use these same functions.
    )annotations)	dataclass)Anyquotec                  <    e Zd ZU dZded<   ded<   ded<   ded<   dS )	PrincipalDataz Data extracted from a principal.
str | Noneurldisplay_namecalendar_home_set_urlz	list[str]calendar_user_addressesN)__name__
__module____qualname____doc____annotations__     Z/root/projects/butler/venv/lib/python3.11/site-packages/caldav/operations/principal_ops.pyr	   r	      sE         **OOO%%%%&&&&&&r   r	   r   r
   returnc                D    | dS d| v rd| vrd| vrt          |           S | S )a]  
    Sanitize calendar home set URL, handling server quirks.

    OwnCloud returns URLs like /remote.php/dav/calendars/tobixen@e.email/
    where the @ should be quoted. Some servers return already-quoted URLs.

    Args:
        url: Calendar home set URL from server

    Returns:
        Sanitized URL with @ properly quoted (if not already)
    N@://z%40r   )r   s    r   _sanitize_calendar_home_set_urlr      s>     {t czze3&&5+;+;SzzJr   	addresses	list[Any]c                &    t          | d           S )a/  
    Sort calendar user addresses by preference.

    The 'preferred' attribute is possibly iCloud-specific but we honor
    it when present.

    Args:
        addresses: List of address elements (lxml elements with text and attributes)

    Returns:
        Sorted list (highest preference first)
    c                J    t          |                     dd                     S )N	preferredr   )intget)xs    r   <lambda>z/_sort_calendar_user_addresses.<locals>.<lambda>?   s    Ck10E0E,F,F+F r   )key)sorted)r   s    r   _sort_calendar_user_addressesr'   2   s     )!F!FGGGGr   list[str | None]c                8    t          |           }d |D             S )z
    Extract calendar user address strings from XML elements.

    Args:
        addresses: List of DAV:href elements

    Returns:
        List of address strings (sorted by preference)
    c                    g | ]	}|j         
S r   )text).0r#   s     r   
<listcomp>z4_extract_calendar_user_addresses.<locals>.<listcomp>M   s    ---qAF---r   )r'   )r   sorted_addressess     r    _extract_calendar_user_addressesr/   B   s'     5Y??--,----r   Nr   addressstrcalendar_user_typer   c                    ddl m}m}  ||          }| r ||           |j        d<   |r ||          |j        d<   |S )a>  
    Create an icalendar vCalAddress object from principal properties.

    Args:
        display_name: The principal's display name (CN parameter)
        address: The primary calendar user address
        calendar_user_type: CalendarUserType (CUTYPE parameter)

    Returns:
        icalendar.vCalAddress object
    r   )vCalAddressvTextcncutype)	icalendarr4   r5   params)r   r0   r2   r4   r5   	vcal_addrs         r   _create_vcal_addressr;   P   sp      -,,,,,,,G$$I 5!&|!4!4	 ?%*U+=%>%>	"r   resultslist[Any] | Nonec                r    | sdS | D ]/}|j                             d          }|rt          |          c S 0dS )aR  
    Extract calendar-home-set URL from PROPFIND results.

    This pure function processes propfind results to find the
    calendar-home-set property, handling URL sanitization.

    Args:
        results: List of PropfindResult objects from parse_propfind_response

    Returns:
        Calendar home set URL, or None if not found
    Nz0{urn:ietf:params:xml:ns:caldav}calendar-home-set)
propertiesr"   r   )r<   resulthome_sets      r   '_extract_calendar_home_set_from_resultsrB   k   s_      t = =$(()[\\ 	=28<<<<<	= 4r   r   client_hostnameboolc                `    | dS d| v r%ddl m}  ||           }|j        r|j        |k    rdS dS )a  
    Check if client base URL should be updated for load-balanced systems.

    iCloud and others use load-balanced systems where each principal
    resides on one named host. If the calendar home set URL has a different
    hostname, we may need to update the client's base URL.

    Args:
        calendar_home_set_url: The sanitized calendar home set URL
        client_hostname: The current client hostname

    Returns:
        True if client URL should be updated
    NFr   r   )urlparseT)urllib.parserF   hostname)r   rC   rF   parseds       r   _should_update_client_base_urlrJ      s_    $ $u %%%))))))/00? 	v/AA45r   )r   r
   r   r
   )r   r   r   r   )r   r   r   r(   )N)r   r
   r0   r1   r2   r
   r   r   )r<   r=   r   r
   )r   r
   rC   r
   r   rD   )r   
__future__r   dataclassesr   typingr   rG   r   r	   r   r'   r/   r;   rB   rJ   r   r   r   <module>rN      s    # " " " " " ! ! ! ! ! !             ' ' ' ' ' ' ' '   0H H H H . . . ." &*    6   4     r   