o
    `f=                     @   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dedee dee	 dee
edf  fddZ		dXdeded	ed
ee dee dee
edf  fddZdedededee	 dee
edf  f
ddZe fdedee dee
edf  fddZe dfdedee dee dee
edf  fddZdeded
ededee
edf  f
ddZe fdedee dee fddZe fdedee defddZe fdedee dee
ee df  fddZe fdedee deee  fddZdeded edefd!d"Zed#d$d%deded edefd&d'Ze fdedee defd(d)Ze fd*edee defd+d,ZeZd-d.ded/ee deee	  fd0d1Zd2ee dedee	 fd3d4Z	-	-	-dYdeded5e	d6ee d7ee d8ee dee fd9d:Zd;ee eee	f  dee fd<d=Z!	-dZdeded5e	d8ee dee f
d>d?Z"	-	-	-dYdeded@ed6ee d7ee d8ee dee fdAdBZ#	-	-	-dYdCedDed6ee d7ee d8ee de$eef fdEdFZ%d[dedee dee
edf  fdGdHZ&e fdedee de
eeee  f fdIdJZ'e fdedKedee de
eeee  f fdLdMZ(de fdNed*ee dee de
eee f fdOdPZ)ed#dQd%dRdS Z*ed#dQd%dTdU Z+ed#dQd%dVdW Z,dS )\JSONCommandszjson commands.namepathargsreturnNc                 G   s8   |t |g}|D ]
}|| | q| jdg|R  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>`_..
        zJSON.ARRAPPENDstrappend_encodeexecute_command)selfr   r   r   pieceso r   U/home/ubuntu/webapp/venv/lib/python3.10/site-packages/redis/commands/json/commands.py	arrappend   s   zJSONCommands.arrappendscalarstartstopc                 C   sJ   |t || |g}|dur|| |dur|| | jdg|R  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>`_.
        NzJSON.ARRINDEX)r   r   r   r   )r   r   r   r   r    r!   r   r   r   r   arrindex   s   

zJSONCommands.arrindexindexc                 G   s:   |t ||g}|D ]
}|| | q	| jdg|R  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>`_.
        zJSON.ARRINSERTr   )r   r   r   r#   r   r   r   r   r   r   	arrinsert6   s   zJSONCommands.arrinsertc                 C      |  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   arrlenC      zJSONCommands.arrlenc                 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.arrpopc                 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   r%   )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   typed      zJSONCommands.typec                 C   r%   )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   respk   r.   zJSONCommands.respc                 C   r%   )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   objkeysr   r)   zJSONCommands.objkeysc                 C   r%   )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   objlen|   r)   zJSONCommands.objlennumberc                 C      |  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   r2   r   r   r   	numincrby   s   zJSONCommands.numincrbyz4.0.0z deprecated since redisjson 1.0.0)versionreasonc                 C   r3   )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.NUMMULTBYr4   r5   r   r   r   	nummultby   s   zJSONCommands.nummultbyc                 C   r%   )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keyc                 C   r%   )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   r.   zJSONCommands.deleteF)	no_escaper=   c                G   sr   |g}|r
| d t|dkr| t  n|D ]	}| t| qz
| jdg|R  W S  ty8   Y dS w )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>`_.
        noescaper   zJSON.GETN)r   lenr   	root_pathr   r   	TypeError)r   r   r=   r   r   pr   r   r   get   s   
zJSONCommands.getkeysc                 C   s,   g }||7 }| t| | jdg|R  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>`_.
        z	JSON.MGETr   r   r   )r   rD   r   r   r   r   r   mget   s   zJSONCommands.mgetobjnxxxdecode_keysc                 C   s`   |rt |}|t|| |g}|r|rtd|r |d n|r'|d | jdg|R  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 bothNXXXzJSON.SET)r   r   r   	Exceptionr   r   )r   r   r   rG   rH   rI   rJ   r   r   r   r   set   s   
zJSONCommands.settripletsc              	   C   sF   g }|D ]}| |d t|d | |d g q| jdg|R  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      z	JSON.MSET)extendr   r   r   )r   rO   r   tripletr   r   r   mset  s   (zJSONCommands.msetc                 C   s2   |rt |}|t|| |g}| jdg|R  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>`_.
        z
JSON.MERGE)r   r   r   r   )r   r   r   rG   rJ   r   r   r   r   merge  s   zJSONCommands.merge	file_namec           	      C   sL   t |d}t| }W d   n1 sw   Y  | 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.

        rNrH   rI   rJ   )openr   readrN   )	r   r   r   rU   rH   rI   rJ   fpfile_contentr   r   r   set_file)  s   zJSONCommands.set_file	json_pathroot_folderc              
   C   s   i }t |D ]6\}}}	|	D ].}
t j||
}z|dd }| j||||||d d||< W q ty<   d||< Y qw 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^   rH   rI   rJ   set_files_resultrootdirsfilesfile	file_pathrU   r   r   r   set_pathB  s(   zJSONCommands.set_pathc                 C   s.   |g}|dur| t| | jdg|R  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>`_.
        NzJSON.STRLENrE   )r   r   r   r   r   r   r   strlenh  s   zJSONCommands.strlenc                 C   r%   )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  r)   zJSONCommands.togglevaluec                 C   s&   |t || |g}| jdg|R  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>`_.
        zJSON.STRAPPEND)r   r   r   )r   r   rm   r   r   r   r   r   	strappend}  s   	zJSONCommands.strappend
subcommandc                 C   sf   ddg}||vrt dt||g}|dkr*|du rt d|| |t| | jdg|R  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>`_.
        MEMORYHELPzThe only valid subcommands are NzNo key specifiedz
JSON.DEBUG)r	   r   r   r   )r   ro   r;   r   valid_subcommandsr   r   r   r   debug  s   
zJSONCommands.debugz/redisjson-py supported this, call get directly.c                 O      | j |i |S N)rC   r   r   kwargsr   r   r   jsonget     zJSONCommands.jsongetc                 O   rt   ru   )rF   rv   r   r   r   jsonmget  ry   zJSONCommands.jsonmgetc                 O   rt   ru   )rN   rv   r   r   r   jsonset  ry   zJSONCommands.jsonset)NN)FFF)Fru   )-__name__
__module____qualname____doc__r   r@   r   r   r   r   r   intr   r"   r$   r(   r+   r,   r-   r/   r0   r1   r6   r
   r9   r:   r<   forgetboolrC   rF   rN   r   rS   rT   r\   r   rj   rk   rl   rn   rs   rx   rz   r{   r   r   r   r   r      s   





&
"





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