
    i                        d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	Z	erddl
Z
 G d de          Z G d d	e          Z G d
 de          Z G d de          Z G d de          ZdS )a  
Data state management for CalendarObjectResource.

This module implements the Strategy/State pattern for managing different
representations of calendar data (raw string, icalendar object, vobject object).

See https://github.com/python-caldav/caldav/issues/613 for design discussion.
    )annotationsN)ABCabstractmethod)TYPE_CHECKINGc                  r    e Zd ZdZedd            Zedd            Zedd            Zdd
ZddZ	ddZ
dS )	DataStatezAbstract base class for calendar data states.

    Each concrete state represents a different "source of truth" for the
    calendar data. The state provides access to all representations, but
    only one is authoritative at any time.
    returnstrc                    dS )zGet raw iCalendar string representation.

        This may involve serialization if the current state holds a
        parsed object.
        N selfs    K/root/projects/butler/venv/lib/python3.11/site-packages/caldav/datastate.pyget_datazDataState.get_data   	     	    icalendar.Calendarc                    dS )zGet a fresh copy of the icalendar object.

        This is safe for read-only access - modifications won't affect
        the stored data.
        Nr   r   s    r   get_icalendar_copyzDataState.get_icalendar_copy'   r   r   vobject.base.Componentc                    dS )zGet a fresh copy of the vobject object.

        This is safe for read-only access - modifications won't affect
        the stored data.
        Nr   r   s    r   get_vobject_copyzDataState.get_vobject_copy0   r   r   
str | Nonec                    |                                  }|j        D ]&}|j        dv rd|v rt          |d                   c S 'dS )zExtract UID without full parsing if possible.

        Default implementation parses the data, but subclasses can optimize.
        VEVENTVTODOVJOURNALUIDN)r   subcomponentsnamer
   r   calcomps      r   get_uidzDataState.get_uid9   s[    
 %%''% 	( 	(Dy;;;4;'''''tr   c                f    |                                  }|j        D ]}|j        dv r	|j        c S dS )zGet the component type (VEVENT, VTODO, VJOURNAL) without full parsing.

        Default implementation parses the data, but subclasses can optimize.
        r   N)r   r    r!   r"   s      r   get_component_typezDataState.get_component_typeD   sM    
 %%''% 	! 	!Dy;;;y    <tr   boolc                    dS )z!Check if this state has any data.Tr   r   s    r   has_datazDataState.has_dataO   s    tr   Nr	   r
   r	   r   r	   r   r	   r   r	   r(   )__name__
__module____qualname____doc__r   r   r   r   r%   r'   r*   r   r   r   r   r      s             ^    ^    ^	 	 	 		 	 	 	     r   r   c                  B    e Zd ZdZddZddZddZdd
ZddZddZ	dS )NoDataStatezNull Object pattern - no data loaded yet.

    This state is used when a CalendarObjectResource is created without
    any initial data. It provides empty/default values for all accessors.
    r	   r
   c                    dS )N r   r   s    r   r   zNoDataState.get_data[   s    rr   r   c                (    t          j                    S N)	icalendarCalendarr   s    r   r   zNoDataState.get_icalendar_copy^   s    !###r   r   c                (    dd l } |j                    S Nr   )vobject	iCalendarr   r>   s     r   r   zNoDataState.get_vobject_copya   s     w """r   r   c                    d S r9   r   r   s    r   r%   zNoDataState.get_uidf       tr   c                    d S r9   r   r   s    r   r'   zNoDataState.get_component_typei   rB   r   r(   c                    dS )NFr   r   s    r   r*   zNoDataState.has_datal   s    ur   Nr+   r,   r-   r.   r/   )
r0   r1   r2   r3   r   r   r   r%   r'   r*   r   r   r   r5   r5   T   s            $ $ $ $# # # #
           r   r5   c                  L     e Zd ZdZddZddZddZdd
Zd fdZddZ	 xZ
S )RawDataStatezState when raw string data is the source of truth.

    This is the most common initial state when data is loaded from
    a CalDAV server.
    datar
   c                    || _         d S r9   _data)r   rG   s     r   __init__zRawDataState.__init__w   s    


r   r	   c                    | j         S r9   rI   r   s    r   r   zRawDataState.get_dataz   s
    zr   r   c                J    t           j                            | j                  S r9   )r:   r;   	from_icalrJ   r   s    r   r   zRawDataState.get_icalendar_copy}   s    !++DJ777r   r   c                4    dd l } |j        | j                  S r=   )r>   readOnerJ   r@   s     r   r   zRawDataState.get_vobject_copy   s    wtz***r   r   c                    t          j        d| j        t           j                  }|r'|                    d                                          S t                                                      S )Nz
^UID:(.+)$   )researchrJ   	MULTILINEgroupstripsuperr%   )r   match	__class__s     r   r%   zRawDataState.get_uid   sR    	-R\BB 	*;;q>>'')))ww   r   c                H    d| j         v rdS d| j         v rdS d| j         v rdS d S )NzBEGIN:VEVENTr   zBEGIN:VTODOr   zBEGIN:VJOURNALr   rI   r   s    r   r'   zRawDataState.get_component_type   s=    TZ''8dj((7++:tr   )rG   r
   r+   r,   r-   r.   )r0   r1   r2   r3   rK   r   r   r   r%   r'   __classcell__)rZ   s   @r   rF   rF   p   s               8 8 8 8+ + + +
! ! ! ! ! !       r   rF   c                  J    e Zd Z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 )IcalendarStatezState when icalendar object is the source of truth.

    This state is entered when:
    - User calls edit_icalendar_instance()
    - User sets icalendar_instance property
    - User modifies the icalendar object
    calendarr   c                    || _         d S r9   	_calendar)r   r_   s     r   rK   zIcalendarState.__init__   s    !r   r	   r
   c                Z    | j                                                             d          S )Nzutf-8)rb   to_icaldecoder   s    r   r   zIcalendarState.get_data   s$    ~%%''..w777r   c                d    t           j                            |                                           S r9   r:   r;   rN   r   r   s    r   r   z!IcalendarState.get_icalendar_copy   s!    !++DMMOO<<<r   c                    | j         S )z|Returns THE icalendar object (not a copy).

        This is the authoritative object - modifications will be saved.
        ra   r   s    r   get_authoritative_icalendarz*IcalendarState.get_authoritative_icalendar   s    
 ~r   r   c                N    dd l } |j        |                                           S r=   r>   rP   r   r@   s     r   r   zIcalendarState.get_vobject_copy   %    wt}}///r   r   c                l    | j         j        D ]&}|j        dv rd|v rt          |d                   c S 'd S )Nr   r   )rb   r    r!   r
   r   r$   s     r   r%   zIcalendarState.get_uid   sJ    N0 	( 	(Dy;;;4;'''''tr   c                H    | j         j        D ]}|j        dv r	|j        c S d S )Nr   )rb   r    r!   rn   s     r   r'   z!IcalendarState.get_component_type   s<    N0 	! 	!Dy;;;y    <tr   N)r_   r   r+   r,   r-   r.   )r0   r1   r2   r3   rK   r   r   ri   r   r%   r'   r   r   r   r^   r^      s         " " " "8 8 8 8= = = =   0 0 0 0
        r   r^   c                  J    e Zd Z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 )VobjectStatezState when vobject object is the source of truth.

    This state is entered when:
    - User calls edit_vobject_instance()
    - User sets vobject_instance property
    - User modifies the vobject object
    vobjr   c                    || _         d S r9   _vobject)r   rr   s     r   rK   zVobjectState.__init__   s    r   r	   r
   c                4    | j                                         S r9   )ru   	serializer   s    r   r   zVobjectState.get_data   s    }&&(((r   r   c                d    t           j                            |                                           S r9   rg   r   s    r   r   zVobjectState.get_icalendar_copy   s!    !++DMMOO<<<r   c                N    dd l } |j        |                                           S r=   rk   r@   s     r   r   zVobjectState.get_vobject_copy   rl   r   c                    | j         S )zzReturns THE vobject object (not a copy).

        This is the authoritative object - modifications will be saved.
        rt   r   s    r   get_authoritative_vobjectz&VobjectState.get_authoritative_vobject   s    
 }r   r   c                z   	 t          | j        d          r#t          | j        j        j        j                  S t          | j        d          r#t          | j        j        j        j                  S t          | j        d          r#t          | j        j        j        j                  S n# t          $ r Y nw xY wd S )Nveventvtodovjournal)	hasattrru   r
   r}   uidvaluer~   r   AttributeErrorr   s    r   r%   zVobjectState.get_uid   s    	t}h// =4=/39:::00 =4=.28999
33 =4=15;<<<= 	 	 	D	ts   7B+ 7B+ 27B+ +
B87B8c                    t          | j        d          rdS t          | j        d          rdS t          | j        d          rdS d S )Nr}   r   r~   r   r   r   )r   ru   r   s    r   r'   zVobjectState.get_component_type   sR    4=(++ 	8T]G,, 	7T]J// 	:tr   N)rr   r   r+   r,   r-   r.   )r0   r1   r2   r3   rK   r   r   r   r{   r%   r'   r   r   r   rq   rq      s            ) ) ) )= = = =0 0 0 0
           r   rq   )r3   
__future__r   rS   abcr   r   typingr   r:   r>   r   r5   rF   r^   rq   r   r   r   <module>r      sR    # " " " " " 				 # # # # # # # #                 NNN; ; ; ; ; ; ; ;|    )   8% % % % %9 % % %P) ) ) ) )Y ) ) )X2 2 2 2 29 2 2 2 2 2r   