o
    Š¨Êhƒ=  ã                   @   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 )z½Append 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   úN/var/www/html/env/lib/python3.10/site-packages/redis/commands/json/commands.pyÚ	arrappend   s   zJSONCommands.arrappendÚ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 )zÏInsert 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 )z³Return 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.arrlenéÿÿÿÿc                 C   s   |   d|t|ƒ|¡S )z¿Pop 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 )zÝTrim 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&   )zŸGet 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&   )z”Return 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&   )z¾Return 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&   )z¹Return 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 )zèIncrement 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   )zéMultiply 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<   )z˜Delete 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 )zµReturn 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<   )z¸Toggle 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 )z¶Return 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   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   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    