
    ՟fn                        d Z ddlZddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZ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$ erddl%m&Z&  eddd      Z' G d d      Z(y)z Base class for Telegram Objects.    N)Sized)contextmanager)deepcopy)chain)MappingProxyType)TYPE_CHECKINGAnyClassVarDictIteratorListMappingOptionalSetTupleTypeTypeVarUnioncast)to_timestamp)DefaultValue)JSONDict)warn)BotTele_coTelegramObjectT)bound	covariantc                       e Zd ZU dZdZ e       Zeee	      e
d<   dZeed       e
d<   dddee   ddfd	Zd
edef fdZdef fdZde	deddf fdZde	ddf fdZde	fdZde	defdZdee	ee	ef   f   fdZdee	ef   ddfdZdedeeef   defdZedee   dee   fd       Z e!	 d1dee   dee   ded   dee   dee   f
d       Z"e!	 d1dee   dee   ded   dee   fd        Z#e!	 d1dee   dee$e      ded   de%ed!f   fd"       Z&e'dede(e   fd#       Z)d2d$Z*d2d%Z+deddfd&Z,d'ede(e	   fd(Z-	 	 	 	 d3d'ed)ed*ed+edee	ee	ef   f   f
d,Z.de	fd-Z/d4d)edefd.Z0d5d/Z1ded   ddfd0Z2 xZ3S )6r   a  Base class for most Telegram objects.

    Objects of this type are subscriptable with strings. See :meth:`__getitem__` for more details.
    The :mod:`pickle` and :func:`~copy.deepcopy` behavior of objects of this type are defined by
    :meth:`__getstate__`, :meth:`__setstate__` and :meth:`__deepcopy__`.

    Tip:
        Objects of this type can be serialized via Python's :mod:`pickle` module and pickled
        objects from one version of PTB are usually loadable in future versions. However, we can
        not guarantee that this compatibility will always be provided. At least a manual one-time
        conversion of the data may be needed on major updates of the library.

    .. versionchanged:: 20.0

        * Removed argument and attribute ``bot`` for several subclasses. Use
          :meth:`set_bot` and :meth:`get_bot` instead.
        * Removed the possibility to pass arbitrary keyword arguments for several subclasses.
        * String representations objects of this type was overhauled. See :meth:`__repr__` for
          details. As this class doesn't implement :meth:`object.__str__`, the default
          implementation will be used, which is equivalent to :meth:`__repr__`.
        * Objects of this class (or subclasses) are now immutable. This means that you can't set
          or delete attributes anymore. Moreover, attributes that were formerly of type
          :obj:`list` are now of type :obj:`tuple`.

    Arguments:
        api_kwargs (Dict[:obj:`str`, any], optional): |toapikwargsarg|

            .. versionadded:: 20.0

    Attributes:
        api_kwargs (:obj:`types.MappingProxyType` [:obj:`str`, any]): |toapikwargsattr|

            .. versionadded:: 20.0

    )_bot_frozen	_id_attrs
api_kwargs_TelegramObject__INIT_PARAMSN"_TelegramObject__INIT_PARAMS_CHECK)r#   r#   returnc                V    d| _         d| _        d | _        t        |xs i       | _        y )NF )r!   r"   r    r   r#   )selfr#   s     S/var/www/cvtools/html/venv/lib/python3.12/site-packages/telegram/_telegramobject.py__init__zTelegramObject.__init__h   s*    
 #-/#'	-=j>NB-O    otherc                 D   t        || j                        r{| j                  s%t        d| j                  j                   dd       |j                  s%t        d|j                  j                   dd       | j                  |j                  k(  S t
        |   |      S )a  Compares this object with :paramref:`other` in terms of equality.
        If this object and :paramref:`other` are `not` objects of the same class,
        this comparison will fall back to Python's default implementation of :meth:`object.__eq__`.
        Otherwise, both objects may be compared in terms of equality, if the corresponding
        subclass of :class:`TelegramObject` has defined a set of attributes to compare and
        the objects are considered to be equal, if all of these attributes are equal.
        If the subclass has not defined a set of attributes to compare, a warning will be issued.

        Tip:
            If instances of a class in the :mod:`telegram` module are comparable in terms of
            equality, the documentation of the class will state the attributes that will be used
            for this comparison.

        Args:
            other (:obj:`object`): The object to compare with.

        Returns:
            :obj:`bool`

        Objects of type z0 can not be meaningfully tested for equivalence.   )
stacklevel)
isinstance	__class__r"   r   __name__super__eq__)r)   r-   r3   s     r*   r6   zTelegramObject.__eq__s   s    * eT^^,>>&t~~'>'>&? @$ $ 
 ??&u'?'?&@ A( ( 
 >>U__44w~e$$r,   c                 z    | j                   r!t        | j                  | j                   f      S t        |          S )zBuilds a hash value for this object such that the hash of two objects is equal if and
        only if the objects are equal in terms of :meth:`__eq__`.

        Returns:
            :obj:`int`
        )r"   hashr3   r5   __hash__)r)   r3   s    r*   r9   zTelegramObject.__hash__   s2     >>899w!!r,   keyvaluec                     |d   dk(  st        | dd      st        | 	  ||       yt        d| d| j                  j
                   d      )	zOverrides :meth:`object.__setattr__` to prevent the overriding of attributes.

        Raises:
            :exc:`AttributeError`
        r   _r!   TNAttribute `` of class `z` can't be set!)getattrr5   __setattr__AttributeErrorr3   r4   )r)   r:   r;   r3   s      r*   rA   zTelegramObject.__setattr__   sW     q6S=i >GU+#l4>>+B+B*C?S
 	
r,   c                     |d   dk(  st        | dd      st        | 	  |       yt        d| d| j                  j
                   d      )	zOverrides :meth:`object.__delattr__` to prevent the deletion of attributes.

        Raises:
            :exc:`AttributeError`
        r   r=   r!   TNr>   r?   z` can't be deleted!)r@   r5   __delattr__rB   r3   r4   )r)   r:   r3   s     r*   rD   zTelegramObject.__delattr__   sV     q6S=i >G$#l4>>+B+B*CCVW
 	
r,   c                 2   | j                  dd      | j                  sj                  dd       nt        | j                        d<   dj	                  fdt        j                               D              }| j                  j                   d| dS )	a  Gives a string representation of this object in the form
        ``ClassName(attr_1=value_1, attr_2=value_2, ...)``, where attributes are omitted if they
        have the value :obj:`None` or are empty instances of :class:`collections.abc.Sized` (e.g.
        :class:`list`, :class:`dict`, :class:`set`, :class:`str`, etc.).

        As this class doesn't implement :meth:`object.__str__`, the default implementation
        will be used, which is equivalent to :meth:`__repr__`.

        Returns:
            :obj:`str`
        F)	recursiveinclude_privater#   Nz, c              3      K   | ]7  }|   0t        |   t              rt        |         dk(  s| d|    9 y w)Nr   =)r2   r   len).0kas_dicts     r*   	<genexpr>z*TelegramObject.__repr__.<locals>.<genexpr>   sP      


&wqz51GAJ1, c71:.!

s   =A ())	
_get_attrsr#   popdictjoinsortedkeysr3   r4   )r)   contentsrM   s     @r*   __repr__zTelegramObject.__repr__   s      //E5/IKKd+ %)$9GL!99 

GLLN+

 

 ..))*!H:Q77r,   itemc                     |dk(  rd}	 t        | |      S # t        $ r,}t        d| j                  j                   d| d      |d}~ww xY w)a  
        Objects of this type are subscriptable with strings, where
        ``telegram_object["attribute_name"]`` is equivalent to ``telegram_object.attribute_name``.

        Tip:
            This is useful for dynamic attribute lookup, i.e. ``telegram_object[arg]`` where the
            value of ``arg`` is determined at runtime.
            In all other cases, it's recommended to use the dot notation instead, i.e.
            ``telegram_object.attribute_name``.

        .. versionchanged:: 20.0

            ``telegram_object['from']`` will look up the key ``from_user``. This is to account for
            special cases like :attr:`Message.from_user` that deviate from the official Bot API.

        Args:
            item (:obj:`str`): The name of the attribute to look up.

        Returns:
            :obj:`object`

        Raises:
            :exc:`KeyError`: If the object does not have an attribute with the appropriate name.
        from	from_userr/   z! don't have an attribute called `z`.N)r@   rB   KeyErrorr3   r4   )r)   rY   excs      r*   __getitem__zTelegramObject.__getitem__   sg    2 6>D	4&& 	"4>>#:#:"; <6 	s    	A
'AA
c                 `    | j                  dddd      }t        | j                        |d<   |S )an  
        Overrides :meth:`object.__getstate__` to customize the pickling process of objects of this
        type.
        The returned state does `not` contain the :class:`telegram.Bot` instance set with
        :meth:`set_bot` (if any), as it can't be pickled.

        Returns:
            state (Dict[:obj:`str`, :obj:`object`]): The state of the object.
        TF)rG   rF   
remove_botconvert_default_vaultr#   )rQ   rS   r#   )r)   outs     r*   __getstate__zTelegramObject.__getstate__
  s;     oo EdZ_  

 !1L
r,   statec           	      H   | j                          d| _        t        t        t        t
        f   |j                  di             }|j                  dd      }|j                         D ]  \  }}	 t        | ||        | j                  |       t!        |      | _        |r| j%                          yy# t        $ rf t        t        | j                  |d      t              r&	 t        | d| |       n'# t        $ r |||<   Y nw xY w|j                  d      rY |||<   Y w xY w)a6  
        Overrides :meth:`object.__setstate__` to customize the unpickling process of objects of
        this type. Modifies the object in-place.

        If any data was stored in the :attr:`api_kwargs` of the pickled object, this method checks
        if the class now has dedicated attributes for those keys and moves the values from
        :attr:`api_kwargs` to the dedicated attributes.
        This can happen, if serialized data is loaded with a new version of this library, where
        the new version was updated to account for updates of the Telegram Bot API.

        If on the contrary an attribute was removed from the class, the value is not discarded but
        made available via :attr:`api_kwargs`.

        Args:
            state (:obj:`dict`): The data to set as attributes of this object.
        Nr#   r!   Fr=   )	_unfreezer    r   r   strobjectrR   itemssetattrrB   r2   r@   r3   property
startswith_apply_api_kwargsr   r#   _freeze)r)   re   r#   frozenr:   vals         r*   __setstate__zTelegramObject.__setstate__  s   " 	 	 $sF{+UYY|R-HI
9e, 	&HC&c3'	&. 	z**:6
 LLN 5 " & gdnnc4@(K.#i5 * .*-
3. ^^C("%
3&s<   .B22.D!!C21D!2D D!DD!D! D!r)   memodictc                 .   | j                   }| j                  d       | j                  }|j                  |      }||t	        |       <   d|_        | j                  d      D ]c  }|dk(  r	|dk(  r4t        ||t        t        t        | j                        |                   B	 t        ||t        t        | |      |             e | j
                  r|j                          |j                  |       | j                  |       |S # t        $ r Y w xY w)a@  
        Customizes how :func:`copy.deepcopy` processes objects of this type.
        The only difference to the default implementation is that the :class:`telegram.Bot`
        instance set via :meth:`set_bot` (if any) is not copied, but shared between the original
        and the copy, i.e.::

            assert telegram_object.get_bot() is copy.deepcopy(telegram_object).get_bot()

        Args:
            memodict (:obj:`dict`): A dictionary that maps objects to their copies.

        Returns:
            :class:`telegram.TelegramObject`: The copied object.
        NFTrG   r!   r#   )r    set_botr3   __new__idr!   _get_attrs_namesrk   r   r   rS   r#   r@   rB   ro   )r)   rs   botclsresultrL   s         r*   __deepcopy__zTelegramObject.__deepcopy__X  s    iiTnnS!#D &&t&< 	AI~L  #3HT$//=RT\4]#^_8GD!,<h#GH	& <<NNsS "  	s   %!D	DDdatac                 *    | dS | j                         S )zShould be called by subclasses that override de_json to ensure that the input
        is not altered. Whoever calls de_json might still want to use the original input
        for something else.
        N)copy)r~   s    r*   _parse_datazTelegramObject._parse_data  s     |t44r,   r{   rz   r   c                    |y 	  | di |d|i}|j                  |       |S # t         $ r}dt        |      vr | j                  | urDt        j                  |       }t        |j                  j                               | _        | | _        |xs i }i }|j                         D ]  \  }}	|	|| j                  v r|n||<     | dd|i|}Y d }~d }~ww xY w)Nr#   z-__init__() got an unexpected keyword argument)rz   r(   )	TypeErrorrh   r%   inspect	signatureset
parametersrV   r$   rj   rv   )
r{   r~   rz   r#   objr^   r   existing_kwargsr:   r;   s
             r*   _de_jsonzTelegramObject._de_json  s     <	@444C" 	
#  	@>c#hN&&c1#--c2	$'	(<(<(A(A(C$D!*-'#)rJ(*O"jjl [
UUZC3+<+<$<*cR[ ???C	@s   % 	CBCCc                 (    | j                  ||      S )a  Converts JSON data to a Telegram object.

        Args:
            data (Dict[:obj:`str`, ...]): The JSON data.
            bot (:class:`telegram.Bot`, optional): The bot associated with this object. Defaults to
                :obj:`None`, in which case shortcut methods will not be available.

                .. versionchanged:: 21.4
                   :paramref:`bot` is now optional and defaults to :obj:`None`

        Returns:
            The Telegram object.

        )r~   rz   )r   r{   r~   rz   s      r*   de_jsonzTelegramObject.de_json  s    $ ||3|//r,   .c                 D     |syt        d  fd|D        D              S )a  Converts a list of JSON objects to a tuple of Telegram objects.

        .. versionchanged:: 20.0

           * Returns a tuple instead of a list.
           * Filters out any :obj:`None` values.

        Args:
            data (List[Dict[:obj:`str`, ...]]): The JSON data.
            bot (:class:`telegram.Bot`, optional): The bot associated with these object. Defaults
                to :obj:`None`, in which case shortcut methods will not be available.

                .. versionchanged:: 21.4
                   :paramref:`bot` is now optional and defaults to :obj:`None`

        Returns:
            A tuple of Telegram objects.

        r(   c              3   &   K   | ]	  }||  y wNr(   )rK   r   s     r*   rN   z)TelegramObject.de_list.<locals>.<genexpr>  s     [S3?S[s   c              3   B   K   | ]  }j                  |        y wr   )r   )rK   drz   r{   s     r*   rN   z)TelegramObject.de_list.<locals>.<genexpr>  s     $GQS[[C%8$Gs   )tupler   s   ` `r*   de_listzTelegramObject.de_list  s"    . [$G$$G[[[r,   c              #   T   K   | j                          |  | j                          yw)zContext manager to temporarily unfreeze the object. For internal use only.

        Note:
            with to._unfrozen() as other_to:
                assert to is other_to
        N)rg   ro   r)   s    r*   	_unfrozenzTelegramObject._unfrozen  s       	
s   &(c                     d| _         y )NTr!   r   s    r*   ro   zTelegramObject._freeze  s	    r,   c                     d| _         y )NFr   r   s    r*   rg   zTelegramObject._unfreeze  s	    r,   c           	      z   t        |j                               D ]  }t        t        | j                  |d      t
              rBt        j                  t              5  t        | d| |j                  |             ddd       jt        | |d      xt        | ||j                  |              y# 1 sw Y   xY w)ab  Loops through the api kwargs and for every key that exists as attribute of the
        object (and is None), it moves the value from `api_kwargs` to the attribute.
        *Edits `api_kwargs` in place!*

        This method is currently only called in the unpickling process, i.e. not on "normal" init.
        This is because
        * automating this is tricky to get right: It should be called at the *end* of the __init__,
          preferably only once at the end of the __init__ of the last child class. This could be
          done via __init_subclass__, but it's hard to not destroy the signature of __init__ in the
          process.
        * calling it manually in every __init__ is tedious
        * There probably is no use case for it anyway. If you manually initialize a TO subclass,
          then you can pass everything as proper argument.
        Nr=   T)listrV   r2   r@   r3   rl   
contextlibsuppressrB   rk   rR   )r)   r#   r:   s      r*   rn   z TelegramObject._apply_api_kwargs  s      
)* 	8C'$..#t<hG((8 BDAcU)Z^^C-@AB BsD)1c:>>##67	8B Bs    B11B:	rG   c                     d | j                   j                  dd D        }t        | d      r$t        || j                  j                               n|}|r|S d |D        S )ay  
        Returns the names of the attributes of this object. This is used to determine which
        attributes should be serialized when pickling the object.

        Args:
            include_private (:obj:`bool`): Whether to include private attributes.

        Returns:
            Iterator[:obj:`str`]: An iterator over the names of the attributes of this object.
        c              3   B   K   | ]  }|j                   D ]  }|   y wr   )	__slots__)rK   css      r*   rN   z2TelegramObject._get_attrs_names.<locals>.<genexpr>  s      Q1Q[[QQQQQs   N__dict__c              3   D   K   | ]  }|j                  d       r|  yw)r=   N)rm   )rK   attrs     r*   rN   z2TelegramObject._get_attrs_names.<locals>.<genexpr>#  s     G$//#2FGs     )r3   __mro__hasattrr   r   rV   )r)   rG   	all_slots	all_attrss       r*   ry   zTelegramObject._get_attrs_names  s`     R 6 6s ;Q	 7>dJ6OE)T]]//12U^ 	 GGGr,   rF   ra   rb   c                 v   i }| j                  |      D ]e  }|r t        j                  t        | |d            nt        | |d      }|*|r"t	        |d      r|j                  d      ||<   X|||<   ^|ra|||<   g |r&|j                  d      r|j                  dd      |d<   |r|j                  dd       |S )	a  This method is used for obtaining the attributes of the object.

        Args:
            include_private (:obj:`bool`): Whether the result should include private variables.
            recursive (:obj:`bool`): If :obj:`True`, will convert any ``TelegramObjects`` (if
                found) in the attributes to a dictionary. Else, preserves it as an object itself.
            remove_bot (:obj:`bool`): Whether the bot should be included in the result.
            convert_default_vault (:obj:`bool`): Whether :class:`telegram.DefaultValue` should be
                converted to its true value. This is necessary when converting to a dictionary for
                end users since DefaultValue is used in some classes that work with
                `tg.ext.defaults` (like `LinkPreviewOptions`)

        Returns:
            :obj:`dict`: A dict where the keys are attribute names and values are their values.
        ru   Nto_dictTrF   r\   r[   r    )ry   r   	get_valuer@   r   r   getrR   )r)   rG   rF   ra   rb   r~   r:   r;   s           r*   rQ   zTelegramObject._get_attrs%  s    , (((I 	"C ) &&wtS$'?@T3-   	!: % =DI %DI!S		" +.88K6DLHHVT"r,   c                 H    t        j                  | j                               S )zGives a JSON representation of object.

        .. versionchanged:: 20.0
            Now includes all entries of :attr:`api_kwargs`.

        Returns:
            :obj:`str`
        )jsondumpsr   r   s    r*   to_jsonzTelegramObject.to_jsonR  s     zz$,,.))r,   c                    | j                  |      }t               }|j                         D ]  \  }}t        |t        t
        f      r|s|j                  |       0g }|D ]  }t        |d      r"|j                  |j                  |             1t        |t        t
        f      r>|j                  |D cg c]"  }t        |d      r|j                  |      n|$ c}       |j                  |        |||<   t        |t        j                        st        |      ||<    |D ]  }|j                  |        |j                  |j                  di              |S c c}w )a:  Gives representation of object as :obj:`dict`.

        .. versionchanged:: 20.0

            * Now includes all entries of :attr:`api_kwargs`.
            * Attributes whose values are empty sequences are no longer included.

        Args:
            recursive (:obj:`bool`, optional): If :obj:`True`, will convert any TelegramObjects
                (if found) in the attributes to a dictionary. Else, preserves it as an object
                itself. Defaults to :obj:`True`.

                .. versionadded:: 20.0

        Returns:
            :obj:`dict`
        r   r   r#   )rQ   r   rj   r2   r   r   addr   appendr   datetimer   rR   update)	r)   rF   rc   pop_keysr:   r;   rq   rY   is	            r*   r   zTelegramObject.to_dict]  sO   $ oo	o2
 !U))+ 	/JC%%/LL%! )DtY/

4<<)<#DE#D5$-8

 *.$% CJ!YBW		I	 >]^ ^ 

4() CE8#4#45'.C1	/4  	CGGCL	 	

377<,-
#s   7'E%c                 H    | j                   t        d      | j                   S )zReturns the :class:`telegram.Bot` instance associated with this object.

        .. seealso:: :meth:`set_bot`

        .. versionadded: 20.0

        Raises:
            RuntimeError: If no :class:`telegram.Bot` instance was set for this object.
        zDThis object has no bot associated with it. Shortcuts cannot be used.)r    RuntimeErrorr   s    r*   get_botzTelegramObject.get_bot  s*     99V  yyr,   c                     || _         y)zSets the :class:`telegram.Bot` instance associated with this object.

        .. seealso:: :meth:`get_bot`

        .. versionadded: 20.0

        Arguments:
            bot (:class:`telegram.Bot` | :obj:`None`): The bot instance.
        N)r    )r)   rz   s     r*   rv   zTelegramObject.set_bot  s     	r,   r   )r&   N)FFFT)T)r&   r   )4r4   
__module____qualname____doc__r   r   r$   r
   r   rh   __annotations__r%   r   r   r   r+   ri   boolr6   intr9   rA   rD   rX   r_   r   r   rd   rr   r   r}   staticmethodr   classmethodr   r   r   r   r   r   r   r   ro   rg   rn   ry   rQ   r   r   r   rv   __classcell__)r3   s   @r*   r   r   9   sG   "H ?I ),M8CH%- =A$'7"89@;? 	Phx&8 	PD 	P#%F #%t #%J	"# 	"
s 
6 
d 

s 
t 
$8# $8L! ! !Fd3c6k(:#:; $:$sF{"3 : :x07 0d3;.? 0G 0d 5(8, 5(1C 5 5 
 *.	']x  e_ X&	
 
'	 > MQ0']0"*8"40;CE?0	'	0 0& SW\']\"*4>":\AI%\	w|	\ \6 	 	HW$5 	 	8H 8 82H H# H4 !& &*++ + 	+
  $+ 
c5f%%	&+Z	* 	*7 7 7r 
8E? 
t 
r,   ))r   r   r   r   r   collections.abcr   r   r   r   	itertoolsr   typesr   typingr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   telegram._utils.datetimer   telegram._utils.defaultvaluer   telegram._utils.typesr   telegram._utils.warningsr   telegramr   r   r   r(   r,   r*   <module>r      sh   & '     ! %   "   " 2 5 * )
)#3t
Dw	 w	r,   