o
    .j}(                     @  s.  d Z ddlmZ ddlmZ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 G dd dZG dd dZG dd dZG dd deZdgZedkrddlZedZ
dZe
 je9  _e
 ZdddZeefD ]Z e!de j e!ejde" dd qdS dS ) z*Components for lazy parsing of components.    )annotations)TYPE_CHECKINGLiteral)ComponentFactory)LazyCalendarIcalParser   )Calendar)Callable)ComponentIcalParser)LazySubcomponent)	Componentc                   @  sT   e Zd ZdZ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 )ParsedSubcomponentsStrategyz9All the subcomponents are parsed and available as a list.c                 C  s
   g | _ d S )N_componentsself r   G/home/thesage/.local/lib/python3.10/site-packages/icalendar/cal/lazy.py__init__   s   
z$ParsedSubcomponentsStrategy.__init__return3tuple[ParsedSubcomponentsStrategy, list[Component]]c                 C  s
   | | j fS )z&Get the subcomponents of the calendar.r   r   r   r   r   get_all_components   s   
z.ParsedSubcomponentsStrategy.get_all_components
componentslist[Component]c                 C  s
   || _ | S &Set the subcomponents of the calendar.r   r   r   r   r   r   set_components   s   z*ParsedSubcomponentsStrategy.set_components	componentr   c                 C  s   | j |  | S z Add a component to the calendar.)r   appendparser   r   r   r   r   add_component&   s   z)ParsedSubcomponentsStrategy.add_componentLiteral[False]c                 C     dS )z>Returns ``False`` because subcomponents are not lazily parsed.Fr   r   r   r   r   is_lazy+      z#ParsedSubcomponentsStrategy.is_lazynamestrc                 C  &   g }| j D ]	}|||7 }q| |fS )z:Get the subcomponents of the calendar with the given name.)r   walkr   r(   resultr   r   r   r   r+   /   s   
z ParsedSubcomponentsStrategy.walkc                 C  r*   )z9Get the subcomponents of the calendar with the given uid.)r   with_uidr,   r   r   r   r.   6   s   
z$ParsedSubcomponentsStrategy.with_uidNr   r   r   r   r   r   )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                   @  s|   e Zd ZU dZdZded< 	 dd Ze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-d#d$Zd%S ).LazySubcomponentsStrategyz'Parse subcomponents only when accessed.)	VTIMEZONEztuple[str, ...]initial_components_to_parsec                 C  s   g | _ d| _d S )NF)r   _initial_parsedr   r   r   r   r   F   s   
z"LazySubcomponentsStrategy.__init__r   r   c                 C  s   t  dd | jD S )zReturn the parsed components.c                 S  s   g | ]}|  qS r   )r!   .0r   r   r   r   
<listcomp>N   s    z7LazySubcomponentsStrategy.as_parsed.<locals>.<listcomp>)r   r   r   r   r   r   r   	as_parsedJ   s   z#LazySubcomponentsStrategy.as_parsedr   c                 C  s   |    | j S )zQGet the subcomponents of the calendar.

        Parse all subcomponents.
        )parse_initial_componentsr<   r   r   r   r   r   r   Q   s   
z,LazySubcomponentsStrategy.get_all_componentsr   r   c                 C  s   t  |S r   )r   r   r   r   r   r   r   Y   s   z(LazySubcomponentsStrategy.set_componentsr   Component | LazySubcomponentc                 C  s   | j | | S r   )r   r    r"   r   r   r   r#   _   s   z'LazySubcomponentsStrategy.add_componentboolc                 C  r%   )z6Return whether the subcomponents may be lazily parsed.Tr   r   r   r   r   r&   f   r'   z!LazySubcomponentsStrategy.is_lazyNonec                 C  s4   | j rdS d| _ | jD ]}|j| jv r|  qdS )zParse the components that are required by other components.

        This mainly concerns the timezone components.
        They are required by other components that have a TZID parameter.
        NT)r8   r   r(   r7   r!   r"   r   r   r   r=   j   s   
z2LazySubcomponentsStrategy.parse_initial_componentsr(   
str | None1tuple[LazySubcomponentsStrategy, list[Component]]c                 C  sB   |du r
| j |S |   g }| jD ]	}|||7 }q| |fS )z|Get the subcomponents of the calendar with the given name.

        Parse only the minimal number of subcomponents.
        N)r<   r+   r=   r   r,   r   r   r   r+   w   s   
zLazySubcomponentsStrategy.walkuidr)   c                 C  s.   |    g }| jD ]	}|||7 }q	| |fS )zGet the subcomponents of the calendar with the given ``uid``.

        Parse only the minimal number of subcomponents.
        )r=   r   r.   )r   rC   r-   r   r   r   r   r.      s
   
z"LazySubcomponentsStrategy.with_uidN)r   r   r/   r0   )r   r>   r   r5   r   r?   )r   r@   )r(   rA   r   rB   )rC   r)   r   rB   )r1   r2   r3   r4   r7   __annotations__r   propertyr<   r   r   r#   r&   r=   r+   r.   r   r   r   r   r5   @   s   
 





r5   c                   @  s   e Zd ZdZd	ddZdS )
InitialSubcomponentsStrategyz>Initial strategy for the calendar.

    No subcomponents.
    r   r   r   r5   c                 C  s   |rt dt S )NzpCannot set subcomponents on an uninitialised LazyCalendar. Parse it first or add components via add_component().)
ValueErrorr5   r   r   r   r   r      s
   z+InitialSubcomponentsStrategy.set_componentsN)r   r   r   r5   )r1   r2   r3   r4   r   r   r   r   r   rG      s    rG   c                      s   e Zd ZU dZded< 	  fddZed&dd	Zej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-d$d%Zejje_  ZS ).LazyCalendara  A calendar that can handle big files.

    Subcomponents of this calendar are evaluated lazily,
    meaning that they are not parsed until they are accessed.
    This allows the calendar to handle large files without
    consuming too much memory or time.

    All properties of the calendar component are parsed immediately.
    Subcomponents and their properties are parsed lazily.

    Examples:

        By accessing the :attr:`~icalendar.cal.calendar.Calendar.events` of the calendar,
        only :class:`~icalendar.cal.event.Event` and
        :class:`~icalendar.cal.timezone.Timezone` are immediately parsed.

        .. code-block:: pycon

            >>> from icalendar import LazyCalendar
            >>> calendar = LazyCalendar.example("issue_1050_all_components")
            >>> len(calendar.events) == 1
            True

        The calendar's subcomponents were not parsed because they were not accessed.
        The calendar is still lazy.

            >>> calendar.is_lazy()
            True

        When you access all :attr:`subcomponents` of the calendar,
        for example by getting their count, the entire calendar is
        parsed and becomes not lazy.

            >>> len(calendar.subcomponents)
            5
            >>> calendar.is_lazy()
            False

    zVLazySubcomponentsStrategy | ParsedSubcomponentsStrategy | InitialSubcomponentsStrategy_subcomponentsc                   s   t  | _t j|i | dS )zInitialize the calendar.N)rG   rJ   superr   )r   argskwargs	__class__r   r   r      s   zLazyCalendar.__init__r   r   c                 C  s   | j  \| _ }|S )zThe subcomponents of the calendar.

        Parse all subcomponents of the calendar and return them as a list.

        You can manipulate this list or set it.
        It has the same behavior as in :class:`~icalendar.cal.calendar.Calendar`.
        )rJ   r   )r   r-   r   r   r   subcomponents   s   	zLazyCalendar.subcomponentsvaluer@   c                 C     | j || _ dS )r   N)rJ   r   )r   rQ   r   r   r   rP      s   ststr | bytesr
   c                 C  s   t ||  | jS )z/Get the iCal parser for the given input string.)r   _get_component_factorytypes_factory)clsrS   r   r   r   _get_ical_parser   s   zLazyCalendar._get_ical_parserr   c                 C  s   t  }||  |S )z,Get the component factory for this calendar.)r   add_component_class)rW   factoryr   r   r   rU      s   
z#LazyCalendar._get_component_factoryr   r   c                 C  rR   )zAdd a component to the calendar.

        Use this instead of appending to
        :attr:`~icalendar.cal.lazy.LazyCalendar.subcomponents`,
        as the latter does not parse the whole calendar.
        N)rJ   r#   r"   r   r   r   r#      s   zLazyCalendar.add_componentr?   c                 C  s
   | j  S )a  Whether the subcomponents will be parsed lazily.

        .. note:: If you believe that the calendar parses more than it should,
            please `open an issue <https://github.com/collective/icalendar/issues/new?template=bug_report.md>`_.

        Returns:
            ``True`` if subcomponents are deferred and not yet parsed.
            ``False`` if all subcomponents have been parsed.
        )rJ   r&   r   r   r   r   r&      s   

zLazyCalendar.is_lazyr(   rA   selectCallable[[Component], bool]c                   sN   | j |\| _ } fdd|D }|d u s| j|kr% | r%|d|  |S )Nc                   s   g | ]} |r|qS r   r   r9   r[   r   r   r;     s    z&LazyCalendar._walk.<locals>.<listcomp>r   )rJ   r+   r(   insert)r   r(   r[   r-   r   r]   r   _walk
  s
   zLazyCalendar._walkrC   r)   c                 C  s,   | j |\| _ }| j|kr|d|  |S )Nr   )rJ   r.   rC   r^   )r   rC   r-   r   r   r   r.     s   
zLazyCalendar.with_uid)r   r   )rQ   r   r   r@   )rS   rT   r   r
   )r   r   )r   r   r   r@   rD   )r(   rA   r[   r\   r   r   )rC   r)   r   r   )r1   r2   r3   r4   rE   r   rF   rP   setterclassmethodrX   rU   r#   r&   r_   r.   r   __classcell__r   r   rN   r   rI      s$   
 (

	

	rI   __main__Nissue_1050_all_componentsi'  caltype[Calendar]c                 C  s    |  t} t| jtksJ dS )zCheck out how fast this is.N)	from_icalicsleneventsCOUNT)re   r   r   r   
_benchmark&  s   
rl   zBenchmarking:z_benchmark(cal))globalsnumber)re   rf   )#r4   
__future__r   typingr   r   icalendar.cal.component_factoryr   icalendar.parser.ical.lazyr   calendarr   collections.abcr	   icalendar.parser.ical.componentr
   r   r   r   r   r5   rG   rI   __all__r1   timeitexamplerk   rP   to_icalrh   rl   re   printlocalsr   r   r   r   <module>   s8    +R{

