
    F2j)                        d Z ddlmZ ddlmZ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mZ dd	lmZ dd
lmZmZ  G d d          ZdS )z%Parsing a component's iCalendar data.    )annotations)TYPE_CHECKINGClassVar)ContentlineContentlines)split_on_unescaped_comma)vBroken)tzp)	ComponentComponentFactory)
Parameters)	VPROPERTYTypesFactoryc                      e Zd ZU dZdZded<   	 d1dZded<   d2dZd3dZd Z	d4dZ
d5dZd5dZd6dZd7dZd6d Zed8d"            Zd9d&Zd:d)Zd;d-Zd<d.Zd=d/Zd0S )>ComponentIcalParserzA parser for a component's iCalendar data.

    This uses the template method pattern, where the main parsing
    logic can be refined in subclasses.
    )DTSTARTDTENDzRECURRENCE-IDDUERDATEEXDATEzClassVar[tuple[str, ...]]datetime_namesdatabytes | str | list[Contentline]component_factoryr   types_factoryr   c                H    || _         || _        || _        t          | _        dS )aL  Initialize the parser with the raw data.

        Parameters:
            data: The raw iCalendar data to parse, either as bytes
                or a list of content lines.
            component_factory: The factory to use for creating components.
            types_factory: The factory to use for creating property values.
        N)_data_component_factory_types_factoryr
   _tzp)selfr   r   r   s       g/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/icalendar/parser/ical/component.py__init__zComponentIcalParser.__init__'   s&     
"3+			    zlist[Contentline]_content_linesnamestrreturnboolc                    |                                   d|                                z   t          fd| j        D                       S )z)Check if the parser contains a component.zBEGIN:c              3     K   | ]<}t          |          t                    k    o|                                k    V  =d S N)lenupper).0content_line
begin_lines     r"   	<genexpr>z9ComponentIcalParser.contains_component.<locals>.<genexpr>@   s`       
 
 Z0W\5G5G5I5IZ5W
 
 
 
 
 
r$   )initialize_parsingr.   anyr%   )r!   r&   r1   s     @r"   contains_componentz&ComponentIcalParser.contains_component<   sb    !!!

,
 
 
 
 
 $ 3
 
 
 
 
 	
r$   uidc                l    |                                   t          fd| j        D                       S )zDetermines whether the component contains a ``uid``.

        Returns:
            ``True`` if the component contains a ``uid``, else ``False``.
        c              3      K   | ]}|v V  	d S r,    )r/   liner6   s     r"   r2   z3ComponentIcalParser.contains_uid.<locals>.<genexpr>L   s'      ??43$;??????r$   )r3   r4   r%   )r!   r6   s    `r"   contains_uidz ComponentIcalParser.contains_uidE   s=     	!!!????4+>??????r$   c                    g | _         g | _        t          | j        t                    r| j        nt          j        | j                  x| _        | _        t          | j                  | _	        d S r,   )
_stack_components
isinstancer   listr   	from_icalr%   iter_content_lines_iteratorr!   s    r"   r3   z&ComponentIcalParser.initialize_parsingN   sf    '),. $*d++4DJJ'
33	

T(
 (,D,?'@'@$$$r$   	exception	Exceptionc                |    | j         }|r|j        s||j                            dt	          |          f           dS )zHandle a line parsing error.N)	componentignore_exceptionserrorsappendr'   )r!   rE   rH   s      r"   handle_line_parse_errorz+ComponentIcalParser.handle_line_parse_errorX   sJ    
 N	 		 ; 	Os9~~ 677777r$   valsNonec                    |                                 }| j                            |          } |            }t          |dd          s||_        | j                            |           dS )z$Handle the beginning of a component.r&    N)r.   r   get_component_classgetattrr&   r=   rK   )r!   rM   c_namec_classrH   s        r"   handle_begin_componentz*ComponentIcalParser.handle_begin_componentb   sk     )==fEE
 GII	y&"-- 	$#IN9%%%%%r$   c                P   | j         st          d          | j                                         }| j         s| j                            |           n | j         d                             |           |                                dk    rd|v rt          j        |           dS dS dS )zHandle the end of a component.z.END encountered without an accompanying BEGIN!	VTIMEZONETZIDN)	r=   
ValueErrorpopr>   rK   add_componentr.   r
   cache_timezone_component)r!   rM   rH   s      r"   handle_end_componentz(ComponentIcalParser.handle_end_componentq   s     { 	OMNNNKOO%%	{ 	5##I....KO)))444::<<;&&6Y+>+>(33333 '&+>+>r$   c                    dS )z_Prepare the parsed components.

        This is called when all components are parsed.
        Nr9   rD   s    r"   prepare_componentsz&ComponentIcalParser.prepare_components   s      r$   list[Component]c                    |                                   |                                  |                                  | j        S )zParse the raw data.)r3   parse_content_linesr`   r>   rD   s    r"   parsezComponentIcalParser.parse   sA    !!!  """!!!r$   c                t   | j         D ]}|s	 |                                \  }}}n,# t          $ r}|                     |           Y d}~Cd}~ww xY w|                                }|dk    r|                     |           {|dk    r|                     |           |                     ||||           dS )zParse the content lines.NBEGINEND)rC   partsrZ   rL   r.   rU   r^   handle_property)r!   r:   r&   paramsrM   eunames          r"   rc   z'ComponentIcalParser.parse_content_lines   s    0 	@ 	@D %)ZZ\\"fdd   ,,Q/// JJLLE++D1111%))$////$$UFD$????	@ 	@s   '
AAAComponent | Nonec                .    | j         r| j         d         nd S )NrW   )r=   rD   s    r"   rH   zComponentIcalParser.component   s    "&+7t{247r$   rj   r   r   c                B    | j                             ||j                  S )zGet the factory for a property.)r   for_propertyvalue)r!   r&   rj   s      r"   get_factory_for_propertyz,ComponentIcalParser.get_factory_for_property   s    "//flCCCr$   r:   r   c                l   | j         s|dk    rdS t          d| d          |r|| j        v r|                    d          nd}|dk    r|                     |||          rdS |g}n.|dk    r|                    d          }n|d	k    r	|d
k    rg }n|g}|D ]}|                     |||||           dS )a4  Handle a property line.

        Add properties to the top of the current stack.

        Parameters:
            name: The name of the property, uppercased.
            params: The parameters of the property.
            vals: The value of the property.
            line: The original content line.
        z	X-COMMENTNz
Property "z#" does not have a parent component.rY   
CATEGORIESFREEBUSY,r   rP   )rH   rZ   r   gethandle_categoriessplitparse_and_add_property)r!   r&   rj   rM   r:   tzid	vals_listvals           r"   ri   z#ComponentIcalParser.handle_property   s    ~ 	U {""S$SSSTTT%+U8K0K0Kvzz&!!!QU <%%fdD99 IIZ

3II W__III  	G 	GC''fc4FFFF	G 	Gr$   r}   r{   
str | Nonec                ^   |                      ||          }	 |r|                    ||          }n|                    |          } ||          }||_        | j                            ||d           dS # t
          t          f$ r$}	|                     |	||||           Y d}	~	dS d}	~	ww xY w)z;Parse a property value and add it to the current component.FencodeN)rr   rA   rj   rH   addrZ   	TypeErrorhandle_property_parse_error)
r!   r&   rj   r}   r{   r:   factory
parsed_val	vals_instrk   s
             r"   rz   z*ComponentIcalParser.parse_and_add_property   s     //f==
	> 4$..sD99

$..s33
  
++I%INtYu===== I& 	I 	I 	I,,Qfc4HHHHHHHHH	Is   .A7 7B,B''B,c                x   | j         j        s"|dd                                         dk    s||                     ||          }t	          |dd          }t          j        |||||          }| j         j                            |t          |          f           | j         
                    ||d           dS )	z&Handle the parse error for a property.N   zX-__name__unknown)	raw_valuerj   property_nameexpected_typeerrorr   r   )rH   rI   r.   rr   rR   r	   from_parse_errorrJ   rK   r'   r   )	r!   rE   r&   rj   r}   r:   r   r   broken_props	            r"   r   z/ComponentIcalParser.handle_property_parse_error   s     ~/ 	RaR8H8HD8P8PO//f==Y??.'
 
 
 	$$dC	NN%;<<<4Q77777r$   c                   t          |          }|                    d          }|dk    r||dz   d         }	 t          |          }|                     d|          } ||          }	||	_        | j                            d|	d           n0# t          $ r#}
|                     |
d|||           Y d}
~
nd}
~
ww xY wdS dS )	z{Handle the special case of CATEGORIES property.

        Returns:
            ``True`` if handled, else ``False``.
        :r      Nrt   r   TF)	r'   rfindr   rr   rj   rH   r   rZ   r   )r!   rj   rM   r:   line_str	colon_idxr   category_listr   r   rk   s              r"   rx   z%ComponentIcalParser.handle_categories  s    t99 NN3''	q== Q1I	 8 C C77fMM#GM22	#)	 ""<1"EEEE   00|VY        4us   AB 
B;B66B;N)r   r   r   r   r   r   )r&   r'   r(   r)   )r6   r'   r(   r)   )rE   rF   )rM   r'   r(   rN   )r(   rN   )r(   ra   )r(   rm   )r&   r'   rj   r   r(   r   )
r&   r'   rj   r   rM   r'   r:   r   r(   rN   )
r&   r'   rj   r   r}   r'   r{   r~   r:   r   )
rE   rF   r&   r'   rj   r   r}   r'   r:   r   )rj   r   rM   r'   r:   r   r(   r)   )r   
__module____qualname____doc__r   __annotations__r#   r5   r;   r3   rL   rU   r^   r`   rd   rc   propertyrH   rr   ri   rz   r   rx   r9   r$   r"   r   r      s         1N       & &%%%
 
 
 
@ @ @ @A A A8 8 8 8& & & &4 4 4 4           @ @ @ @& 8 8 8 X8D D D D)G )G )G )GV> > > >,8 8 8 80     r$   r   N)r   
__future__r   typingr   r   icalendar.parser.content_liner   r   icalendar.parser.propertyr   icalendar.propr	   icalendar.timezoner
   icalendar.calr   r   icalendar.parser.parameterr   r   r   r   r9   r$   r"   <module>r      s	   + + " " " " " " * * * * * * * * C C C C C C C C > > > > > > " " " " " " " " " " " " 79999999955555566666666N N N N N N N N N Nr$   