
    iG                       d Z ddlmZ ddlZddl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 ddlmZ dd	lmZ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  G d de          Z	 d7d8dZ ej         d          Z! ej         d          Z"d9d:dZ# ej         d          Z$d7d;d Z%d<d=d(Z&d>d?d+Z'd@dAd.Z( G d/ d0e          Z) ej         d1          Z*dBd3Z+ ej         d4          Z,dBd5Z-g d6Z.dS )Cz!Functions for parsing parameters.    )annotationsN)datetimetime)TYPE_CHECKINGAnyProtocol)CaselessDict)JCalParsingError)validate_token)DEFAULT_ENCODINGSEQUENCE_TYPES)tzid_from_dt)CallableSequence)VALUE)	VPROPERTYc                      e Zd ZdZddZdS )	HasToIcalz+Protocol for objects with a to_ical method.returnbytesc                    dS )zConvert to iCalendar format.N selfs    U/root/projects/butler/venv/lib/python3.11/site-packages/icalendar/parser/parameter.pyto_icalzHasToIcal.to_ical   s        N)r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r      s.        55     r   r   FvalueSequence[str] | str | HasToIcalalways_quoteboolr   strc                f   t          | t                    r$t          t          t          |           |          S t          | t
                    rt          t	          |           |          S t          t	          |                                                     t                                        S )a.  Convert a parameter value to its iCalendar representation.

    Applies :rfc:`6868` escaping and optionally quotes the value according
    to :rfc:`5545` parameter value formatting rules.

    Parameters:
        value: The parameter value to convert. Can be a sequence, string, or
            object with a ``to_ical()`` method.
        always_quote: If ``True``, always enclose the value in double quotes.
            Defaults to ``False`` (only quote when necessary).

    Returns:
        The formatted parameter value, escaped and quoted as needed.
    r$   )

isinstancer   q_joinmaprfc_6868_escaper&   dquoter   decoder   )r"   r$   s     r   param_valuer/   #   s    " %(( Nc/511MMMM% Ioe,,<HHHH/%--//"8"89I"J"JKKLLLr   z[ -
-",:;]z
[ -
-"]TquotedNonec                n    |rt           nt          }|                    |           rt          |           dS )a,  Validate a parameter value for unsafe characters.

    Checks parameter values for characters that are not allowed according to
    :rfc:`5545`. Uses different validation rules for quoted and unquoted values.

    Parameters:
        value: The parameter value to validate.
        quoted: If ``True``, validate as a quoted value (allows more characters).
            If ``False``, validate as an unquoted value (stricter).
            Defaults to ``True``.

    Raises:
        ValueError: If the value contains unsafe characters for its quote state.
    N)QUNSAFE_CHARUNSAFE_CHARfindall
ValueError)r"   r0   	validators      r   validate_param_valuer8   B   s@     !'7KI     r   u   [,;:’]valc                v    |                      dd          } t                              |           s|rd|  dS | S )ak  Enclose parameter values in double quotes when needed.

    Parameter values containing special characters ``,``, ``;``,
    ``:`` or ``'`` must be enclosed
    in double quotes according to :rfc:`5545`. Double-quote characters in the
    value are replaced with single quotes since they're forbidden in parameter
    values.

    Parameters:
        val: The parameter value to quote.
        always_quote: If ``True``, always enclose in quotes regardless of content.
            Defaults to ``False`` (only quote when necessary).

    Returns:
        The value, enclosed in double quotes if needed or requested.
    "')replaceQUOTABLEsearch)r9   r$   s     r   r-   r-   [   sE    & ++c3

Cs | 3zzzJr   ,stsepmaxsplitint	list[str]c                <   |dk    r| gS g }d}t          |           }d}d}t          |           D ]k\  }}	|	dk    r| }|s-|	|k    r'|                    | ||                    |dz   }|dz  }|dz   |k    s||k    r|                    | |d                     nl|S )aE  Split a string on a separator, respecting double quotes.

    Splits the string on the separator character, but ignores separators that
    appear inside double-quoted sections. This is needed for parsing parameter
    values that may contain quoted strings.

    Parameters:
        st: The string to split.
        sep: The separator character. Defaults to ``,``.
        maxsplit: Maximum number of splits to perform. If ``-1`` (default),
            then perform all possible splits.

    Returns:
        The split string parts.

    Examples:
        .. code-block:: pycon

            >>> from icalendar.parser import q_split
            >>> q_split('a,b,c')
            ['a', 'b', 'c']
            >>> q_split('a,"b,c",d')
            ['a', '"b,c"', 'd']
            >>> q_split('a;b;c', sep=';')
            ['a', 'b', 'c']
    r   r;      N)len	enumerateappend)
rB   rC   rD   resultcursorlengthinquotesplitsichs
             r   q_splitrS   u   s    6 1}}tFFWWFGF2 	 	299!kG 	299MM"VAX,'''UFaKFq5F??f00MM"VWW+&&&E 1 Mr   lstSequence[str]c                F    |                     fd| D                       S )a  Join a list with a separator, quoting items as needed.

    Joins list items with the separator, applying :func:`dquote` to each item
    to add double quotes when they contain special characters.

    Parameters:
        lst: The list of items to join.
        sep: The separator to use. Defaults to ``,``.
        always_quote: If ``True``, always quote all items. Defaults to ``False``
            (only quote when necessary).

    Returns:
        The joined string with items quoted as needed.

    Examples:
        .. code-block:: pycon

            >>> from icalendar.parser import q_join
            >>> q_join(['a', 'b', 'c'])
            'a,b,c'
            >>> q_join(['plain', 'has,comma'])
            'plain,"has,comma"'
    c              3  :   K   | ]}t          |           V  dS )r(   N)r-   ).0itmr$   s     r   	<genexpr>zq_join.<locals>.<genexpr>   s0      JJsF3\:::JJJJJJr   )join)rT   rC   r$   s     `r   r*   r*      s,    0 88JJJJcJJJJJJr   funcCallable | Nonec                ,    fd}| |S  ||           S )a  Create a parameter getter/setter for a single string parameter.

    Parameters:
        upper: Convert the value to uppercase
        func: The function to decorate.

    Returns:
        The property for the parameter or a decorator for the parameter
        if func is ``None``.
    c                    | j         t          j        |           d	fd            }d
fd}d	fdt          ||| j                  S )Nr   
Parametersc                b    |                                }|r|                                }|S )zGet the value.)getupper)r   r"   namerc   s     r   fgetz8single_string_parameter.<locals>.decorator.<locals>.fget   s1     HHTNNE U Lr   r"   
str | Nonec                \    | |            dS r|                                 }|| <   dS )zSet the valueNrc   )r   r"   fdelrd   rc   s     r   fsetz8single_string_parameter.<locals>.decorator.<locals>.fset   s;    }T




 *!KKMME"T


r   c                4    |                      d           dS )zDelete the value.N)pop)r   rd   s    r   ri   z8single_string_parameter.<locals>.decorator.<locals>.fdel   s    HHT4     r   )doc)r   r`   )r   r`   r"   rf   )r   	functoolswrapspropertyr!   )r\   re   rj   ri   rd   rc   s      @@r   	decoratorz*single_string_parameter.<locals>.decorator   s    }				 	 	 	 	 	 
			# 	# 	# 	# 	# 	# 	# 	#	! 	! 	! 	! 	! 	! dDdl;;;;r   r   )r\   rc   rq   s    ` r   single_string_parameterrr      s7    < < < < <4 |9T??r   c                       e Zd ZdZ fdZdZddiZd Zd$d%d
Ze	d&d            Z
 ed          d'd            Zd(dZd&d)dZed*d            Zd Zd+dZe	d,d             Ze	d-d#            Z xZS ).r`   a/  Parser and generator of Property parameter strings.

    It knows nothing of datatypes.
    Its main concern is textual structure.

    Examples:

        Modify parameters:

        .. code-block:: pycon

            >>> from icalendar import Parameters
            >>> params = Parameters()
            >>> params['VALUE'] = 'TEXT'
            >>> params.value
            'TEXT'
            >>> params
            Parameters({'VALUE': 'TEXT'})

        Create new parameters:

        .. code-block:: pycon

            >>> params = Parameters(value="BINARY")
            >>> params.value
            'BINARY'

        Set a default:

        .. code-block:: pycon

            >>> params = Parameters(value="BINARY", default_value="TEXT")
            >>> params
            Parameters({'VALUE': 'BINARY'})

    c                   |r|d         
|dd         }fdt                                                    D             } t                      j        |i  |                                D ]\  }}|                     ||           dS )zCreate new parameters.r   NrH   c                    i | ]G}|                                                     d           )|dd                             |          HS )default_   N)lower
startswithrl   )rX   keykwargss     r   
<dictcomp>z'Parameters.__init__.<locals>.<dictcomp>  sY     
 
 
yy{{%%j11
GVZZ__
 
 
r   )listkeyssuper__init__items
setdefault)r   argsr{   defaultsrz   r"   	__class__s     `   r   r   zParameters.__init__  s     	DGO8D
 
 
 
FKKMM**
 
 

 	$)&)))"..** 	( 	(JCOOC''''	( 	(r   )	ALTREPzDELEGATED-FROMzDELEGATED-TODIRMEMBERzSENT-BYz	X-ADDRESSzX-TITLELINKRELCNz 'c                *    |                                  S )zpIn RFC 5545 keys are called parameters, so this is to be consitent
        with the naming conventions.
        )r~   r   s    r   paramszParameters.params3  s     yy{{r   Tsortedr%   c                |  	 g }t          |                                           }|r|                                 |D ]\  }	|dk    r	dk    r|                                }| j                            |                                          }|| j        v p|ot          	fd|D                       }t          	|          }t          |t                    r|                    t                    }|                    |                    t                    dz   |z              d                    |          S )zReturns an :rfc:`5545` representation of the parameters.

        Parameters:
            sorted (bool): Sort the parameters before encoding.
            exclude_utc (bool): Exclude TZID if it is set to ``"UTC"``
        TZIDUTCc              3      K   | ]}|v V  	d S Nr   )rX   cr"   s     r   rZ   z%Parameters.to_ical.<locals>.<genexpr>N  s'      GGqU
GGGGGGr   r(      =   ;)r}   r   sortrc   
quote_alsorb   always_quotedanyr/   r)   r&   encoder   rK   r[   )
r   r   rL   r   rz   	upper_keycheck_quoteable_charactersr$   quoted_valuer"   s
            @r   r   zParameters.to_ical9  sD    TZZ\\"" 	JJLLL 	T 	TJCf}}% 		I)-)<)<SYY[[)I)I&$(:: * HGGGG,FGGGGG  'u<HHHL,,, E+223CDDMM)**+;<<tClRSSSSyy   r   Fc                    |             }t          |d          D ]m}	 t          |dd          \  }}t          |           g }t          |d          D ]}|                    d          r^|                    d          rI|                    d          }	t          |	d           |                    t          |	                     ut          |d	           |r5|                    t          |                                                     |                    t          |                     |s|||<   n$t          |          dk    r|d
         ||<   n|||<   H# t          $ r}
t          |d|
           |
d}
~
ww xY w|S )z2Parses the parameter format from ical text format.;=rH   )rD   r@   r;   T)r0   Fr   z" is not a valid parameter string: N)rS   r   ry   endswithstripr8   rK   rfc_6868_unescaperc   rI   r6   )clsrB   strictrL   paramrz   r9   valsvv2excs              r   	from_icalzParameters.from_icalW  s   
 R%% 	 	E"5#:::Ss###  c** 
> 
>A||C(( 	>QZZ__ 	>WWS\\,R====$5b$9$9::::,Qu====! > KK(9!''))(D(DEEEE KK(9!(<(<==== '"%F3KKYY!^^"&q'F3KK"&F3K    GG#GG  s   EE##
F-FFrh   r   VALUE | str | Nonec                    dS )a|  The VALUE parameter from :rfc:`5545`.

        Description:
            This parameter specifies the value type and format of
            the property value.  The property values MUST be of a single value
            type.  For example, a "RDATE" property cannot have a combination
            of DATE-TIME and TIME value types.

            If the property's value is the default value type, then this
            parameter need not be specified.  However, if the property's
            default value type is overridden by some other allowable value
            type, then this parameter MUST be specified.

            Applications MUST preserve the value data for x-name and iana-
            token values that they don't recognize without attempting to
            interpret or parse the value data.

        For convenience, using this property, the value will be converted to
        an uppercase string.

        .. code-block:: pycon

            >>> from icalendar import Parameters
            >>> params = Parameters()
            >>> params.value = "unknown"
            >>> params
            Parameters({'VALUE': 'UNKNOWN'})

        Nr   r   s    r   r"   zParameters.value{        r   r"   str | float | list | VPROPERTY7str | int | float | list[str] | list[int] | list[float]c                    t          |t                    r fd|D             S t          |d          r9|                                }t	          |          dk    r|d         S |dd         S t
          t          t          fD ]}t          ||          r ||          c S  t          dt          |           d|          )zConvert a parameter value to jCal format.

        Parameters:
            value: The parameter value

        Returns:
            The jCal representation of the parameter value
        c                :    g | ]}                     |          S r   )_parameter_value_to_jcal)rX   r   r   s     r   
<listcomp>z7Parameters._parameter_value_to_jcal.<locals>.<listcomp>  s'    DDDD11!44DDDr   to_jcal      Nz6Unsupported parameter value type for jCal conversion:  )
r)   r}   hasattrr   rI   rE   floatr&   	TypeErrortype)r   r"   jcalts   `   r   r   z#Parameters._parameter_value_to_jcal  s     eT"" 	EDDDDeDDDD5)$$ 	==??D4yyA~~Aw8Ouc" 	  	 A%##  qxx 'E{{' '"' '
 
 	
r   dict[str, str]c                      fd                                  D             }|r|                    d          dk    r|d= |S )zReturn the jCal representation of the parameters.

        Parameters:
            exclude_utc (bool): Exclude the TZID parameter if it is UTC
        c                    i | ]E\  }}|                                 d k    |                                                     |          FS r"   )rx   r   )rX   kr   r   s      r   r|   z&Parameters.to_jcal.<locals>.<dictcomp>  sR     
 
 
1wwyyG## GGIIt44Q77###r   tzidr   )r   rb   )r   exclude_utcr   s   `  r   r   zParameters.to_jcal  s_    
 
 
 



 
 

  	488F++u44Vr   rf   c                    dS )z$The TZID parameter from :rfc:`5545`.Nr   r   s    r   r   zParameters.tzid  r   r   c                    | j         dk    S )z"Whether the TZID parameter is UTC.r   )r   r   s    r   is_utczParameters.is_utc  s    yE!!r   dtdatetime | time | Anyr1   c                ~    t          |t          t          f          rt          |          }|dk    r|| _        dS dS dS )zUpdate the TZID parameter from a datetime object.

        This sets the TZID parameter or deletes it according to the datetime.
        :rfc:`5545#section-3.2.19` prohibits TZID on UTC datetimes,
        which use the ``Z`` suffix instead.
        r   N)r)   r   r   r   r   )r   r   r   s      r   update_tzid_fromzParameters.update_tzid_from  sK     b8T*++ 	!##Du}} 					! 	!}r   r   dict[str:str | list[str]]c                   t          |t                    st          d|           |                                D ]\  }}t          |t                    st          d| |          t          |t
                    rt          d |D                       r|s5t          |t          t          t          f          st          d| ||           | |          S )zParse jCal parameters.z!The parameters must be a mapping.z$All parameter names must be strings.r   c              3  Z   K   | ]&}t          |t          t          t          f          V  'd S r   )r)   r&   rE   r   )rX   r   s     r   rZ   z'Parameters.from_jcal.<locals>.<genexpr>  s3      LLJq3U*;<<LLLLLLr   zGParameter values must be a string, integer or float or a list of those.)	r)   dictr
   r   r&   r}   allrE   r   )r   r   rd   r"   s       r   	from_jcalzParameters.from_jcal  s    $%% 	M"#FLLL::<< 	 	KD%dC(( &:Ct   
 ud++ LLeLLLLL 	 ec3%677 '0    s4yyr   jcal_propertyr}   c                4   t          |t                    rt          |          dk     rt          d|           |d         }t          j        d          5  |                     |          }ddd           n# 1 swxY w Y   |                                r|`|S )a*  Create the parameters for a jCal property.

        Parameters:
            jcal_property (list): The jCal property [name, params, value, ...]
            default_value (str, optional): The default value of the property.
                If this is given, the default value will not be set.
        r   z2The property must be a list with at least 4 items.rH   N)r)   r}   rI   r
   reraise_with_path_addedr   r   r   )r   r   jcal_paramsr   s       r   from_jcal_propertyzParameters.from_jcal_property  s     -.. 	#m2D2Dq2H2H"Dc   $A&5a88 	. 	.==--D	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.;;== 		s   A77A;>A;T)r   r%   F)r   r   )r"   r   r   r   )r   r   )r   rf   )r   r   r   r1   )r   r   )r   r}   )r   r   r    r!   r   r   r   r   r   classmethodr   rr   r"   r   r   r   r   r   r   r   __classcell__)r   s   @r   r`   r`      s       # #J( ( ( ( (M  	dJ
  ! ! ! ! !< ! ! ! [!F 4(((   )(>
 
 
 
8     3 3 3 3" " "! ! ! !    [4    [    r   r`   z\^\^|\^n|\^'r/   c                `    dt           j        ddt                              fd|           S )zTake care of :rfc:`6868` unescaping.

    - ^^ -> ^
    - ^n -> system specific newline
    - ^' -> "
    - ^ with others stay intact
    ^r;   )^^^n^'c                |                         |                     d          |                     d                    S Nr   rb   groupmreplacementss    r   <lambda>z#rfc_6868_unescape.<locals>.<lambda>  +    ,""1771::qwwqzz:: r   )oslinesepRFC_6868_UNESCAPE_REGEXsubr/   r   s    @r   r   r     sD     j L
 #&&::::K  r   z\^|\r\n|\r|\n|"c                P    ddddddt                               fd|           S )zXTake care of :rfc:`6868` escaping.

    - ^ -> ^^
    - " -> ^'
    - newline -> ^n
    r   r   r   )r   
z
r;   c                |                         |                     d          |                     d                    S r   r   r   s    r   r   z!rfc_6868_escape.<locals>.<lambda>2  r   r   )RFC_6868_ESCAPE_REGEXr   r   s    @r   r,   r,   #  sH      L !$$::::K  r   )r`   r-   r/   r*   rS   r,   r   r8   r   )r"   r#   r$   r%   r   r&   r   )r"   r&   r0   r%   r   r1   )r9   r&   r$   r%   r   r&   )r@   rA   )rB   r&   rC   r&   rD   rE   r   rF   )r@   F)rT   rU   rC   r&   r$   r%   r   r&   )NF)r\   r]   )r/   r&   r   r&   )/r!   
__future__r   rn   r   rer   r   typingr   r   r   icalendar.caselessdictr	   icalendar.errorr
   icalendar.parser.stringr   icalendar.parser_toolsr   r   icalendar.timezone.tzidr   collections.abcr   r   icalendar.enumsr   icalendar.propr   r   r/   compiler4   r3   r8   r>   r-   rS   r*   rr   r`   r   r   r   r,   __all__r   r   r   <module>r     s   ' ' " " " " " "     				 				 # # # # # # # # / / / / / / / / / / / / / / / / , , , , , , 2 2 2 2 2 2        1 0 0 0 0 0 )22222222%%%%%%((((((        BGM M M M M6 bj788rz566         , 2:j!!    4- - - - -`K K K K K6( ( ( ( (V] ] ] ] ] ] ] ]@	 %"*_55    $ #
#566    &	 	 	r   