B
     9-e                 @   s   d Z 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	m
Z
 ddl	mZ ddlmZ eeZeddd	Zedd
dZedddZeedddZeedddZdS )z
Copyright (C) 2019 Interactive Brokers LLC. All rights reserved. This code is subject to the terms
 and conditions of the IB API Non-Commercial License or the IB API Commercial License, as applicable.
    N)UNSET_INTEGERUNSET_DOUBLEDOUBLE_INFINITYINFINITY_STR)ClientException)isAsciiPrintable)INVALID_SYMBOL)returnc             C   s$   t dt|  t| t| }|S )z adds the length prefix z!I%ds)structpacklenstrencode)textmsg r   G:\My Drive\STUDY\EPAT\09 TBP - Trading & Back-testing Platforms\TBP04 - Backtesting & Live Trading\IB IBridgePy API\02 Python 3.7 IBridgePy_Win_Anaconda37_64\ibapi\comm.pymake_msg   s     r   c             C   s   | dkrt d| dk	r\t| tkr\| r\t| s\tt t | jt	j
jddt	j
jt| tkrpt| } t| d }|S )z! adds the NULL string terminator NzCannot send None to TWSignore)errors )
ValueErrortyper   r   r   r   coder   r   sysstdoutencodingdecodeboolint)valfieldr   r   r   
make_field   s     ,r"   c             C   s8   | d krt dt| ks t| kr$d} t| kr0t} t| S )NzCannot send None to TWS )r   r   r   r   r   r"   )r    r   r   r   make_field_handle_empty0   s    r$   )bufr	   c             C   s   t | dk rdd| fS td| dd d }td| t | d |krtd| | dd|  d }||| d| d fS |d| fS dS )z> first the size prefix and then the corresponding msg payload    r   r#   z!Izread_msg: size: %dz!%dsN)r   r
   unpackloggerdebug)r%   sizer   r   r   r   read_msg=   s    
 r+   c             C   s,   t | tr|  } | d}t|dd S )N    r   )
isinstancer   r   splittuple)r%   fieldsr   r   r   read_fieldsK   s    

r2   )__doc__r
   loggingr   ibapi.commonr   r   r   r   ibapi.utilsr   r   ibapi.errorsr   	getLogger__name__r(   bytesr   r   r"   r$   r0   r+   r2   r   r   r   r   <module>   s   
