o
    >hk=                     @   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
  fddZ		dXdeded	e
d
ee
 dee
 deee
  fddZdedede
dee	 deee
  f
ddZe fdedee deee
  fddZe dfdedee dee
 deee  fddZdeded
e
de
deee
  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   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
  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returnc                 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   |/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/redis/commands/json/commands.py	arrappend   s   zJSONCommands.arrappendNscalarstartstopc                 C   sR   |t || |g}|dur|| |dur|| | jdg|R 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>`_.
        NzJSON.ARRINDEXkeys)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      | 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      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   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   r   respk   r0   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   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   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   r4   r   r   r   	numincrby   s   zJSONCommands.numincrbyz4.0.0z deprecated since redisjson 1.0.0)versionreasonc                 C   r5   )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.NUMMULTBYr6   r7   r   r   r   	nummultby   s   zJSONCommands.nummultbyc                 C      |  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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   s   zJSONCommands.deleteF)	no_escaper@   c                G   sz   |g}|r
| d t|dkr| t  n|D ]	}| t| qz| jdg|R d|giW S  ty<   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.GETr"   N)r   lenr   	root_pathr   r   	TypeError)r   r   r@   r   r   pr   r   r   get   s   
zJSONCommands.getr"   c                 C   s2   g }||7 }| t| | jdg|R 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>`_.
        z	JSON.MGETr"   r   r   r   )r   r"   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   rI   rJ   rK   rL   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   rQ   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   rI   rL   r   r   r   r   merge  s   zJSONCommands.merge	file_namec           	      C   sJ   t |}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.

        NrJ   rK   rL   )openr   readrP   )	r   r   r   rW   rJ   rK   rL   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   rX   TF)oswalkr   joinrsplitr]   r   )r   r^   r_   rJ   rK   rL   set_files_resultrootdirsfilesfile	file_pathrW   r   r   r   set_pathB  s(   zJSONCommands.set_pathc                 C   s6   |g}|dur| t| | jdg|R 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>`_.
        NzJSON.STRLENr"   rG   )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  s   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   rn   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   rp   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)rF   r   r   kwargsr   r   r   jsonget     zJSONCommands.jsongetc                 O   ru   rv   )rH   rw   r   r   r   jsonmget  rz   zJSONCommands.jsonmgetc                 O   ru   rv   )rP   rw   r   r   r   jsonset  rz   zJSONCommands.jsonset)NN)FFF)Frv   )-__name__
__module____qualname____doc__r   rC   r   r   r   r   intr   r#   r%   r*   r-   r.   r/   r1   r2   r3   r8   r
   r;   r=   r?   forgetboolrF   rH   rP   r   rU   rV   r]   r   rk   rl   r   rm   ro   rt   ry   r{   r|   r   r   r   r   r      s   











&
"





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