
    F2j'                        d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	 erd dl
mZmZ 	 d dlmZ n# e$ r	 d dlmZ Y nw xY w ed          Z ed          Z	 dddZ	 dddZ G d de          Zg dZd	S )    )annotations)OrderedDict)TYPE_CHECKINGAnyTypeVar)
to_unicode)IterableMapping)SelfKTVTNkeysIterable[KT]canonical_orderIterable[KT] | Nonereturnlist[KT]c                    d t          |pg           D             fd| D             }fd| D             }t          |fd          t          |          z   S )a  Sort leading keys according to a canonical order.

    Keys specified in ``canonical_order`` appear first in that order.
    Remaining keys appear alphabetically at the end.

    Parameters:
        keys: The keys to sort.
        canonical_order: The preferred order for leading keys.
            Keys not in this sequence are sorted alphabetically after
            the canonical ones. If ``None``, all keys are sorted
            alphabetically.

    Returns:
        A new list of keys sorted by canonical order first, then alphabetically.

    Example:
        ..  code-block:: pycon

            >>> from icalendar.caselessdict import canonsort_keys
            >>> canonsort_keys(["C", "A", "B"], ["B", "C"])
            ['B', 'C', 'A']
    c                    i | ]\  }}||	S  r   ).0iks      ^/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/icalendar/caselessdict.py
<dictcomp>z"canonsort_keys.<locals>.<dictcomp>-   s    GGGdaQGGG    c                    g | ]}|v |	S r   r   r   r   canonical_maps     r   
<listcomp>z"canonsort_keys.<locals>.<listcomp>.   s#    222!qM11A111r   c                    g | ]}|v|	S r   r   r   s     r   r    z"canonsort_keys.<locals>.<listcomp>/   s#    666!q55A555r   c                    |          S Nr   )r   r   s    r   <lambda>z canonsort_keys.<locals>.<lambda>0   s    mA&6 r   )key)	enumeratesorted)r   r   headtailr   s       @r   canonsort_keysr*      s    2 HGi0E2&F&FGGGM2222t222D6666t666D$6666777&,,FFr   dict1Mapping[KT, VT]list[tuple[KT, VT]]c                `      fdt                                           |          D             S )a4  Sort items from a mapping according to a canonical key order.

    Parameters:
        dict1: The mapping whose items to sort.
        canonical_order: The preferred order for leading keys.
            If ``None``, all keys are sorted alphabetically.

    Returns:
        A list of ``(key, value)`` tuples sorted by canonical order.

    Example:
        ..  code-block:: pycon

            >>> from icalendar.caselessdict import canonsort_items
            >>> canonsort_items({"C": 3, "A": 1, "B": 2}, ["B", "C"])
            [('B', 2), ('C', 3), ('A', 1)]
    c                $    g | ]}||         fS r   r   )r   r   r+   s     r   r    z#canonsort_items.<locals>.<listcomp>G   s!    QQQaQaMQQQr   )r*   r   )r+   r   s   ` r   canonsort_itemsr0   3   s2    ( RQQQ>%**,,#P#PQQQQr   c                       e Zd ZdZd$ fdZdZd% fd
Zd& fdZd' fdZd( fdZ	d)d* fdZ
d)d+ fdZd)d* fdZd, fdZd( fdZd$dZd- fdZd.dZd/dZd/dZdZd0d!Zd1d#Z xZS )2CaselessDicta>  A case-insensitive dictionary that uses strings as keys.

    All keys are stored in uppercase internally, but values retain
    their original case. Keys can be provided as ``str`` or ``bytes``.
    They are converted to Unicode via :func:`~icalendar.parser_tools.to_unicode`,
    then uppercased before storage.
    argsr   kwargsr   Nonec                    t                      j        |i | |                                 D ]R\  }}t          |                                          }||k    r&t                                          |           || |<   SdS )a9  Parameters:
            *args: Positional arguments passed to :class:`~collections.OrderedDict`.
            **kwargs: Keyword arguments passed to :class:`~collections.OrderedDict`.

        Example:

            Create a new ``CaselessDict`` and normalize existing keys to uppercase.

            ..  code-block:: pycon

                >>> from icalendar.caselessdict import CaselessDict
                >>> d = CaselessDict(summary="Meeting")
                >>> d["SUMMARY"]
                'Meeting'
                >>> "summary" in d
                True
        N)super__init__itemsr   upper__delitem__)selfr3   r4   r%   value	key_upper	__class__s         r   r8   zCaselessDict.__init__S   s    $ 	$)&)))**,, 	( 	(JC"3--//Ii##C((("'Y		( 	(r   Nr%   c                    t          |          }t                                          |                                          S )aF  Get the item from the ``CaselessDict`` instance by
        ``key``, case-insensitively.

        Parameters:
            key: The key to look up, case-insensitively.

        Returns:
            The (key, value) pair associated with the uppercased key.

        Raises:
            KeyError: If the key is not found.
        )r   r7   __getitem__r:   r<   r%   r?   s     r   rA   zCaselessDict.__getitem__n   s0     ooww""399;;///r   r=   c                    t          |          }t                                          |                                |           dS )zSet a (key, value) pair, storing the key in uppercase.

        Parameters:
            key: The key of the pair, case-insensitive.
            value: The value to associate with the key.
        N)r   r7   __setitem__r:   r<   r%   r=   r?   s      r   rD   zCaselessDict.__setitem__~   s8     ooCIIKK/////r   c                    t          |          }t                                          |                                           dS )zDelete a (key, value) pair by its case-insensitive key.

        Parameters:
            key: The key to delete, case-insensitively.

        Raises:
            KeyError: If the key is not found.
        N)r   r7   r;   r:   rB   s     r   r;   zCaselessDict.__delitem__   s6     ooCIIKK(((((r   boolc                    t          |          }t                                          |                                          S )zCheck whether a key exists in the mapping, case-insensitively.

        Parameters:
            key: The key to check case-insensitively.

        Returns:
            ``True`` if the uppercased key exists, else ``False``.
        r   r7   __contains__r:   rB   s     r   rJ   zCaselessDict.__contains__   s0     ooww##CIIKK000r   defaultc                    t          |          }t                                          |                                |          S )a;  Return the ``key``, optionally with a ``default`` value.

        Parameters:
            key: The key to look up, case-insensitively.
            default: The value to return if the key is not found.

        Returns:
            The value for the key, if present, else the value specified by ``default``.
        )r   r7   getr:   r<   r%   rK   r?   s      r   rM   zCaselessDict.get   0     ooww{{399;;000r   c                    t          |          }t                                          |                                |          S )aU  Create the (key, value) pair, optionally with a ``value``.

        Once set, to change default value use :meth:`update`.

        Parameters:
            key: The key to look up or create, case-insensitively.
            value: The default value to set, if given, else ``None``.

        Returns:
            The value for the key.
        )r   r7   
setdefaultr:   rE   s      r   rQ   zCaselessDict.setdefault   s2     ooww!!#))++u555r   c                    t          |          }t                                          |                                |          S )a+  Remove and return the value for ``key``, or ``default`` if not found.

        Parameters:
            key: The key to remove, case-insensitively.
            default: The value to return if the key is not found.

        Returns:
            The removed value, or the value of ``default``.
        )r   r7   popr:   rN   s      r   rS   zCaselessDict.pop   rO   r   tuple[Any, Any]c                D    t                                                      S )zRemove and return the last inserted (key, value) pair.

        Returns:
            A (key, value) tuple.

        Raises:
            KeyError: If the dictionary is empty.
        )r7   popitemr<   r?   s    r   rV   zCaselessDict.popitem   s     ww   r   c                    t          |          }t                                          |                                          S )a  Check whether a key exists, case-insensitively.

        This is a legacy method. Use ``key in dict`` instead.

        Parameters:
            key: The key to check, case-insensitively.

        Returns:
            ``True`` if the key exists, else ``False``.
        rI   rB   s     r   has_keyzCaselessDict.has_key   s0     ooww##CIIKK000r   c                    t          |          |gz   }|D ]@}t          |d          r!t          |                                          }|D ]
\  }}|| |<   AdS )aT  Update the dictionary with (key, value) pairs, normalizing keys to uppercase.

        Multiple keys that differ only in case will overwrite each other.
        Only the last value is retained.

        Parameters:
            *args: Mappings or iterables of (key, value) pairs.
            **kwargs: Additional (key, value) pairs.
        r9   N)listhasattriterr9   )r<   r3   r4   mappingsmappingr%   r=   s          r   updatezCaselessDict.update   sy     ::( 	" 	"Gw(( 0w}}//% " "
U!S		"	" 	"r   r   c                p     t          |           t                                                                S )zReturn a shallow copy of the dictionary.

        Returns:
            A new instance of the same type with the same contents.
        )typer7   copyrW   s    r   rc   zCaselessDict.copy   s'     tDzz%'',,..)))r   strc                P    t          |           j         dt          |            dS )zReturn a string representation of the dictionary.

        Returns:
            A string in the form ``CaselessDict({...})``.
        ())rb   __name__dictr<   s    r   __repr__zCaselessDict.__repr__   s*     t**%55T

5555r   otherobjectc                    t          |t                    st          S | |u pCt          |                                           t          |                                          k    S )a  Check equality with another dictionary.

        Two ``CaselessDict`` instances are equal if they contain the same
        (key, value) pairs after uppercasing keys. Comparison with a regular
        ``dict`` also works.

        Parameters:
            other: The object to compare.

        Returns:
            ``True`` if equal, ``NotImplemented`` if ``other`` is not a ``dict``.
        )
isinstanceri   NotImplementedr9   r<   rl   s     r   __eq__zCaselessDict.__eq__  sP     %&& 	"!!u}ITZZ\\ 2 2d5;;==6I6I IIr   c                    | |k     S )zCheck inequality with another dictionary.

        Parameters:
            other: The object to compare.

        Returns:
            ``True`` if not equal, else ``False``.
        r   rq   s     r   __ne__zCaselessDict.__ne__  s     5=  r   	list[str]c                P    t          |                                 | j                  S )zSort keys according to the canonical order for this class.

        Keys listed in :attr:`canonical_order` appear first in that order.
        Remaining keys appear alphabetically at the end.

        Returns:
            A sorted list of keys.
        )r*   r   r   rj   s    r   sorted_keyszCaselessDict.sorted_keys$  s     diikk4+?@@@r   list[tuple[Any, Any]]c                ,    t          | | j                  S )a  Sort items according to the canonical order for this class.

        Items whose keys are listed in :attr:`canonical_order` appear first
        in that order. Remaining items appear alphabetically by key.

        Returns:
            A sorted list of (key, value) tuples.
        )r0   r   rj   s    r   sorted_itemszCaselessDict.sorted_items/  s     tT%9:::r   )r3   r   r4   r   r   r5   )r%   r   r   r   )r%   r   r=   r   r   r5   )r%   r   r   r5   )r%   r   r   rG   r#   )r%   r   rK   r   r   r   )r%   r   r=   r   r   r   )r   rT   )r   r   )r   rd   )rl   rm   r   rG   )r   ru   )r   rx   )rh   
__module____qualname____doc__r8   __hash__rA   rD   r;   rJ   rM   rQ   rS   rV   rY   r`   rc   rk   rr   rt   r   rw   rz   __classcell__)r?   s   @r   r2   r2   J   s        ( ( ( ( ( (2 H0 0 0 0 0 0 0 0 0 0 0 0
) 
) 
) 
) 
) 
)
1 
1 
1 
1 
1 
11 1 1 1 1 1 16 6 6 6 6 6 61 1 1 1 1 1 1	! 	! 	! 	! 	! 	!1 1 1 1 1 1" " " "$* * * * * *6 6 6 6J J J J"	! 	! 	! 	! O	A 	A 	A 	A	; 	; 	; 	; 	; 	; 	; 	;r   r2   )r2   r0   r*   r#   )r   r   r   r   r   r   )r+   r,   r   r   r   r-   )
__future__r   collectionsr   typingr   r   r   icalendar.parser_toolsr   collections.abcr	   r
   r   ImportErrortyping_extensionsr   r   r*   r0   r2   __all__r   r   r   <module>r      sr   " " " " " " # # # # # # . . . . . . . . . . - - - - - - 211111111' ' ' '&&&&&&&&' WT]]WT]] @DG G G G G@ DHR R R R R.n; n; n; n; n;; n; n; n;b @
?
?s   / ==