
    i"                        d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZmZmZmZ erddlmZ dd	lmZ dd
lmZ ddlmZmZmZ  G d de          ZdgZdS )zBase class for all adapters.    )annotationsN)ABCabstractmethod)TYPE_CHECKINGOptionalSequence)	vDDDTypes)cached_propertymake_comparabletime_span_contains_eventto_recurrence_ids)Alarm)	Component)Series)UIDRecurrenceIDsTimec                  <   e Zd ZdZg dZeed7d                        Zd8dZe	d9d
            Z
e	d:d            Ze	d;d            Ze	d;d            Zed             Ze	ed                         Ze	ed                         Ze	d<d            Zed=d            Z	 	 	 d>d?d!Zed@d#            ZedAd$            ZdAd%ZedBd'            Zd7d(ZedCd*            ZedDd,            ZedEd.            ZedFd0            ZdGd3ZedHd5            Z edFd6            Z!dS )IComponentAdapterz6A unified interface to work with icalendar components.)RRULERDATEEXDATEreturnstrc                     dS )zThe icalendar component name.N r       c/root/projects/butler/venv/lib/python3.11/site-packages/recurring_ical_events/adapters/component.pycomponent_namezComponentAdapter.component_name         r   	componentr   c                    || _         dS )zCreate a new adapter.N)
_component)selfr!   s     r   __init__zComponentAdapter.__init__$   s    #r   list[Alarm]c                6    | j                             d          S )zThe alarms in this component.VALARM)r#   walkr$   s    r   alarmszComponentAdapter.alarms(   s     ##H---r   
str | Nonec                    dS )zThe name of the end property.Nr   r*   s    r   end_propertyzComponentAdapter.end_property-   s	     tr   r   c                    | j         d         S )zThe start time.r   spanr*   s    r   startzComponentAdapter.start2        y|r   c                    | j         d         S )zThe end time.   r0   r*   s    r   endzComponentAdapter.end7   r3   r   c                Z    t          | j        | j        f          \  }}||k    r||fS ||fS )zReturn (start, end).)r   	raw_startraw_end)r$   r2   r6   s      r   r1   zComponentAdapter.span<   s;     %dndl%CDD
s3;;:czr   c                    dS z+Return the start property of the component.Nr   r*   s    r   r8   zComponentAdapter.raw_startD   r    r   c                    dS r;   r   r*   s    r   r9   zComponentAdapter.raw_endI   r    r   r   c                v    | j                             dt          t          | j                                       S )zyThe UID of a component.

        UID is required by RFC5545.
        If the UID is absent, we use the Python ID.
        r   )r#   getr   idr*   s    r   uidzComponentAdapter.uidN   s.     ""5#b.A.A*B*BCCCr   sourcesuppress_errorstuple[Exception]Sequence[Series]c                r    ddl m}  ||                                 |                               ||          S )zNCollect all components for this adapter.

        This is a shortcut.
        r   )ComponentsWithName)$recurring_ical_events.selection.namerF   r   collect_series_from)clsrA   rB   rF   s       r   rH   z$ComponentAdapter.collect_series_fromW   sM     	LKKKKK!!#"4"4"6"6<<PPO
 
 	
r   NTr2   Optional[Time]stopkeep_recurrence_attributesboolc                   | j                                         }t          || j        n|          |d<   |                    dd           | j         t          || j        n|          || j        <   |s| j        D ]	}||v r||= 
| j         j        D ]}|	                    |           d|vrt          |d         j
                  |d<   |S )zECreate a shallow copy of the source event and modify some attributes.NDTSTARTDURATIONRECURRENCE-ID)r#   copyr	   r2   popr.   r6   ATTRIBUTES_TO_DELETE_ON_COPYsubcomponentsadd_componentdt)r$   r2   rK   rL   copied_component	attributesubcomponents          r   as_componentzComponentAdapter.as_componente   s
     ?//11&/em

QV&W&W#Z...(2; Ld3 3T./ * 	4!> 4 4	 000(3 O9 	9 	9L**<8888"22209 +.1 1_-  r   r   c                f    | j                             d          }|dS t          |j                  S )zFThe recurrence ids of the component that might be used to identify it.rQ   Nr   )r#   r>   r   rW   r$   recurrence_ids     r   recurrence_idszComponentAdapter.recurrence_ids   s5     ++O<< 2 !1222r   c                v    | j                             d          }|dS d|j        v r|j        d         dk    S dS )z6The recurrence ids has a thisand future range propertyrQ   NFRANGETHISANDFUTURE)r#   r>   paramsr]   s     r   this_and_futurez ComponentAdapter.this_and_future   sJ     ++O<< 5m*** '0OCCur   c                *    t          | j                  S )z&Whether the adapter is a modification.)rM   r_   r*   s    r   is_modificationz ComponentAdapter.is_modification   s    D'(((r   intc                8    | j                             dd          S )zjThe sequence in the history of modification.

        The sequence is negative if none was found.
        SEQUENCE)r#   r>   r*   s    r   sequencezComponentAdapter.sequence   s     "":r222r   c                r    d| j         j         d| j         d| j         d| j         d| j         d| j         dS )z$Debug representation with more info.<z UID=z start=z recurrence_ids=z
 sequence=z end=>)	__class____name__r@   r2   r_   rk   r6   r*   s    r   __repr__zComponentAdapter.__repr__   sl    '  dh  tz  "1 =A] 8  	
r   
list[Time]c                    g }| j                             dg           }t          |t                    s|fn|D ]&}|                    d |j        D                        '|S )zA list of exdates.r   c              3  $   K   | ]}|j         V  d S NrW   ).0exdates     r   	<genexpr>z+ComponentAdapter.exdates.<locals>.<genexpr>   s$      >>&)>>>>>>r   r#   r>   
isinstancelistextenddts)r$   resultexdatess      r   r   zComponentAdapter.exdates   so      /%%h33)3GT)B)BOzz 	? 	?GMM>>'+>>>>>>>r   set[str]c                    | j                             dd          }|st                      S d t          |t                    r|n|gD             S )z#A list of rrules of this component.r   Nc                Z    h | ](}|                                                                 )S r   )to_icaldecode)rw   rrules     r   	<setcomp>z*ComponentAdapter.rrules.<locals>.<setcomp>   s<     
 
 
 MMOO""$$
 
 
r   )r#   r>   setr{   r|   )r$   ruless     r   rruleszComponentAdapter.rrules   sb     ##GT22 	55L
 
#-eT#:#:G%%
 
 
 	
r   list[Time, tuple[Time, Time]]c                    | j                             dg           }g }t          |t                    s|fn|D ]&}|                    d |j        D                        '|S )z$A list of rdates, possibly a period.r   c              3  $   K   | ]}|j         V  d S ru   rv   )rw   rdates     r   ry   z*ComponentAdapter.rdates.<locals>.<genexpr>   s$      ;;u%(;;;;;;r   rz   )r$   rdatesr   s      r   r   zComponentAdapter.rdates   so     $$Wb11'1&$'?'?KviiV 	< 	<FMM;;
;;;;;;;r   datetime.timedeltac                     | j         | j        z
  S )zThe duration of the component.)r6   r2   r*   s    r   durationzComponentAdapter.duration   s     x$*$$r   
span_start	span_stopc                :    t          ||| j        | j                  S )z,Return whether the component is in the span.)r   r2   r6   )r$   r   r   s      r   
is_in_spanzComponentAdapter.is_in_span   s    '
Itz48TTTr   -tuple[datetime.timedelta, datetime.timedelta]c                    | j         }t          j        d          }| j                            d          }|r;t          | j        | j        |j        f          \  }}}||k     r||z
  }||k    r||z
  }||fS )a  Calculate how much we extend the query span.

        If an event is long, we need to extend the query span by the event's duration.
        If an event has moved, we need to make sure that that is included, too.

        This is so that the RECURRENCE-ID falls within the modified span.
        Imagine if the span is exactly a second. How much would we need to query
        forward and backward to capture the recurrence id?

        Returns two positive spans: (subtract_from_start, add_to_stop)
        r   rQ   )	r   datetime	timedeltar#   r>   r   r2   r6   rW   )r$   subtract_from_startadd_to_stoprecurrence_id_propr2   r6   r^   s          r   extend_query_span_byz%ComponentAdapter.extend_query_span_by   s     #m(++!_00AA 	:(7TX'9'<=) )%E3 }$$+e3}$$&)M&9#"K//r   c                    | j         rI| j                            d          }|s
J d            t          | j        |j        f          \  }}||z
  S t          j        d          S )a  Occurrences of this component should be moved by this amount.

        Usually, the occurrence starts at the new start time.
        However, if we have a RANGE=THISANDFUTURE, we need to move the occurrence.

        RFC 5545:

            When the given recurrence instance is
            rescheduled, all subsequent instances are also rescheduled by the
            same time difference.  For instance, if the given recurrence
            instance is rescheduled to start 2 hours later, then all
            subsequent instances are also rescheduled 2 hours later.
            Similarly, if the duration of the given recurrence instance is
            modified, then all subsequence instances are also modified to have
            this same duration.
        rQ   z*RANGE=THISANDFUTURE implies RECURRENCE-ID.r   )rd   r#   r>   r   r2   rW   r   r   )r$   r   r2   r^   s       r   move_recurrences_byz$ComponentAdapter.move_recurrences_by   ss    $  	)!%!4!4_!E!E%SS'SSSS#2DJ@R@U3V#W#W E==((!!$$$r   )r   r   )r!   r   )r   r&   )r   r,   )r   r   )r   r   )rA   r   rB   rC   r   rD   )NNT)r2   rJ   rK   rJ   rL   rM   )r   r   )r   rM   )r   rg   )r   rr   )r   r   )r   r   )r   r   )r   r   r   r   r   rM   )r   r   )"rp   
__module____qualname____doc__rT   staticmethodr   r   r%   propertyr+   r.   r2   r6   r
   r1   r8   r9   r@   classmethodrH   r[   r_   rd   rf   rk   rq   r   r   r   r   r   r   r   r   r   r   r   r      s       @@#?#?#? , , , ^ \,$ $ $ $ . . . X.    X    X    X   _ : : ^ X: : : ^ X: D D D XD 
 
 
 [
 !%#+/	         4 3 3 3 _3    _) ) ) ) 3 3 3 _3
 
 
 
    _ 
 
 
 _
    _ % % % _%U U U U 0 0 0 _02 % % % _% % %r   r   )r   
__future__r   r   abcr   r   typingr   r   r   icalendar.propr	   recurring_ical_events.utilr
   r   r   r   	icalendarr   icalendar.calr   recurring_ical_events.seriesr   recurring_ical_events.typesr   r   r   r   __all__r   r   r   <module>r      sJ   " " " " " " " "  # # # # # # # # 4 4 4 4 4 4 4 4 4 4 $ $ $ $ $ $             E''''''333333DDDDDDDDDDb% b% b% b% b%s b% b% b%J 
r   