o
    .jx                     @  s   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 deZG dd	 d	eZG d
d deZG dd deZG d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.

TODO: verify that we have sufficient test coverage - both through unit tests
and integration tests
    )annotationsN)ABCabstractmethod)TYPE_CHECKINGc                   @  sX   e Zd ZdZedddZedddZedd
dZdddZdddZ	d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                 C     dS )zGet raw iCalendar string representation.

        This may involve serialization if the current state holds a
        parsed object.
        N selfr
   r
   E/home/thesage/.local/lib/python3.10/site-packages/caldav/datastate.pyget_data!      zDataState.get_dataicalendar.Calendarc                 C  r	   )zGet a fresh copy of the icalendar object.

        This is safe for read-only access - modifications won't affect
        the stored data.
        Nr
   r   r
   r
   r   get_icalendar_copy*   r   zDataState.get_icalendar_copyvobject.base.Componentc                 C  r	   )zGet a fresh copy of the vobject object.

        This is safe for read-only access - modifications won't affect
        the stored data.
        Nr
   r   r
   r
   r   get_vobject_copy3   r   zDataState.get_vobject_copy
str | Nonec                 C  s:   |   }|jD ]}|jdv rd|v rt|d   S qdS )zExtract UID without full parsing if possible.

        Default implementation parses the data, but subclasses can optimize.
        VEVENTVTODOVJOURNALFREEBUSYUIDN)r   subcomponentsnamer   r   calcompr
   r
   r   get_uid<   s   
zDataState.get_uidc                 C  s,   |   }|jD ]}|jdv r|j  S qdS )zGet the component type (VEVENT, VTODO, VJOURNAL, FREEBUSY) without full parsing.

        Default implementation parses the data, but subclasses can optimize.
        r   N)r   r   r   r   r
   r
   r   get_component_typeG   s   


zDataState.get_component_typeboolc                 C  r	   )z!Check if this state has any data.Tr
   r   r
   r
   r   has_dataR   s   zDataState.has_dataN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   c                   @  sL   e Zd ZdZdddZdddZdd
dZdddZdddZd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                 C  r	   )N r
   r   r
   r
   r   r   ^      zNoDataState.get_datar   c                 C  s   t  S N)	icalendarCalendarr   r
   r
   r   r   a   s   zNoDataState.get_icalendar_copyr   c                 C  s   dd l }| S Nr   )vobject	iCalendarr   r4   r
   r
   r   r   d   s   zNoDataState.get_vobject_copyr   c                 C     d S r0   r
   r   r
   r
   r   r    i   r/   zNoDataState.get_uidc                 C  r7   r0   r
   r   r
   r
   r   r!   l   r/   zNoDataState.get_component_typer"   c                 C  r	   )NFr
   r   r
   r
   r   r#   o   r/   zNoDataState.has_dataNr$   r%   r&   r'   r(   )
r)   r*   r+   r,   r   r   r   r    r!   r#   r
   r
   r
   r   r-   W   s    




r-   c                      sT   e Zd ZdZdddZdddZdd
dZdddZd fddZdddZ	  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                 C  
   || _ d S r0   _data)r   r9   r
   r
   r   __init__z      
zRawDataState.__init__r   c                 C     | j S r0   r;   r   r
   r
   r   r   }   s   zRawDataState.get_datar   c                 C  s   t j| jS r0   )r1   r2   	from_icalr<   r   r
   r
   r   r      s   zRawDataState.get_icalendar_copyr   c                 C  s   dd l }|| jS r3   )r4   readOner<   r6   r
   r
   r   r      s   zRawDataState.get_vobject_copyr   c                   s.   t d| jt j}|r|d S t  S )Nz
^UID:(.+)$   )researchr<   	MULTILINEgroupstripsuperr    )r   match	__class__r
   r   r       s   
zRawDataState.get_uidc                 C  s<   d| j v rdS 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   zBEGIN:FREEBUSY	VFREEBUSYr;   r   r
   r
   r   r!      s   



zRawDataState.get_component_type)r9   r   r$   r%   r&   r'   )r)   r*   r+   r,   r=   r   r   r   r    r!   __classcell__r
   r
   rJ   r   r8   s   s    



r8   c                   @  sV   e Zd ZdZdddZddd	Zdd
dZdddZdddZdddZ	d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                 C  r:   r0   	_calendar)r   rO   r
   r
   r   r=      r>   zIcalendarState.__init__r   r   c                 C  s   | j  dS )Nzutf-8)rQ   to_icaldecoder   r
   r
   r   r         zIcalendarState.get_datac                 C     t j|  S r0   r1   r2   r@   r   r   r
   r
   r   r      s   z!IcalendarState.get_icalendar_copyc                 C  r?   )z|Returns THE icalendar object (not a copy).

        This is the authoritative object - modifications will be saved.
        rP   r   r
   r
   r   get_authoritative_icalendar      z*IcalendarState.get_authoritative_icalendarr   c                 C     dd l }||  S r3   r4   rA   r   r6   r
   r
   r   r         zIcalendarState.get_vobject_copyr   c                 C  s4   | j jD ]}|jdv rd|v rt|d   S qd S )Nr   r   r   rL   r   )rQ   r   r   r   r   r   r
   r
   r   r       s
   zIcalendarState.get_uidc                 C  s&   | j jD ]}|jdv r|j  S qd S )Nr\   )rQ   r   r   r]   r
   r
   r   r!      s
   

z!IcalendarState.get_component_typeN)rO   r   r$   r%   r&   r'   )r)   r*   r+   r,   r=   r   r   rW   r   r    r!   r
   r
   r
   r   rN      s    





rN   c                   @  sV   e Zd ZdZdddZddd	ZdddZdddZdddZdddZ	d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                 C  r:   r0   _vobject)r   r_   r
   r
   r   r=      r>   zVobjectState.__init__r   r   c                 C  s
   | j  S r0   )ra   	serializer   r
   r
   r   r      r>   zVobjectState.get_datar   c                 C  rU   r0   rV   r   r
   r
   r   r      rT   zVobjectState.get_icalendar_copyc                 C  rY   r3   rZ   r6   r
   r
   r   r      r[   zVobjectState.get_vobject_copyc                 C  r?   )zzReturns THE vobject object (not a copy).

        This is the authoritative object - modifications will be saved.
        r`   r   r
   r
   r   get_authoritative_vobject   rX   z&VobjectState.get_authoritative_vobjectr   c                 C  s   z?t | jdrt| jjjjW S t | jdrt| jjjjW S t | jdr.t| jjjjW S t | jdr=t| jjjjW S W d S  t	yI   Y d S w )Nveventvtodovjournal	vfreebusy)
hasattrra   r   rd   uidvaluere   rf   rg   AttributeErrorr   r
   r
   r   r       s   zVobjectState.get_uidc                 C  sD   t | jdrdS t | jdrdS t | jdrdS t | jdr dS d S )	Nrd   r   re   r   rf   r   rg   rL   )rh   ra   r   r
   r
   r   r!      s   zVobjectState.get_component_typeN)r_   r   r$   r%   r&   r'   )r)   r*   r+   r,   r=   r   r   r   rc   r    r!   r
   r
   r
   r   r^      s    





r^   )r,   
__future__r   rC   abcr   r   typingr   r1   r4   r   r-   r8   rN   r^   r
   r
   r
   r   <module>   s    >*,