U
    Thcy                     @   s  d dl Z d dlZG dd dZedkr~dddiiZdZeeed	Zeej ddd
giiZdZeeed	Zeej ddd
dgiiZdZeeed	Zeej dddgiddidZdZeeed	Zeej dddddgiiZdZeeed	Zeej dddddgiiZdZeeed	Zeej ddddddgiiiZdZeeed	Zeej dddiiZdZeeed	Zeej dS )    Nc                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )
JsonByPathc                 K   s6   |d }|d | _ t | _t | _| | d| _dS )z[ path = re.sub(
            r'[\s	
](?=(\`[^\`]*\`|[^\`])*$)', "", data['path']
        ) pathjson N)jsonInlistvalue
error_pathprocesscurrent_path)selfdatar    r   =/home/www/html/webservice/gn_integracao/generic/JsonByPath.py__init__   s    

zJsonByPath.__init__c                 C   s   | j S N)r   )r   r   r   r   __repr__   s    zJsonByPath.__repr__c                 C   s@   || _ | | j d| _| | j}t|r6|d | _nd| _d S )Nz||r   r   )r   explodeallonlyValidValueslenr   )r   r   _filterr   r   r   r
      s    zJsonByPath.processc                 C   s   | |}tt| j|S r   )splitr   mapexecute)r   Zstr_to_split	separatorpathsr   r   r   r      s    
zJsonByPath.explodec                 C   s   dd |D S )z? 
            Metodo para retornar valores verdadeiros
        c                 S   s   g | ]}|s|d kr|qS )r   r   ).0r   r   r   r   
<listcomp>)   s       z.JsonByPath.onlyValidValues.<locals>.<listcomp>r   )r   valuesr   r   r   r   #   s    zJsonByPath.onlyValidValuesc              
   C   s   |   | _}d}z>d|krD| |d}| |}dtt|}n
| |}W n. tk
r~ } zt	d|  W 5 d }~X Y nX |S )Nr   z&&zerror: )
stripr   r   r   joinr   strgoThroughPath
ValueErrorprint)r   r   r   plusr   errorr   r   r   r   +   s    
zJsonByPath.executec                 C   sL   t d|}d|kr$t|r$|d S |d}| | j|d }| ||S )N	\`(.*?)\`/r   )refindallr   r   tryPathr   getValue)r   r   Z	has_quotepath_splitedZcurrent_valuer   r   r   r#   ;   s    
zJsonByPath.goThroughPathc                 C   s(   zt | W n tk
r"   Y dS X dS )NFT)floatr$   r   r   r   r   r   isnumberL   s
    zJsonByPath.isnumberc           	         s&  |}t dd  D ]
\ }zt|}W n   Y nX |dkrt|tsd|d ksnd|krd|kr tkrtd|}|r|d nd}d }d }td	|}|r|d d
}|d |d  }} fdd|D }|	t
t|||   S ||}q|S )N   r   *r   []r(    z	\[(.*?)\]:c                    s$   g | ]}  d  d |qS )r2   N)r-   )r   valindexr.   r   r   r   r   k   s     z'JsonByPath.getValue.<locals>.<listcomp>)	enumerateint
isinstancer   r*   r+   r   tryIntr   r!   r   r"   r,   )	r   r.   Zinitial_valuer   keyZ_charToJoinkey1key2ZkeySr   r9   r   r-   S   s*    DzJsonByPath.getValuec                 C   s   z
t |W S    Y d S X d S r   )r<   r0   r   r   r   r>   s   s    
zJsonByPath.tryIntc              	   C   s8   z
|| W S    | j d| j|p&d  Y dS X d S )NzPATH:{0} - KEY: {1}r   )r	   appendformatr   )r   arrr?   r   r   r   r,   y   s
    
zJsonByPath.tryPathN)__name__
__module____qualname__r   r   r
   r   r   r   r#   r1   r-   r>   r,   r   r   r   r   r      s   	 r   __main__IZwantzthis value in a dictzI/want)r   r   zthis value in a listzI/want/0zand this value in a listzI/want/0 &&``&& I/want/1z
this valuezand this valuethis)rI   orzI/want/* || or/thiszJust last valuezI/want/[-1: ]zMaybe this valuezI/want/[0:2]`|`ZtheseZvalue1Zvalue2Zvalue3z(`My values: ` && I/want/these/[0:2]` | `r   Zexistz$this/path/doesnotexist  || this/path)r*   r   r   rE   r   Z	extractedr%   r   r   r   r   r   <module>   s    
 
 
 
 
 
 
 
 
