
    +h<                         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      Zy)    N)JSONDecodeErrorloads)DictListOptionalTupleUnion)	DataError)deprecated_function   )JsonType)decode_dict_keys)Pathc                   t   e Zd ZdZ ej
                         fdedee   dee	   dee
edf      fdZ	 	 d<dededed	ee   d
ee   dee
edf      fdZdedededee	   dee
edf      f
dZ ej
                         fdedee   dee
edf      fdZ ej
                         dfdedee   dee   dee
edf      fdZdeded	ed
edee
edf      f
dZ ej
                         fdedee   dee   fdZ ej
                         fdedee   defdZ ej
                         fdedee   dee
ee   df      fdZ ej
                         fdedee   defdZdedededefdZ edd      dedededefd       Z ej
                         fdedee   defdZ ej
                         fdedee   defdZeZdd ded!ee   dee	   fd"Zd#ee   dedee	   fd$Z	 	 	 d=deded%e	d&ee   d'ee   d(ee   dee   fd)Zd*ee eee	f      dee   fd+Z!	 d>deded%e	d(ee   dee   f
d,Z"	 	 	 d=deded-ed&ee   d'ee   d(ee   dee   fd.Z#	 	 	 d=d/ed0ed&ee   d'ee   d(ee   dee$eef      fd1Z%d?dedee   dee
edf      fd2Z& ej
                         fdedee   de
eeee      f   fd3Z' ej
                         fded4edee   de
eeee      f   fd5Z(d ej
                         fd6edee   dee   de
eee   f   fd7Z) edd8      d9        Z* edd8      d:        Z+ edd8      d;        Z,y)@JSONCommandszjson commands.namepathargsreturnNc                     |t        |      g}|D ]"  }|j                  | j                  |             $  | j                  dg| 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os         O/var/www/html/venv/lib/python3.12/site-packages/redis/commands/json/commands.py	arrappendzJSONCommands.arrappend   sN     D	" 	+AMM$,,q/*	+#t##$4>v>>    scalarstartstopc                     |t        |      | j                  |      g}|$|j                  |       ||j                  |        | j                  dg| 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   s          r   arrindexzJSONCommands.arrindex   sY    " D	4<<#78MM% d##t##O=f==r!   indexc                     |t        |      |g}|D ]"  }|j                  | j                  |             $  | j                  dg| 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   s          r   	arrinsertzJSONCommands.arrinsert6   sP     D	5) 	+AMM$,,q/*	+#t##$4>v>>r!   c                 :    | j                  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   s      r   arrlenzJSONCommands.arrlenC        ##M4TCCr!   c                 <    | j                  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'   s       r   arrpopzJSONCommands.arrpopM   s     ##M4TEJJr!   c                 >    | j                  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$   s        r   arrtrimzJSONCommands.arrtrimZ   s!     ##ND#d)UDQQr!   c                 :    | j                  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,   s      r   typezJSONCommands.typed       
 ##Ks4yAAr!   c                 :    | j                  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,   s      r   respzJSONCommands.respk   r6   r!   c                 :    | j                  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,   s      r   objkeyszJSONCommands.objkeysr   s     ##ND#d)DDr!   c                 :    | j                  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,   s      r   objlenzJSONCommands.objlen|   s     ##M4TCCr!   numberc                 Z    | j                  d|t        |      | j                  |            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   r=   s       r   	numincrbyzJSONCommands.numincrby   s-     ##dCIt||F/C
 	
r!   z4.0.0z deprecated since redisjson 1.0.0)versionreasonc                 Z    | j                  d|t        |      | j                  |            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?   r@   s       r   	nummultbyzJSONCommands.nummultby   s-     ##dCIt||F/C
 	
r!   c                 :    | j                  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,   s      r   clearzJSONCommands.clear   s     ##L$D	BBr!   keyc                 :    | j                  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   rH   r   s      r   deletezJSONCommands.delete   s    
 ##JSY??r!   F)	no_escaperK   c                   |g}|r|j                  d       t        |      dk(  r$|j                  t        j                                n!|D ]  }|j                  t	        |              	  | j
                  dg| S # t        $ r Y yw xY 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   rK   r   r   ps         r   getzJSONCommands.get   s     MM*%t9>MM$..*+  &c!f%&
	'4''
<V<< 		s   +A> >	B
	B
keysc                 j    g }||z  }|j                  t        |              | j                  dg| 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   rS   r   r   s       r   mgetzJSONCommands.mget   s:     $c$i #t##K9&99r!   objnxxxdecode_keysc                     |rt        |      }|t        |      | j                  |      g}|r|rt        d      |r|j	                  d       n|r|j	                  d        | j
                  dg| 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   rW   rX   rY   rZ   r   s           r   setzJSONCommands.set   s{    , "3'CD	4<<#45 "2  MM$MM$#t##J888r!   tripletsc           	          g }|D ]7  }|j                  |d   t        |d         | j                  |d         g       9  | j                  dg| 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   r`   r   triplets       r   msetzJSONCommands.mset   s^      	SGMM71:s71:WQZ8PQR	S#t##K9&99r!   c                 |    |rt        |      }|t        |      | j                  |      g} | j                  dg| 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   rW   rZ   r   s         r   mergezJSONCommands.merge  sB      "3'CD	4<<#45#t##L:6::r!   	file_namec                     t        |d      5 }t        |j                               }ddd       | j                  |||||      S # 1 sw Y    xY w)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rX   rY   rZ   )openr   readr_   )	r   r   r   rh   rX   rY   rZ   fpfile_contents	            r   set_filezJSONCommands.set_file'  sR    ( )S! 	,R +L	, xxdLRBKxXX	, 	,s   AA	json_pathroot_folderc           
         i }t        j                  |      D ]^  \  }}}	|	D ]S  }
t         j                  j                  ||
      }	 |j	                  d      d   }| j                  ||||||       d||<   U ` |S # t        $ r d||<   Y jw xY w)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   rk   TF)oswalkr   joinrsplitrp   r   )r   rq   rr   rX   rY   rZ   set_files_resultrootdirsfilesfile	file_pathrh   s                r   set_pathzJSONCommands.set_path@  s    $ !#!5 	8D$ 8GGLLt4	8 ) 0 0 5a 8IMM!!!$/ "  37$Y/8	8"   ' 827$Y/8s   0A;;BBc                 f    |g}||j                  t        |              | j                  dg| 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>`_.
        zJSON.STRLENrU   )r   r   r   r   s       r   strlenzJSONCommands.strlenf  s9     MM#d)$#t##M;F;;r!   c                 :    | j                  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,   s      r   togglezJSONCommands.toggleq  r.   r!   valuec                 b    |t        |      | j                  |      g} | j                  dg| 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   r   r   r   s        r   	strappendzJSONCommands.strappend{  s6     D	4<<#67#t##$4>v>>r!   
subcommandc                     ddg}||vrt        dt        |            |g}|dk(  r8|t        d      |j                  |       |j                  t        |              | j                  dg| 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 zNo key specifiedz
JSON.DEBUG)r
   r   r   r   )r   r   rH   r   valid_subcommandsr   s         r   debugzJSONCommands.debug  s     &v...=sCT?UVV!{ 233MM#MM#d)$#t##L:6::r!   z/redisjson-py supported this, call get directly.c                 &     | j                   |i |S N)rR   r   r   kwargss      r   jsongetzJSONCommands.jsonget       txx(((r!   c                 &     | j                   |i |S r   )rV   r   s      r   jsonmgetzJSONCommands.jsonmget  s     tyy$)&))r!   c                 &     | j                   |i |S r   )r_   r   s      r   jsonsetzJSONCommands.jsonset  r   r!   )NN)FFF)Fr   )-__name__
__module____qualname____doc__r   rO   r   r   r   r   r	   intr    r&   r)   r-   r1   r3   r5   r8   r:   r<   rA   r   rE   rG   rJ   forgetboolrR   rV   r_   r   re   rg   rp   r   r   r   r   r   r   r   r   r    r!   r   r   r      s    0>t~~/???'}?HLX?	eCI	?$  $">> > 	>
 }> sm> 
eCI	>2??"?+.?7;H~?	eCI	? 0>t~~/?DD'}D	eCI	D -dnn.!	KK smK }	K
 
eCI	KRR"R+.R69R	eCI	R 5CDNN4D B BHSM Bc B 5CDNN4D B BHSM B B 0>t~~/?EE'}E	eDItO$	%E 7Ednn6F D3 Dhsm D3 D
c 
 
c 
c 
 1ST
c 
 
c 
c 
 U
 6DT^^5E 	C# 	CXc] 	C# 	C 6DT^^5E @# @Xc] @# @ F =B+3D>	h<
:c 
:# 
:$x. 
:" #"&+%9%9 %9 	%9
 TN%9 TN%9 d^%9 
#%9N:T%S((:";< :# :, ',;; ; 	;
 d^; 
#;8 #"&+YY Y 	Y
 TNY TNY d^Y 
#Y: #"&+$ $  $  TN	$ 
 TN$  d^$  
d39o	$ L	<3 	<hsm 	<tE#t)DT?U 	< 0>t~~/?DD'}D	tT(3-((	)D <J4>>;K
?
? #
?+3C=
?	sD#''	(
? ",dnn.	;; c]; sm	;
 
sDI~	;,  Q))  Q**  Q))r!   r   )ru   jsonr   r   typingr   r   r   r   r	   redis.exceptionsr
   redis.utilsr   _utilr   decodersr   r   r   r   r   r!   r   <module>r      s,    	 ' 5 5 & +  & `) `)r!   