U
    2Òtcì/  ã                   @   s¬   d Z ddlZddlZddlmZmZmZmZmZm	Z	 ddl
mZmZmZmZ ddlmZmZ ddlmZ erxddl
mZ G dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZdS )z?This module contains an object that represents a Telegram Poll.é    N)ÚTYPE_CHECKINGÚAnyÚDictÚListÚOptionalÚClassVar)ÚMessageEntityÚTelegramObjectÚUserÚ	constants)Úfrom_timestampÚto_timestamp)ÚJSONDict)ÚBotc                   @   s:   e Zd ZU dZdZeeedœdd„Ze	j
Zee ed< dS )Ú
PollOptiona'  
    This object contains information about one answer option in a poll.

    Objects of this class are comparable in terms of equality. Two objects of this class are
    considered equal, if their :attr:`text` and :attr:`voter_count` are equal.

    Args:
        text (:obj:`str`): Option text, 1-100 characters.
        voter_count (:obj:`int`): Number of users that voted for this option.

    Attributes:
        text (:obj:`str`): Option text, 1-100 characters.
        voter_count (:obj:`int`): Number of users that voted for this option.

    )Úvoter_countÚtextÚ	_id_attrs)r   r   Ú_kwargsc                 K   s   || _ || _| j | jf| _d S ©N)r   r   r   )Úselfr   r   r   © r   ú1/tmp/pip-unpacked-wheel-2u9rcp5s/telegram/poll.pyÚ__init__5   s    zPollOption.__init__Ú
MAX_LENGTHN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__ÚstrÚintr   r   r   ÚMAX_POLL_OPTION_LENGTHr   r   Ú__annotations__r   r   r   r   r   "   s
   
r   c                   @   sJ   e Zd ZdZdZeeee e	dœdd„Z
eee ded  dœdd	„ƒZd
S )Ú
PollAnswera5  
    This object represents an answer of a user in a non-anonymous poll.

    Objects of this class are comparable in terms of equality. Two objects of this class are
    considered equal, if their :attr:`poll_id`, :attr:`user` and :attr:`options_ids` are equal.

    Attributes:
        poll_id (:obj:`str`): Unique poll identifier.
        user (:class:`telegram.User`): The user, who changed the answer to the poll.
        option_ids (List[:obj:`int`]): Identifiers of answer options, chosen by the user.

    Args:
        poll_id (:obj:`str`): Unique poll identifier.
        user (:class:`telegram.User`): The user, who changed the answer to the poll.
        option_ids (List[:obj:`int`]): 0-based identifiers of answer options, chosen by the user.
            May be empty if the user retracted their vote.

    )Ú
option_idsÚuserÚpoll_idr   )r'   r&   r%   r   c                 K   s,   || _ || _|| _| j | jt| jƒf| _d S r   )r'   r&   r%   Útupler   )r   r'   r&   r%   r   r   r   r   r   U   s    zPollAnswer.__init__r   ©ÚdataÚbotÚreturnc                 C   s2   |   |¡}|sdS t | d¡|¡|d< | f |ŽS )ú,See :meth:`telegram.TelegramObject.de_json`.Nr&   )Ú_parse_datar
   Úde_jsonÚget©Úclsr*   r+   r   r   r   r/   \   s
    
zPollAnswer.de_jsonN)r   r   r   r   r   r    r
   r   r!   r   r   Úclassmethodr   r   r/   r   r   r   r   r$   ?   s
   r$   c                       sô   e Zd ZU dZdZdeeee ee	e	ee	eeee
 eejedœdd„Zeee ded  dœd	d
„ƒZedœ‡ fdd„Ze
edœdd„Zdee ee
ef dœdd„ZejZee ed< ejZee ed< ejZee ed< ejZ ee ed< ‡  Z!S )ÚPolla’  
    This object contains information about a poll.

    Objects of this class are comparable in terms of equality. Two objects of this class are
    considered equal, if their :attr:`id` is equal.

    Attributes:
        id (:obj:`str`): Unique poll identifier.
        question (:obj:`str`): Poll question, 1-300 characters.
        options (List[:class:`PollOption`]): List of poll options.
        total_voter_count (:obj:`int`): Total number of users that voted in the poll.
        is_closed (:obj:`bool`): :obj:`True`, if the poll is closed.
        is_anonymous (:obj:`bool`): :obj:`True`, if the poll is anonymous.
        type (:obj:`str`): Poll type, currently can be :attr:`REGULAR` or :attr:`QUIZ`.
        allows_multiple_answers (:obj:`bool`): :obj:`True`, if the poll allows multiple answers.
        correct_option_id (:obj:`int`): Optional. Identifier of the correct answer option.
        explanation (:obj:`str`): Optional. Text that is shown when a user chooses an incorrect
            answer or taps on the lamp icon in a quiz-style poll.
        explanation_entities (List[:class:`telegram.MessageEntity`]): Optional. Special entities
            like usernames, URLs, bot commands, etc. that appear in the :attr:`explanation`.
        open_period (:obj:`int`): Optional. Amount of time in seconds the poll will be active
            after creation.
        close_date (:obj:`datetime.datetime`): Optional. Point in time when the poll will be
            automatically closed.

    Args:
        id (:obj:`str`): Unique poll identifier.
        question (:obj:`str`): Poll question, 1-300 characters.
        options (List[:class:`PollOption`]): List of poll options.
        is_closed (:obj:`bool`): :obj:`True`, if the poll is closed.
        is_anonymous (:obj:`bool`): :obj:`True`, if the poll is anonymous.
        type (:obj:`str`): Poll type, currently can be :attr:`REGULAR` or :attr:`QUIZ`.
        allows_multiple_answers (:obj:`bool`): :obj:`True`, if the poll allows multiple answers.
        correct_option_id (:obj:`int`, optional): 0-based identifier of the correct answer option.
            Available only for polls in the quiz mode, which are closed, or was sent (not
            forwarded) by the bot or to the private chat with the bot.
        explanation (:obj:`str`, optional): Text that is shown when a user chooses an incorrect
            answer or taps on the lamp icon in a quiz-style poll, 0-200 characters.
        explanation_entities (List[:class:`telegram.MessageEntity`], optional): Special entities
            like usernames, URLs, bot commands, etc. that appear in the :attr:`explanation`.
        open_period (:obj:`int`, optional): Amount of time in seconds the poll will be active
            after creation.
        close_date (:obj:`datetime.datetime`, optional): Point in time (Unix timestamp) when the
            poll will be automatically closed. Converted to :obj:`datetime.datetime`.

    )Útotal_voter_countÚallows_multiple_answersÚopen_periodÚoptionsÚtypeÚexplanation_entitiesÚis_anonymousÚ
close_dateÚ	is_closedÚidÚexplanationÚquestionÚcorrect_option_idr   N)r>   r@   r8   r5   r=   r;   r9   r6   rA   r?   r:   r7   r<   r   c                 K   s\   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|| _
|| _|| _| j f| _d S r   )r>   r@   r8   r5   r=   r;   r9   r6   rA   r?   r:   r7   r<   r   )r   r>   r@   r8   r5   r=   r;   r9   r6   rA   r?   r:   r7   r<   r   r   r   r   r   ª   s    zPoll.__init__r   r)   c                    s^   |   |¡}|sdS ‡ fdd„|d D ƒ|d< t | d¡ˆ ¡|d< t| d¡ƒ|d< | f |ŽS )r-   Nc                    s   g | ]}t  |ˆ ¡‘qS r   )r   r/   )Ú.0Úoption©r+   r   r   Ú
<listcomp>Ó   s     z Poll.de_json.<locals>.<listcomp>r8   r:   r<   )r.   r   Zde_listr0   r   r1   r   rD   r   r/   Ë   s    
zPoll.de_json)r,   c                    sN   t ƒ  ¡ }dd„ | jD ƒ|d< | jr8dd„ | jD ƒ|d< t| d¡ƒ|d< |S )z,See :meth:`telegram.TelegramObject.to_dict`.c                 S   s   g | ]}|  ¡ ‘qS r   ©Úto_dict)rB   Úxr   r   r   rE   Ý   s     z Poll.to_dict.<locals>.<listcomp>r8   c                 S   s   g | ]}|  ¡ ‘qS r   rF   )rB   Úer   r   r   rE   ß   s     r:   r<   )ÚsuperrG   r8   r:   r   r0   )r   r*   ©Ú	__class__r   r   rG   Ù   s    
zPoll.to_dict)Úentityr,   c                 C   sd   | j stdƒ‚tjdkr0| j |j|j|j … S | j  d¡}||jd |j|j d … }| d¡S )a¦  Returns the text from a given :class:`telegram.MessageEntity`.

        Note:
            This method is present because Telegram calculates the offset and length in
            UTF-16 codepoint pairs, which some versions of Python don't handle automatically.
            (That is, you can't just slice ``Message.text`` with the offset and length.)

        Args:
            entity (:class:`telegram.MessageEntity`): The entity to extract the text from. It must
                be an entity that belongs to this message.

        Returns:
            :obj:`str`: The text of the given entity.

        Raises:
            RuntimeError: If the poll has no explanation.

        zThis Poll has no 'explanation'.iÿÿ  z	utf-16-leé   )r?   ÚRuntimeErrorÚsysÚ
maxunicodeÚoffsetÚlengthÚencodeÚdecode)r   rM   Zentity_textr   r   r   Úparse_explanation_entityä   s    
zPoll.parse_explanation_entity)Útypesr,   c                    s(   ˆdkrt j‰‡ ‡fdd„ˆ jp"g D ƒS )a  
        Returns a :obj:`dict` that maps :class:`telegram.MessageEntity` to :obj:`str`.
        It contains entities from this polls explanation filtered by their ``type`` attribute as
        the key, and the text that each entity belongs to as the value of the :obj:`dict`.

        Note:
            This method should always be used instead of the :attr:`explanation_entities`
            attribute, since it calculates the correct substring from the message text based on
            UTF-16 codepoints. See :attr:`parse_explanation_entity` for more info.

        Args:
            types (List[:obj:`str`], optional): List of ``MessageEntity`` types as strings. If the
                    ``type`` attribute of an entity is contained in this list, it will be returned.
                    Defaults to :attr:`telegram.MessageEntity.ALL_TYPES`.

        Returns:
            Dict[:class:`telegram.MessageEntity`, :obj:`str`]: A dictionary of entities mapped to
            the text that belongs to them, calculated based on UTF-16 codepoints.

        Nc                    s"   i | ]}|j ˆkr|ˆ  |¡“qS r   )r9   rV   )rB   rM   ©r   rW   r   r   Ú
<dictcomp>  s   
þ z3Poll.parse_explanation_entities.<locals>.<dictcomp>)r   Z	ALL_TYPESr:   rX   r   rX   r   Úparse_explanation_entities  s
    þzPoll.parse_explanation_entitiesÚREGULARÚQUIZÚMAX_QUESTION_LENGTHÚMAX_OPTION_LENGTH)NNNNN)N)"r   r   r   r   r   r    r   r   r!   Úboolr   Údatetimer   r   r3   r   r   r/   rG   rV   r   rZ   r   ZPOLL_REGULARr[   r   r#   Z	POLL_QUIZr\   ZMAX_POLL_QUESTION_LENGTHr]   r"   r^   Ú__classcell__r   r   rK   r   r4   i   sB   
/     òñ!r4   )r   r`   rP   Útypingr   r   r   r   r   r   Ztelegramr   r	   r
   r   Ztelegram.utils.helpersr   r   Ztelegram.utils.typesr   r   r   r$   r4   r   r   r   r   Ú<module>   s    *