o
    r¨ÊhÁ  ã                   @   s¾   d Z ddlmZ ddlmZ ddlmZ z
ddlmZm	Z	 W n e
y)   e
dƒ‚w ddlmZ ddlmZ d	d
lmZ ddgiZg d¢ZG dd„ deƒZG dd„ deƒZdd„ ZeZeZdS )z!
Sends updates to a Discord bot.
é    )Úabsolute_import)Údeepcopy)Úgetenv)ÚClientÚClientConfigzPlease `pip install disco-py`)Útqdm)Ú_rangeé   )Ú
MonoWorkerzgithub.com/Ú	casperdcl)Ú	DiscordIOÚtqdm_discordÚtdranger   Útrangec                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )r   z.Non-blocking file-like IO using a Discord Bot.c              
      sz   t t| ƒ ¡  tƒ }||_t|ƒ}| jj| _z|j	 
|| j¡| _W dS  ty< } zt t|ƒ¡ W Y d}~dS d}~ww )z0Creates a new message in the given `channel_id`.N)Úsuperr   Ú__init__r   Útokenr   Ú	__class__Ú__name__ÚtextÚapiÚchannels_messages_createÚmessageÚ	ExceptionÚ	tqdm_autoÚwriteÚstr)Úselfr   Ú
channel_idÚconfigÚclientÚe©r   © úF/var/www/html/env/lib/python3.10/site-packages/tqdm/contrib/discord.pyr      s   
ÿ€ÿzDiscordIO.__init__c              
   C   s€   |sdS |  dd¡ ¡ }|| jkrdS || _z|  | jjd| d ¡}W |S  ty? } zt t	|ƒ¡ W Y d}~dS d}~ww )z,Replaces internal `message`'s text with `s`.NúÚ ú`)
ÚreplaceÚstripr   Úsubmitr   Úeditr   r   r   r   )r   ÚsÚfuturer!   r#   r#   r$   r   #   s   
ý€ÿzDiscordIO.write)r   Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__classcell__r#   r#   r"   r$   r      s    r   c                       s8   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡ fdd„Z‡  ZS )r   a  
    Standard `tqdm.auto.tqdm` but also sends updates to a Discord Bot.
    May take a few seconds to create (`__init__`).

    - create a discord bot (not public, no requirement of OAuth2 code
      grant, only send message permissions) & invite it to a channel:
      https://discordpy.readthedocs.io/en/latest/discord.html
    - copy the bot `{token}` & `{channel_id}` and paste below

    >>> from tqdm.contrib.discord import tqdm, trange
    >>> for i in tqdm(iterable, token='{token}', channel_id='{channel_id}'):
    ...     ...
    c                    s\   t |ƒ}t| dtdƒ¡| dtdƒ¡ƒ| _td| dd¡ƒ|d< tt| ƒj	|i |¤Ž dS )a”  
        Parameters
        ----------
        token  : str, required. Discord token
            [default: ${TQDM_DISCORD_TOKEN}].
        channel_id  : int, required. Discord channel ID
            [default: ${TQDM_DISCORD_CHANNEL_ID}].
        mininterval  : float, optional.
          Minimum of [default: 1.5] to avoid rate limit.

        See `tqdm.auto.tqdm.__init__` for other parameters.
        r   ÚTQDM_DISCORD_TOKENr   ÚTQDM_DISCORD_CHANNEL_IDg      ø?ÚminintervalN)
r   r   Úpopr   ÚdioÚmaxÚgetr   r   r   )r   ÚargsÚkwargsr"   r#   r$   r   A   s   þztqdm_discord.__init__c                    sz   t t| ƒjdi |¤Ž | j}d|v r!|d r!|d  dd¡|d< nd|d< |d  dd¡|d< | j | jdi |¤Ž¡ d S )NÚ
bar_formatz<bar/>z{bar}z{l_bar}{bar}{r_bar}z	{bar:10u}r#   )r   r   ÚdisplayÚformat_dictr(   r6   r   Úformat_meter)r   r:   Úfmtr"   r#   r$   r<   V   s   ztqdm_discord.displayc              	      sÂ   |   ¡ � ztj| _W n	 ty   Y nw W d  ƒ n1 sw   Y  tt| ƒj| g|¢R i |¤Ž}|   ¡ �  z	| j tj¡ W n	 tyJ   Y nw | jt_W d  ƒ |S 1 sZw   Y  |S )z‰
        Workaround for mixed-class same-stream nested progressbars.
        See [#509](https://github.com/tqdm/tqdm/issues/509)
        N)Úget_lockr   Ú
_instancesÚAttributeErrorr   r   Ú__new__Úupdate)Úclsr9   r:   Úinstancer"   r#   r$   rC   `   s(   
ÿ€ý
ÿ

úùztqdm_discord.__new__)r   r.   r/   r0   r   r<   rC   r1   r#   r#   r"   r$   r   3   s
    
r   c                  O   s   t t| Ž fi |¤ŽS )z„
    A shortcut for `tqdm.contrib.discord.tqdm(xrange(*args), **kwargs)`.
    On Python3+, `range` is used instead of `xrange`.
    )r   r   )r9   r:   r#   r#   r$   r   u   s   r   N)r0   Ú
__future__r   Úcopyr   Úosr   Údisco.clientr   r   ÚImportErrorÚ	tqdm.autor   r   Ú
tqdm.utilsr   Úutils_workerr
   Ú
__author__Ú__all__r   r   r   r   r#   r#   r#   r$   Ú<module>   s&    ÿ
B	