o
    $^i                     @   s   d Z 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 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 e
e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d$ddZd%dd Zd#d!d"ZdS )&z!Define the line_protocol handler.    )absolute_import)division)print_function)unicode_literals)datetime)Integral)UTC)parse)binary_type	text_typeinteger_typesPY2c                 C   s6   | t  }|jd d }|jd }|jd }|| | S )NiQ  ʚ;  )EPOCHZdaysZsecondsZmicroseconds)	timestampZdeltaZnanos_in_daysZnanos_in_secondsZnanos_in_micros r   8/usr/lib/python3/dist-packages/influxdb/line_protocol.py	_to_nanos   s
   

r   Nc                 C   s   t | tr| S t t| trt| } t | trY| jst| } t	| }|d u s+|dkr-|S |dkr5|d S |dkr=|d S |dkrE|d S |dkrO|d d	 S |d
krY|d d S t
| )Nnur   msi@B sr   m<   hi  )
isinstancer   _get_unicoder   r	   r   Ztzinfor   localizer   
ValueError)r   	precisionnsr   r   r   _convert_timestamp   s*   


r"   c                 C   s8   t | dd} | ddddddd	d
ddS )NT)force\\\ z\ ,z\,=z\=
\n)r   replace)tagr   r   r   _escape_tag<   s"   r-   c                 C   s   t | }|dr|d7 }|S )Nr$   r&   )r-   endswith)valueretr   r   r   _escape_tag_valueK   s   
r1   c                 C   s"   d | ddddddS )zIndent the quotes.z"{}"r$   r%   "z\"r)   r*   formatr+   r/   r   r   r   quote_identR   s
   r6   c                 C   s   d | ddddS )zQuote provided literal.z'{}'r$   r%   'z\'r3   r5   r   r   r   quote_literalZ   s   r8   c              	   C   s(   zt |  W dS  ttfy   Y dS w )NFT)float	TypeErrorr   r5   r   r   r   	_is_floata   s   
r;   c                 C   st   | d u rdS t | } t| trt| S t| tr#t| ts#t| d S t| tr,t| S t| r6tt	| S t| S )N i)
r   r   r   r6   r   boolstrr;   reprr9   r5   r   r   r   _escape_valuej   s   

rA   Fc                 C   s<   t | tr
| dS | du rdS |rtrt| S t| S | S )z<Try to return a text aka unicode object from the given data.zutf-8Nr<   )r   r
   decoder   Zunicoder?   )datar#   r   r   r   r   ~   s   

r   c                 C   s  |pi }|pi }t t| }g }t| D ]}t |}t || }	|dkr4|	dkr4|dj||	d q|r@|dd| 7 }g }
t| D ]}t |}t|| }	|dkrf|	dkrf|
dj||	d qH|
rr|dd|
 7 }|durttt	t
||}|d| 7 }|S )z7Extract the actual point from a given measurement line.r<   z{key}={value})keyr/   r'   r&   N)r-   r   sortedkeysappendr4   joinrA   r?   intr"   )measurementtagsfieldstimer    lineZtag_listZtag_keyrD   r/   Z
field_listZ	field_keyr   r   r   r   	make_line   s@   rO   c              	   C   s   g }|  d}| d D ]5}|rt|}|| dpi  n| dp$i }t| d|  d|| d|| dd}|| qd|d S )zExtract points from given dict.

    Extracts the points from the given dict and returns a Unicode string
    matching the line protocol introduced in InfluxDB 0.9.0.
    rK   ZpointsrJ   rL   rM   )rK   rL   r    rM   r)   )getdictupdaterO   rG   rH   )rC   r    linesZstatic_tagsZpointrK   rN   r   r   r   
make_lines   s    
rT   )N)F)NNNN) __doc__Z
__future__r   r   r   r   r   Znumbersr   Zpytzr   Zdateutil.parserr	   Zsixr
   r   r   r   r   Zutcfromtimestampr   r   r"   r-   r1   r6   r8   r;   rA   r   rO   rT   r   r   r   r   <module>   s,   
!	

,