3
uQh                  @   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-build-2nz6shyl/telethon/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  j  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 )Nr   )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 )N)r    )r(   r   r   r   r+   2   s    zMemorySession.server_addressc             C   s   | j S )N)r!   )r(   r   r   r   r,   6   s    zMemorySession.portc             C   s   | j S )N)r"   )r(   r   r   r   auth_key:   s    zMemorySession.auth_keyc             C   s
   || _ d S )N)r"   )r(   valuer   r   r   r.   >   s    c             C   s   | j S )N)r#   )r(   r   r   r   
takeout_idB   s    zMemorySession.takeout_idc             C   s
   || _ d S )N)r#   )r(   r/   r   r   r   r0   F   s    c             C   s   | j j|d S )N)r'   get)r(   	entity_idr   r   r   get_update_stateJ   s    zMemorySession.get_update_statec             C   s   || j |< d S )N)r'   )r(   r2   stater   r   r   set_update_stateM   s    zMemorySession.set_update_statec             C   s
   | j j S )N)r'   items)r(   r   r   r   get_update_statesP   s    zMemorySession.get_update_statesc             C   s   d S )Nr   )r(   r   r   r   closeS   s    zMemorySession.closec             C   s   d S )Nr   )r(   r   r   r   saveV   s    zMemorySession.savec             C   s   d S )Nr   )r(   r   r   r   deleteY   s    zMemorySession.deletec             C   s   | ||||fS )Nr   )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 ytj|dd}tj|}W n tk
r>   d S X t |ttfrV|j}nt |t	rfd}nd S t
|dd pxd }|d k	r|j }t
|dd }tj|pd }| j|||||S )NF)Z
allow_selfr   r=   r>   )
isinstancer   r   get_input_peerget_peer_id	TypeErrorr   r   access_hashr   getattrlowerZget_display_namer@   )r(   epZ	marked_idZp_hashr=   r>   r?   r   r   r   _entity_to_rowc   s&    

zMemorySession._entity_to_rowc             C   s   t |t rtj|r|}ntg }t|dr6|j|j t|drL|j|j t|drntj|jrn|j	|j t|drtj|j
r|j	|j
 g }x$|D ]}| j|}|r|j| qW |S )Nuserchatchatsusers)rA   r   r   Zis_list_likehasattrappendrK   rL   rM   extendrN   rJ   )r(   tloentitiesZrowsrH   rowr   r   r   _entities_to_rows   s"    



zMemorySession._entities_to_rowsc             C   s   |  j t| j|O  _ d S )N)r&   r%   rU   )r(   rR   r   r   r   process_entities   s    zMemorySession.process_entitiesc                s2   yt  fdd| jD S  tk
r,   Y nX d S )Nc             3   s(   | ] \}}}}}| kr||fV  qd S )Nr   ).0r;   r<   _Zfound_phone)r>   r   r   	<genexpr>   s    z9MemorySession.get_entity_rows_by_phone.<locals>.<genexpr>)nextr&   StopIteration)r(   r>   r   )r>   r   get_entity_rows_by_phone   s
    z&MemorySession.get_entity_rows_by_phonec                s2   yt  fdd| jD S  tk
r,   Y nX d S )Nc             3   s(   | ] \}}}}}| kr||fV  qd S )Nr   )rW   r;   r<   Zfound_usernamerX   )r=   r   r   rY      s    z<MemorySession.get_entity_rows_by_username.<locals>.<genexpr>)rZ   r&   r[   )r(   r=   r   )r=   r   get_entity_rows_by_username   s
    z)MemorySession.get_entity_rows_by_usernamec                s2   yt  fdd| jD S  tk
r,   Y nX d S )Nc             3   s(   | ] \}}}}}| kr||fV  qd S )Nr   )rW   r;   r<   rX   Z
found_name)r?   r   r   rY      s    z8MemorySession.get_entity_rows_by_name.<locals>.<genexpr>)rZ   r&   r[   )r(   r?   r   )r?   r   get_entity_rows_by_name   s
    z%MemorySession.get_entity_rows_by_nameTc                sz   y`|rt  fdd| jD S tjt tjt tjt ft fdd| jD S W n tk
rt   Y nX d S )Nc             3   s(   | ] \}}}}}| kr||fV  qd S )Nr   )rW   found_idr<   rX   )r;   r   r   rY      s    z6MemorySession.get_entity_rows_by_id.<locals>.<genexpr>c             3   s(   | ] \}}}}}| kr||fV  qd S )Nr   )rW   r_   r<   rX   )idsr   r   rY      s    )rZ   r&   r   rC   r   r	   r
   r[   )r(   r;   exactr   )r;   r`   r   get_entity_rows_by_id   s    z#MemorySession.get_entity_rows_by_idc             C   sf  y|j d
kr|S tj|S  ttfk
r^   t|trFtj|}d}nt|t pX|dk }Y nX d }t|t	rtj
|}|r| j|}qtj|\}}|r| r| j|}qtj|d }|r| j|dd}nt|tr| j||}| ot|t	r | j|}|rX|\}}	tj|\}}
|
tkr0t||	S |
tkrBt|S |
tkrbt||	S n
td	|d S )N   9    F?L @Tr   r   F)ra   z%Could not find input entity with key )rc   rd   re   )ZSUBCLASS_OF_IDr   rB   AttributeErrorrD   rA   r   rC   intstrZparse_phoner\   Zparse_usernamer]   Zresolve_invite_linkrb   r^   Z
resolve_idr   r   r	   r   r
   r   r   )r(   keyra   resultr>   r=   Zinvitetupr2   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jt|f}|j|jf}|| j	|< d S )NzCannot cache %s instance)
rA   r   r   rD   typer   r   r;   rE   r$   )r(   
md5_digest	file_sizeinstanceri   r/   r   r   r   
cache_file   s
    zMemorySession.cache_filec             C   s8   ||t j|f}y|| j|  S  tk
r2   d S X d S )N)r   r   r$   KeyError)r(   ro   rp   r   ri   r   r   r   get_file   s
    zMemorySession.get_file)T)r   r   r   r   r-   propertyr*   r+   r,   r.   setterr0   r3   r5   r7   r8   r9   r:   r   r@   rJ   rU   rV   r\   r]   r^   rb   rm   rr   rt   __classcell__r   r   )r)   r   r      s4   
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   (