
    i8                       U d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
mZmZ 	 ddlmZ n# e$ r	 ddlmZ Y nw xY wddlZddlm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 ddlmZ ddl 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( ej)        dk    ree*e+e         z  ez           Z,de-d<   nee*         Z,de-d<    G d d          Z.ddgZ/dS )z:Core functionality: querying the calendar for occurrences.    )annotationsN)TYPE_CHECKINGClassVar	GeneratorOptionalSequence)	TypeAlias)ComponentAdapter)DATE_MAX_DTDATE_MIN_DT)BadRuleStringFormatInvalidCalendarPeriodEndBeforeStart)OccurrenceID)Pages)SelectComponents)compare_greater)	Component)
Occurrence)Series)DateArgumentTime)   
   r	   T_COMPONENTSc                  Z   e Zd ZU dZeeej        gZde	d<   ddl
mZ 	 	 	 d8d9dZed:d            Zd;dZ ej        d           ej        d           ej        d           ej        d          gZd:dZd<dZd=d#Zd>d&Zd?d'Zd@d)ZdAd+ZdBd-ZedCd.            Z	 	 	 dDdEd7Zd/S )FCalendarQuerya  Query a calendar for occurrences.

    Functions like :meth:`at`, :meth:`between` andm :meth:`after`
    can be used to query the selected components.
    If any malformed icalendar information is found,
    an :class:`InvalidCalendar` exception is raised.
    For other bad arguments, you should expect a :class:`ValueError`.

    Attributes:
        suppressed_errors: a list of errors to suppress when
            skip_bad_series is True
    zClassVar[type[Exception]]suppressed_errorsr   )ComponentsWithNameFVEVENTcalendarr   keep_recurrence_attributesbool
componentsr   skip_bad_seriesc                x   || _         |                    dd          dk    rt          d          g | _        |rt	          | j                  nd| _        |D ]b}t          |t                    r| 	                    |          }n|}| j        
                    |                    || j                             cdS )aA  Create an unfoldable calendar from a given calendar.

        Arguments:
            calendar: an :class:`icalendar.cal.Calendar` component like
                :class:`icalendar.cal.Calendar`.
            keep_recurrence_attributes: Whether to keep attributes that are only used
                to calculate the recurrence (``RDATE``, ``EXDATE``, ``RRULE``).
            components: A list of component type names of which the recurrences
                should be returned. This can also be instances of
                :class:`SelectComponents`.
                Examples: ``("VEVENT", "VTODO", "VJOURNAL", "VALARM")``
            skip_bad_series: Whether to skip series of components that contain
                errors. You can use :attr:`CalendarQuery.suppressed_errors` to
                specify which errors to skip.
        CALSCALE	GREGORIANz'Only Gregorian calendars are supported. N)r#   getr   seriestupler   _skip_errors
isinstancestrr   extendcollect_series_from)selfr"   r#   r%   r&   component_adapter_idcomponent_adapters          V/root/projects/butler/venv/lib/python3.11/site-packages/recurring_ical_events/query.py__init__zCalendarQuery.__init__D   s    , +E'<<
K00K??!"KLLL$&=LTE$"8999RT$. 	 	 .44 9$($;$;<P$Q$Q!!$8!K!55h@QRR   	 	    dater   c                |   t          | t                    r| f} t          | t                    r&| ddt          |           z
  z  z  } t	          j        |  S t          | t
                    rSt          |           dk    r t          j                            | d          S t          j                            | d          S | S )a  Convert date inputs of various sorts into a datetime object.

        Arguments:
            date: A date specification.

        Date Specification:

        - a year like ``(2019,)`` or ``2019`` (:class:`int`)
        - a month like ``(2019, 1)`` for January of 2019
        - a day like ``(2019, 1, 19)`` for the first of January 2019
        - a day with hours, ``(2019, 1, 19, 1)``
        - a day with minutes, ``(2019, 1, 19, 13, 30 )``
        - a day with seconds, ``(2019, 1, 19, 13, 30, 59)``
        - a :class:`datetime.datetime` or :class:`datetime.date`
        - a :class:`str` in the format ``yyyymmdd``
        - a :class:`str` in the format ``yyyymmddThhmmssZ``

        )   r      z%Y%m%dz%Y%m%dT%H%M%SZ)r/   intr-   lendatetimer0   strptime)r9   s    r6   to_datetimezCalendarQuery.to_datetimej   s    ( dC   	7DdE"" 	,DAD		M**D$d++dC   	F4yyA~~(11$AAA$--d4DEEEr8   returnGenerator[Component]c                6    |                      t                    S )zGenerate all Components.

        The Components are sorted from the first to the last Occurrence.
        Calendars can contain millions of Occurrences. This iterates
        safely across all of them.
        )afterr   )r3   s    r6   allzCalendarQuery.all   s     zz+&&&r8   r;   days)hoursminutes)secondsc                   t          |t                    r|f}t          |t                    rt          |          dk    s|                                st          d|d          t          |dd         d          t          |dd         d          t          |dd                   f}t          |t          j                  r|                     ||          S t          |t          j                  r,|                     ||t          j	        d	          z             S t          |          dk    r+|                     |d
         ddf|d
         dz   ddf          S t          |          dk    rI|\  }}|dk    r|                     |ddf|dz   ddf          S |                     ||df||dz   df          S | 
                    |          }|                     ||| j        t          |          dz
           z             S )a  Return all events within the next 24 hours of starting at the given day.

        Arguments:
            date: A date specification, see :meth:`to_datetime`.

        This is translated to :meth:`between` in the following way:

        - A year returns all occurrences within that year.
            Example: ``(2019,)``, ``2019``
        - A month returns all occurrences within that month.
            Example: ``(2019, 1)``
        - A day returns all occurrences within that day.
            Examples:

            - ``(2019, 1, 19)``
            - ``datetime.date(2019, 1, 19)``
            - ``"20190101"``

        - An hour returns all occurrences within that hour.
            Example: ``(2019, 1, 19, 1)``
        - A minute returns all occurrences within that minute.
            Example: ``(2019, 1, 19, 13, 30 )``
        - A second returns all occurrences at that exact second.
            Examples:

            - ``(2019, 1, 19, 13, 30, 59)``
            - ``datetime.datetime(2019, 1, 19, 13, 30, 59)``
            - ``datetime.datetime(2019, 1, 19, tzinfo=datetime.timezone.utc)``,
            - ``datetime.datetime(2019, 1, 19, 13, 30, 59, tzinfo=ZoneInfo('Europe/London'))``
            - ``"20190119T133059Z"``
        r<   zFormat yyyymmdd expected for .N   r      r;   rG   r         r   )r/   r=   r0   r>   isdigit
ValueErrorr?   betweenr9   	timedeltarA   _between_DELTAS)r3   r9   yearmonthdts        r6   atzCalendarQuery.at   s   @ dC   	7DdC   	J4yyA~~T\\^^~ !J!J!J!JKKKRaR"%%s4!9b'9'93tABBx==IDdH-.. 	,<<d+++dHM** 	I<<dX-?Q-G-G-G&GHHHt99>><<a!Q$q'A+q!1DEEEt99>>KD%{{||T2qMD1Ha3CDDD<<ua 04A2FGGGd##}}Rdl3t99q=&A!ABBBr8   startstop!DateArgument | datetime.timedeltac                    |                      |          }t          |t          j                  r||z   n|                      |          }|                     ||          S )a  Return events at a time between start (inclusive) and end (inclusive)

        Arguments:
            start: A date specification. See :meth:`to_datetime`.
            stop: A date specification or a :class:`datetime.timedelta`
                relative to start.

        .. warning::

            If you pass a :class:`datetime.datetime` to both ``start`` and
            ``stop``, make sure the :attr:`datetime.datetime.tzinfo` is
            the same.
        )rA   r/   r?   rV   rW   )r3   r]   r^   s      r6   rU   zCalendarQuery.between   sb       '' $ 233(EDLL!!$'' 	
 }}UD)))r8   occurrenceslist[Occurrence]list[Component]c                       fd|D             S )zMap occurrences to components.c                D    g | ]}|                     j                  S r*   )as_componentr#   ).0
occurrencer3   s     r6   
<listcomp>z<CalendarQuery._occurrences_to_components.<locals>.<listcomp>   s:     
 
 
 ##D$CDD
 
 
r8   r*   )r3   ra   s   ` r6   _occurrences_to_componentsz(CalendarQuery._occurrences_to_components   s.    
 
 
 
)
 
 
 	
r8   r   endc                T    |                      |                     ||                    S )z5Return the occurrences between the start and the end.)rj   _occurrences_between)r3   r]   rk   s      r6   rW   zCalendarQuery._between   s'    ..t/H/HPS/T/TUUUr8   c                    g }| j         D ]\}t          j        | j                  5  |                    |                    ||                     ddd           n# 1 swxY w Y   ]|S )z4Return the components between the start and the end.N)r,   
contextlibsuppressr.   r1   rU   )r3   r]   rk   ra   r,   s        r6   rm   z"CalendarQuery._occurrences_between   s    (*k 	? 	?F$T%677 ? ?""6>>%#=#=>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ?s   *AA	"A	earliest_endc              #     K   |                      |          }|                     |          D ]}|                    | j                  V  dS )a  Iterate over components happening during or after earliest_end.

        Arguments:
            earliest_end: A date specification. See :meth:`to_datetime`.
                Anything happening during or after earliest_end is returned
                in the order of start time.
        N)rA   _afterrf   r#   )r3   rq   rh   s      r6   rE   zCalendarQuery.after   sd       ''55++l33 	K 	KJ))$*IJJJJJJ	K 	Kr8   Generator[Occurrence]c              #    K   t          j        d          }t          j        d          }d}t                      }|s	 ||z   }n,# t          $ r t          }t          ||          rY dS d}Y nw xY w|                     ||          }|                                 |D ])}|j        |vr|V  |	                    |j                   *t          |r|dz  n|dz  |          }|}|dS dS )	z@Iterate over occurrences happening during or after earliest_end.r;   rG      rJ   FNTrQ   )r?   rV   setOverflowErrorr   r   rm   sortidaddmax)	r3   rq   	time_spanmin_time_spandone
result_idsnext_endra   rh   s	            r6   rs   zCalendarQuery._after  sF     &A...	 *2666(+
 	$')3    &"<:: FF 33L(KKK) 2 2
=
22$$$$NN:=111!,?	A)a- I $L)  	$ 	$ 	$ 	$ 	$s    A !A/*A/.A/r=   c                B    d}|                                  D ]}|dz  }|S )zReturn the amount of recurring components in this calendar.

        .. warning::

            Do not use this in production as it generates all occurrences.
        r   r;   )rF   )r3   i_s      r6   countzCalendarQuery.count#  s0      	 	AFAAr8   c                T    |                                  D ]}|c S t          d          )zReturn the first recurring component in this calendar.

        Returns:
            The first recurring component in this calendar.

        Raises:
            IndexError: if the calendar is empty
        zNo components found.)rF   
IndexError)r3   	components     r6   firstzCalendarQuery.first/  s6      	 	I/000r8   N 	page_sizeOptional[DateArgument]latest_startnext_page_idr0   r   c                f   |dn|                      |          }|t          n|                      |          }|rt          j        |          }t	          ||j                  s|                     |j                  }g }|D ]m}|                    |           |j        }	|	|k    rt          j
        |g|          } n2t	          |	j        |j                  rt          j
        ||          } nnn+|                     |          }n|                     |          }t          |||| j                  S )at  Return pages for pagination.

        Args:
            page_size: the number of components per page
            earliest_end: the start of the first page
                All components occur after this date.
                See :meth:`to_datetime` for possible values.
            latest_start: the end of the last page
                All components occur before this date.
                See :meth:`to_datetime` for possible values.
            next_page_id: The id of the next page.
                This is optional for the first page.
                These are safe to pass outside of the application and back in.
        N)occurrence_iteratorsizer^   r#   )rA   r   r   from_stringr   r]   rs   appendrz   	itertoolschainr   r#   )
r3   r   rq   r   r   first_occurrence_iditeratorlost_occurrencesrh   oids
             r6   paginatezCalendarQuery.paginate=  sZ   *  ,3tt9I9I,9W9W'/KKT5E5El5S5S 	  	1".":<"H"H"<1D1JKK 5;;':'@AA#% "*  J$++J777$-C111#,?J<#J#J&sy2E2KLL #,?3CX#N#N  ;;|44{{<00H ('+'F	
 
 
 	
r8   )Fr    F)r"   r   r#   r$   r%   r   r&   r$   )r9   r   )rB   rC   )r]   r   r^   r_   )ra   rb   rB   rc   )r]   r   rk   r   rB   rc   )r]   r   rk   r   rB   rb   )rq   r   rB   rC   )rq   r   rB   rt   )rB   r=   )rB   r   )NNr   )
r   r=   rq   r   r   r   r   r0   rB   r   )__name__
__module____qualname____doc__r   r   	icalendarr   r   __annotations__$recurring_ical_events.selection.namer   r7   staticmethodrA   rF   r?   rV   rX   r\   rU   rj   rW   rm   rE   rs   r   propertyr   r   r*   r8   r6   r   r   /   s          	!4    
 HGGGGG
 ,1#. %$ $ $ $ $L    \>' ' ' ' 	"""###1%%%1%%%	G2C 2C 2C 2Ch* * * *,
 
 
 
V V V V   
K 
K 
K 
K$ $ $ $:
 
 
 
 1 1 1 X1  04/30
 0
 0
 0
 0
 0
 0
r8   r   )0r   
__future__r   ro   r?   r   systypingr   r   r   r   r   r	   ImportErrortyping_extensionsr   (recurring_ical_events.adapters.componentr
   recurring_ical_events.constantsr   r   recurring_ical_events.errorsr   r   r    recurring_ical_events.occurrencer   recurring_ical_events.pagesr   $recurring_ical_events.selection.baser   recurring_ical_events.utilr   r   r   recurring_ical_events.seriesr   recurring_ical_events.typesr   r   version_infor0   typer   r   r   __all__r*   r8   r6   <module>r      sm   @ @ @ " " " " " "          



 I I I I I I I I I I I I I I,        , , ,++++++++,     E E E E E E D D D D D D D D         
 : 9 9 9 9 9 - - - - - - A A A A A A 6 6 6 6 6 6 ######;;;;;;333333       
 w&sT2B-C'CFV'VWLWWWWW 'smL++++~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
B
 ?
+s   0 >>