b6ddlZddlmZmZddlmZmZmZmZddl m Z ddl m Z ddl m Z ddlmZdd lmZGd d Zy) N)JSONDecodeErrorloads)DictListOptionalUnion) deprecated) DataError)JsonType)decode_dict_keys)Pathc eZdZdZej fdedeedee dee e dffdZ d9deded e d ee d ee dee e dff d Z deded e dee dee e dff dZej fdedeedee e dffdZej dfdedeed ee dee edffdZdeded e d e dee e dff dZej fdedeedeefdZej fdedeedefdZej fdedeedee eedffdZej fdedeede fdZdedede defdZedddedede defdZej fdedeede fdZej fdedeede fdZeZdd ded!eedee fd"Zd#eededee fd$Z d:deded%e d&eed'eed(eedeefd)Z d:deded*ed&eed'eed(eedeefd+Z d:d,ed-ed&eed'eed(eedee!eeff d.Z"d;dedeedee e dffd/Z#ej fdedeede eeee ffd0Z$ej fded1edee de e eee ffd2Z%dej fd3edeedeede e eeffd4Z&edd5d6Z'edd5d7Z(edd5d8Z)y)< JSONCommandszjson commands.namepathargsreturnNc|t|g}|D]"}|j|j|$|jdg|S)zAppend the objects ``args`` to the array under the ``path` in key ``name``. For more information see `JSON.ARRAPPEND `_.. zJSON.ARRAPPENDstrappend_encodeexecute_command)selfrrrpiecesos >/usr/lib/python3/dist-packages/redis/commands/json/commands.py arrappendzJSONCommands.arrappendsND " +A MM$,,q/ * +#t##$4>v>>scalarstartstopc^|jd|t||j|||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 `_. z JSON.ARRINDEXrrr)rrrr"r#r$s rarrindexzJSONCommands.arrindexs1"## T3t9dll6.BE4  r indexc|t||g}|D]"}|j|j|$|jdg|S)zInsert the objects ``args`` to the array at index ``index`` under the ``path` in key ``name``. For more information see `JSON.ARRINSERT `_. zJSON.ARRINSERTr)rrrr(rrrs r arrinsertzJSONCommands.arrinsert3sPD 5) +A MM$,,q/ * +#t##$4>v>>r c:|jd|t|S)zReturn the length of the array JSON value under ``path`` at key``name``. For more information see `JSON.ARRLEN `_. z JSON.ARRLENrrrrrs rarrlenzJSONCommands.arrlen@##M4TCCr c<|jd|t||S)zPop the element at ``index`` in the array JSON value under ``path`` at key ``name``. For more information see `JSON.ARRPOP `_. z JSON.ARRPOPr,)rrrr(s rarrpopzJSONCommands.arrpopJs##M4TEJJr c>|jd|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 `_. z JSON.ARRTRIMr,)rrrr#r$s rarrtrimzJSONCommands.arrtrimXs!##ND#d)UDQQr c:|jd|t|S)zGet the type of the JSON value under ``path`` from key ``name``. For more information see `JSON.TYPE `_. z JSON.TYPEr,r-s rtypezJSONCommands.typeb ##Ks4yAAr c:|jd|t|S)zReturn the JSON value under ``path`` at key ``name``. For more information see `JSON.RESP `_. z JSON.RESPr,r-s rrespzJSONCommands.respir6r c:|jd|t|S)zReturn the key names in the dictionary JSON value under ``path`` at key ``name``. For more information see `JSON.OBJKEYS `_. z JSON.OBJKEYSr,r-s robjkeyszJSONCommands.objkeysps##ND#d)DDr c:|jd|t|S)zReturn the length of the dictionary JSON value under ``path`` at key ``name``. For more information see `JSON.OBJLEN `_. z JSON.OBJLENr,r-s robjlenzJSONCommands.objlenzs ##M4TCCr numbercZ|jd|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 `_. zJSON.NUMINCRBYr&rrrr=s r numincrbyzJSONCommands.numincrbys- ## dCIt||F/C  r z4.0.0z deprecated since redisjson 1.0.0)versionreasoncZ|jd|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 `_. zJSON.NUMMULTBYr&r?s r nummultbyzJSONCommands.nummultbys-## dCIt||F/C  r c:|jd|t|S)aEmpty 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 `_. z JSON.CLEARr,r-s rclearzJSONCommands.clears##L$D BBr keyc:|jd|t|S)zDelete the JSON value stored at key ``key`` under ``path``. For more information see `JSON.DEL `_. zJSON.DELr,)rrGrs rdeletezJSONCommands.deletes ##JSY??r F) no_escaperJc|g}|r|jdt|dk(r$|jtjn!|D]}|jt | |j dg|S#t $rYywxYw)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 `_. noescaperzJSON.GETN)rlenr root_pathrr TypeError)rrrJrrps rgetzJSONCommands.gets  MM* % t9> MM$..* + & c!f% &  '4'' > B  B keyscjg}||z }|jt||jdg|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 `_. z JSON.MGETrrr)rrRrrs rmgetzJSONCommands.mgets:$ c$i #t##K9&99r objnxxx decode_keysc|r t|}|t||j|g}|r |r td|r|j dn|r|j d|j dg|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 `_. zNnx and xx are mutually exclusive: use one, the other or neither - but not bothNXXXzJSON.SET)r rr Exceptionrr)rrrrVrWrXrYrs rsetzJSONCommands.sets{, "3'CD 4<<#45 "2  MM$   MM$ #t##J888r file_namect|d5}t|j}ddd|j|||||S#1swY xYw)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. rNrWrXrY)openrreadr^) rrrr_rWrXrYfp file_contents rset_filezJSONCommands.set_filesR()S ! ,R +L ,xxdLRBKxXX , ,s AA json_path root_folderc i}tj|D]^\}}} | D]S} tjj|| } | j dd} |j | || |||d|| <U`|S#t $rd|| <YjwxYw)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. .rrbTF)oswalkrjoinrsplitrgr) rrhrirWrXrYset_files_resultrootdirsfilesfile file_pathr_s rset_pathzJSONCommands.set_paths$!#!5 8 D$ 8GGLLt4  8 ) 0 0 5a 8IMM!!!$/ "37$Y/ 8 8" '827$Y/8s0A;;B  B cf|g}||jt||jdg|S)zReturn the length of the string JSON value under ``path`` at key ``name``. For more information see `JSON.STRLEN `_. z JSON.STRLENrT)rrrrs rstrlenzJSONCommands.strlen<s9    MM#d) $#t##M;F;;r c:|jd|t|S)zToggle boolean value under ``path`` at key ``name``. returning the new value. For more information see `JSON.TOGGLE `_. z JSON.TOGGLEr,r-s rtogglezJSONCommands.toggleGr/r valuecb|t||j|g}|jdg|S)aCAppend 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 `_. zJSON.STRAPPEND)rrr)rrr{rrs r strappendzJSONCommands.strappendQs6D 4<<#67#t##$4>v>>r subcommandcddg}||vrtdt||g}|dk(r8| td|j||jt||jdg|S)zReturn the memory usage in bytes of a value under ``path`` from key ``name``. For more information see `JSON.DEBUG `_. MEMORYHELPzThe only valid subcommands are zNo key specifiedz JSON.DEBUG)r rrr)rr~rGrvalid_subcommandsrs rdebugzJSONCommands.debug]s&v. . .=sCT?UV V  !{ 233 MM#  MM#d) $#t##L:6::r z/redisjson-py supported this, call get directly.c&|j|i|SN)rQrrkwargss rjsongetzJSONCommands.jsongetstxx(((r c&|j|i|Sr)rUrs rjsonmgetzJSONCommands.jsonmgetystyy$)&))r c&|j|i|Sr)r^rs rjsonsetzJSONCommands.jsonsetrr )rr!)FFFr)*__name__ __module__ __qualname____doc__rrNrrrr rintrr'r*r.r1r3r5r8r:r<r@r rDrFrIforgetboolrQrUr^rgrrvrxrzr}rrrrr rrrsO0>t~~/? ? ?'} ?HLX ? eCI  ?$ !      }  sm   eCI  * ? ?" ?+. ?7;H~ ? eCI  ?0>t~~/?DD'}D eCI D-dnn.! K Ksm K} K eCI  KRR"R+.R69R eCI R5CDNN4DBBHSMBcB5CDNN4DBBHSMBB0>t~~/?EE'}E eDItO$ %E7Ednn6FD3DhsmD3D c  c c (JK c  c c L 6DT^^5E C# CXc] C# C6DT^^5E@#@Xc]@#@F=B+3D> h< :c :# :$x. :"#"&+%9%9%9 %9 TN %9 TN %9d^%9 #%9X#"&+YYY Y TN Y TN Yd^Y #Y:#"&+ $ $ $  TN $ TN $ d^ $  d39o $ L <3 t~~/?DD'}D tT(3-(( )D>;K ? ? # ?+3C= ? sD#'' ( ?",dnn. ;;c];sm ; sDI~  ;, Q)) Q** Q))r r)rljsonrrtypingrrrrr redis.exceptionsr _utilr decodersr rrrrr rrs, '..!&&u)u)r