bs*ddlZddlZddlZddlZddlZddlZddlmZddlm Z m Z m Z m Z ddl mZmZmZddlmZmZmZmZmZmZmZmZddlmZddlmZmZdZd Z d Z!d Z"d Z#Gd de$Z%dZ&dZ'dZ(dZ)ide*de*de*de*de*de*de*de*de*de*de*de*de*d e*d!e*d"e*d#e*e*e*e*e*e*e*d$Z+d%Z,d&Z-d'Z.d(Z/d)Z0d\d*Z1d+Z2d,Z3d-Z4d.Z5d/Z6d0Z7d1Z8d2Z9d3Z:d4Z;d5Zd8Z?d9Z@d:ZAd;ZBd<ZCd=ZDd>ZEd?ZFd@ZGdAZHdBZIdCZJdDZKdEZLdFZMdGZNdHZOdIZPdJZQdKZRdLZSdMZTdNZUdOZVGdPdQZWGdRdSeWe e e ZXeXZYGdTdUZZGdVdWZ[GdXdYejZ]GdZd[eXZ^y)]N)chain) CoreCommandsRedisModuleCommandsSentinelCommands list_or_args)ConnectionPool SSLConnectionUnixDomainSocketConnection)ConnectionErrorExecAbortError ModuleError PubSubError RedisError ResponseError TimeoutError WatchErrorLock)safe_str str_if_bytesEMPTY_RESPONSE NEVER_DECODEc||sy t|}tjj|S#t$rYywxYw)z5Converts a unix timestamp to a Python datetime objectN)int ValueErrordatetime fromtimestampresponses ./usr/lib/python3/dist-packages/redis/client.pytimestamp_to_datetimer"$sC x=    * *8 44 s / ;;cJtj|j|SN)dictfromkeyssplit) key_stringcallbacks r!string_keys_to_dictr*/s ==))+X 66rc\eZdZdZdZfdZfdZfdZd fd ZfdZ fdZ xZ S) CaseInsensitiveDictz?Case insensitive dict implementation. Assumes string keys only.cZ|jD]\}}|||j<yr$)itemsupper)selfdatakvs r!__init__zCaseInsensitiveDict.__init__6s*JJL DAqDO rc>t||jSr$)super __contains__r/r0r2 __class__s r!r7z CaseInsensitiveDict.__contains__:sw#AGGI..rc@t||jyr$)r6 __delitem__r/r8s r!r;zCaseInsensitiveDict.__delitem__=s AGGI&rc>t||jSr$)r6 __getitem__r/r8s r!r=zCaseInsensitiveDict.__getitem__@sw"1779--rc@t||j|Sr$)r6getr/)r0r2defaultr9s r!r?zCaseInsensitiveDict.getCsw{1779g..rcBt||j|yr$)r6 __setitem__r/)r0r2r3r9s r!rBzCaseInsensitiveDict.__setitem__Fs AGGIq)rc:t|}t| |yr$)r,r6update)r0r1r9s r!rDzCaseInsensitiveDict.updateIs"4( trr$) __name__ __module__ __qualname____doc__r4r7r;r=r?rBrD __classcell__r9s@r!r,r,3s.E /'./*rr,ct|}d|z}td|jD}d}|D]}||vst||||<|S)zDParse the results of Redis's DEBUG OBJECT command into a Python dictztype:c3>K|]}|jdyw:Nr').0kvs r! z%parse_debug_object..Ts=bBHHSM=s)refcountserializedlengthlrulru_seconds_idle)rr%r'r)r int_fieldsfields r!parse_debug_objectrYNsgH%H!H=HNN,<==HMJ3 H !(5/2HUO3 Orc$|dvr t|S|S)z&Parse the results of an OBJECT command)idletimerS) int_or_none)r infotypes r! parse_objectr^`s++8$$ Orci}t|}fd|jD]}|s|jdr|jddk7rh|j dd\}}|dk(r|j dd\}}|dk(r(|j dgj||||<|j d gj||S) z;Parse the result of Redis's INFO command into a Python dictcd|vsd|vr d|vr t|St|Si}|jdD]"}|j dd\}}|||<$|S#t$r|cYSwxYw)N,=.)floatrrr'rsplit)valuesub_dictitemr2r3 get_values r!rjzparse_info..get_valuels e s%/ %< <'u:%H C( +{{3*1'l  +O   sA A A-,A-#rNrd cmdstat_hostmodulemodules__raw__)r splitlines startswithfindr'rf setdefaultappend)r infolinekeyrgrjs @r! parse_inforygs DH%H ##%< ,yy~#"ZZQ/ U.(!%S!!4JC(?OOIr299)E:JK )% 0DI 2.55d;%<( Krc t|dd}|jD](\}}|jdst|dd||<*|S)z!Parse the results of MEMORY STATST) decode_keysdecode_string_valueszdb.) pairs_to_dictr.rr)r kwargsstatsrxrgs r!parse_memory_statsrsQ (4 PEkkm U >>% &4dE#J Lrzcan-failover-its-masterz config-epochzdown-after-millisecondszfailover-timeoutz info-refreshzlast-hello-messagezlast-ok-ping-replyzlast-ping-replyzlast-ping-sentzmaster-link-down-timez master-portznum-other-sentinelsz num-slavesz o-down-timezpending-commandszparallel-syncsport)quorumzrole-reported-timez s-down-timezslave-priorityzslave-repl-offsetzvoted-leader-epochct|t}t|djd}dD] \}}||v||<|S)Nflagsra)) is_mastermaster)is_slaveslave)is_sdowns_down)is_odowno_down) is_sentinelsentinel)is_disconnected disconnected)is_master_down master_down)pairs_to_dict_typedSENTINEL_STATE_TYPESsetr')riresultrnameflags r!parse_sentinel_statersS '; 5) X &&rc V|jddr|dSt|d|d<|S)NrFrdrrs r!parse_xautoclaimr6s1{{>5){#HQK0HQK Orc ^t|d}|jdds<|d}||dt|df|d<|d}||dt|df|d<|S|d Dcic]\}}|t|c}}|d <|d Dcgc]}t|dc}|d <|Scc}}wcc}w) NTrfullFz first-entryrrdz last-entryentriesr)r}r?)r rr1firstlast_identrygroups r!parse_xinfo_streamr=s t 4D ;;vu %]#  #(8]58-D"ED L!  "&q'=a+A!BD  K HLIWe3 e 44WY@DX 7QqT$QqT* + >> >s&c |jddr t|S|dxsgDcgc]\}}|t|d}}}|d|d|d|dScc}}w) N parse_detailF)rpendingrrdr)rminmax consumers)r?parse_xpending_ranger)r rrprs r!parse_xpendingrTsl{{>5)#H--.{sH4DJJsA.Hs )rrqrur%r')r rclientscs r!parse_client_listrwsLG ( # . . 0JtH1773<HHIJ Nrc j|Dcgc]}| t|nd}}|xr t|xsiScc}wr$)rr})r rrs r!parse_config_getrs=DLMq1= Qd:MHM  / h/ 525Ns0c &|\}}t||fSr$rr rcursorrs r! parse_scanrsIFA v;>rc H|\}}t||xr t|xsifSr$)rr}rs r! parse_hscanrs)IFA v;.mA.4" 44rc |jdt}|\}}t|}t|t t |t ||fS)Nr)r?rerrrrr)r rrrrrs r! parse_zscanrsGkk"3U;OIFA aB v;SS"%=>? ??rc F|Dcgc]}| t|ndc}Scc}wr$r)r rscores r! parse_zmscorers$EM NEE-E%L4 7 NN Nsc p|jddrdndfd}|Dcgc] }|| c}Scc}w)Ndecode_responsesFr c|dt|dt|dd}t|dtrj|d|d<|S|d|d<j|d|d<|S) Nrrdr)id start_timedurationrcommand complexity)r isinstancerjoin)rirspaces r! parse_itemz%parse_slowlog_get..parse_items~Qs47|TRSWV d1gt $ % 47 3F9  $(7F< % 47 3F9  r)r?)r rrrirs @r!parse_slowlog_getrs8;;159CtE *2 2Jt  22 2s3c |jddr t|S|jddr|jddr=|dDcgc].}t|dgttt|ddz0}}n)|dDcgc]}ttt|}}t |d|t |d t|d iSt |Scc}wcc}w) a Parse the response from `STRALGO` command. Without modifiers the returned value is string. When LEN is given the command returns the length of the result (i.e integer). When IDX is given the command returns a dictionary with the LCS length and all the ranges in both the strings, start and end offset for each string, where there are matches. When WITHMATCHLEN is given, each array representing a match will also have the length of the match at the beginning of the array. lenFidx withmatchlenrdrlNrrr)r?rrrtupler)r rmatchmatchess r! parse_stralgors{{5% 8}{{5% ;;~u -&a[eBi."T#eU3BZ*@%AAG =EQKH5tCu-.HGH ! %w ! %s8A;'7    !! Is 3C C"c Xt|}td|jDS)Nc3DK|]}|s|jdywrMrO)rPrws r!rRz%parse_cluster_info..sJDT 3Js  )rr%rqrs r!parse_cluster_infor s&H%H JH,?,?,AJ JJrc |jd}|jddd\}}}}}}}} |jdd}||||||gg| dk(rdndd } t|d k\rt|dd\} } | | c| d <| d <|| fS) Nr@r connectedTF) node_idr master_idlast_ping_sentlast_pong_rcvdepochslots migrationsr$ r*r+)r'r _parse_slots) rw line_itemsr%addrrr&pingpongr)r$ node_dictr*r+s r!_parse_node_liner3sCJDHJJsOTVUVDWAGT5)T4 ::c?1 D&+5T5 I :!(AB8z6;Z3 'Il3 ?rcPgg}}|D]}d|vr.|ddjdd\}}|j||dd5d|vr.|ddjdd\}}|j||ddg|jdDcgc]}|}}|j|||fScc}w) Nz->-rdrl migrating)slotr%rz-<- importing-)r'ru) slot_rangesr*r+s_rangeslot_id dst_node_id src_node_idsls r!r-r-sB:E " G #*1R=#6#6ua#@ G[    [;O g #*1R=#6#6ua#@ G[    [;O %,MM#$67br7G7 LL ! " * 8s B#c rt|ttfr|j}t d|DS)z @see: https://redis.io/commands/cluster-nodes # string / bytes @see: https://redis.io/commands/cluster-replicas # list of string / bytes c3DK|]}tt|ywr$)r3r)rPnodes r!rRz&parse_cluster_nodes..sJ d!34Js )rstrbytesrqr%rs r!parse_cluster_nodesrDs1 (S%L)&&( JJ JJrc :|ds|dr|St|tk7r|g}n|}|ds |ds|ds|Stdtd}dg}|d Dcgc] }||s ||c}z }|Dcgc]"}tt d t ||$c}Scc}wcc}w) z Parse the response of 'GEOSEARCH', GEORADIUS' and 'GEORADIUSBYMEMBER' commands according to 'withdist', 'withhash' and 'withcoord' labels. store store_distwithdist withcoordwithhashc:t|dt|dfSrrlls r!z)parse_geosearch_generic..sr!uuRU| <r)rHrIrJc|Sr$xs r!rNz)parse_geosearch_generic..s1r)rHrJrIc|d|dSrrP)fvs r!rNz)parse_geosearch_generic..s1be r)typerrerrr)r r response_listcastfors r!parse_geosearch_genericrZs w7<0 H~!  : w{';GJDW< D  A@ OaGAJ$q' OOACP QaD,c!Qi8 9 QQ P Qs BB)'Bc 2i}|D]}i}t|d}||d<t|d|d<|dDcgc] }t|c}|d<|d|d<|d |d <|d |d <t|d kDr|d |d<|d|d<|d|d<|||<|Scc}w)Nrrrdarityrrr first_key_posr last_key_pos step_counttipsr"key_specificationsr, subcommands)rrr)r rcommandsrcmd_dictcmd_namers r! parse_commandrh"sH & +# OB;>O 2:D\8, - [ rjH #))#.% '>   g & OOG $ %  D#Dd7m+DO K s) F)c |yt|trZg}|D]Q}t|dd}|jdd}t ||d<t |d|d<|j |S|St|}|S)NTz client-inforpz age-seconds)rrr}r?parse_client_inforerur)r rr1loglog_data client_infos r! parse_acl_logr}ds(D! "C$S$5H",,}b9K&7 &DH] #',H],C&DH] # KK ! " Kx  Krci}t|jd}|D]}|jd\}}|||<dD]}t||||<|S)zg Parsing client-info in ACL Log in following format. "key1=value1 key2=value2 key3=value3" rrb>tot-memargv-mem qbuf-freedbr ageoblollsubidleomempsubqbufmulti)rr'r)rgr|infosrvrxint_keys r!ryryvsw K   % %c *E!ZZ_ U C! 9 #;w#78 G!9" rc(t|tr|yNT)rr rs r!parse_module_resultrs(K( rc L|jdr|S|xrt|dk(S)z Handle SET result since GET argument is available since Redis 6.2. Parsing SET result into: - BOOL - String when GET argument is used r?r)r?rrs r!parse_set_resultrs,{{5  6 X.$66rc eZdZiedeedeedeeddedeedeede ed d ed d ed e eddede ede ede eddidddedededdddde ded e d!e d"d#d$ed%ed&ed'ed(ed)eid*e d+d,d-e d.ed/d0d1e d2e d3d4d5d6d7e d8e d9e d:e d;d<d=ed>d?d@e idAedBedCe dDe dEe dFe dGe dHedIedJedKdLdMedNe dOe dPedQe dRe idSe dTdUdVdWdXedYedZed[d\d]ed^ed_ed`e daedbe dceddededfdgeidhdidje dke dle!dmdndoedpe"dqdrdse dte duedve dwe dxe dye#dze$d{e%id|e d}e d~e de&de de&de'de(dede de"ddde)de*de dedee e+e+e,e-e.e/e0dZ1y) AbstractRediszyAUTH COPY EXPIRE EXPIREAT PEXPIRE PEXPIREAT HEXISTS HMSET MOVE MSETNX PERSIST PSETEX RENAMENX SISMEMBER SMOVE SETEX SETNXaBITCOUNT BITPOS DECRBY DEL EXISTS GEOADD GETBIT HDEL HLEN HSTRLEN INCRBY LINSERT LLEN LPUSHX PFADD PFCOUNT RPUSHX SADD SCARD SDIFFSTORE SETBIT SETRANGE SINTERSTORE SREM STRLEN SUNIONSTORE UNLINK XACK XDEL XLEN XTRIM ZCARD ZLEXCOUNT ZREM ZREMRANGEBYLEX ZREMRANGEBYRANK ZREMRANGEBYSCOREzINCRBYFLOAT HINCRBYFLOATz LPUSH RPUSHcJt|txr|xst|dk(Srrlrs r!rNzAbstractRedis.s jC(.QI,q/T2IrSORTzZSCORE ZINCRBY GEODISTz|FLUSHALL FLUSHDB LSET LTRIM MSET PFMERGE ASKING READONLY READWRITE RENAME SAVE SELECT SHUTDOWN SLAVEOF SWAPDB WATCH UNWATCH z BLPOP BRPOPc(|xr t|xsdSr$)rrs r!rNzAbstractRedis.sq~U1X7MrzSDIFF SINTER SMEMBERS SUNIONc8|xr t|xs tSr$)rrs r!rNzAbstractRedis.salCF6KcerzSZPOPMAX ZPOPMIN ZINTER ZDIFF ZUNION ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCOREzBZPOPMIN BZPOPMAXc@|xr|d|dt|dfxsdS)Nrrdrrrs r!rNzAbstractRedis.s'1+J!A$!eAaDk1J+RdrzZRANK ZREVRANKzXREVRANGE XRANGEzXREAD XREADGROUPzBGREWRITEAOF BGSAVEcyrrPrs r!rNzAbstractRedis.srzACL CATc4ttt|Sr$rrrrs r!rNzAbstractRedis.T#lA"67rz ACL DELUSERz ACL GENPASSz ACL GETUSERzACL HELPc4ttt|Sr$rrs r!rNzAbstractRedis.d3|Q#78rzACL LISTc4ttt|Sr$rrs r!rNzAbstractRedis.rrzACL LOADzACL LOGzACL SAVEz ACL SETUSERz ACL USERSc4ttt|Sr$rrs r!rNzAbstractRedis.stC a$89rz ACL WHOAMIzCLIENT GETNAMEz CLIENT IDz CLIENT KILLz CLIENT LISTz CLIENT INFOzCLIENT SETNAMEzCLIENT UNBLOCKc.|xrt|dk(xsdS)NrdFrrs r!rNzAbstractRedis.sA$5#a&A+$>rz CLIENT PAUSEzCLIENT GETREDIRzCLIENT TRACKINGINFOc4ttt|Sr$rrs r!rNzAbstractRedis.sc,.B)CrzCLUSTER ADDSLOTSzCLUSTER ADDSLOTSRANGEzCLUSTER COUNT-FAILURE-REPORTSct|Sr$rrQs r!rNzAbstractRedis.s 3q6rzCLUSTER COUNTKEYSINSLOTct|Sr$rrQs r!rNzAbstractRedis.s SVrzCLUSTER DELSLOTSzCLUSTER DELSLOTSRANGEzCLUSTER FAILOVERzCLUSTER FORGETzCLUSTER GETKEYSINSLOTc4ttt|Sr$rrs r!rNzAbstractRedis.s4L!0D+Erz CLUSTER INFOzCLUSTER KEYSLOTct|Sr$rrQs r!rNzAbstractRedis.s SVrz CLUSTER MEETz CLUSTER NODESzCLUSTER REPLICASzCLUSTER REPLICATEz CLUSTER RESETzCLUSTER SAVECONFIGzCLUSTER SET-CONFIG-EPOCHzCLUSTER SETSLOTzCLUSTER SLAVESCOMMANDz COMMAND COUNTzCOMMAND GETKEYSc4ttt|Sr$rrs r!rNzAbstractRedis.sT#lA*>%?rz CONFIG GETzCONFIG RESETSTATz CONFIG SETz DEBUG OBJECTzFUNCTION DELETEzFUNCTION FLUSHzFUNCTION RESTOREGEOHASHc4ttt|Sr$rrs r!rNzAbstractRedis. rrGEOPOSc.ttd|S)NcB|t|dt|dfSdSrrrLs r!rNz(AbstractRedis... s#2>E"Q%L%1,7tr)rrrs r!rNzAbstractRedis. sD SUV W r GEOSEARCH GEORADIUSGEORADIUSBYMEMBERHGETALLc(|xr t|xsiSr$rrs r!rNzAbstractRedis.sQ3=#39rrHSCANINFOLASTSAVEz MEMORY PURGEz MEMORY STATSz MEMORY USAGEz MODULE LOADz MODULE UNLOADz MODULE LISTc>|Dcgc] }t|c}Scc}wr$r)rms r!rNzAbstractRedis.sA!>q-"2!>!>sOBJECTPINGct|dk(S)NPONGrrs r!rNzAbstractRedis.s,q/V3rQUITSTRALGOz PUBSUB NUMSUB RANDOMKEYc|xr|xsdSr$rPrs r!rNzAbstractRedis.!sqwQ$rRESETSCAN SCRIPT EXISTSc4ttt|Sr$)rrboolrs r!rNzAbstractRedis.$s4D! #5rz SCRIPT FLUSHz SCRIPT KILL SCRIPT LOADzSENTINEL CKQUORUMzSENTINEL FAILOVERzSENTINEL FLUSHCONFIGz SENTINEL GET-MASTER-ADDR-BY-NAMEzSENTINEL MASTERzSENTINEL MASTERSzSENTINEL MONITORzSENTINEL RESETzSENTINEL REMOVEzSENTINEL SENTINELSz SENTINEL SETzSENTINEL SLAVESSETz SLOWLOG GETz SLOWLOG LENz SLOWLOG RESETSSCANTIMEc:t|dt|dfSrrrQs r!rNzAbstractRedis.9s3qt9c!A$i0rXCLAIM XAUTOCLAIMz XGROUP CREATEzXGROUP DELCONSUMERzXGROUP DESTROY)z XGROUP SETIDzXINFO CONSUMERSz XINFO GROUPSz XINFO STREAMXPENDINGZADDZSCANZMSCOREN)2rErFrGr*rrrerrrrr\rrrrwr}rmrryr rDrhrrYrZrryr"rrr^rrjrrrrrrrrrrrrrrrRESPONSE_CALLBACKSrPrr!rrs\  :   \  >   \ 8% @\   I !\* f&8 9+\, 6 F-\.  H  /\8 m-M N9\:  *,K ;\@  )  A\J  !R K\P . U\V 3^ DW\X 7Y\Z s[\\ |]\^ (_\` 8a\b 8c\d Ge\f =g\h Gi\j wk\l 9m\n lo\p ,q\r Ss\t (u\v (w\x (y\z '{\| >}\~ \@ 3A\B CC\D GE\F G\H ()9I\J "#3K\L GM\N O\P GQ\R 'S\T !EU\V *W\X +Y\Z [\\ ,]\^ /_\` Wa\b c\d ge\f #Gg\h 7i\j -k\l =m\n o\p ?q\r &s\t Gu\v gw\x *y\z 7{\| '}\~ G\@ 7A\B  C\H ,I\J ,K\L 4M\N 9O\P Q\R  S\T )U\V W\X *Y\Z  [\\ *]\^ ,_\` >a\b ,c\d 3e\f g\h =i\j ,k\l .m\n o\p  q\r 5s\t u\v ww\x |y\z W{\| W}\~ \@ +,EA\B 0C\D 2E\F GG\H 'I\J 7K\L AM\N O\P >Q\R S\T (U\V sW\X Y\Z [\\ 0]\^ ,_\` &a\b c\d ce\f $g\h .+*" w\rrceZdZdZedZ ddZdZdZdZ dZ d Z dd Z d Z dd Zd ZdZdZdZdZdZdZdZdZdZdZy)Redisa Implementation of the Redis protocol. This abstract class provides a Python interface to all Redis commands and an implementation of the Redis protocol. Pipelines derive from this, implementing how the commands are sent and received to the Redis server. Based on configuration, an instance will either use a ConnectionPool, or Connection object to talk to redis. c @tj|fi|}||S)a Return a Redis client object configured from the given URL For example:: redis://[[username]:[password]]@localhost:6379/0 rediss://[[username]:[password]]@localhost:6379/0 unix://[[username]:[password]]@/path/to/socket.sock?db=0 Three URL schemes are supported: - `redis://` creates a TCP socket connection. See more at: - `rediss://` creates a SSL wrapped TCP socket connection. See more at: - ``unix://``: creates a Unix Domain Socket connection. The username, password, hostname, path and all querystring values are passed through urllib.parse.unquote in order to replace any percent-encoded values with their corresponding characters. There are several ways to specify a database number. The first value found will be used: 1. A ``db`` querystring option, e.g. redis://localhost?db=0 2. If using the redis:// or rediss:// schemes, the path argument of the url, e.g. redis://localhost/0 3. A ``db`` keyword argument to this function. If none of these options are specified, the default db=0 is used. All querystring options are cast to their appropriate Python types. Boolean arguments can be specified with string values "True"/"False" or "Yes"/"No". Values that cannot be properly cast cause a ``ValueError`` to be raised. Once parsed, the querystring arguments and keyword arguments are passed to the ``ConnectionPool``'s class initializer. In the case of conflicting arguments, querystring arguments always win. )connection_pool)rfrom_url)clsurlr~rs r!rzRedis.from_urlWs%T)11#@@?33rNc&|| s| tjtd| } | tjtd|} |sg}|dur|jt||#||| | ||t j |$||!|"|%d }&| |&j| tdn<|&j|||||d|r#|&jt||||||||||||d td i|&} | |_ d|_ | r |jjd |_ t|jj |_y) a4 Initialize a new Redis client. To specify a retry policy for specific errors, first set `retry_on_error` to a list of the error/s to retry on, then set `retry` to a valid `Retry` object. To retry on TimeoutError, `retry_on_timeout` can also be set to `True`. Nz/"charset" is deprecated. Use "encoding" insteadz5"errors" is deprecated. Use "encoding_errors" insteadT) rusernamepasswordsocket_timeoutencodingencoding_errorsrretry_on_errorretrymax_connectionshealth_check_interval client_nameredis_connect_func)pathconnection_class)hostrsocket_connect_timeoutsocket_keepalivesocket_keepalive_options) r ssl_keyfile ssl_certfile ssl_cert_reqs ssl_ca_certs ssl_ca_datassl_check_hostname ssl_password ssl_ca_pathssl_validate_ocsp_stapledssl_validate_ocspssl_ocsp_contextssl_ocsp_expected_cert_rP)warningswarnDeprecationWarningrurcopydeepcopyrDr r rr connectionget_connectionr,r9rresponse_callbacks)'r0rrrrrrrrrunix_socket_pathrrcharseterrorsrretry_on_timeoutrsslrrrrrrrrrrrrrsingle_connection_clientrrrrrr~s' r!r4zRedis.__init__ss^" &I #! &O #)!!#4'%%l3$$"0$#2$4"0u-#2)>*&8F  + 0,F $ $2H,<4L MM0=+6,8-:,8+62D,8+69R1B0@6L"-6v6O. #"22AA#FDO"5dnn6W6W"Xrc^t|jdt|jdS)N<>)rUrEreprrr0s r!__repr__zRedis.__repr__s,t*%%&aT-A-A(B'C1EErc6|jjS)z!Get the connection pool's encoder)r get_encoderr s r!rzRedis.get_encoders##//11rc.|jjS)z'Get the connection's key-word arguments)rconnection_kwargsr s r!get_connection_kwargszRedis.get_connection_kwargs s##555rc"||j|<y)zSet a custom Response CallbackN)r)r0rr)s r!set_response_callbackzRedis.set_response_callbacks+3(rct|||y)a This function can be used to add externally defined redis modules, and their namespaces to the redis client. funcname - A string containing the name of the function to create func - The function, being added to this class. ex: Assume that one has a custom redis module named foomod that creates command named 'foo.dothing' and 'foo.anotherthing' in redis. To load function functions into this namespace: from redis import Redis from foomodule import F r = Redis() r.load_external_module("foo", F) r.foo().dothing('your', 'arguments') For a concrete example see the reimport of the redisjson module in tests/test_connection.py::test_loading_external_modules N)setattr)r0funcnamefuncs r!load_external_modulezRedis.load_external_modules* h%rcFt|j|j||S)a_ Return a new pipeline object that can queue multiple commands for later execution. ``transaction`` indicates whether all commands should be executed atomically. Apart from making a group of operations atomic, pipelines are useful for reducing the back-and-forth overhead between the client and server. )Pipelinerr)r0 transaction shard_hints r!pipelinezRedis.pipeline)s&  $"9"9;   rcx|jdd}|jdd}|jdd}|jd|5} |r|j|||}|j} |r|n| cdddS#t$r||dkDrt j |YbwxYw#1swYyxYw)z Convenience method for executing the callable `func` as a transaction while watching all keys specified in `watches`. The 'func' callable should expect a single argument which is a Pipeline object. rNvalue_from_callableF watch_delayTr)poprwatchexecutertimesleep) r0rwatchesr~rrr pipe func_value exec_values r!rzRedis.transaction5s ZZ d3 $jj)>Fjj5 ]]4 ,  " G,!%dJ!%J)<:*L  "".;? ;/  s* B0 .B%B-*B0,B--B00B9c 0|t}||||||||S)aL Return a new Lock object using key ``name`` that mimics the behavior of threading.Lock. If specified, ``timeout`` indicates a maximum life for the lock. By default, it will remain locked until release() is called. ``sleep`` indicates the amount of time to sleep per loop iteration when the lock is in blocking mode and another client is currently holding the lock. ``blocking`` indicates whether calling ``acquire`` should block until the lock has been acquired or to fail immediately, causing ``acquire`` to return False and the lock not being acquired. Defaults to True. Note this value can be overridden by passing a ``blocking`` argument to ``acquire``. ``blocking_timeout`` indicates the maximum amount of time in seconds to spend trying to acquire the lock. A value of ``None`` indicates continue trying forever. ``blocking_timeout`` can be specified as a float or integer, both representing the number of seconds to wait. ``lock_class`` forces the specified lock implementation. Note that as of redis-py 3.0, the only lock class we implement is ``Lock`` (which is a Lua-based lock). So, it's unlikely you'll need this parameter, unless you have created your own custom lock class. ``thread_local`` indicates whether the lock token is placed in thread-local storage. By default, the token is placed in thread local storage so that a thread only sees its token, not a token set by another thread. Consider the following timeline: time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds. thread-1 sets the token to "abc" time: 1, thread-2 blocks trying to acquire `my-lock` using the Lock instance. time: 5, thread-1 has not yet completed. redis expires the lock key. time: 5, thread-2 acquired `my-lock` now that it's available. thread-2 sets the token to "xyz" time: 6, thread-1 finishes its work and calls release(). if the token is *not* stored in thread local storage, then thread-1 would see the token value as "xyz" and would be able to successfully release the thread-2's lock. In some use cases it's necessary to disable thread local storage. For example, if you have code where one thread acquires a lock and passes that lock instance to a worker thread to release later. If thread local storage isn't disabled in this case, the worker thread won't see the token set by the thread that acquired the lock. Our assumption is that these cases aren't common and as such default to using thread local storage.)timeoutr%blockingblocking_timeout thread_localr)r0rr+r%r,r- lock_classr.s r!lockz Redis.lockKs2|  J  -%  rc .t|jfi|S)z Return a Publish/Subscribe object. With this object, you can subscribe to channels and listen for messages that get published to them. )PubSubr)r0r~s r!pubsubz Redis.pubsubs d**5f55rc,t|jSr$)Monitorrr s r!monitorz Redis.monitorst++,,rc<|j|jdS)NT)rr)r9rr s r!clientz Redis.clients#~~ 004  rc|Sr$rPr s r! __enter__zRedis.__enter__ rc$|jyr$closer0exc_type exc_value tracebacks r!__exit__zRedis.__exit__  rc$|jyr$r=r s r!__del__z Redis.__del__rDrct|dsy|j}|r#d|_|jj|yy)Nr)hasattrrrreleaser0conns r!r>z Redis.closes=t\*  "DO  ( ( . rcH|j||j||fi|S)z7 Send a command and parse the response ) send_commandparse_response)r0rK command_nameargsrs r!_send_command_parse_responsez"Redis._send_command_parse_responses. 4 "t""4AAArc|j|j!t|t|jdur|y)z Close the connection and raise an exception if retry_on_error is not set or the error is not one of the specified error types NF) disconnectrrrr0rKerrors r!_disconnect_raisezRedis._disconnect_raises?     '%t':':!;<EKFrcBj}djxs|jfi jj fdfdjs|j SS#js|j wwxYw)z.Execute a command and return a parsed responserc2jgiSr$rQrPrOrKrr0sr!rNz'Redis.execute_command..s*999,)-18rc(j|Sr$)rVrUrKr0s r!rNz'Redis.execute_command..sd44T5Ar)rrrrcall_with_retryrI)r0rPrpoolrOrKs``` @@r!execute_commandzRedis.execute_commands##Aw N"5$"5"5l"Ng"N #::--B ?? T"#4?? T"#s &A>> Bc t|vr|jd}n|j}||jvr|j||fi|S|S#t$rt|vr |tcYSwxYw)z'Parses a response from the Redis serverT)disable_decoding)r read_responserrr)r0rrOrr s r!rNzRedis.parse_responses w&%33T3J%335 422 284**<8MWM M  (~..  s+AA1/A1)% localhostirNNNNNNNzutf-8strictNNFFNFNNrequiredNNNFNFFNNNFrNNNN)TN)Ng?TNNT)rErFrGrH classmethodrr4r rrrrrrr0r3r6r8r:rCrFr>rQrVr_rNrPrr!rrJs  *4*4\  #!%    "'#!&M}Y~F264&.  2H T6-  /B #" rrcveZdZdZej dZej dZdZdZ dZ dZ dZ y ) r5z Monitor is useful for handling the MONITOR command to the redis server. next_command() method returns one command from monitor listen() method yields commands from monitor. z\[(\d+) (.*)\] (.*)z"(.*?)(?z PubSub.closewrDrc|jj|jj|jrVi}|jj D]%\}}|||j j |d<'|jdi||jrWi}|jj D]%\}}|||j j |d<'|jdi|yy)zBRe-subscribe to any channels and patterns previously subscribed toTrnNrP) rrrrqr.rwrx subscriber psubscribe)r0rrqr2r3rs r! on_connectzPubSub.on_connectzs ))//1 ))//1 ==H ++- A1?@,,Qd,;< A DNN &X & ==H ++- A1?@,,Qd,;< A DOO 'h ' rc6|jjS)z@Indicates if there are subscriptions to any channels or patterns)ris_setr s r! subscribedzPubSub.subscribeds$$++--rcl|jP|jjd|j|_|jj |j |j}d|j i}|j s|j|j||jg|i|y)z#Execute a publish/subscribe commandNr3 check_health) rrrrregister_connect_callbackrrclean_health_check_responses_executerM)r0rPrr~s r!r_zPubSub.execute_commands ?? ""22AA$//DO OO 5 5doo F__  doo"56  - - / j*"9"9KDKFKrcd}|j}|jdkDr|dkDr|j||j|jr]|j||j }|j |r|xjdzc_ntdj||dz}|jdkDr|dkDryyyy)zG If any health check responses are present, clean them rr+rdz?A non health check response was cleaned by execute_command: {0}N) rrrcan_readrrbis_health_check_responserformat)r0ttlrKr s r!rz#PubSub.clean_health_check_responsess0014q}}T4==$:M:M}N==t/A/AB00:66!;6%//5vh/? 1HC0014q44rc|j|jrt|ts||j y)z Close the connection and raise an exception if retry_on_timeout is not set or the error is not a TimeoutError. Otherwise, try to reconnect N)rSrrrconnectrTs r!_disconnect_raise_connectz PubSub._disconnect_raise_connects. %%*UL*IK rcVjjfdfdS)aU Connect manually upon disconnection. If the Redis server is down, this will fail and raise a ConnectionError as desired. After reconnection, the ``on_connect`` callback should have been called by the # connection to resubscribe us to any channels and patterns we were previously listening to ciSr$rP)rPrr~sr!rNz!PubSub._execute..sGT,V,rc(j|Sr$)rr\s r!rNz!PubSub._execute..s$88uEr)rr])r0rKrrPr~s`````r!rzPubSub._executes"zz)) , E  rc |j}| td|j|s|j||j|sy|j||j }|j |r|xjdzc_y|S)z3Parse the response from a publish/subscribe commandNNpubsub connection not set: did you forget to call subscribe() or psubscribe()?rrd)r RuntimeErrorrrrrbrr)r0blockr+rKr s r!rNzPubSub.parse_responses <F  T]]4]P==t'9'9:  ( ( 2  . .! 3 .rc6||j|jfvS)z Check if the response is a health check response. If there are no subscriptions redis responds to PING command with a bulk response, instead of a multi-bulk with "pong" and the response. )rrrks r!rzPubSub.is_health_check_responses(   & &  ( (   rc|j}| td|jrVtj|jkDr4|j d|j d|xjdz c_yyy)NrrF)rrd)rrrr$next_health_checkrMrrrJs r!rzPubSub.check_healthsv <F   % %$))+8N8N*N   fd&?&?e  T  . .! 3 .+O %rc |jj}|jj}|jDcic]\}}||||c}}Scc}}w)z normalize channel/pattern names to be either bytes or strings based on whether responses are automatically decoded. this saves us from coercing the value for each message coming in. )rwrrxr.)r0r1rrxr2r3s r!_normalize_keyszPubSub._normalize_keyssN $$$$15>Avay!1$>>>sAc|rt|d|dd}tj|}|j||jdg|j }|j |}|jj||js!|jjd|_ |jj||S)aE Subscribe to channel patterns. Patterns supplied as keyword arguments expect a pattern name as the key and a callable as the value. A pattern's callable will be invoked automatically when a message is received on that pattern rather than producing a message via ``listen()``. rrdN PSUBSCRIBE)rr%r&rDr_rrrrrrrrdifference_update)r0rPr~ new_patternsret_vals r!rzPubSub.psubscribes Qab2D}}T* F#&$&&|Jl6G6G6IJ++L9  \*  ! ! % % '12D . ));;LIrc|r7t|d|dd}|jtj|}n |j}|j j ||jdg|S)zj Unsubscribe from the supplied patterns. If empty, unsubscribe from all patterns. rrdN PUNSUBSCRIBE)rrr%r&rrrDr_)r0rPrs r!rzPubSub.punsubscribesk Qab2D++DMM$,?@H}}H ))00:#t##N:T::rc|rt|d|dd}tj|}|j||jdg|j }|j |}|jj||js!|jjd|_ |jj||S)aR Subscribe to channels. Channels supplied as keyword arguments expect a channel name as the key and a callable as the value. A channel's callable will be invoked automatically when a message is received on that channel rather than producing a message via ``listen()`` or ``get_message()``. rrdN SUBSCRIBE)rr%r&rDr_rrrqrrrrrr)r0rPr~ new_channelsrs r!rzPubSub.subscribe,s Qab2D}}T* F#&$&&{I\5F5F5HI++L9  \*  ! ! % % '12D . ));;LIrc|r7t|d|dd}|jtj|}n |j}|j j ||jdg|S)zi Unsubscribe from the supplied channels. If empty, unsubscribe from all channels rrdN UNSUBSCRIBE)rrr%r&rqrrDr_)r0rPrqs r!rzPubSub.unsubscribeFsk Qab2D++DMM$,?@H}}H ))00:#t##M9D99rc#K|jr5|j|jd}|||jr4yyw)zBListen for messages on channels this client has been subscribed toT)rN)rhandle_messagerNrks r!rz PubSub.listenSs?oo**4+>+>T+>+JKH#oos AAAc|jsYtj}|jj|dur'tj|z }t d||z }ny|j d|}|r|j ||Sy)z Get the next message if one is available, otherwise None. If timeout is specified, the system will wait for `timeout` seconds before returning. Timeout should be specified as a floating point number. TgNF)rr+)rr$rwaitrrNr)r0rr+r  time_spentr s r! get_messagezPubSub.get_messageZsJ$$))'2d:"YY[:5 c7Z#78&&UG&D &&x1JK Krc2|dn|}|jd|S)z' Ping the Redis server rprr_)r0rs r!r0z PubSub.pingus" "W##FG44rc(|yt|d}|dk(r||d|d|dd}n|dk(r |dd|dd}n |d|d|dd}||jvr|d k(rK|d}||jvr|jj||jj |dnJ|d}||j vr7|j j||jj |d|js&|js|jj||jvrQ|dk(r |jj|d d}n|jj|d d}|r ||y|S|dk7r|s |jry|S) z Parses a pub/sub message. If the channel or pattern was subscribed to with a message handler, the handler is invoked instead of a parsed message being returned. Nrrrdrr)rUpatternchannelr1r1rrr) rUNSUBSCRIBE_MESSAGE_TYPESrremoverr!rrqrrPUBLISH_MESSAGE_TYPESr?r)r0r r message_typerrrhandlers r!rzPubSub.handle_message|s  #HQK0 : %$#A;#A;  G V #$  G%#A;  G 499 9~-"1+d???55<E--++GI,>E  V #)D,J,Jrc|jjD]\}}| td|d|jjD]\}}| td|dt ||||}|j |S)Nz Channel: 'z' has no handler registeredz Pattern: ')daemonexception_handler)rqr.rrPubSubWorkerThreadstart)r0 sleep_timerrrrrthreads r! run_in_threadzPubSub.run_in_threads $ 3 3 5 U GW!Jwi7R"STT U!% 3 3 5 U GW!Jwi7R"STT U$ *V?P    r)NFN)Tr)Frr$)F)rFN)!rErFrGrHrrrr4r:rCrFrr>rpropertyrr_rrrrNrrrrrrrrrr0rrrPrr!r2r26s4 ?2 "' 0 &($..L($   *   4?4 ;4 :65?B rr2c,eZdZdfd ZdZdZxZS)rct|||_||_||_||_t j|_yr$) r6r4rr3rrrr_running)r0r3rrrr9s r!r4zPubSubWorkerThread.__init__s9   $!2!) rc|jjry|jj|j}|j}|jjr/ |j d||jjr/|jy#t $r*}|j|j|||Yd}~Zd}~wwxYw)NT)rr+) rrrr3rr BaseExceptionrr>)r0r3res r!runzPubSubWorkerThread.runs ==   !  __ mm""$ 8""T:"Vmm""$   ! 8))1&&q&$77 8s)B(( C1 CCc8|jjyr$)rrr s r!stopzPubSubWorkerThread.stops r)FN)rErFrGr4rrrIrJs@r!rrs* rrceZdZdZhdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZddZdZdZdZy)ra_ Pipelines provide a way to transmit multiple commands to the Redis server in one transmission. This is convenient for batch processing, such as saving all the values in a list to Redis. All commands executed within a pipeline are wrapped with MULTI and EXEC calls. This guarantees all commands executed in the pipeline will be executed atomically. Any command raising an exception does *not* halt the execution of subsequent commands in the pipeline. Instead, the exception is caught and its instance is placed into the response list returned by execute(). Code iterating over the response list should be able to deal with an instance of an exception as a potential value. In general, these will be ResponseError exceptions, such as those raised when issuing a command on a key of a different datatype. >EXECDISCARDUNWATCHcx||_d|_||_||_||_d|_|j y)NF)rrrrrwatchingr)r0rrrrs r!r4zPipeline.__init__s8."4&$  rc|Sr$rPr s r!r:zPipeline.__enter__ r;rc$|jyr$rr?s r!rCzPipeline.__exit__ rDrcD |jy#t$rYywxYwr$rr s r!rFzPipeline.__del__s"  JJL   rc,t|jSr$)r command_stackr s r!__len__zPipeline.__len__s4%%&&rcy)z1Pipeline instances should always evaluate to TrueTrPr s r!__bool__zPipeline.__bool__srcg|_t|_|jrB|jr6 |jj d|jj d|_d|_ |jr-|jj|jd|_yy#t$r|jjYmwxYw)NrF) rrscriptsrrrMrbr rSexplicit_transactionrrIr s r!rzPipeline.resetsu  ==T__ -,,Y7--/  $)! ??  ( ( 9"DO # -**, -s5B--#CCcn|jr td|jr tdd|_y)z Start a transactional block of the pipeline after WATCH commands are issued. End the transactional block with `execute`. z"Cannot issue nested calls to MULTIz:Commands without an initial WATCH have already been issuedTN)rrrr s r!rzPipeline.multi4s<  $ $AB B   O %)!rc|js|ddk(r|js|j|i|S|j|i|S)NrWATCH)rrimmediate_execute_commandpipeline_execute_command)r0rPr~s r!r_zPipeline.execute_commandAsL MMT!W/9R9R14114B6B B,t,,d=f==rc|j|jr|jtd|jrt |t s|jy)z Close the connection, reset watching state and raise an exception if we were watching, retry_on_timeout is not set, or the error is not a TimeoutError =A ConnectionError occurred on while watching one or more keysN)rSrrrrrrrTs r!_disconnect_reset_raisez Pipeline._disconnect_reset_raiseFsV  == JJLR  %%*UL*I JJL +Jrcdjs-jjj_jj fdfdS)z Execute a command immediately, but don't auto-retry on a ConnectionError if we're already WATCHing a variable. Used when issuing WATCH or subsequent commands retrieving their values but before MULTI is called. rc2jgiSr$rYrZsr!rNz4Pipeline.immediate_execute_command..ks*5D55l%)-4rc(j|Sr$)r r\s r!rNz4Pipeline.immediate_execute_command..ns$66tUCr)rrrrrr])r0rPrrOrKs```@@r!rz"Pipeline.immediate_execute_command\s_Aw ''66|T__UD"DOzz))  D   rc@|jj||f|S)ar Stage a command to be executed when execute() is next called Returns the current Pipeline object back so commands can be chained together, such as: pipe = pipe.set('foo', 'bar').incr('baz').decr('bang') At some other point, you can then run: pipe.execute(), which will execute all commands queued in the pipe. )rru)r0rPrs r!rz!Pipeline.pipeline_execute_commandqs! !!4/2 rcvtdifg|difg}|j|Dcgc]\}}t|vs|c}}}|j|g} |j |dt|D]A\} } t| dvr|j | | dtf/ |j |dC |j |d} d|_ | td|D]\} } | j| | t| t|k7r%|jjt d|r|j!|| g} t#| |D]T\}}t%|t&s.|\}}|d}||j(vr|j(||fi|}| j |V| Scc}}w#t $r} |j d| fYd} ~ qd} ~ wwxYw#t $r7} |j| | dz| d|j | | fYd} ~ d} ~ wwxYw#t$r |r|ddwxYw) N)MULTI)rrrrdFzWatched variable changed.z6Wrong number of response items from pipeline execution)r pack_commandsrsend_packed_commandrNrru enumerateannotate_exceptionr rrinsertrrrSraise_first_errorrrrr)r0rreraise_on_errorcmdsrPrall_cmdsrrrrr r1rcmdrOs r!_execute_transactionzPipeline._execute_transactionsjz2&'YO3DE++'+ MmdG~W/LT M  &&x0  "    C 0 $H- *JAw+ q'!*^"<=>*'' C8  * **:s;H  89 9 "DAq OOAq ! " x=CM ) OO & & (K    " "8X 6(H- FAsa+ # g#Aw 4#:#::=// =aK7KA KKN   } N " MM1a& ! ! "%*++Aq1ugajAMM1a&))* Qil"  sFF0 F0 F6"G 7H#6 G?GG H ),HH #H8cX|j|Dcgc]\}}| c}}}|j|g}|D],\}} |j|j||dfi|.|r|j |||Scc}}w#t$r} |j| Yd} ~ jd} ~ wwxYwr)rrrurNrr) r0rrerrPrrr rrs r!_execute_pipelinezPipeline._execute_pipelines++,JgdAT,JK&&x0% #MD' # 3 3 3JQ S7 ST #   " "8X 6-K! #"" #s A? &B B)B$$B)ct|D]3\}}t|ts|j||dz||d|y)Nrdr)rrrr)r0rer rrs r!rzPipeline.raise_first_errorsGh' DAq!]+''1q5(1+a.A rcdjtt|}d|d|d|jd}|f|jddz|_y)Nrz Command # z (z) of pipeline caused error: rrd)rrrrP)r0 exceptionnumberrrmsgs r!rzPipeline.annotate_exceptions]hhs8W-.3%(&^^A./ 1 ).."44 rc |tj|||fi|}||jvr d|_|S|dk(rd|_|S)NFrT)rrNUNWATCH_COMMANDSr)r0rrOrrs r!rNzPipeline.parse_responsesM%%dJ PP 400 0!DM W $ DM rc t|j}|j}|Dcgc]}|j}}|dg|}t |s0t ||D] \}}|r |d|j |_"yycc}w)Nrr)rrrshaallrscript)r0r immediatesshasexistsexists r! load_scriptszPipeline.load_scriptsst||$22 &'!''?2T26{0 ?5%mQXX>AE ? (sBc|j|jr td|jrt |t s|j y)z Close the connection, raise an exception if we were watching, and raise an exception if retry_on_timeout is not set, or the error is not a TimeoutError r N)rSrrrrrrrTs r!_disconnect_raise_resetz Pipeline._disconnect_raise_resetsM  ==R  %%*UL*I JJL +JrcjsjsgSjrjjs j r j n jjs-jjdj_ jjfdfdjS#jwxYw)z0Execute all the commands in the current pipelinercSr$rP)rKr#rstacksr!rNz"Pipeline.execute..se^<rc(j|Sr$)r1r\s r!rNz"Pipeline.execute.. sd::4Gr)rrrr/rrrrrrrrrr]r)r0rrKr#r4s``@@@r!r#zPipeline.execute s""T]]I <<       t88//G,,G''66wPD#DO ::--<G JJLDJJLs +%C!!C3c&|jdy)zg Flushes all previously queued commands See: https://redis.io/commands/DISCARD rNrr s r!discardzPipeline.discard%s Y'rcV|jr td|jdg|S)z$Watches the values at keys ``names``z"Cannot issue a WATCH after a MULTIr)rrr_)r0namess r!r"zPipeline.watch,s/  $ $AB B#t##G4e44rcH|jxr|jdxsdS)z'Unwatches all previously specified keysrT)rr_r s r!unwatchzPipeline.unwatch2s!}}@!5!5i!@HDHrN)T)rErFrGrHr%r4r:rCrFrrrrr_r rrrrrrrNr/r1r#r7r"r;rPrr!rrs$6 '#. )> , * AF  5 ?(6(5 Irr)FF)_rrrrr$r itertoolsrredis.commandsrrrrredis.connectionrr r redis.exceptionsr r r rrrrr redis.lockr redis.utilsrr SYM_EMPTYrrr"r*r%r,rYr^ryrrrrrrrrr}rrrr\rrrrrrrrrrrrrrrrrrrrr r3r-rDrZrhrjrmrwr}ryrrrr StrictRedisr5r2ThreadrrrPrr!rEs    WV   .  ! 57$6$)XsCs  C  # #scS33#3 c!" C#$/6  =P@!&  3: 5<' "? /  *6  5 @O 3$":K *(KRB&5* "J$@ 7]]@cM. >NcL  @&@&FSSl ))>IIuIIr