U
    g=                     @   s   d dl Z d dlmZmZ d dlmZmZmZmZ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 G d
d dZdS )    N)JSONDecodeErrorloads)DictListOptionalTupleUnion)	DataError)deprecated_function   )JsonType)decode_dict_keys)Pathc                
   @   s  e Zd ZdZe feee ee	 ee
edf  dddZdSeeeee ee ee
edf  dddZeeeee	 ee
edf  d	d
dZe feee ee
edf  dddZe dfeee ee ee
edf  dddZeeeeee
edf  dddZe feee ee dddZe feee edddZe feee ee
ee df  dddZe feee eee  dddZeeeeddd Zed!d"d#eeeedd$d%Ze feee edd&d'Ze feee ed(d)d*ZeZd+d,eee eee	  d-d.d/Zee eee	 d0d1d2ZdTeee	ee ee ee ee d3d4d5Zee eee	f  ee d6d7d8Z!dUeee	ee ee d9d:d;Z"dVeeeee ee ee ee d<d=d>Z#dWeeee ee ee e$eef d?d@dAZ%dXeee ee
edf  ddBdCZ&e feee e
eeee  f ddDdEZ'e feeee e
eeee  f dFdGdHZ(de feee ee e
eee f dIdJdKZ)ed!dLd#dMdN Z*ed!dLd#dOdP Z+ed!dLd#dQdR Z,dS )YJSONCommandszjson commands.N)namepathargsreturnc                 G   s4   |t |g}|D ]}|| | q| jd| S )zAppend the objects ``args`` to the array under the
        ``path` in key ``name``.

        For more information see `JSON.ARRAPPEND <https://redis.io/commands/json.arrappend>`_..
        JSON.ARRAPPEND)r   strappend_encodeexecute_command)selfr   r   r   pieceso r   @/tmp/pip-unpacked-wheel-f3sx1i9r/redis/commands/json/commands.py	arrappend   s    zJSONCommands.arrappend)r   r   scalarstartstopr   c                 C   sN   |t || |g}|dk	r8|| |dk	r8|| | jd|d|giS )a:  
        Return the index of ``scalar`` in the JSON array under ``path`` at key
        ``name``.

        The search can be limited using the optional inclusive ``start``
        and exclusive ``stop`` indices.

        For more information see `JSON.ARRINDEX <https://redis.io/commands/json.arrindex>`_.
        NJSON.ARRINDEXkeys)r#   )r   r   r   r   )r   r   r   r    r!   r"   r   r   r   r   arrindex   s    

zJSONCommands.arrindex)r   r   indexr   r   c                 G   s6   |t ||g}|D ]}|| | q| jd| S )zInsert the objects ``args`` to the array at index ``index``
        under the ``path` in key ``name``.

        For more information see `JSON.ARRINSERT <https://redis.io/commands/json.arrinsert>`_.
        JSON.ARRINSERT)r'   r   )r   r   r   r&   r   r   r   r   r   r   	arrinsert6   s    zJSONCommands.arrinsert)r   r   r   c                 C   s   | j d|t||gdS )zReturn the length of the array JSON value under ``path``
        at key``name``.

        For more information see `JSON.ARRLEN <https://redis.io/commands/json.arrlen>`_.
        zJSON.ARRLENr$   r   r   r   r   r   r   r   r   arrlenC   s    zJSONCommands.arrlen)r   r   r&   r   c                 C   s   |  d|t||S )zPop the element at ``index`` in the array JSON value under
        ``path`` at key ``name``.

        For more information see `JSON.ARRPOP <https://redis.io/commands/json.arrpop>`_.
        zJSON.ARRPOPr*   )r   r   r   r&   r   r   r   arrpopM   s    zJSONCommands.arrpop)r   r   r!   r"   r   c                 C   s   |  d|t|||S )zTrim the array JSON value under ``path`` at key ``name`` to the
        inclusive range given by ``start`` and ``stop``.

        For more information see `JSON.ARRTRIM <https://redis.io/commands/json.arrtrim>`_.
        zJSON.ARRTRIMr*   )r   r   r   r!   r"   r   r   r   arrtrimZ   s    zJSONCommands.arrtrimc                 C   s   | j d|t||gdS )zGet the type of the JSON value under ``path`` from key ``name``.

        For more information see `JSON.TYPE <https://redis.io/commands/json.type>`_.
        z	JSON.TYPEr)   r*   r+   r   r   r   typed   s    zJSONCommands.typec                 C   s   | j d|t||gdS )zReturn the JSON value under ``path`` at key ``name``.

        For more information see `JSON.RESP <https://redis.io/commands/json.resp>`_.
        z	JSON.RESPr)   r*   r+   r   r   r   respk   s    zJSONCommands.respc                 C   s   | j d|t||gdS )zReturn the key names in the dictionary JSON value under ``path`` at
        key ``name``.

        For more information see `JSON.OBJKEYS <https://redis.io/commands/json.objkeys>`_.
        zJSON.OBJKEYSr)   r*   r+   r   r   r   objkeysr   s    zJSONCommands.objkeysc                 C   s   | j d|t||gdS )zReturn the length of the dictionary JSON value under ``path`` at key
        ``name``.

        For more information see `JSON.OBJLEN <https://redis.io/commands/json.objlen>`_.
        zJSON.OBJLENr)   r*   r+   r   r   r   objlen|   s    zJSONCommands.objlen)r   r   numberr   c                 C   s   |  d|t|| |S )zIncrement the numeric (integer or floating point) JSON value under
        ``path`` at key ``name`` by the provided ``number``.

        For more information see `JSON.NUMINCRBY <https://redis.io/commands/json.numincrby>`_.
        zJSON.NUMINCRBYr   r   r   r   r   r   r4   r   r   r   	numincrby   s       zJSONCommands.numincrbyz4.0.0z deprecated since redisjson 1.0.0)versionreasonc                 C   s   |  d|t|| |S )zMultiply the numeric (integer or floating point) JSON value under
        ``path`` at key ``name`` with the provided ``number``.

        For more information see `JSON.NUMMULTBY <https://redis.io/commands/json.nummultby>`_.
        zJSON.NUMMULTBYr5   r6   r   r   r   	nummultby   s       zJSONCommands.nummultbyc                 C   s   |  d|t|S )a  Empty arrays and objects (to have zero slots/keys without deleting the
        array/object).

        Return the count of cleared paths (ignoring non-array and non-objects
        paths).

        For more information see `JSON.CLEAR <https://redis.io/commands/json.clear>`_.
        z
JSON.CLEARr*   r+   r   r   r   clear   s    	zJSONCommands.clear)keyr   r   c                 C   s   |  d|t|S )zDelete the JSON value stored at key ``key`` under ``path``.

        For more information see `JSON.DEL <https://redis.io/commands/json.del>`_.
        zJSON.DELr*   )r   r<   r   r   r   r   delete   s    zJSONCommands.deleteF)	no_escape)r   r>   r   c                G   s|   |g}|r| d t|dkr0| t  n|D ]}| t| q4z| jd|d|giW S  tk
rv   Y dS X dS )aD  
        Get the object stored as a JSON value at key ``name``.

        ``args`` is zero or more paths, and defaults to root path
        ```no_escape`` is a boolean flag to add no_escape option to get
        non-ascii characters

        For more information see `JSON.GET <https://redis.io/commands/json.get>`_.
        Znoescaper   JSON.GETr$   N)r?   )r   lenr   	root_pathr   r   	TypeError)r   r   r>   r   r   pr   r   r   get   s    
zJSONCommands.get)r$   r   r   c                 C   s.   g }||7 }| t| | jd|d|iS )z
        Get the objects stored as a JSON values under ``path``. ``keys``
        is a list of one or more keys.

        For more information see `JSON.MGET <https://redis.io/commands/json.mget>`_.
        	JSON.MGETr$   )rE   r   r   r   )r   r$   r   r   r   r   r   mget   s    zJSONCommands.mget)r   r   objnxxxdecode_keysr   c                 C   s^   |rt |}|t|| |g}|r2|r2tdn|rB|d n|rP|d | jd| S )a  
        Set the JSON value at key ``name`` under the ``path`` to ``obj``.

        ``nx`` if set to True, set ``value`` only if it does not exist.
        ``xx`` if set to True, set ``value`` only if it exists.
        ``decode_keys`` If set to True, the keys of ``obj`` will be decoded
        with utf-8.

        For the purpose of using this within a pipeline, this command is also
        aliased to JSON.SET.

        For more information see `JSON.SET <https://redis.io/commands/json.set>`_.
        zNnx and xx are mutually exclusive: use one, the other or neither - but not bothZNXZXXJSON.SET)rL   )r   r   r   	Exceptionr   r   )r   r   r   rH   rI   rJ   rK   r   r   r   r   set   s    
zJSONCommands.set)tripletsr   c              	   C   sB   g }|D ]*}| |d t|d | |d g q| jd| S )a  
        Set the JSON value at key ``name`` under the ``path`` to ``obj``
        for one or more keys.

        ``triplets`` is a list of one or more triplets of key, path, value.

        For the purpose of using this within a pipeline, this command is also
        aliased to JSON.MSET.

        For more information see `JSON.MSET <https://redis.io/commands/json.mset>`_.
        r   r      	JSON.MSET)rQ   )extendr   r   r   )r   rO   r   Ztripletr   r   r   mset  s    (zJSONCommands.mset)r   r   rH   rK   r   c                 C   s.   |rt |}|t|| |g}| jd| S )aa  
        Merges a given JSON value into matching paths. Consequently, JSON values
        at matching paths are updated, deleted, or expanded with new children

        ``decode_keys`` If set to True, the keys of ``obj`` will be decoded
        with utf-8.

        For more information see `JSON.MERGE <https://redis.io/commands/json.merge>`_.
        
JSON.MERGE)rT   )r   r   r   r   )r   r   r   rH   rK   r   r   r   r   merge  s    zJSONCommands.merge)r   r   	file_namerI   rJ   rK   r   c           	   	   C   s6   t |}t| }W 5 Q R X | j||||||dS )ah  
        Set the JSON value at key ``name`` under the ``path`` to the content
        of the json file ``file_name``.

        ``nx`` if set to True, set ``value`` only if it does not exist.
        ``xx`` if set to True, set ``value`` only if it exists.
        ``decode_keys`` If set to True, the keys of ``obj`` will be decoded
        with utf-8.

        rI   rJ   rK   )openr   readrN   )	r   r   r   rV   rI   rJ   rK   fpZfile_contentr   r   r   set_file)  s    
zJSONCommands.set_file)	json_pathroot_folderrI   rJ   rK   r   c              
   C   s   i }t |D ]p\}}}	|	D ]`}
t j||
}z0|dd }| j||||||d d||< W q tk
rz   d||< Y qX qq|S )au  
        Iterate over ``root_folder`` and set each JSON file to a value
        under ``json_path`` with the file name as the key.

        ``nx`` if set to True, set ``value`` only if it does not exist.
        ``xx`` if set to True, set ``value`` only if it exists.
        ``decode_keys`` If set to True, the keys of ``obj`` will be decoded
        with utf-8.

        .r   rW   TF)oswalkr   joinrsplitr[   r   )r   r\   r]   rI   rJ   rK   Zset_files_resultrootdirsfilesfile	file_pathrV   r   r   r   set_pathB  s$    zJSONCommands.set_pathc                 C   s2   |g}|dk	r| t| | jd|d|giS )zReturn the length of the string JSON value under ``path`` at key
        ``name``.

        For more information see `JSON.STRLEN <https://redis.io/commands/json.strlen>`_.
        NJSON.STRLENr$   )ri   rF   )r   r   r   r   r   r   r   strlenh  s    zJSONCommands.strlenc                 C   s   |  d|t|S )zToggle boolean value under ``path`` at key ``name``.
        returning the new value.

        For more information see `JSON.TOGGLE <https://redis.io/commands/json.toggle>`_.
        zJSON.TOGGLEr*   r+   r   r   r   toggles  s    zJSONCommands.toggle)r   valuer   r   c                 C   s"   |t || |g}| jd| S )aC  Append to the string JSON value. If two options are specified after
        the key name, the path is determined to be the first. If a single
        option is passed, then the root_path (i.e Path.root_path()) is used.

        For more information see `JSON.STRAPPEND <https://redis.io/commands/json.strappend>`_.
        JSON.STRAPPEND)rm   )r   r   r   )r   r   rl   r   r   r   r   r   	strappend}  s    	zJSONCommands.strappend)
subcommandr<   r   r   c                 C   sb   ddg}||krt dt||g}|dkrT|dkr<t d|| |t| | jd| S )zReturn the memory usage in bytes of a value under ``path`` from
        key ``name``.

        For more information see `JSON.DEBUG <https://redis.io/commands/json.debug>`_.
        ZMEMORYZHELPzThe only valid subcommands are NzNo key specified
JSON.DEBUG)rp   )r	   r   r   r   )r   ro   r<   r   Zvalid_subcommandsr   r   r   r   debug  s    
zJSONCommands.debugz/redisjson-py supported this, call get directly.c                 O   s   | j ||S N)rD   r   r   kwargsr   r   r   jsonget  s    zJSONCommands.jsongetc                 O   s   | j ||S rr   )rG   rs   r   r   r   jsonmget  s    zJSONCommands.jsonmgetc                 O   s   | j ||S rr   )rN   rs   r   r   r   jsonset  s    zJSONCommands.jsonset)NN)FFF)F)FFF)FFF)N)-__name__
__module____qualname____doc__r   rA   r   r   r   r   r   intr   r%   r(   r,   r.   r/   r0   r1   r2   r3   r7   r
   r:   r;   r=   ZforgetboolrD   rG   rN   r   rS   rU   r[   r   rh   rj   rk   rn   rq   ru   rv   rw   r   r   r   r   r      s0             
"  




 
   '"       
&$    
 
 r   )r_   jsonr   r   typingr   r   r   r   r   Zredis.exceptionsr	   Zredis.utilsr
   _utilr   Zdecodersr   r   r   r   r   r   r   r   <module>   s   