
    <i;              
          d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
mZmZmZmZ d dlZd dlmZ d dlmZmZmZmZ d dlmZmZmZmZmZ d dlmZ ddd	d
dZ  ej!        dd          Z" ej#                    5   ej$        e e"                   Z%ddd           n# 1 swxY w Y    ej&        d          Z'd ddddZ(ej)        ej*        ej*        ej+        dZ,de	de-de-de-fdZ.de%j/        de
e         ddfdZ0de%j/        de
e         de1deddf
dZ2 e3            Z4dedefd Z5dedefd!Z6de%j/        de
e         d"ede7fd#Z8de%j/        de
e         d$e-defd%Z9 G d& d'          Z:dS )(    N)CallableAnyStrSetAnyTupleListOptional)_msgs)commandInt	SignatureFloat)SimpleErrorSimpleStringnull_terminateOKdecode_command_bytes)VersionTypez
lupa.lua51z
lupa.lua52z
lupa.lua53z
lupa.lua54)5.1z5.2z5.3z5.4FAKEREDIS_LUA_VERSIONr   	fakeredis         )s	   LOG_DEBUGs   LOG_VERBOSEs
   LOG_NOTICEs   LOG_WARNING)r   r   r   r   sencoding
replaceerrreturnc                 ~    t          | t                    r|                     ||          }nt          |           }|S )N)r   errors)
isinstancebytesdecodestr)r   r   r   ress       /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/fakeredis/commands_mixins/scripting_mixin.py_ensure_strr'   1   s;    !U hhh<<!ffJ    lua_runtimeexpected_globalsc                 .   t          |                                                                           |z
  }t          |          dk    rKd |D             }t	          t
          j                            d                    |                              d S )Nr   c                 0    g | ]}t          |d d          S )utf-8replace)r'   ).0vars     r&   
<listcomp>z*_check_for_lua_globals.<locals>.<listcomp><   s$    YYYsk#w	::YYYr(   z, )	setglobalskeyslenr   msgsGLOBAL_VARIABLE_MSGformatjoin)r)   r*   unexpected_globals
unexpecteds       r&   _check_for_lua_globalsr<   9   s    [00227799::=MM
""YYFXYYY
$299$))J:O:OPPQQQ #"r(   lvlargsc                    t          | |           t          |          dk     r-t          t          j                            dd                    |t                                          vrt          t          j                  d	                    d |D                       }t                              t          |         |           d S )Nr   zredis.log()two c                     g | ]P}t          |t                    t          |t                    r|                    d           nt	          |          QS )r-   )r!   boolr"   r#   r$   )r/   xs     r&   r1   z"_lua_redis_log.<locals>.<listcomp>F   sP    sssa_ijkmq_r_rsAu)=)=IAHHW%%%3q66sssr(   )r<   r5   r   r6   REQUIRES_MORE_ARGS_MSGr8   REDIS_LOG_LEVELS_TO_LOGGINGr4   LOG_INVALID_DEBUG_LEVEL_MSGr9   LOGGERlog)r)   r*   r=   r>   msgs        r&   _lua_redis_logrK   @   s    ;(8999
4yy1}}$5<<]ERRSSS
-224444$:;;;
((ssSWsss
t
tC
JJ*3/55555r(   objc                    | t           S t          | t                    r|                                 S t          | t                    rd | D             S t          | t
                    rd |                                 D             S | S )ziConvert a pure python object obtained after JSON deserialization into a usable object in the lua runtime.Nc                 ,    g | ]}t          |          S  _cjson_python_to_luar/   items     r&   r1   z(_cjson_python_to_lua.<locals>.<listcomp>T   s!    ;;;t$T**;;;r(   c                 N    i | ]"\  }}t          |          t          |          #S rO   rP   r/   keyvalues      r&   
<dictcomp>z(_cjson_python_to_lua.<locals>.<dictcomp>V   s1    eee:3PU$S))+?+F+Feeer(   )_lua_cjson_nullr!   r$   encodelistdictitems)rL   s    r&   rQ   rQ   M   s    
{#s zz||#t <;;s;;;;#t feeY\YbYbYdYdeeeeJr(   c           	      8   | t           u rdS t          | t                    r|                                 S t                              |           }|dk    r| S t          |                                           }t          d |D                       o=t          |          t          t          dt          |          dz                       k    }|rd |                                 D             S t          |           }d |                                D             S )zaConvert a passed lua runtime object obtained before JSON serialization into a pure python object.Ntablec              3   @   K   | ]}t          |t                    V  d S N)r!   int)r/   ks     r&   	<genexpr>z'_cjson_lua_to_python.<locals>.<genexpr>i   s,      44!:a%%444444r(   r   c                 ,    g | ]}t          |          S rO   _cjson_lua_to_pythonrR   s     r&   r1   z(_cjson_lua_to_python.<locals>.<listcomp>l   s!    DDDt$T**DDDr(   c                 N    i | ]"\  }}t          |          t          |          #S rO   rf   rU   s      r&   rX   z(_cjson_lua_to_python.<locals>.<dictcomp>p   s0    ___zsE %%';E'B'B___r(   )rY   r!   r"   r#   
LUA_MODULElua_typer[   r4   allsortedranger5   valuesr\   r]   )rL   rj   r4   is_arrayds        r&   rg   rg   Z   s   
ot#u zz||""3''H7

 

D44t44444feTUWZ[_W`W`cdWdNeNeIfIf9fH EDDszz||DDDD 	S		A__UVU\U\U^U^____r(   rW   c                     t          | |           t          |          }t          j        |d                                          S )N),:)
separators)r<   rg   jsondumpsrZ   )r)   r*   rW   s      r&   _lua_cjson_encoderw   s   sA    ;(8999 ''E:e
333::<<<r(   json_strc                     t          | |           t          j        |          }t          |          }t	          |t
          t          f          r|                     |d          }|S )NT)	recursive)r<   ru   loadsrQ   r!   r\   r[   
table_from)r)   r*   rx   json_objs       r&   _lua_cjson_decoder~   y   sc    ;(8999z(##H#H--H(T4L)) D))(d)CCOr(   c            
       j    e Zd ZU eegeeedef                  ef         f         e	d<   eedef         ee
e         egef         e	d<   dedef fdZdej        ded	efd
Zdej        ded	efdZd)deded	efdZdej        dee         deded	ef
dZdej        dee         deded	ef
dZ eeefefej                  dededed	efd            Z edeefefej                  dededed	efd            Z edefefej                  ded	efd            Z edd efej                  ded	e
e         fd!            Z ed"d efej                  ded	efd#            Z  ed ej                  ded	d$fd%            Z! ed&d '          ded	e
e         fd(            Z" xZ#S )*ScriptingCommandsMixin._name_to_func_run_commandr>   kwargsc                    |  t                      | _        |                    dd           pt                      }t          |          dk    rt                              d d          }|D ]}	 |                    |                                           | j                            |           E# t          j	        $ r*}t                              d| d|            Y d }~yd }~ww xY w t          t          |           j        |i | d S )Nlua_modulesr   Tr   unpack_returned_tupleszFailed to load LUA module "z", make sure it is installed: )r2   load_lua_modulespopr5   ri   
LuaRuntimerequirerZ   addLuaErrorrH   errorsuperr   __init__)selfr>   r   lua_modules_setr)   moduleex	__class__s          r&   r   zScriptingCommandsMixin.__init__   s/   ! #$*JJ}d$C$C$Lsuu!##1;1F1FPTmq1F1r1rK) k kk''888)--f5555!* k k kLL!iv!i!ieg!i!ijjjjjjjjk 	5$d++4dEfEEEEEs   /AB11C*  C%%C*r)   rW   r   c                     t          |          t          u r|S t          |          t          t          hv r'd                    |                                          S t          t          j                  )Nz{:.17g})	typer"   rb   floatr8   rZ   r   r6   LUA_COMMAND_ARG_MSG)r   r)   rW   s      r&   _convert_redis_argz)ScriptingCommandsMixin._convert_redis_arg   s^    ;;%L%[[S%L((##E**11333d6777r(   resultc                 2    t          |t          t          f          r|S t          |t                    rt	          j        |d          S t          |t                    r                    d|j        i          S |dS t          |t                    r$ fd|D             }                    |          S t          |t                    rQt          t          j        |                                           } fd|D             }                    |          S t          |t                    r5|j                            d          rt          t           j                  |t%          d                    t)          |                              )NF)humanfriendly   okc                 <    g | ]}                     |          S rO   _convert_redis_resultr/   rS   r)   r   s     r&   r1   z@ScriptingCommandsMixin._convert_redis_result.<locals>.<listcomp>   )    ZZZ433KFFZZZr(   c                 <    g | ]}                     |          S rO   r   r   s     r&   r1   z@ScriptingCommandsMixin._convert_redis_result.<locals>.<listcomp>   r   r(   zERR wrong number of argumentsz%Unexpected return type from redis: {})r!   r"   rb   r   r   rZ   r   r|   rW   r[   r\   	itertoolschainr]   r   
startswithr6   WRONG_ARGS_MSG7RuntimeErrorr8   r   )r   r)   r   	converteds   ``  r&   r   z,ScriptingCommandsMixin._convert_redis_result   s   fucl++ 	Mfe$$ 	5<e<<<<-- 	5))5&,*?@@@^5%% 	5ZZZZZSYZZZI)))444fd## 		])/6<<>>:;;FZZZZZSYZZZI)))444,, 	]|&&'FGG 8!$"6777LFMMdSYll[[\\\r(   Tnestedc                    t                               |          dk    rdD ]}||v r|                     ||                   }t          |t                    st          t          j                  |dk    rt          |          c S |r%t          |	                    dd                    c S t          |	                    dd                    g }t          j        d          D ]8}||vr n1||         }|                    |                     |                     9|S t          |t                    r|                                S t          |t                    rt!          |          S t          |t"                    r|rdnd S |S )Nr_   )r      errr   r-   r.   r   )ri   rj   _convert_lua_resultr!   r"   r   r6   LUA_WRONG_NUMBER_ARGS_MSGr   r#   r   countappendr$   rZ   r   rb   rC   )r   r   r   rV   rJ   result_listindexrS   s           r&   r   z*ScriptingCommandsMixin._convert_lua_result   s   v&&'11& 
J 
J&==226#;??C%c511 J)$*HIIIe||+C00000 J*3::gy+I+IJJJJJ)#**Wi*H*HIII ! K"++ C C&&Ee}""4#;#;D#A#ABBBB$$ 	)==??"&& 	)v;;%% 	)(11D(r(   r*   opc                      t          |                                t          |                    \  }} fd|D             }                     |||d          }                     |          }|S )Nc                 <    g | ]}                     |          S rO   )r   )r/   argr)   r   s     r&   r1   z:ScriptingCommandsMixin._lua_redis_call.<locals>.<listcomp>   s)    NNN#D++K==NNNr(   T)r<   r   r   r   r   )	r   r)   r*   r   r>   funcsignew_argsr   s	   ``       r&   _lua_redis_callz&ScriptingCommandsMixin._lua_redis_call   s     	{,<===&&';B'?'?@@	cNNNNNNNN""4h==++K@@r(   c                     	  | j         |||g|R  S # t          $ r.}|                    dt          |          i          cY d }~S d }~ww xY w)Nr   )r   	Exceptionr|   r$   )r   r)   r*   r   r>   r   s         r&   _lua_redis_pcallz'ScriptingCommandsMixin._lua_redis_pcall   st    	='4'5ErQDQQQQ 	= 	= 	=))63r77*;<<<<<<<<	=s    
A
#AA
A
)flagsscriptnumkeyskeys_and_argsc                 x   |t          |          k    rt          t          j                  |dk     rt          t          j                  t          j        |                                                                          }|| j	        j
        |<   t                              d d          }d                    d | j        D                       }|                    d| d          }t!                      } ||                    |d |                   |                    ||d                    t%          j        | j        ||          t%          j        | j        ||          t%          j        t,          ||          t                              t0                    t%          j        t2          ||          t%          j        t4          ||          t6          	  	         |                    |                                                                           	 |                    |          }	nb# t          $ r}
|
j         t          j!        k    r| j"        dk     rt          t          j#                  | j	        j$        d	k    r>t          t          j%        &                    |'                                                    t          t          j!                  | j"        dk     r?t          t          j(        &                    |'                                |
                    t          |
j                   d }
~
wt          j)        $ rD}
t          t          j(        &                    |'                                |
                    d }
~
ww xY wtU          ||           | +                    |	d
          S )Nr   Tr   
c                     g | ]
}| d | dS )z = require('z')rO   )r/   r   s     r&   r1   z/ScriptingCommandsMixin.eval.<locals>.<listcomp>   s)    'n'n'nf6(I(Iv(I(I(I'n'n'nr(   a  
            function(keys, argv, redis_call, redis_pcall, redis_log, redis_log_levels, cjson_encode, cjson_decode, cjson_null)
                redis = {}
                redis.call = redis_call
                redis.pcall = redis_pcall
                redis.log = redis_log
                for level, pylevel in python.iterex(redis_log_levels.items()) do
                    redis[level] = pylevel
                end
                redis.error_reply = function(msg) return {err=msg} end
                redis.status_reply = function(msg) return {ok=msg} end

                cjson = {}
                cjson.encode = cjson_encode
                cjson.decode = cjson_decode
                cjson.null = cjson_null

                KEYS = keys
                ARGV = argv
                z
            end
               valkeyF)r   ),r5   r   r6   TOO_MANY_KEYS_MSGNEGATIVE_KEYS_MSGhashlibsha1	hexdigestrZ   _serverscript_cacheri   r   r9   r   evalr2   r|   	functoolspartialr   r   rK   as_attrgetterREDIS_LOG_LEVELSrw   r~   rY   updater3   r4   executerW   r   versionLUA_COMMAND_ARG_MSG6server_typeVALKEY_LUA_COMMAND_ARG_MSGr8   r#   SCRIPT_ERROR_MSGr   r<   r   )r   r   r   r   r   r)   modules_import_strset_globalsr*   r   r   s              r&   r   zScriptingCommandsMixin.eval   sH   S''''d4555Q;;d4555|F##--//6688*0!$'-7-B-BDim-B-n-n!YY'n'nX\Xm'n'n'noo!&&& $'  
 
0 &)UU""='#:;;""=#:;;d2KAQRRd3[BRSSnk;KLL$$%566/>NOO/>NOO
	
 
	
 
	
 	 3 3 5 5 : : < <===	O ((00FF 
	( 
	( 
	(x4333<$&&%d&?@@@\-99%d&E&L&LT[[]]&[&[\\\%d&>???|d""!$"7">">t{{}}b"Q"QRRRbh'''" 	O 	O 	Od3::4;;=="MMNNN	O 	{,<===''u'===s%   H3 3N>C>L<<N?NNEVALSHA)namefixedrepeatr   r   c                     	 | j         j        |         }n'# t          $ r t          t          j                  w xY w | j        ||g|R  S ra   )r   r   KeyErrorr   r6   NO_MATCHING_SCRIPT_MSGr   )r   r   r   r   r   s        r&   evalshazScriptingCommandsMixin.evalsha)  s`    	;\.t4FF 	; 	; 	;d9:::	;ty9=9999s    $9zSCRIPT LOADc                 "   t          |          dk    r,t          t          j                            d                    |d         }t          j        |                                                                          }|| j	        j
        |<   |S )Nr   SCRIPTr   )r5   r   r6   BAD_SUBCOMMAND_MSGr8   r   r   r   rZ   r   r   )r   r>   r   r   s       r&   script_loadz"ScriptingCommandsMixin.script_load1  st    t99>>d5<<XFFGGGa|F##--//6688*0!$'r(   zSCRIPT EXISTSrO   c                       j         dk    r,t          |          dk    rt          t          j                   fd|D             S )Nr   r   c                 F    g | ]}t          |j        j        v           S rO   )rb   r   r   )r/   r   r   s     r&   r1   z8ScriptingCommandsMixin.script_exists.<locals>.<listcomp>>  s+    HHH4DDL5566HHHr(   )r   r5   r   r6   r   r   r>   s   ` r&   script_existsz$ScriptingCommandsMixin.script_exists:  sI    <4CIINNd2333HHHH4HHHHr(   zSCRIPT FLUSHc                     t          |          dk    s*t          |          dk    rCt          |d                   dvr,t          t          j                            d                    i | j        _        t          S )Nr   r   >      sync   asyncr   )	r5   r   r   r6   r   r8   r   r   r   r   s     r&   script_flushz#ScriptingCommandsMixin.script_flush@  sd    t99q==SYY!^^tAw0G0GOb0b0bd5<<XFFGGG$&!	r(   Nc                 Z    t          t          j                            d                    )Nr   )r   r6   r   r8   r   s     r&   r   zScriptingCommandsMixin.scriptG  s!    $188BBCCCr(   zSCRIPT HELP)r   r   c                 Z    ddddddddd	d
dddddd| j         dk     rdndg}d |D             S )Nz?SCRIPT <subcommand> [<arg> [value] [opt] ...]. Subcommands are:zDEBUG (YES|SYNC|NO)z7    Set the debug mode for subsequent scripts executed.zEXISTS <sha1> [<sha1> ...]zN    Return information about the existence of the scripts in the script cache.zFLUSH [ASYNC|SYNC]z<    Flush the Lua scripts cache. Very dangerous on replicas.zU    When called without the optional mode argument, the behavior is determined by thezF    lazyfree-lazy-user-flush configuration directive. Valid modes are:z4    * ASYNC: Asynchronously flush the scripts cache.z2    * SYNC: Synchronously flush the scripts cache.KILLz,    Kill the currently executing Lua script.zLOAD <script>z>    Load a script into the scripts cache without executing it.HELP)r   r   z    Prints this help.z    Print this help.c                 6    g | ]}|                                 S rO   )rZ   )r/   r   s     r&   r1   z6ScriptingCommandsMixin.script_help.<locals>.<listcomp>a  s     111q

111r(   )r   )r   r>   help_stringss      r&   script_helpz"ScriptingCommandsMixin.script_helpK  sh     N!E(\ JcTB@:L(,v(=(=$$CY#
( 21L1111r(   )T)$__name__
__module____qualname__r   r$   r   r	   r   r   __annotations__r   rC   r   ri   r   r"   r   r   r   r   r   r   r   r   r6   FLAG_NO_SCRIPTrb   r   r   r   r   r   r   r   r   __classcell__)r   s   @r&   r   r      s        SE5(382D)Ey)P#QQRRRRHS#X.	49dKSPQQQQFc FS F F F F F F8j.C 8C 8TY 8 8 8 8]1F ]PS ]X[ ] ] ] ]. # t s    :	%0	DGH	RW	`c			 	 	 	=%0=DGH=RW=`c=	= = = = WeS\E84+>???A>5 A>3 A> A># A> A> A> @?A>F W)E3<H[\\\:E :C : :3 : : : ]\: W-xH[\\\ 5    ]\ W/UHDDWXXXI5 IT#Y I I I YXI
 W.E84CVWWW% L    XW WRt*+++DE Dd D D D ,+D W-r***2 24; 2 2 2 +*2 2 2 2 2r(   r   );r   r   	importlibr   ru   loggingostypingr   r   r   r   r   r   r	   lupar   r
   r6   fakeredis._commandsr   r   r   r   fakeredis._helpersr   r   r   r   r   fakeredis._typingr   __LUA_RUNTIMES_MAPgetenvLUA_VERSIONallow_lua_module_loadingimport_moduleri   	getLoggerrH   r   DEBUGINFOWARNINGrF   r$   r'   r   r<   rb   rK   objectrY   rQ   rg   r"   rw   r~   r   rO   r(   r&   <module>r     s                  				 D D D D D D D D D D D D D D D D D D  # # # # # # > > > > > > > > > > > >              * ) ) ) ) ) 	   bi/77"T"$$ J J(();K)HIIJJ J J J J J J J J J J J J J J 
	;	'	'	   }||	  6 S c c    R
(= RQTUXQY R^b R R R R6
 5 6S 6X[ 6dg 6lp 6 6 6 6 &((
c 
c 
 
 
 
`c `c ` ` ` `2=:#8 =CPSH =]` =ej = = = =:#8 CPSH `c hk    _2 _2 _2 _2 _2 _2 _2 _2 _2 _2s    B##B'*B'