U
    d7                     @   s~   d dl Z d dlmZm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 ddlmZ dd	lmZ G d
d dZdS )    N)JSONDecodeErrorloads)DictListOptional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dNe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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	 d.d/d0Zee eee	 d1d2d3ZdOeee	ee ee ee ee d4d5d6ZdPeeeee ee ee ee d7d8d9Z dQeeee ee ee ee!eef  d:d;d<Z"dReee ee
edf  dd=d>Z#e feee e
eeee  f dd?d@Z$e feeee e
eeee  f dAdBdCZ%de feee ee e
eee f dDdEdFZ&ed"dGd$dHdI Z'ed"dGd$dJdK Z(ed"dGd$dLdM Z)dS )S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-f5h5_hbx/redis/commands/json/commands.py	arrappend   s    zJSONCommands.arrappendr   )r   r   scalarstartstopr   c                 C   s   |  d|t|| |||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>`_.
        zJSON.ARRINDEX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   	arrinsert2   s    zJSONCommands.arrinsert)r   r   r   c                 C   s   |  d|t|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   arrlen?   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   arrpopI   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   arrtrimW   s    zJSONCommands.arrtrimc                 C   s   |  d|t|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   typea   s    zJSONCommands.typec                 C   s   |  d|t|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   resph   s    zJSONCommands.respc                 C   s   |  d|t|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   objkeyso   s    zJSONCommands.objkeysc                 C   s   |  d|t|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   objleny   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.NUMINCRBYr#   r   r   r   r1   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.NUMMULTBYr#   r2   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   r8   r   r   r   r   delete   s    zJSONCommands.deleteF)	no_escape)r   r:   r   c                G   st   |g}|r| d t|dkr0| t  n|D ]}| t| q4z| jd| W S  tk
rn   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.GETN)r;   )r   lenr   	root_pathr   r   	TypeError)r   r   r:   r   r   pr   r   r   get   s    
zJSONCommands.get)keysr   r   c                 C   s(   g }||7 }| t| | jd| 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.MGET)rB   r   r   r   )r   rA   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)rI   )r   r   r   	Exceptionr   r   )r   r   r   rE   rF   rG   rH   r   r   r   r   set   s    
zJSONCommands.set)r   r   	file_namerF   rG   rH   r   c           	   	   C   s8   t |d}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.

        rrF   rG   rH   )openr   readrK   )	r   r   r   rL   rF   rG   rH   fpZfile_contentr   r   r   set_file   s    zJSONCommands.set_file)	json_pathroot_folderrF   rG   rH   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   rN   TF)oswalkr   joinrsplitrR   r   )r   rS   rT   rF   rG   rH   Zset_files_resultrootdirsfilesfile	file_pathrL   r   r   r   set_path  s$    zJSONCommands.set_pathc                 C   s*   |g}|dk	r| t| | jd| 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.STRLEN)r`   rC   )r   r   r   r   r   r   r   strlen;  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   toggleF  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)rd   )r   r   r   )r   r   rc   r   r   r   r   r   	strappendP  s    	zJSONCommands.strappend)
subcommandr8   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)rg   )r   r   r   r   )r   rf   r8   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)r@   r   r   kwargsr   r   r   jsongetr  s    zJSONCommands.jsongetc                 O   s   | j ||S ri   )rD   rj   r   r   r   jsonmgetx  s    zJSONCommands.jsonmgetc                 O   s   | j ||S ri   )rK   rj   r   r   r   jsonset~  s    zJSONCommands.jsonset)r   r   )FFF)FFF)FFF)N)*__name__
__module____qualname____doc__r   r=   r   r   r   r   r   intr   r$   r'   r*   r+   r,   r-   r.   r/   r0   r3   r	   r6   r7   r9   Zforgetboolr@   rD   rK   rR   r   r_   ra   rb   re   rh   rl   rm   rn   r   r   r   r   r      s             
" 


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