U
    h                     @   s   d dl Z d dlZd dlZd dlmZmZmZmZ d dlZeed dd  Z	ee
d dd  Ze	jejdZdd Zdd ZG d	d
 d
ZG dd deZdS )    N)datetimedate	timedeltatimezone   tzinfoc                 C   sD   | j d kr| jtjd} t| t  }tdt	d|d@ d S )Nr   iIl    r   )
r   replacer   utcint_EPOCHtotal_secondsstructunpackpack)dtZsecs r   8/tmp/pip-unpacked-wheel-c81u5j2r/telethon/tl/tlobject.py_datetime_to_timestamp   s    
r   c                 C   s8   t | trt| dS t | tr,|  S t| S d S )Nascii)
isinstancebytesbase64	b64encodedecoder   	isoformatrepr)valuer   r   r   _json_default   s
    

r    c                   @   s   e Zd ZdZdZed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efddZdd Zdd Zedd ZdS )TLObjectNc                 C   s8  |dkrt | tr|  } t | trLd| ddddd |  D S t | ts`t | t	rht
| S t| drd	dd
d | D S t
| S ng }t | tr|  } t | tr~|| dd |d | rr|d |d7 }|  D ]R\}}|dkr
q|d|  || |d |t|| |d q|  |d8 }|d |d|  |d nt | tst | t	r|t
|  nt| dr|d |d7 }| D ]0}|d|  |t|| |d q|d8 }|d|  |d n|t
|  d|S dS )z
        Pretty formats the given object as a string which is returned.
        If indent is None, a single line will be returned.
        Nz{}({})_dictz, c                 s   s,   | ]$\}}|d krd |t|V  qdS )r"   z{}={}N)formatr!   pretty_format).0kvr   r   r   	<genexpr>1   s    z)TLObject.pretty_format.<locals>.<genexpr>__iter__z[{}]c                 s   s   | ]}t |V  qd S Nr!   r%   )r&   xr   r   r   r)   9   s     (
   	=z,
)z[
] )r   r!   to_dictr#   r$   getjoinitemsstrr   r   hasattrappendr%   pop)objindentresultr'   r(   r-   r   r   r   r%   &   sd    











zTLObject.pretty_formatc                 C   s   t | ts2t | tr | d} ntdt| g }t| dk rt| d d }|dkrbd| }|tt| g ||  n\t| d }|dkrd| }|tdt| d t| d? d t| d	? d g ||  |t| d
	|S )z(Write bytes by using Telegram guidelineszutf-8zbytes or str expected, not {}   r0      r                )
r   r   r:   encode	TypeErrorr$   typelenr<   r8   )datarpaddingr   r   r   serialize_bytesi   s2    



zTLObject.serialize_bytesc                 C   s   | st | tsdS t | tr&t| } nPt | trHtt| j| j| j} n.t | tr\t	| } nt | trvtt
 |  } t | t	rtd| S td| d S )Ns       z<iz Cannot interpret "{}" as a date.)r   r   r   r   r   yearmonthdayfloatr   utcnowr   r   rH   r$   )r   r   r   r   serialize_datetime   s    






zTLObject.serialize_datetimec                 C   s   t |t| o|  | kS r+   r   rI   r6   selfor   r   r   __eq__   s    zTLObject.__eq__c                 C   s    t |t|  p|  | kS r+   rU   rV   r   r   r   __ne__   s    zTLObject.__ne__c                 C   s
   t | S r+   r,   rW   r   r   r   __str__   s    zTLObject.__str__c                 C   s   t j| ddS )Nr   )r?   r,   r[   r   r   r   	stringify   s    zTLObject.stringifyc                 C   s   t d S r+   NotImplementedErrorr[   r   r   r   r6      s    zTLObject.to_dictc                 K   s>   |   }|r$tj||fd|i|S tj|fd|i|S dS )an  
        Represent the current `TLObject` as JSON.

        If ``fp`` is given, the JSON will be dumped to said
        file pointer, otherwise a JSON string will be returned.

        Note that bytes and datetimes cannot be represented
        in JSON, so if those are found, they will be base64
        encoded and ISO-formatted, respectively, by default.
        defaultN)r6   jsondumpdumps)rW   fpr`   kwargsdr   r   r   to_json   s    zTLObject.to_jsonc                 C   s,   z
|   W S  tk
r&   tdY nX d S )Nz0a TLObject was expected but found something else)_bytesAttributeErrorrH   r[   r   r   r   	__bytes__   s    
zTLObject.__bytes__c                 C   s   t d S r+   r^   r[   r   r   r   rh      s    zTLObject._bytesc                 C   s   t d S r+   r^   )clsreaderr   r   r   from_reader   s    zTLObject.from_reader)N)__name__
__module____qualname__ZCONSTRUCTOR_IDZSUBCLASS_OF_IDstaticmethodr%   rN   rT   rY   rZ   r\   r]   r6   r    rg   rj   rh   classmethodrm   r   r   r   r   r!   "   s$   B
"
r!   c                   @   s$   e Zd ZdZedd Zdd ZdS )	TLRequestzO
    Represents a content-related `TLObject` (a request that can be sent).
    c                 C   s   |   S r+   )Ztgread_object)rl   r   r   r   read_result   s    zTLRequest.read_resultc                    s   d S r+   r   )rW   clientutilsr   r   r   resolve   s    zTLRequest.resolveN)rn   ro   rp   __doc__rq   rt   rw   r   r   r   r   rs      s   
rs   )r   ra   r   r   r   r   r   timegmtimeZ_EPOCH_NAIVE	localtimeZ_EPOCH_NAIVE_LOCALr   r   r   r   r    r!   rs   r   r   r   r   <module>   s   	 4