
    F2j,                        d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 erddl
mZ  G d d	e	          Z G d
 d          Zd	dgZdS )z(Special parsing for calendar components.    )annotations)TYPE_CHECKINGClassVar)Contentline   )ComponentIcalParser)	Componentc                  J     e Zd ZU dZdZded<   	  fdZdd
ZddZd Z	 xZ
S )LazyCalendarIcalParsera  A parser for calendar components.

    Instead of parsing the components, LazyComponents are created.
    Parsing can happen on demand.

    A calendar may grow over time, with its subcomponents greatly increasing
    in number, and requiring more memory and time to fully parse. This
    optimization lazily parses calendar files without consuming more memory
    than necessary, reducing the initial time it takes to access meta data.
    )	VCALENDARzClassVar[tuple[str, ...]]parse_instantlyc                    |                                 }|| j        v s | j        r| j                                        s#t	                                          |           dS |                     |           dS )zIBegin a new component.

        This may be the first component.
        N)upperr   	componentis_lazysuperhandle_begin_componenthandle_lazy_begin_component)selfvalsc_name	__class__s      b/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/icalendar/parser/ical/lazy.pyr   z-LazyCalendarIcalParser.handle_begin_component!   sz    
 d***> +>))++ +
 GG**400000,,V44444    component_namestrreturnNonec                   t          d|           g}| j        D ]k}|                    |           |dd                                         dk    r4|dd                                                                         |k    r nl| j        t          d| d          | j                            t          || 	                    |                               dS )zBegin a new component, but do not parse it yet.

        Parameters:
            component_name:
                The upper case name of the component, for example, ``"VEVENT"``.
        zBEGIN:N   zEND:z+ encountered outside of a parent component.)
r   _content_lines_iteratorappendr   stripr   
ValueErroradd_componentLazySubcomponentget_subcomponent_parser)r   r   content_lineslines       r   r   z2LazyCalendarIcalParser.handle_lazy_begin_component1   s     %%>n%>%>??@0 	 	D  &&&RaR  F**HNN$$**,,>>>!TTTT   	$$,,];; 	
 	
 	
 	
 	
r   r(   list[Contentline]r   c                8    t          || j        | j                  S )zGet the parser for a subcomponent.

        Parameters:
            content_lines: The content lines of the subcomponent.
        )r   _component_factory_types_factory)r   r(   s     r   r'   z.LazyCalendarIcalParser.get_subcomponent_parserK   s#     #42D4G
 
 	
r   c                    dS )z%Prepare the lazily parsed components.N r   s    r   prepare_componentsz)LazyCalendarIcalParser.prepare_componentsW   s      r   )r   r   r   r   )r(   r*   r   r   )__name__
__module____qualname____doc__r   __annotations__r   r   r'   r1   __classcell__)r   s   @r   r   r      s         	 	 2@O????
5 5 5 5 5 
 
 
 
4

 

 

 

4 4 4 4 4 4 4r   r   c                  b    e Zd ZdZddZedd            Zdd
ZddZddZ	ddZ
ddZddZdS )r&   zwA subcomponent that is evaluated lazily.

    This class holds the raw data of the subcomponent ready for parsing.
    namer   parserr   c                0    || _         || _        d| _        dS )z3Initialize the lazy subcomponent with the raw data.N)_name_parser
_component)r   r9   r:   s      r   __init__zLazySubcomponent.__init__a   s    
,0r   r   c                    | j         S )zdThe name of the subcomponent.

        The name is uppercased, per :rfc:`5545#section-2.1`.
        )r<   r0   s    r   r9   zLazySubcomponent.nameg   s     zr   boolc                    | j         duS )z2Return whether the subcomponent is already parsed.N)r>   r0   s    r   	is_parsedzLazySubcomponent.is_parsedo   s    d**r   r	   c                    | j         `| j                                        }t          |          dk    r t	          dt          |           d          |d         | _         d| _        | j         S )z,Parse the raw data and return the component.Nr   z<Expected exactly one component in the subcomponent, but got .r   )r>   r=   parselenr$   )r   
componentss     r   rF   zLazySubcomponent.parses   sy    ?"++--J:!## 2":2 2 2   )mDODLr   c                    dS )zReturn whether the subcomponents were accessed and parsed lazily.

        Call :meth:`parse` to get the fully parsed component.
        Tr/   r0   s    r   r   zLazySubcomponent.is_lazy   s	    
 tr   c                B    d| j          d|                                  dS )NzLazySubcomponent(name=z	, parsed=))r<   rC   r0   s    r   __repr__zLazySubcomponent.__repr__   s'    P
PPT^^=M=MPPPPr   list[Component]c                    t          |t                    st          d          || j        k    s!| j        rA| j                            |          r'|                                                     |          S g S )zWalk through this component.

        This only parses the component if necessary.

        Parameters:
            name: The name to match for all components in the calendar,
                then walk through and parse the resulting matches.
        zname must be a string.)
isinstancer   	TypeErrorr9   r=   contains_componentrF   walk)r   r9   s     r   rR   zLazySubcomponent.walk   sw     $$$ 	6455549L !\<<TBB  ::<<$$T***	r   uidc                    | j         r| j                             |          sg S |                                                     |          S )zReturn the components containing the given ``uid``.

        This only parses the component if necessary.

        Parameters:
            uid: The UID of the components.
        )r=   contains_uidrF   with_uid)r   rS   s     r   rV   zLazySubcomponent.with_uid   sF     < 	 9 9# > > 	Izz||$$S)))r   N)r9   r   r:   r   )r   r   )r   rA   )r   r	   )r9   r   r   rM   )rS   r   r   rM   )r2   r3   r4   r5   r?   propertyr9   rC   rF   r   rL   rR   rV   r/   r   r   r&   r&   [   s         
1 1 1 1    X+ + + +      Q Q Q Q   "
* 
* 
* 
* 
* 
*r   r&   N)r5   
__future__r   typingr   r   icalendar.parser.content_liner   r   r   icalendar.cal.componentr	   r   r&   __all__r/   r   r   <module>r]      s    . . " " " " " " * * * * * * * * 5 5 5 5 5 5 * * * * * * 2111111I4 I4 I4 I4 I40 I4 I4 I4XJ* J* J* J* J* J* J* J*Z $%7
8r   