B
    ÔùÓ`Œ  ã               @   s„   d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	m
Z
 dee
edœd	d
„Zeeejeje eje f dœdd„ZdS )z¼anyconfig.query module to support query data with JMESPath expressions.

Changelog:

.. versionadded:: 0.8.3

   - Added to query config data with JMESPath expression, http://jmespath.org
é    Né   )Ú	InDataExTÚInDataT)Úis_dict_likeé   )Ú	MaybeJexp)ÚdataÚjexpÚreturnc             K   s^   |dks|s| S t | ƒs0t dt| ƒ› �¡ | S tt t| ¡t t|¡f|Ž\}}|rZ|‚|S )z<
    Try to query data with JMESPath expression `jexp`.
    NzBCould not query because given data is not a mapping object (type? )	r   ÚwarningsÚwarnÚtypeÚqueryÚtypingÚcastr   Ústr)r   r	   ÚoptionsÚodataÚexc© r   ú9/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/query/query.pyÚ	try_query   s     r   c          
   K   sT   d}yt  |¡}| | ¡|fS  tk
rB } z| |fS d}~X Y n   d|fS dS )an  
    Filter data with given JMESPath expression.

    See also: https://github.com/jmespath/jmespath.py and http://jmespath.org.

    :param data: Target object (a dict or a dict-like object) to query
    :param jexp: a string represents JMESPath expression
    :param options: Keyword options

    :return: A tuple of query result and maybe exception if failed
    N)ÚjmespathÚcompileÚsearchÚ
ValueError)r   r	   Ú_optionsr   Zpexpr   r   r   r   0   s    
r   )N)Ú__doc__r   r   r   Úcommonr   r   Úutilsr   Z	datatypesr   r   r   ZTupleÚOptionalÚ	Exceptionr   r   r   r   r   Ú<module>   s   