
    Zi=                        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 d dl	m
Z
mZmZ d dlmZmZ d dl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mZmZmZmZ d d
lm Z m!Z! d dl"m#Z# d dl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ d dl,m-Z-m.Z.m/Z/ d dl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: d dl;m<Z<m=Z=m>Z>m?Z? d dl@mAZA 	 d dlBZBn# eC$ r d dlDZBY nw xY wejE        eefZFdZGd ZHdeIeJe
f         fdZK G d deL          ZMd ZNd ZOdS )    N)datetime	timedelta)AnyOptionalUnion)UUIDuuid4)tzutc)string_types)Consumer)ExceptionCapture)exc_info_from_errorexceptions_from_error_tuplehandle_in_appexception_is_already_capturedmark_exception_as_captured)InconclusiveMatchErrormatch_feature_flag_properties)Poller)DEFAULT_HOSTAPIError
batch_postdetermine_server_hostflagsgetremote_config)_get_current_contextget_context_distinct_idget_context_session_id)
FeatureFlagFeatureFlagResultFlagMetadataFlagsAndPayloadsFlagsResponse	FlagValuenormalize_flags_responseto_flags_and_payloadsto_payloads	to_values)SizeLimitedDictcleanguess_timezoneremove_trailing_slash)VERSIONiP  c                  ~   d} d}t           j        }|                    d          r5d} t          t          d          rt          j                    d         }|r|}n|dk    r5d} t          t          d          rt          j                    d         }|r|}n|                    d	          r&d
} t          j                    }|d         r|d         }nj|                    d          r+d} t          t          d          rt          j                    }n*|} t          t          d          rt          j                    }| |fS )zr
    Returns standardized OS name and version information.
    Similar to how user agent parsing works in JS.
     winWindows	win32_verr   darwinzMac OS Xmac_verlinuxLinuxversionfreebsdFreeBSDrelease)	sysplatform
startswithhasattrr3   r5   distroinfor;   )os_name
os_versionplatform_namewin_versionmac_version
linux_infos         I/root/projects/butler/venv/lib/python3.11/site-packages/posthog/client.pyget_os_inforI   B   sR   
 GJLM&& ,8[)) 	)",..q1K )(
	(	"	"8Y'' 	)"*,,Q/K )(
		!	!'	*	* ,[]]
i  	/#I.J		!	!)	,	, ,8Y'' 	,!)++J  8Y'' 	,!)++JJ    returnc                  ~    t                      \  } }t          j                    dt          j        d d         z  | |dS )Nz%s.%s.%s   )z$python_runtimez$python_versionz$osz$os_version)rI   r=   python_implementationr<   version_info)rB   rC   s     rH   system_contextrP   m   sG    %--GZ $9;;%)9"1")=>!	  rJ   c                      e Zd ZdZ ej        d          Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dEdZed             Z	e	j
        d             Z		 	 	 	 	 	 dFdZ	 	 	 	 dGdeeeeef         f         fdZ	 	 	 	 dGdeeef         fdZ	 	 	 	 dGdefdZ	 	 	 	 dGdefdZ	 	 	 	 	 	 	 	 	 dHdZ	 	 	 	 	 	 dFdZ	 	 	 	 	 	 dFdZ	 	 	 	 	 	 	 	 dIdZ	 	 	 	 	 	 dFdZ	 	 	 	 	 	 	 dJdZ	 	 	 	 	 	 	 dJdZd Zd Zd Zd  Z d! Z!d" Z"i i i dd#de#fd$Z$i i i dddd%d&Z%di i i dddd'd(e&e#         de&e'         fd)Z(i i i dddd%de&e'         fd*Z)i i i dddd%de&e#         fd+Z*d,ed-ed.eeef         d/eeef         d0eeef         de&e#         fd1Z+di i i dddd2d3e&e#         fd4Z,d,ed-ed.eeef         d/eeef         d0eeef         d5e&e         de-e&e.         e&e         f         fd6Z/d-ed,ed7e&e#         d8e&e         d9ed.eeef         d5e&e         d:e&e         d;e&e.         fd<Z0d,efd=Z1d,ed3e#de&e         fd>Z2i i i ddd?de&eeeeef         f                  fd@Z3i i i ddd?defdAZ4i i i dd#de-eef         fdBZ5dC Z6dD Z7dS )KClientzCreate a new PostHog client.posthogNF'  Td         ?rM            c                 V   t          j        |          | _         |p|| _        t          d| j        t                     || _        || _        || _        || _        |pt          | _
        t          |          | _        |	| _        || _        d | _        d | _        d | _        d | _        || _        || _        d | _        t-          t.          t0                    | _        || _        || _        || _        || _        || _        || _        || _         d | _!        || _"        |'	 tG          j$                    }n# tJ          $ r d }Y nw xY w|| _&        || _'        |r8tQ          j)                     | j*        +                    tP          j,                   n$| j*        +                    tP          j-                   |9t]          |          r|| _/        n)| j*        0                    d           d | _/        nd | _/        | j        rtc          | | j                   | _!        |r	d | _2        d S |rtg          j4        | j5                   tm          |          D ]a}g | _2        to          | j         | j        | j        ||||	|
||
  
        }| j2        8                    |           |r|9                                 bd S )Napi_keyz/before_send is not callable, it will be ignored)integrations)hoston_errorflush_atflush_intervalgzipretriestimeouthistorical_migration):queueQueuer[   requirer   r^   debugsend	sync_moder   raw_hostr   r]   ra   rc   _feature_flagsfeature_flags_by_keygroup_type_mappingcohortspoll_interval%feature_flags_request_timeout_secondspollerr*   MAX_DICT_SIZEset#distinct_ids_feature_flags_reporteddisableddisable_geoiprd   super_propertiesenable_exception_autocapturelog_captured_exceptions"exception_autocapture_integrationsexception_captureprivacy_modeosgetcwd	Exceptionproject_rootpersonal_api_keyloggingbasicConfiglogsetLevelDEBUGWARNINGcallablebefore_sendwarningr   	consumersatexitregisterjoinranger   appendstart)selfr[   r]   rh   max_queue_sizeri   r^   r_   r`   ra   max_retriesrj   rc   threadrp   r   project_api_keyrv   rw   rd   rq   rx   ry   rz   r{   r   r}   r   nconsumers                                 rH   __init__zClient.__init__}   s   < [00
 '1'	4<666 
	",)$//		"$(!"&*1 	2 3B=RU3V3V0 *$8! 0,H)'>$2T/!%($!y{{ $ $ $#$ ) !1 	/ !!!Hgm,,,,Hgo..."$$ (#.    !RSSS#'  #D, 	%54#J& & &D"  	%!DNNN  +	***6]] % %!##JL%%#1'#)=   %%h///  %NN$$$%% %s   D0 0D?>D?c                     | j         S )z9
        Get the local evaluation feature flags.
        )rl   r   s    rH   feature_flagszClient.feature_flags   s    
 ""rJ   c                 f    |pg | _         d | j         D             | _        | j        
J d            dS )z9
        Set the local evaluation feature flags.
        c                 J    i | ] }|                     d           |d          |!S )keyr   ).0flags     rH   
<dictcomp>z(Client.feature_flags.<locals>.<dictcomp>  s8     %
 %
 %
xx* K***rJ   NDfeature_flags_by_key should be initialized when feature_flags is set)rl   rm   )r   r   s     rH   r   zClient.feature_flags  sV    
 $kr%
 %
+%
 %
 %
!
 (44R 54444rJ   c                 @   |t          j        dt          d           |t                      }|pi }t	          d|t
                     t	          d|t                     d|vrt                      rt                      |d<   |||d|d}|                     ||          S )	NNThe 'context' parameter is deprecated and will be removed in a future version.   
stackleveldistinct_id
properties$session_idz	$identify	timestampr   $seteventuuid)	warningswarnDeprecationWarningr   rg   ID_TYPESdictr   _enqueuer   r   r   contextr   r   rw   msgs           rH   identifyzClient.identify  s     M`"    133K%2
{H555j$///
**/E/G/G*(>(@(@J}% #& 
 
 }}S-000rJ   rK   c                 V    |                      |||||          }t          |          pi S )zP
        Get feature flag variants for a distinct_id by calling decide.
        )get_flags_decisionr)   r   r   groupsperson_propertiesgroup_propertiesrw   	resp_datas          rH   get_feature_variantszClient.get_feature_variants9  s;     ++!24Dm
 
	 ##)r)rJ   c                 V    |                      |||||          }t          |          pi S )zP
        Get feature flag payloads for a distinct_id by calling decide.
        )r   r(   r   s          rH   get_feature_payloadszClient.get_feature_payloadsI  s;     ++!24Dm
 
	 9%%++rJ   c                 R    |                      |||||          }t          |          S )zU
        Get feature flags and payloads for a distinct_id by calling decide.
        )r   r'   )r   r   r   r   r   rw   resps          rH   get_feature_flags_and_payloadsz%Client.get_feature_flags_and_payloadsY  s6     &&!24Dm
 
 %T***rJ   c                    |t                      }t          d|t                     || j        }|rt          d|t                     ni }|||||d}t          | j        | j        fd| j        i|}t          |          S )zd
        Get feature flags decision, using either flags() or decide() API based on rollout.
        Nr   r   )r   r   r   r   geoip_disablerc   )
r   rg   r   rw   r   r   r[   r]   rq   r&   )r   r   r   r   r   rw   request_datar   s           rH   r   zClient.get_flags_decisioni  s     133K{H555  .M 	Hfd++++F '!2 0*
 
 LI
 
 >
 	
 
	 (	222rJ   c
                    |t          j        dt          d           i |pi t                      }d|vrt	                      rt	                      |d<   |t                      }t          d|t                     t          d|t                     t          d|t                     t                      }
|
r+|
                                }|                    |           |}|||||d}|r!t          d	|t                     ||d         d
<   i }i }|rN	 |                     |||	          }n\# t          $ r'}| j                            d|            Y d }~n0d }~ww xY w| j        r!|dk    r|                     ||pi |	d          }|pi                                 D ]\  }}||d| <   d |pi                                 D             }|r||d<   |ri ||d         |d<   |                     ||	          S )Nr   r   r   r   r   r   r   )r   r   r   r   r   r   z$groups)rw   z0[FEATURE FLAGS] Unable to get feature variants: $feature_flag_calledT)r   rw   only_evaluate_locally	$feature/c                      g | ]\  }}|d u	|S )F )r   r   values      rH   
<listcomp>z"Client.capture.<locals>.<listcomp>  s1      
  
  
eE!! !!!rJ   z$active_feature_flags)r   r   r   rP   r   r   rg   r   r   r   r   collect_tagsupdater   r   r   	exceptionr   get_all_flagsitemsr   )r   r   r   r   r   r   r   r   send_feature_flagsrw   current_contextcontext_tagsr   extra_propertiesfeature_variantsefeaturevariantactive_feature_flagss                      rH   capturezClient.capture  s    M`"    @)r?n.>.>?

**/E/G/G*(>(@(@J}%133K{H555j$///---.00 	&*7799L
+++%J %"&
 
  	2Hfd++++1Ci(+-BD 	#'#<#<} $= $ $      ""JqJJ       
  	E-C$C$C#11"+&*	  2     "2!7R > > @ @ 	> 	>GW6=22233 
  
!1!7R > > @ @ 
  
  

   	M8L45 	J I#3 Is<7H IC}}S-000s   D. .
E8EEc                     |t          j        dt          d           |t                      }|pi }t	          d|t
                     t	          d|t                     |||d|d}|                     ||          S )Nr   r   r   r   r   r   r   r   r   r   r   rg   r   r   r   r   s           rH   rt   z
Client.set  s     M`"    133K%2
{H555j$/// #&
 
 }}S-000rJ   c                     |t          j        dt          d           |t                      }|pi }t	          d|t
                     t	          d|t                     |||d|d}|                     ||          S )Nr   r   r   r   r   	$set_once)r   r   r   r   r   r   r   s           rH   set_oncezClient.set_once
  s     M`"    133K%2
{H555j$/// #&# 
 
 }}S-000rJ   c	                 l   |t          j        dt          d           |pi }t          d|t                     t          d|t                     t          d|t
                     |rt          d|t                     nd                    ||          }d	|||d
|||d}	|                     |	|          S )Nr   r   r   
group_type	group_keyr   r   z${}_{}z$groupidentify)z$group_type
$group_keyz
$group_set)r   r   r   r   r   )r   r   r   rg   r   r   formatr   )
r   r   r   r   r   r   r   rw   r   r   s
             rH   group_identifyzClient.group_identify+  s     M`"   
  %2
j(333Y111j$/// 	AM;9999"//*i@@K &)'( 
 '"

 

 }}S-000rJ   c                     |t          j        dt          d           |t                      }t	          d|t
                     t	          d|t
                     ||d|d|d}|                     ||          S )	Nr   r   r   previous_idr   )r   aliasz$create_alias)r   r   r   r   )r   r   r   r   rg   r   r   )r   r   r   r   r   r   rw   r   s           rH   r   zClient.aliasT  s     M`"    133K{H555{H555  +$  #$&
 
 }}S-000rJ   c                 v   |t          j        dt          d           |t                      }|pi }t	          d|t
                     t	          d|t                     d|vrt                      rt                      |d<   t	          d|t                     ||d<   d	||||d
}| 	                    ||          S )Nr   r   r   r   r   r   urlz$current_urlz	$pageview)r   r   r   r   r   )
r   r   r   r   rg   r   r   r   r   r   )	r   r   r   r   r   r   r   rw   r   s	            rH   pagezClient.pagev  s     M`"    133K%2
{H555j$///
**/E/G/G*(>(@(@J}%sL)))%(
>" !$"&
 
 }}S-000rJ   c           	         |t          j        dt          d           |t                      }	 |pi }|+t	          |          r| j                            d           d S |d|d<   t                      }t          d|t                     t          d|t                     |t          |          }	nt          j                    }	|	|	d	k    r| j                            d
           d S t          |	          }
t!          dd|
ii| j                  }|d         d         }|d                             d          |d                             d          |t'          | j                   d| j         d| d|}| j        r| j                            ||           |                     |d|||||          }|t3          |           |S # t4          $ r(}| j                            d|            Y d }~d S d }~ww xY w)Nr   r   r   z$Exception already captured, skippingFz$process_person_profiler   r   )NNNz"No exception information availabler   values)r   r   typer   z	/project/z/person/)z$exception_typez$exception_messagez$exception_listz$exception_personURL)extraz
$exceptionzFailed to capture exception: )r   r   r   r   r   r   rh   r	   rg   r   r   r   r<   exc_infor   r   r   r   r   r-   rk   r[   rz   r   r   r   r   )r   r   r   r   r   r   r   r   kwargsr   all_exceptions_with_tracer   $all_exceptions_with_trace_and_in_appresr   s                  rH   capture_exceptionzClient.capture_exception  s    M`"    133K?	D#)rJ $)Fy)Q)Q$EFFF "8=
45#ggM;999L*d333$.y99<>>8/A#A#A  !EFFF )DH(M(M% " ";"
 ".  E 493Eh3O0 $H#J#N#Nv#V#V&J1&M&Q&Q' ' $H+@+O+O(}(}Z^Zf(}(}p{(}(}  J + <""9F";;;,,\:w	4QW C
 $*9555J 	D 	D 	DHBqBBCCCCCCCCC	Ds%   /G !BG +C!G 
G?G::G?c                 B   | j         rdS |d         }|!t          j        t                                }t	          d|t                     t          |          }|                                |d<   d|v r)|                    d          }|rt          |          |d<   |	                    d          si |d<   d|d         d<   t          |d         d	<   || j        }|rd
|d         d<   | j        ri |d         | j        |d<   t          |	                    dd                    |d<   t          |          }| j        rk	 |                     |          }|| j                            d           dS |}n4# t"          $ r'}| j                            d|            Y d}~nd}~ww xY w| j                            d|           | j        sd
|fS | j        rT| j                            d|d                    t+          | j        | j        | j        | j        |g| j                   d
|fS 	 | j                            |d           | j                            d|d                    d
|fS # t6          j        $ r! | j                            d           d|fcY S w xY w)z8Push a new `msg` onto the queue, return `(success, msg)`)Frv   r   Ntzr   r   zposthog-pythonz$libz$lib_versionTz$geoip_disabler   z%Event dropped by before_send callback)TNzError in before_send callback: zqueueing: %szenqueued with blocking %s.r   )ra   rc   batchrd   F)blockzenqueued %s.zanalytics-python queue is full)rv   r   nowr
   rg   r,   	isoformatpopstringify_idr   r.   rw   rx   r+   r   r   rh   r   r   ri   rj   r   r[   r]   ra   rc   rd   re   putFullr   )r   r   rw   r   r   modified_msgr   s          rH   r   zClient._enqueue  s    = 	%$$$	 000IY111 #9--	$..00K S==776??D 1*400Fww|$$ 	# "C$4L&!,3L.)  .M 	726C./  	O N3|#4 N8M NC)#''-*F*FGGMCjj 	JJ#//44'HNN#JKKK%:" J J J""#HQ#H#HIIIIIIIIJ 	~s+++ y 	9> 	HNN7WFFF	Ye%)%>    9	JNN3eN,,,HNN>3w<8889z 	 	 	H=>>>#:	s1   1E7 4E7 7
F(F##F(-A I. .-JJc                     | j         } |j                    } |j                     | j                            d|           dS )z4Forces a flush from the internal queue to the serverz$successfully flushed about %s items.N)re   qsizer   r   rh   )r   re   sizes      rH   flushzClient.flushD  s@    
u{}}
=tDDDDDrJ   c                     | j         D ];}|                                 	 |                                 ,# t          $ r Y 8w xY w| j        r| j                                         dS dS )zbEnds the consumer thread once the queue is empty.
        Blocks execution until finished
        N)r   pauser   RuntimeErrorrr   stop)r   r   s     rH   r   zClient.joinL  s      	 	HNN    ; 	K	 	s   4
A Ac                     |                                   |                                  | j        r| j                                         dS dS )z2Flush all messages and cleanly shutdown the clientN)r  r   r|   closer   s    rH   shutdownzClient.shutdown[  sL    

		! 	+"((*****	+ 	+rJ   c                 $   	 t          | j        d| j         d| j        d          }|d         pg | _        |d         pi | _        |d         pi | _        n# t          $ r}|j        dk    r3| j	        
                    d	           | j        rt          dd
          np|j        dk    rH| j	                            d           g | _        i | _        i | _        | j        rt          dd          n| j	        
                    d|            Y d }~nVd }~wt          $ rF}| j	                            d| j        z             | j	                            |           Y d }~nd }~ww xY wt          j        t#                                | _        d S )Nz*/api/feature_flag/local_evaluation/?token=z&send_cohorts
   rc   r   rn   ro   i  z[FEATURE FLAGS] Error loading feature flags: To use feature flags, please set a valid personal_api_key. More information: https://posthog.com/docs/api/overviewzYou are using a write-only key with feature flags. To use feature flags, please set a personal_api_key More information: https://posthog.com/docs/api/overview)statusmessagei  z[FEATURE FLAGS] PostHog feature flags quota limited, resetting feature flag data.  Learn more about billing limits at https://posthog.com/docs/billing/limits-alertsz#PostHog feature flags quota limitedz-[FEATURE FLAGS] Error loading feature flags: z`[FEATURE FLAGS] Fetching feature flags failed with following error. We will retry in %s seconds.r   )r   r   r[   r]   r   rn   ro   r   r  r   errorrh   r   r   rp   r   r  r
   _last_feature_flag_poll)r   responser   s      rH   _load_feature_flagszClient._load_feature_flagsc  s   -	 %XT\XXX		  H "*'!2!8bD&./C&D&JD##I.4"DLL 	T 	T 	Tx3 v   : ""!R    S   {   &("*,'!: "" E    RqRRSSS 	  	  	 Hr$%   HQ	  (0|uww'?'?'?$$$s%   AA 
E'!B.DE'!<E""E'c                 T   | j         s#| j                            d           g | _        d S |                                  | j        r| j                                        sIt          t          | j	                  | j                  | _        | j        
                                 d S d S )NzL[FEATURE FLAGS] You have to specify a personal_api_key to use feature flags.)seconds)intervalexecute)r   r   r   r   r  rr   is_aliver   r   rp   r   r   s    rH   load_feature_flagszClient.load_feature_flags  s    $ 	H^   "$DF  """ 	  4 4 6 6 	  "4+=>>>0  DK K	  	 rJ   r   r   r   warn_on_unknown_groupsc                   |                     dd          rt          d          |                     d          sdS |                     d          pi }|                     d          }|| j                             t          |                    }	|	s5| j                            d| d|d	                     t          d
          |	|vrM|r%| j                            d|d	          d           n$| j                            d|d	          d           dS ||	         }
t          |||	         |
          S t          |||| j                  S )Nensure_experience_continuityFz&Flag has experience continuity enabledactivefiltersaggregation_group_type_indexz)[FEATURE FLAGS] Unknown group type index z for feature flag r   z!Flag has unknown group type indexz2[FEATURE FLAGS] Can't compute group feature flag: z without group names passed in)	r   r   rn   strr   r   rh   r   ro   )r   feature_flagr   r   r   r   r&  flag_filtersr+  
group_namefocused_group_propertiess              rH   _compute_flag_locallyzClient._compute_flag_locally  s    :EBB 	S()QRRR)) 	5#''	228b'3'7'78V'W'W$'3044S9U5V5VWWJ R   F@\  F  Fp|  ~C  qD  F  F   --PQQQ'' * H$$ A\Z_M`  A  A  A    HNN A\Z_M`  A  A  A   u'7
'C$0fZ02J   1k+<dl  rJ   r   r   r   r   send_feature_flag_eventsrw   c          
      b    |                      ||||||||          }	|	d S t          |	          S )Nr2  )get_feature_flagbool)
r   r   r   r   r   r   r   r3  rw   r  s
             rH   feature_enabledzClient.feature_enabled  sO     ((/-"7%=' ) 	
 	
 4H~~rJ   override_match_valuer   r   r   r   r3  rw   r9  c                   t          d|t                     t          d|t                     t          d|t                     | j        rd S |                     ||||          \  }}d }
d }d }|                     |||||          }|d u}|r5|p|}|r|                     ||          nd }t          j	        |||          }
n|s	 | 
                    ||||||	          \  }}t          j        ||          }
| j                            d| d|
            n4# t          $ r'}| j                            d|            Y d }~nd }~ww xY w|r<|                     |||
r|
                                nd |
r|
j        nd |||	||	  	         |
S )Nr   r   r   z&Successfully computed flag remotely: #z -> #z-[FEATURE FLAGS] Unable to get flag remotely: )rg   r   r   r   rv   &_add_local_person_and_group_properties_locally_evaluate_flag_compute_payload_locallyr!   from_value_and_payload%_get_feature_flag_details_from_decidefrom_flag_detailsr   rh   r   r   _capture_feature_flag_called	get_valuepayload)r   r   r   r9  r   r   r   r   r3  rw   flag_resultflag_details
request_id
flag_valueflag_was_locally_evaluatedlookup_match_valuerC  r   s                     rH   _get_feature_flag_resultzClient._get_feature_flag_result  sS    	sL))){H555&$'''= 	4 77V%68H  	,+ 
00f&79I
 

 &0t%;"% 	X!5!C &--c3EFFF 
 ,B' KK ' 	XX+/+U+U%$!, ,(j 0A "6  TSTT{TT     X X X""#VST#V#VWWWWWWWWX $ 	--+6@%%'''D'2<##*
 
 
 s   AD 
E!EEc          
      <    |                      ||||||||          S )aL  
        Get a FeatureFlagResult object which contains the flag result and payload for a key by evaluating locally or remotely
        depending on whether local evaluation is enabled and the flag can be locally evaluated.

        This also captures the $feature_flag_called event unless send_feature_flag_events is False.
        r2  )rJ  )	r   r   r   r   r   r   r   r3  rw   s	            rH   get_feature_flag_resultzClient.get_feature_flag_resultA  s9    $ ,,/-"7%=' - 	
 	
 		
rJ   c          
      l    |                      ||||||||          }	|	r|	                                ndS )a#  
        Get a feature flag value for a key by evaluating locally or remotely
        depending on whether local evaluation is enabled and the flag can be
        locally evaluated.

        This also captures the $feature_flag_called event unless send_feature_flag_events is False.
        r2  N)rL  rB  )
r   r   r   r   r   r   r   r3  rw   feature_flag_results
             rH   r5  zClient.get_feature_flag^  sW    & #::/-"7%=' ; 	
 	
 3FO",,...4OrJ   r   r   r   r   r   c                    | j         | j        r|                                  d }| j         r| j        
J d            | j                            |          }|r	 |                     |||||          }| j                            d| d|            nf# t          $ r*}| j                            d| d|            Y d }~n7d }~wt          $ r'}| j        
                    d|            Y d }~nd }~ww xY w|S )Nr   r   r   r   z$Successfully computed flag locally: z -> zFailed to compute flag z
 locally: z7[FEATURE FLAGS] Error while computing variant locally: )r   r   r$  rm   r   r1  r   rh   r   r   r   )	r   r   r   r   r   r   r  r   r   s	            rH   r<  zClient._locally_evaluate_flag}  s    %$*?%##%%% 	,88V 988 ,0055D #99#%*;)9  :    H HNNRsRRRR    . Q Q QHNN#OS#O#OA#O#OPPPPPPPP    H&&URSUU        s$   :B 
C8 CC8C33C8)match_valuer   r   r   r   r3  rw   rQ  c                T    |                      |||||||||		  	        }
|
r|
j        nd S )Nr8  )rJ  rC  )r   r   r   rQ  r   r   r   r   r3  rw   rN  s              rH   get_feature_flag_payloadzClient.get_feature_flag_payload  sQ     #;;!,/-"7%=' < 

 

 /BK"**tKrJ   rw   c                     |                      |||||          }|                    d          }|                    d          }	|	r |	j        |          nd}
|
|fS )zK
        Calls /decide and returns the flag details and request id
        	requestIdr   N)r   r   )r   r   r   r   r   r   rw   r   rF  r   rE  s              rH   r?  z,Client._get_feature_flag_details_from_decide  sm     ++!24Dm
 
	 ]];//
g&&).8yuy~~~DZ''rJ   r  rC  rH  rF  rE  c
                    | d|dnt          |           }
|
| j        |         vrd|d|d|d| |i}|r||d<   |r||d<   t          |	t                    rr|	j        r|	j        j        r|	j        j        |d	<   t          |	j        t                    r6|	j        j        r|	j        j        |d
<   |	j        j	        r|	j        j	        |d<   | 
                    |d|||           | j        |                             |
           d S d S )N_z::null::z$feature_flagz$feature_flag_responselocally_evaluatedr   z$feature_flag_payloadz$feature_flag_request_idz$feature_flag_reasonz$feature_flag_versionz$feature_flag_idr   )r   rw   )r,  ru   
isinstancer    reasondescriptionmetadatar"   r8   idr   add)r   r   r   r  rC  rH  r   rw   rF  rE  feature_flag_reported_keyr   s               rH   rA  z#Client._capture_feature_flag_called  s    HH8#3ZZXHH 	"
 &;KHI I  ((#%?!C!!8	*J  >6=
23 D9C
56,44 	R& Y<+>+J Y9E9L9XJ56l3\BB R#,4 (19 ##:; $,/ R9E9N9Q
#56LL&+     4[AEE)    EI IrJ   c                 
   | j         rd S | j        | j                            d           d S 	 t	          | j        | j        || j                  S # t          $ r(}| j                            d|            Y d }~d S d }~ww xY w)Nz`[FEATURE FLAGS] You have to specify a personal_api_key to fetch decrypted feature flag payloads.r  z>[FEATURE FLAGS] Unable to get decrypted feature flag payload: )	rv   r   r   r   r   r]   rq   r   r   )r   r   r   s      rH   get_remote_config_payloadz Client.get_remote_config_payload  s    = 	4 (Hr   4
	 %	B	     	 	 	HTQRTT        	s   !A 
BA==Bc                 ,   d }| j         |S | j                             |          }|rl|                    d          pi }|                    d          pi }t          |t                    r|rdnt	          |          }|                    |d           }|S )Nr*  payloadstrue)rm   r   rY  r6  r,  )r   r   rQ  rC  flag_definitionr.  flag_payloadslookup_values           rH   r=  zClient._compute_payload_locally  s     $,N377<< 
	<*..y99?RL(,,Z88>BM
 k400&5@&%% 
 $''d;;GrJ   r   r   r   r   rw   c                H    |                      ||||||          }|d         S )Nrh  featureFlags)get_all_flags_and_payloads)r   r   r   r   r   r   rw   r  s           rH   r   zClient.get_all_flags4  s=     22/-"7' 3 
 
 ''rJ   c                P   | j         rd d dS |                     ||||          \  }}|                     ||||          \  }}|r`|s^	 |                     |||||          }	t	          |	          S # t
          $ r'}
| j                            d|
            Y d }
~
nd }
~
ww xY w|S )Nrj  featureFlagPayloadsrP  )r   r   r   rw   z:[FEATURE FLAGS] Unable to get feature flags and payloads: )rv   r;  #_get_all_flags_and_payloads_locallyr   r'   r   r   r   )r   r   r   r   r   r   rw   r  fallback_to_decidedecide_responser   s              rH   rk  z!Client.get_all_flags_and_payloadsI  s,    = 	G$(FFF 77V%68H  	,+ (,'O'O/-	 (P (
 (
$$  	&; 	"&"9"9!&7%5"/ #: # # -_===   ""TQRTT       
 s   	(A2 2
B#<BB#c          	         t          d|t                     t          d|t                     | j        | j        r|                                  i }i }d}| j        r| j        D ]}		 |                     |	|||||          ||	d         <   |                     |	d         ||	d                            }
|
r|
||	d         <   ]# t          $ r d}Y kt          $ r)}| j
                            d|            d}Y d }~d }~ww xY wnd}||d|fS )	Nr   r   Fr%  r   Tz;[FEATURE FLAGS] Error while computing variant and payload: rm  )rg   r   r   r   r   r$  r1  r=  r   r   r   r   )r   r   r   r   r   r&  r   rc  rp  r   matched_payloadr   s               rH   ro  z*Client._get_all_flags_and_payloads_locallyt  s    	{H555&$'''%$*?%##%%%&(#%" 	&* . ..)-)C)C#%*;)9/E *D * *E$u+& '+&C&CUU4;%7' 'O ' @0?e-- . . .)-&&&  . . .H&&YVWYY   *.&&&&&&	.%.0 "& "#+
 
  	s   &AC  D 	D C;;D c                     | j         S N)r   r   s    rH   feature_flag_definitionszClient.feature_flag_definitions  s    !!rJ   c                 t    d|i|pi }i }|r)|D ]&}d||         i|                     |          pi ||<   '||fS )Nr   r   r   )r   r   r   r   r   all_person_propertiesall_group_propertiesr/  s           rH   r;  z-Client._add_local_person_and_group_properties  s     ;!
 &B!

  " 	$  
 &"44'++J77=24$Z00
 %&:::rJ   )NNFrT   TNrU   rV   FrM   FrW   rX   rY   NNFTFrM   NFFNNFN)NNNNNN)NNNN)	NNNNNNNFN)NNNNNNNN)NNNNNNN)8__name__
__module____qualname____doc__r   	getLoggerr   r   propertyr   setterr   r   r,  r   r6  r   r   r#   r   r$   r   r   rt   r   r   r   r   r   r   r  r   r  r  r$  r%   r1  r7  r   r!   rJ  rL  r5  r<  rS  tupler    r?  rA  ra  r=  r   rk  ro  rv  r;  r   rJ   rH   rR   rR   x   s       &&
'
I
&
&C "./%* %+/9% % % %B # # X# 
 
 
  #1 #1 #1 #1P * * 
c5s##	$* * * *& , , 
c3h, , , ,& + + 
+ + + +& '3 '3 
'3 '3 '3 '3V  U1 U1 U1 U1r 1 1 1 1F 1 1 1 1F '1 '1 '1 '1V  1  1  1  1H &1 &1 &1 &1T VD VD VD VDpL L L\E E E  + + +0@ 0@ 0@d     * #0 0 0 
0 0 0 0n #!%    @ 59#!%M M M
 'y1M 
#	$M M M Mh #!%
 
 
 
#	$
 
 
 
D #!%P P P 
)	P P P P>$$ $ S#X	$
  S>$ sCx.$ 
)	$ $ $ $V ,0#!%L L L
 i(L L L L4(( ( S#X	(
  S>( sCx.(  ~( 
x$hsm3	4( ( ( ((55 5 9%	5
 #5 %)5 S#X5  ~5 SM5 {+5 5 5 5nS    ,%.	#   4 #( ( ( 
$sE$),,-	.( ( ( (2 #) ) ) 
) ) ) )^ $1 1 1 
%	&1 1 1 1f" " "; ; ; ; ;rJ   rR   c                 r    t          ||          s&d                    | ||          }t          |          dS )z8Require that the named `field` has the right `data_type`z{0} must have {1}, got: {2}N)rY  r   AssertionError)namefield	data_typer   s       rH   rg   rg     sC    eY'' "+224EJJS!!!" "rJ   c                 V    | d S t          | t                    r| S t          |           S ru  )rY  r   r,  )vals    rH   r  r    s.    
{t#|$$ 
s88OrJ   )Pr   r   numbersr~   r=   r<   r   r   r   typingr   r   r   r   r   r	   r@   dateutil.tzr
   sixr   posthog.consumerr   posthog.exception_capturer   posthog.exception_utilsr   r   r   r   r   posthog.feature_flagsr   r   posthog.pollerr   posthog.requestr   r   r   r   r   r   r   posthog.scopesr   r   r   posthog.typesr    r!   r"   r#   r$   r%   r&   r'   r(   r)   posthog.utilsr*   r+   r,   r-   posthog.versionr.   re   ImportErrorrf   Numberr   rs   rI   r   r,  rP   objectrR   rg   r  r   rJ   rH   <module>r     ss      				  



  ( ( ( ( ( ( ( ( ' ' ' ' ' ' ' ' ' '                      % % % % % % 6 6 6 6 6 6              X W W W W W W W ! ! ! ! ! !                          
                        X W W W W W W W W W W W # # # # # #LLLL    NL$/( ( (VS#X    B; B; B; B; B;V B; B; B;J*" " "    s   6B; ;	CC