o
    XzjY9  ã                   @   s   d Z ddlZddlZddlZddlZddl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 G dd„ dejƒZe e¡Zd)dd	„Zd
d„ Zdd„ Zdd„ Zddœdd„Zdd„ Zd)dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ ZG d#d$„ d$e ƒZ!G d%d&„ d&ej"ƒZ#d'd(„ Z$dS )*z6Various helpers not related to the Telegram API itselfé    N)ÚPath)Úsha1c                   @   s   e Zd ZdZdZdZdS )Ú_EntityTyper   é   é   N)Ú__name__Ú
__module__Ú__qualname__ÚUSERÚCHATÚCHANNEL© r   r   úK/var/www/kodo/tsheker/venv/lib/python3.10/site-packages/telethon/helpers.pyr      s    r   Tc                 C   s   t jt d¡| dd�S )zEGenerates a random long integer (8 bytes), which is optionally signedé   Úlittle)ÚsignedÚ	byteorder)ÚintÚ
from_bytesÚosÚurandom©r   r   r   r   Úgenerate_random_long   s   r   c                 C   s&   t j | ¡}|rt j|dd� dS dS )z(Ensures that the parent directory existsT)Úexist_okN)r   ÚpathÚdirnameÚmakedirs)Ú	file_pathÚparentr   r   r   Úensure_parent_dir_exists    s   ÿr   c                 C   s   d  dd„ | D ƒ¡S )NÚ c              
   s   sR   � | ]$}d t |ƒ  krdkr#n nd dd„ t d| d¡¡D ƒ¡n|V  qdS )i   iÿÿ r    c                 s   ó   � | ]}t |ƒV  qd S ©N)Úchr)Ú.0Úyr   r   r   Ú	<genexpr>+   ó   € z*add_surrogate.<locals>.<genexpr>.<genexpr>z<HHzutf-16leN)ÚordÚjoinÚstructÚunpackÚencode©r$   Úxr   r   r   r&   (   s   € "ÿ
üz add_surrogate.<locals>.<genexpr>)r)   ©Útextr   r   r   Úadd_surrogate'   s   
ür1   c                 C   s   |   dd¡ d¡S )Nzutf-16Úsurrogatepass)r,   Údecoder/   r   r   r   Údel_surrogate0   s   r4   )Úlengthc                C   sh   |du rt | ƒ}d|  k ot | ƒk n  o3d| |d    ko#dkn  o3d| |   ko1dkS   S )zS
    `True` if ``index`` is within a surrogate (before and after it, not at!).
    Nr   u   í €u   í¯¿u   í¿¿)Úlen)r0   Úindexr5   r   r   r   Úwithin_surrogate4   s   ÿýýr8   c                 C   sè   |s|   ¡ S t| ƒ}|  ¡ } |t| ƒ }|  ¡ } t| ƒ}ttt|ƒƒƒD ]M}|| }|jdkr3||= q$|j|j |krU|j|krH| j|8  _n|j|j | |_d|_n||= q$|j|j |krbq$|j|krk||= q$||j |_q$| S )zý
    Strips whitespace from the given surrogated text modifying the provided
    entities, also removing any empty (0-length) entities.

    This assumes that the length of entities is greater or equal to 0, and
    that no entity is out of bounds.
    r   )Ústripr6   ÚlstripÚrstripÚreversedÚranger5   Úoffset)r0   ÚentitiesÚlen_oriÚleft_offsetÚ	len_finalÚiÚer   r   r   Ú
strip_textB   s2   

	

rE   c                 c   sH   � |r| du s| dk s| d7 } d}|| kr"|d7 }|V  || ksdS dS )z¿
    Generates an integer sequence starting from 1. If `retries` is
    not a zero or a positive integer value, the sequence will be
    infinite, otherwise it will end at `retries + 1`.
    Nr   r   r   )ÚretriesÚforce_retryÚattemptr   r   r   Úretry_rangeŒ   s   €	þrI   c                 Ã   s   �t  | ¡r| I d H S | S r"   )ÚinspectÚisawaitable)Úvaluer   r   r   Ú_maybe_awaitŸ   s   €

rM   c                 Ë   s¼   �|  ¡ D ]V\}}|sq| ¡  z|I dH  W q tjy!   Y q ty)   Y q tyI } z|jdkr?|  d|t|ƒ|¡ W Y d}~qd}~w t	y[   |  d|t|ƒ|¡ Y qw dS )zL
    Helper to cancel one or more tasks gracefully, logging exceptions.
    N)z"yield from wasn't used with futurez4Unhandled exception from %s after cancelling %s (%s))
ÚitemsÚcancelÚasyncioÚCancelledErrorÚRuntimeErrorÚAssertionErrorÚargsÚ	exceptionÚtypeÚ	Exception)ÚlogÚtasksÚnameÚtaskrD   r   r   r   Ú_cancel¦   s.   €

ÿ€
ÿÿär\   c                 C   s8   t | dƒr	| j}n| jj}| ¡ rtdƒ‚| |  ¡ ¡S )za
    Helps to cut boilerplate on async context
    managers that offer synchronous variants.
    Úloopz[You must use "async with" if the event loop is running (i.e. you are inside an "async def"))Úhasattrr]   Ú_clientÚ
is_runningrR   Úrun_until_completeÚ
__aenter__)Úselfr]   r   r   r   Ú_sync_enterË   s   
ÿrd   c                 G   s*   t | dƒr	| j}n| jj}| | j|Ž ¡S )Nr]   )r^   r]   r_   ra   Ú	__aexit__)rc   rT   r]   r   r   r   Ú
_sync_exitÞ   s   
rf   c                 C   sˆ   z| j dvrtd | ¡ƒ‚W n ty   td | ¡ƒ‚w | jj}d|v r(tjS d|v r/tjS d|v r6tj	S d|v r=tjS td | ¡ƒ‚)N)i‡VÔl   ¶9 l   F?ÓL iýò@iwy¡-l   ”]^ i¹aFl   —&4) z {} does not have any entity typez2{} is not a TLObject, cannot determine entity typeÚUserÚChatÚChannelÚSelf)
ÚSUBCLASS_OF_IDÚ	TypeErrorÚformatÚAttributeErrorÚ	__class__r   r   r
   r   r   )ÚentityrZ   r   r   r   Ú_entity_typeç   s"   

öÿrq   c                 C   s„   | j dddd�} |j dddd�}t||  ƒ ¡ }t| | ƒ ¡ }t|| ƒ ¡ }||dd…  }|dd… | |dd	…  }||fS )
z7Generates the key data corresponding to the given nonceé   r   Tr   é    Né   é   é   )Úto_bytesr   Údigest)Úserver_nonceÚ	new_nonceÚhash1Úhash2Úhash3ÚkeyÚivr   r   r   Úgenerate_key_data_from_nonce  s   r€   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )Ú	TotalListaµ  
    A list with an extra `total` property, which may not match its `len`
    since the total represents the total amount of items *available*
    somewhere else, not the items *in this list*.

    Examples:

        .. code-block:: python

            # Telethon returns these lists in some cases (for example,
            # only when a chunk is returned, but the "total" count
            # is available).
            result = await client.get_messages(chat, limit=10)

            print(result.total)  # large number
            print(len(result))  # 10
            print(result[0])  # latest message

            for x in result:  # show the 10 messages
                print(x.text)

    c                    s   t ƒ j|i |¤Ž d| _d S )Nr   )ÚsuperÚ__init__Útotal©rc   rT   Úkwargs©ro   r   r   rƒ   8  s   
zTotalList.__init__c                 C   ó   d  d dd„ | D ƒ¡| j¡S )Nú[{}, total={}]ú, c                 s   r!   r"   )Ústrr-   r   r   r   r&   >  r'   z$TotalList.__str__.<locals>.<genexpr>©rm   r)   r„   ©rc   r   r   r   Ú__str__<  ó   ÿzTotalList.__str__c                 C   rˆ   )Nr‰   rŠ   c                 s   r!   r"   )Úreprr-   r   r   r   r&   B  r'   z%TotalList.__repr__.<locals>.<genexpr>rŒ   r�   r   r   r   Ú__repr__@  r�   zTotalList.__repr__)r   r   r	   Ú__doc__rƒ   rŽ   r‘   Ú__classcell__r   r   r‡   r   r�   !  s
    r�   c                   @   sÆ   e Zd ZdZddœdd„Zdd„ Zdd	„ Zed
d„ ƒZedd„ ƒZ	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ ZdS ).Ú_FileStreama:  
    Proxy around things that represent a file and need to be used as streams
    which may or not need to be closed.

    This will handle `pathlib.Path`, `str` paths, in-memory `bytes`, and
    anything IO-like (including `aiofiles`).

    It also provides access to the name and file size (also necessary).
    N)Ú	file_sizec                C   s8   t |tƒrt| ¡ ƒ}|| _d | _|| _d | _d | _d S r"   )	Ú
isinstancer   r‹   ÚabsoluteÚ_fileÚ_nameÚ_sizeÚ_streamÚ_close_stream)rc   Úfiler•   r   r   r   rƒ   O  s   

z_FileStream.__init__c                 Ã   sˆ  �t | jtƒr#tj | j¡| _tj | j¡| _t	| jdƒ| _
d| _| S t | jtƒr;t| jƒ| _t | j¡| _
d| _| S tt| jdd ƒƒsHtdƒ‚t| jdd ƒ| _| j| _
d| _| jd u rÂtt| jdd ƒƒrpt| j ¡ ƒI d H }nd}|r¥t| j ¡ ƒI d H }t| j dtj¡ƒI d H  t| j ¡ ƒI d H | _t| j |tj¡ƒI d H  | S t d	¡ t| j ¡ ƒI d H }t|ƒ| _t |¡| _
d| _| S )
NÚrbTÚreadz,file description should have a `read` methodrZ   FÚseekabler   zRCould not determine file size beforehand so the entire file will be read in-memory)r–   r˜   r‹   r   r   Úbasenamer™   Úgetsizerš   Úopenr›   rœ   Úbytesr6   ÚioÚBytesIOÚcallableÚgetattrrl   rM   r    ÚtellÚseekÚSEEK_ENDÚSEEK_SETÚ_logÚwarningrŸ   )rc   r    ÚposÚdatar   r   r   rb   Y  sF   €
÷ÿ
z_FileStream.__aenter__c                 Ã   s.   �| j r| jrt| j ¡ ƒI d H  d S d S d S r"   )rœ   r›   rM   Úclose)rc   Úexc_typeÚexc_valÚexc_tbr   r   r   re   …  s   €ÿz_FileStream.__aexit__c                 C   ó   | j S r"   )rš   r�   r   r   r   r•   ‰  ó   z_FileStream.file_sizec                 C   rµ   r"   )r™   r�   r   r   r   rZ   �  r¶   z_FileStream.namec                 O   ó   | j j|i |¤ŽS r"   )r›   rŸ   r…   r   r   r   rŸ   ’  ó    z_FileStream.readc                 O   r·   r"   )r›   Úreadintor…   r   r   r   r¹   “  r¸   z_FileStream.readintoc                 O   r·   r"   )r›   Úwriter…   r   r   r   rº   ”  r¸   z_FileStream.writec                 O   r·   r"   )r›   Úfilenor…   r   r   r   r»   •  r¸   z_FileStream.filenoc                 O   r·   r"   )r›   Úflushr…   r   r   r   r¼   –  r¸   z_FileStream.flushc                 O   r·   r"   )r›   Úisattyr…   r   r   r   r½   —  r¸   z_FileStream.isattyc                 O   r·   r"   )r›   Úreadabler…   r   r   r   r¾   ˜  r¸   z_FileStream.readablec                 O   r·   r"   )r›   Úreadliner…   r   r   r   r¿   ™  r¸   z_FileStream.readlinec                 O   r·   r"   )r›   Ú	readlinesr…   r   r   r   rÀ   š  r¸   z_FileStream.readlinesc                 O   r·   r"   )r›   rª   r…   r   r   r   rª   ›  r¸   z_FileStream.seekc                 O   r·   r"   )r›   r    r…   r   r   r   r    œ  r¸   z_FileStream.seekablec                 O   r·   r"   )r›   r©   r…   r   r   r   r©   �  r¸   z_FileStream.tellc                 O   r·   r"   )r›   Útruncater…   r   r   r   rÁ   ž  r¸   z_FileStream.truncatec                 O   r·   r"   )r›   Úwritabler…   r   r   r   rÂ   Ÿ  r¸   z_FileStream.writablec                 O   r·   r"   )r›   Ú
writelinesr…   r   r   r   rÃ      r¸   z_FileStream.writelinesc                 O   s   d S r"   r   r…   r   r   r   r±   ¦  s   z_FileStream.close)r   r   r	   r’   rƒ   rb   re   Úpropertyr•   rZ   rŸ   r¹   rº   r»   r¼   r½   r¾   r¿   rÀ   rª   r    r©   rÁ   rÂ   rÃ   r±   r   r   r   r   r”   E  s2    	
,

r”   c                  C   sV   zt  ¡ W S  ty*   zt  ¡ W  Y S  ty)   t  ¡ } t  | ¡ |  Y  Y S w w r"   )rP   Úget_running_looprR   Úget_event_loopÚnew_event_loopÚset_event_loop)r]   r   r   r   rÅ   «  s   

ýýrÅ   )T)%r’   rP   r¥   Úenumr   r*   rJ   ÚloggingÚ	functoolsÚsysÚpathlibr   Úhashlibr   ÚEnumr   Ú	getLoggerr   r­   r   r   r1   r4   r8   rE   rI   rM   r\   rd   rf   rq   r€   Úlistr�   ÚIOBaser”   rÅ   r   r   r   r   Ú<module>   s<    

	
J%	($f