U
    dRf                     @   s   d Z ddlm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
dZdd Zdd Zdd Zdd Zdd ZdS )zG
    thriftpy2.parser
    ~~~~~~~~~~~~~~~

    Thrift parser using ply
    )absolute_importN   )parseparse_fpthreadlocal_cast)ThriftParserError   )TPayloadMetautf-8c           	      C   s   t |}t| ||||d}tjr*t|| |r~|tj|< |jd dd }|r~| }|tjkrJ|tj|j	< |
|jd  qJ|S )a2  Load thrift file as a module.

    The module loaded and objects inside may only be pickled if module_name
    was provided.

    Note: `include_dir` will be depreacated in the future, use `include_dirs`
    instead. If `include_dir` was provided (not None), it will be appended to
    `include_dirs`.
    )include_dirsinclude_direncodingZincludesN)boolr   r   incomplete_typefill_incomplete_ttypesysmodulesZ__thrift_meta__pop__name__extend)	pathmodule_namer   r   r   Zreal_modulethriftZsub_modulesmodule r   =/tmp/pip-unpacked-wheel-jqs7l_7o/thriftpy2/parser/__init__.pyload   s      


r   c              
      s  t |tr|d dkrDt tj|d  d |d }t||d S |d tjkrr|d t ftj|d   fS |d tjkrt ftj|d   }|d |d |d |d fS t |d tr|d t |d fS nt |tjr"|j	
 D ](\}}|dr
qt||t|| qnt |trp|j
 D ]2\}}|d tjkrt t ftj|d   }t |tr|d |d |d |d f|j|< n6t t ftj|d   ft|dd  |j|< n|d tjkr"|d |d t t ftj|d   |d f|j|< nHt |d tr8 fdd	|d |d t|d |d f|j|< q8nXt|d
r|j	
 D ]@\}}t|dsq|j
 D ]\}}t ||j|< qq|S )z>Second pass of parser to handle out-of-order definitions.
    r   ZUNKNOWN_CONSTr      r	   __Nc                    sB   t | tr tfdd| D S | tjkr>t ftj|   S | S )Nc                 3   s   | ]} |V  qd S )Nr   ).0x)walkr   r   	<genexpr>   s     z6fill_incomplete_ttype.<locals>.walk.<locals>.<genexpr>)
isinstancetupler   r   get_definition)parttmoduler"   r   r   r"      s
    

z#fill_incomplete_ttype.<locals>.walkZthrift_servicesthrift_spec)r$   r%   r&   r   r   r   r   types
ModuleType__dict__items
startswithsetattrr
   r*   hasattr)r)   Z
definitionttypeZ	real_typenameattrindexvaluer   r(   r   r   5   s    
   	 	 r   c                 C   s   | }| dD ]p}t| |d}|dkr6td||f t|trZ|dk rZtdtj|  t|drvt|d|f  S |  S qdS )z?Get definition from thrift module and incomplete type map.
    .NzNo type found: %r, at line %dr   Z_ttype)splitgetattrr   r$   intr   r   r1   )r   r3   linenoZref_typenr   r   r   r&      s    
r&   c                 C   s   t | |}|tj|< |S )z.Load thrift file like object as a module.
    )r   r   r   )sourcer   r   r   r   r   load_fp   s    

r>   c                 C   s:   d| kr.|  dd\}}tt|d d |g|S t| S d S )Nr7   r   )rsplitr9   
__import__)import_namer   objr   r   r   _import_module   s    rC   c                 C   s   |  dstd| tjkr&tj|  S d| krl| dd\}}t|}tjtj	|j
}tj||}n| }d|dd }t|| d}|tj| < tj|  S )	zLoad thrift_file by fullname, fullname should have '_thrift' as
    suffix.
    The loader will replace the '_thrift' with '.thrift' and use it as
    filename to locate the real thrift file.
    Z_thriftz4thriftpy2 can only load module with '_thrift' suffixr7   r   z	{}.thriftNi)r   )endswithImportErrorr   r   r?   rC   osr   dirnameabspath__file__joinformatr   )fullnamer   Zthrift_module_namer   Zpath_prefixr   Zthrift_filer   r   r   load_module   s     



rM   )NNNr   )__doc__
__future__r   rF   r   r+   parserr   r   r   r   excr   r   r
   r   r   r&   r>   rC   rM   r   r   r   r   <module>   s"       
 a