U
    h                      @   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	m
Z
mZmZmZmZmZmZ G dd	 d	eZG d
d deZdS )    )Enum   )Session   )utils)TLObject)PeerUserPeerChatPeerChannelInputPeerUserInputPeerChatInputPeerChannel
InputPhotoInputDocumentc                   @   s    e Zd ZdZdZedd ZdS )_SentFileTyper   r   c                 C   s(   | t krtjS | tkrtjS tdd S )Nz/The cls must be either InputDocument/InputPhoto)r   r   DOCUMENTr   PHOTO
ValueError)cls r   </tmp/pip-unpacked-wheel-c81u5j2r/telethon/sessions/memory.py	from_type   s
    z_SentFileType.from_typeN)__name__
__module____qualname__r   r   staticmethodr   r   r   r   r   r      s   r   c                       s
  e Zd Z fddZdd Zedd Zedd Zed	d
 Zedd Z	e	j
dd Z	edd Zej
dd Zdd Zdd Zdd Zdd Zdd Z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d4d,d-Zd.d/ Zd0d1 Zd2d3 Z  ZS )5MemorySessionc                    s@   t    d| _d | _d | _d | _d | _i | _t | _	i | _
d S Nr   )super__init___dc_id_server_address_port	_auth_key_takeout_id_filesset	_entities_update_statesself	__class__r   r   r      s    
zMemorySession.__init__c                 C   s   |pd| _ || _|| _d S r   )r    r!   r"   )r*   dc_idserver_addressportr   r   r   set_dc)   s    
zMemorySession.set_dcc                 C   s   | j S N)r    r)   r   r   r   r-   .   s    zMemorySession.dc_idc                 C   s   | j S r1   )r!   r)   r   r   r   r.   2   s    zMemorySession.server_addressc                 C   s   | j S r1   )r"   r)   r   r   r   r/   6   s    zMemorySession.portc                 C   s   | j S r1   r#   r)   r   r   r   auth_key:   s    zMemorySession.auth_keyc                 C   s
   || _ d S r1   r2   r*   valuer   r   r   r3   >   s    c                 C   s   | j S r1   r$   r)   r   r   r   
takeout_idB   s    zMemorySession.takeout_idc                 C   s
   || _ d S r1   r6   r4   r   r   r   r7   F   s    c                 C   s   | j |d S r1   )r(   get)r*   	entity_idr   r   r   get_update_stateJ   s    zMemorySession.get_update_statec                 C   s   || j |< d S r1   )r(   )r*   r9   stater   r   r   set_update_stateM   s    zMemorySession.set_update_statec                 C   s
   | j  S r1   )r(   itemsr)   r   r   r   get_update_statesP   s    zMemorySession.get_update_statesc                 C   s   d S r1   r   r)   r   r   r   closeS   s    zMemorySession.closec                 C   s   d S r1   r   r)   r   r   r   saveV   s    zMemorySession.savec                 C   s   d S r1   r   r)   r   r   r   deleteY   s    zMemorySession.deletec                 C   s   | ||||fS r1   r   )idhashusernamephonenamer   r   r   _entity_values_to_row\   s    z#MemorySession._entity_values_to_rowc                 C   s   t |tsd S ztj|dd}t|}W n tk
r@   Y d S X t |ttfrX|j}nt |t	rhd}nd S t
|dd pzd }|d k	r| }t
|dd }t|pd }| |||||S )NF)Z
allow_selfr   rD   rE   )
isinstancer   r   get_input_peerget_peer_id	TypeErrorr   r   access_hashr   getattrlowerZget_display_namerG   )r*   epZ	marked_idZp_hashrD   rE   rF   r   r   r   _entity_to_rowc   s0    

    zMemorySession._entity_to_rowc                 C   s   t |tst|r|}ntg }t|dr4||j t|drJ||j t|drlt|jrl|	|j t|drt|j
r|	|j
 g }|D ]}| |}|r|| q|S )Nuserchatchatsusers)rH   r   r   Zis_list_likehasattrappendrR   rS   rT   extendrU   rQ   )r*   tloentitiesZrowsrO   rowr   r   r   _entities_to_rows   s"    


zMemorySession._entities_to_rowsc                 C   s   |  j t| |O  _ d S r1   )r'   r&   r\   )r*   rY   r   r   r   process_entities   s    zMemorySession.process_entitiesc                    s4   zt  fdd| jD W S  tk
r.   Y nX d S )Nc                 3   s(   | ] \}}}}}| kr||fV  qd S r1   r   ).0rB   rC   _Zfound_phonerE   r   r   	<genexpr>   s    z9MemorySession.get_entity_rows_by_phone.<locals>.<genexpr>nextr'   StopIteration)r*   rE   r   r`   r   get_entity_rows_by_phone   s    
z&MemorySession.get_entity_rows_by_phonec                    s4   zt  fdd| jD W S  tk
r.   Y nX d S )Nc                 3   s(   | ] \}}}}}| kr||fV  qd S r1   r   )r^   rB   rC   Zfound_usernamer_   rD   r   r   ra      s    z<MemorySession.get_entity_rows_by_username.<locals>.<genexpr>rb   )r*   rD   r   rf   r   get_entity_rows_by_username   s    
z)MemorySession.get_entity_rows_by_usernamec                    s4   zt  fdd| jD W S  tk
r.   Y nX d S )Nc                 3   s(   | ] \}}}}}| kr||fV  qd S r1   r   )r^   rB   rC   r_   Z
found_namerF   r   r   ra      s    z8MemorySession.get_entity_rows_by_name.<locals>.<genexpr>rb   )r*   rF   r   rh   r   get_entity_rows_by_name   s    
z%MemorySession.get_entity_rows_by_nameTc                    s~   zd|r t  fdd| jD W S tt tt tt ft fdd| jD W S W n tk
rx   Y nX d S )Nc                 3   s(   | ] \}}}}}| kr||fV  qd S r1   r   r^   Zfound_idrC   r_   )rB   r   r   ra      s    z6MemorySession.get_entity_rows_by_id.<locals>.<genexpr>c                 3   s(   | ] \}}}}}| kr||fV  qd S r1   r   rj   )idsr   r   ra      s    )rc   r'   r   rJ   r   r	   r
   rd   )r*   rB   exactr   )rB   rk   r   get_entity_rows_by_id   s    
z#MemorySession.get_entity_rows_by_idc              	   C   sh  z|j dkr|W S t|W S  ttfk
rb   t|trJt|}d}nt|t p\|dk }Y nX d }t|t	rt
|}|r| |}qt|\}}|r|s| |}qt|d }|r| j|dd}nt|tr| ||}|st|t	r| |}|rZ|\}}	t|\}}
|
tkr2t||	S |
tkrDt|S |
tkrdt||	S n
td|d S )N)l   9 l   F?L i@Tr   r   F)rl   z%Could not find input entity with key )ZSUBCLASS_OF_IDr   rI   AttributeErrorrK   rH   r   rJ   intstrZparse_phonere   Zparse_usernamerg   Zresolve_invite_linkrm   ri   Z
resolve_idr   r   r	   r   r
   r   r   )r*   keyrl   resultrE   rD   Zinvitetupr9   Zentity_hashkindr   r   r   get_input_entity   sD    










zMemorySession.get_input_entityc                 C   sL   t |ttfstdt| ||tt|f}|j|jf}|| j	|< d S )NzCannot cache %s instance)
rH   r   r   rK   typer   r   rB   rL   r%   )r*   
md5_digest	file_sizeinstancerq   r5   r   r   r   
cache_file   s
    zMemorySession.cache_filec                 C   s<   ||t |f}z|| j|  W S  tk
r6   Y d S X d S r1   )r   r   r%   KeyError)r*   rw   rx   r   rq   r   r   r   get_file   s
    zMemorySession.get_file)T)r   r   r   r   r0   propertyr-   r.   r/   r3   setterr7   r:   r<   r>   r?   r@   rA   r   rG   rQ   r\   r]   re   rg   ri   rm   ru   rz   r|   __classcell__r   r   r+   r   r      sD   








1r   N)enumr   Zabstractr    r   tlr   Ztl.typesr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   <module>   s   (