o
    5˜Žjn6  ã                   @  s  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 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 erXd d	lm Z m!Z! ed
eeƒZ"edƒZ#G dd„ deee" ƒZ$ddd„Z%G dd„ de$e ƒZ&G dd„ de$e ƒZ'dS )é    )Úannotations)ÚABC)Údeepcopy)Úchain)Úcycle)ÚTYPE_CHECKINGÚAnyÚGenericÚTypeVarÚcast)ÚCallbackData)ÚCallbackGameÚCopyTextButtonÚInlineKeyboardButtonÚInlineKeyboardMarkupÚKeyboardButtonÚKeyboardButtonPollTypeÚKeyboardButtonRequestChatÚKeyboardButtonRequestUsersÚLoginUrlÚReplyKeyboardMarkupÚSwitchInlineQueryChosenChatÚ
WebAppInfo)Ú	GeneratorÚIterableÚ
ButtonTypeÚTc                   @  sÖ   e Zd ZU dZdZded< dZded< dZded< 	d?d@dd„Ze	dAdd„ƒZ
dBdd„ZdCdd„ZdDdd„ZdEd d!„ZdFd$d%„ZdGd&d'„ZdHd)d*„Zdd+œdId.d/„Zd0d1œdJd4d5„ZdKd7d8„ZdLd:d;„ZdMd=d>„ZdS )NÚKeyboardBuilderz¡
    Generic keyboard builder that helps to adjust your markup with defined shape of lines.

    Works both of InlineKeyboardMarkup and ReplyKeyboardMarkup.
    r   ÚintÚ	max_widthÚ	min_widthÚmax_buttonsNÚbutton_typeútype[ButtonType]Úmarkupúlist[list[ButtonType]] | NoneÚreturnÚNonec                 C  sF   t |ttfƒsd|› d�}t|ƒ‚|| _|r|  |¡ ng }|| _d S )NzButton type z are not allowed here)Ú
issubclassr   r   Ú
ValueErrorÚ_button_typeÚ_validate_markupÚ_markup)Úselfr"   r$   Úmsg© r/   úS/var/www/kodo/Anonymous/send/lib/python3.10/site-packages/aiogram/utils/keyboard.pyÚ__init__+   s   
zKeyboardBuilder.__init__ú!Generator[ButtonType, None, None]c                 c  s   � t  |  ¡ ¡E dH  dS )zB
        Get flatten set of all buttons

        :return:
        N)r   Úfrom_iterableÚexport©r-   r/   r/   r0   Úbuttons:   s   €zKeyboardBuilder.buttonsÚbuttonr   Úboolc                 C  s8   | j }t||ƒs|›d|j›dt|ƒj›�}t|ƒ‚dS )zb
        Check that button item has correct type

        :param button:
        :return:
        z should be type z not T)r*   Ú
isinstanceÚ__name__Útyper)   )r-   r7   Úallowedr.   r/   r/   r0   Ú_validate_buttonC   s
   
z KeyboardBuilder._validate_buttonr6   c                 G  s   t t| j|ƒƒS )zi
        Check that all passed button has correct type

        :param buttons:
        :return:
        )ÚallÚmapr=   )r-   r6   r/   r/   r0   Ú_validate_buttonsP   s   z!KeyboardBuilder._validate_buttonsÚrowúlist[ButtonType]c                 C  sj   t |tƒsd|›d| jj› dt|ƒj› �}t|ƒ‚t|ƒ| jkr.d|›d| j› d�}t|ƒ‚| j|Ž  dS )z­
        Check that row of buttons are correct
        Row can be only list of allowed button types and has length 0 <= n <= 8

        :param row:
        :return:
        zRow z should be type 'list[z]' not type z is too long (max width: ú)T)	r9   Úlistr*   r:   r;   r)   Úlenr   r@   )r-   rA   r.   r/   r/   r0   Ú_validate_rowY   s   
ÿÿ
zKeyboardBuilder._validate_rowúlist[list[ButtonType]]c                 C  sr   d}t |tƒsd| jj› dt|ƒj›�}t|ƒ‚|D ]}|  |¡ |t|ƒ7 }q|| jkr7d| j› �}t|ƒ‚dS )z™
        Check that passed markup has correct data structure
        Markup is list of lists of buttons

        :param markup:
        :return:
        r   z!Markup should be type 'list[list[z]]' not type z.Too much buttons detected Max allowed count - T)	r9   rD   r*   r:   r;   r)   rF   rE   r!   )r-   r$   Úcountr.   rA   r/   r/   r0   r+   m   s   
ÿÿ

z KeyboardBuilder._validate_markupÚsizer   c                 C  sT   t |tƒsd}t|ƒ‚|t| j| jd ƒvr(d|› d| j› d| j› d�}t|ƒ‚|S )z[
        Validate that passed size is legit

        :param size:
        :return:
        zOnly int sizes are allowedé   z	Row size z is not allowed, range: [z, ú])r9   r   r)   Úranger    r   )r-   rI   r.   r/   r/   r0   Ú_validate_size„   s   
zKeyboardBuilder._validate_sizec                 C  s
   t | jƒS )aB  
        Export configured markup as list of lists of buttons

        .. code-block:: python

            >>> builder = KeyboardBuilder(button_type=InlineKeyboardButton)
            >>> ... # Add buttons to builder
            >>> markup = InlineKeyboardMarkup(inline_keyboard=builder.export())

        :return:
        )r   r,   r5   r/   r/   r0   r4   “   s   
zKeyboardBuilder.exportúKeyboardBuilder[ButtonType]c                 G  sº   | j |Ž  |  ¡ }|r1t|d ƒ| jk r1|d }| jt|ƒ }|d|… ||d… }}| |¡ | jdkrQ|rP|d| j… || jd… }}| t|ƒ¡ |s8n| t|ƒ¡ || _| S )z^
        Add one or many buttons to markup.

        :param buttons:
        :return:
        éÿÿÿÿNr   )r@   r4   rE   r   ÚextendÚappendrD   r,   )r-   r6   r$   Úlast_rowÚposÚheadrA   r/   r/   r0   Úadd¡   s    


þ€zKeyboardBuilder.add)ÚwidthrV   ú
int | Nonec                  sN   ˆdu r| j ‰|  ˆ¡ | jˆ Ž  | j ‡ ‡fdd„tdtˆ ƒˆƒD ƒ¡ | S )z®
        Add row to markup

        When too much buttons is passed it will be separated to many rows

        :param buttons:
        :param width:
        :return:
        Nc                 3  s$   � | ]}t ˆ ||ˆ … ƒV  qd S ©N)rD   )Ú.0rS   ©r6   rV   r/   r0   Ú	<genexpr>Ì   s   € 
ÿz&KeyboardBuilder.row.<locals>.<genexpr>r   )r   rM   r@   r,   rP   rL   rE   )r-   rV   r6   r/   rZ   r0   rA   ½   s   


ÿzKeyboardBuilder.rowF)ÚrepeatÚsizesr\   c          	      G  sŒ   |s| j f}t| j|ƒ}|rt|ƒnt|ƒ}t|ƒ}g }g }| jD ]}t|ƒ|kr4| |¡ t|ƒ}g }| |¡ q!|rA| |¡ || _	| S )a‰  
        Adjust previously added buttons to specific row sizes.

        By default, when the sum of passed sizes is lower than buttons count the last
        one size will be used for tail of the markup.
        If repeat=True is passed - all sizes will be cycled when available more buttons
        count than all sizes

        :param sizes:
        :param repeat:
        :return:
        )
r   r?   rM   Ú
repeat_allÚrepeat_lastÚnextr6   rE   rQ   r,   )	r-   r\   r]   Úvalidated_sizesÚ
sizes_iterrI   r$   rA   r7   r/   r/   r0   ÚadjustÑ   s"   


zKeyboardBuilder.adjustÚkwargsc                 K  s:   t | d¡ }tƒr| ¡ |d< | jdi |¤Ž}|  |¡S )zO
        Add button to markup

        :param kwargs:
        :return:
        Úcallback_dataNr/   )r9   Úgetr   Úpackr*   rU   )r-   rd   re   r7   r/   r/   r0   Ú_buttonò   s   
zKeyboardBuilder._buttonú*InlineKeyboardMarkup | ReplyKeyboardMarkupc                 K  sR   | j tu rtttt  |  ¡ ƒ}tdd|i|¤ŽS tttt  |  ¡ ƒ}t|d�S )NÚkeyboard©Úinline_keyboardr/   )r*   r   r   rD   r4   r   r   r   )r-   rd   rj   rl   r/   r/   r0   Ú	as_markupþ   s
   

zKeyboardBuilder.as_markupÚbuilderc                 C  sb   t |tƒsdt|ƒj› �}t|ƒ‚|j| jur'd| jj› d|jj› �}t|ƒ‚| j | ¡ ¡ | S )Nz*Only KeyboardBuilder can be attached, not z9Only builders with same button type can be attached, not z and )	r9   r   r;   r:   r)   r*   r,   rP   r4   )r-   rn   r.   r/   r/   r0   Úattach  s   
ÿÿÿzKeyboardBuilder.attachrX   )r"   r#   r$   r%   r&   r'   )r&   r2   )r7   r   r&   r8   )r6   r   r&   r8   )rA   rB   r&   r8   )r$   rG   r&   r8   )rI   r   r&   r   )r&   rG   )r6   r   r&   rN   )r6   r   rV   rW   r&   rN   )r]   r   r\   r8   r&   rN   )rd   r   r&   rN   )rd   r   r&   ri   )rn   rN   r&   rN   )r:   Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r    r!   r1   Úpropertyr6   r=   r@   rF   r+   rM   r4   rU   rA   rc   rh   rm   ro   r/   r/   r/   r0   r       s*   
 ý


	




!
r   ÚitemsúIterable[T]r&   úGenerator[T, None, None]c                 c  sh   � t | ƒ}zt|ƒ}W n
 ty   Y d S w |V  d}	 |s0zt|ƒ}W n ty/   d}Y nw |V  q)NFT)Úiterr`   ÚStopIteration)ru   Ú
items_iterÚvalueÚfinishedr/   r/   r0   r_     s$   €þÿúr_   c                      s–   e Zd ZU dZdZded< dZded< dZded< d	d	d	d	d	d	d	d	d	d	d	d	d
œd5d$d%„Zd6‡ fd'd(„Z	d7d8‡ fd,d-„Z
d9d/d0„Zed:d3d4„ƒZ‡  ZS );ÚInlineKeyboardBuilderzK
    Inline keyboard builder inherits all methods from generic builder
    é   r   r   rJ   r    éd   r!   N)Úicon_custom_emoji_idÚstyleÚurlre   Úweb_appÚ	login_urlÚswitch_inline_queryÚ switch_inline_query_current_chatÚswitch_inline_query_chosen_chatÚ	copy_textÚcallback_gameÚpayÚtextÚstrr€   ú
str | Noner�   r‚   re   ústr | CallbackData | Nonerƒ   úWebAppInfo | Noner„   úLoginUrl | Noner…   r†   r‡   ú"SwitchInlineQueryChosenChat | Nonerˆ   úCopyTextButton | Noner‰   úCallbackGame | NonerŠ   úbool | Nonerd   r   r&   c                K  s2   t t| jd|||||||||	|
|||dœ|¤ŽƒS )N)r‹   r€   r�   r‚   re   rƒ   r„   r…   r†   r‡   rˆ   r‰   rŠ   r/   )r   r}   rh   )r-   r‹   r€   r�   r‚   re   rƒ   r„   r…   r†   r‡   rˆ   r‰   rŠ   rd   r/   r/   r0   r7   .  s(   óòþzInlineKeyboardBuilder.buttonr   c                   ó   t ttƒ jdi |¤ŽƒS )z!Construct an InlineKeyboardMarkupNr/   )r   r   Úsuperrm   ©r-   rd   ©Ú	__class__r/   r0   rm   T  ó   zInlineKeyboardBuilder.as_markupr$   ú'list[list[InlineKeyboardButton]] | Noner'   c                   ó   t ƒ jt|d� d S ©N)r"   r$   )r–   r1   r   ©r-   r$   r˜   r/   r0   r1   X  ó   zInlineKeyboardBuilder.__init__r-   c                 C  ó   t |  ¡ d�S ©zQ
        Make full copy of current builder with markup

        :return:
        ©r$   )r}   r4   r5   r/   r/   r0   Úcopy[  ó   zInlineKeyboardBuilder.copyÚclsútype[InlineKeyboardBuilder]c                 C  ó   | |j d�S ©z^
        Create builder from existing markup

        :param markup:
        :return:
        r¢   rk   ©r¥   r$   r/   r/   r0   Úfrom_markupc  s   z!InlineKeyboardBuilder.from_markup)r‹   rŒ   r€   r�   r�   r�   r‚   r�   re   rŽ   rƒ   r�   r„   r�   r…   r�   r†   r�   r‡   r‘   rˆ   r’   r‰   r“   rŠ   r”   rd   r   r&   r}   )rd   r   r&   r   rX   )r$   r›   r&   r'   )r-   r}   r&   r}   )r¥   r¦   r$   r   r&   r}   ©r:   rp   rq   rr   r   rs   r    r!   r7   rm   r1   r£   Úclassmethodrª   Ú__classcell__r/   r/   r˜   r0   r}   %  s.   
 ñ&
r}   c                	      sŽ   e Zd ZU dZdZded< dZded< dZded< d	d	d	d	d	d	d	d	d
œd-dd„Zd.‡ fd!d"„Z	d/d0‡ fd&d'„Z
d1d)d*„Zed2d+d,„ƒZ‡  ZS )3ÚReplyKeyboardBuilderzJ
    Reply keyboard builder inherits all methods from generic builder
    é
   r   r   rJ   r    i,  r!   N)r€   r�   Úrequest_usersÚrequest_chatÚrequest_contactÚrequest_locationÚrequest_pollrƒ   r‹   rŒ   r€   r�   r�   r°   ú!KeyboardButtonRequestUsers | Noner±   ú KeyboardButtonRequestChat | Noner²   r”   r³   r´   úKeyboardButtonPollType | Nonerƒ   r�   rd   r   r&   c       	         K  s*   t t| jd|||||||||	dœ	|
¤ŽƒS )N)	r‹   r€   r�   r°   r±   r²   r³   r´   rƒ   r/   )r   r®   rh   )r-   r‹   r€   r�   r°   r±   r²   r³   r´   rƒ   rd   r/   r/   r0   r7   z  s    ÷
öþzReplyKeyboardBuilder.buttonr   c                   r•   )zConstruct a ReplyKeyboardMarkupNr/   )r   r   r–   rm   r—   r˜   r/   r0   rm   ˜  rš   zReplyKeyboardBuilder.as_markupr$   ú!list[list[KeyboardButton]] | Noner'   c                   rœ   r�   )r–   r1   r   rž   r˜   r/   r0   r1   œ  rŸ   zReplyKeyboardBuilder.__init__r-   c                 C  r    r¡   )r®   r4   r5   r/   r/   r0   r£   Ÿ  r¤   zReplyKeyboardBuilder.copyc                 C  r§   r¨   )rj   r©   r/   r/   r0   rª   §  s   z ReplyKeyboardBuilder.from_markup)r‹   rŒ   r€   r�   r�   r�   r°   rµ   r±   r¶   r²   r”   r³   r”   r´   r·   rƒ   r�   rd   r   r&   r®   )rd   r   r&   r   rX   )r$   r¸   r&   r'   )r-   r®   r&   r®   )r$   r   r&   r®   r«   r/   r/   r˜   r0   r®   q  s&   
 õ
r®   N)ru   rv   r&   rw   )(Ú
__future__r   Úabcr   r£   r   Ú	itertoolsr   r   r^   Útypingr   r   r	   r
   r   Úaiogram.filters.callback_datar   Úaiogram.typesr   r   r   r   r   r   r   r   r   r   r   r   Úcollections.abcr   r   r   r   r   r_   r}   r®   r/   r/   r/   r0   Ú<module>   s"    8 
tL