
    Qi81              	          U d dl Z d dlmZ d dlmZmZmZmZmZm	Z	m
Z
 e
rd dlmZ  G d d      Z e j                  dd      Ze j                  ee      ed	<   d
ee   fdZe	 	 	 d(dededed   fd       Zdeded
dfdZd
eeef   fdZded
dfdZded
dfdZd
ee   fdZd
ee   fdZded
dfdZded
dfdZded
dfd Zd
ee   fd!Zd
ee   fd"Z d
ee   fd#Z! ed$ed%ef   &      Z"d)dedefd'Z#y)*    N)contextmanager)OptionalAnyCallableDictTypeVarcastTYPE_CHECKING)Clientc                       e Zd Z	 	 	 	 ddededed   fdZdefdZd	efd
Zdede	fdZ
defdZdefdZdefdZd Zdee   fdZdee   fdZdeee	f   fdZdee   fdZdee   fdZdee   fdZy)ContextScopeNfreshcapture_exceptionsclientr   c                     || _         || _        || _        || _        d | _        d | _        i | _        d | _        d | _        d | _	        y N)
r   parentr   r   
session_iddistinct_idtags capture_exception_code_variablescode_variables_mask_patternscode_variables_ignore_patterns)selfr   r   r   r   s        L/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/posthog/contexts.py__init__zContextScope.__init__   sO     )/
"4)-*.$&	@D-<@)>B+    r   c                     || _         y r   )r   )r   r   s     r   set_session_idzContextScope.set_session_id   s	    $r   r   c                     || _         y r   )r   )r   r   s     r   set_distinct_idzContextScope.set_distinct_id    s
    &r   keyvaluec                 "    || j                   |<   y r   )r   )r   r"   r#   s      r   add_tagzContextScope.add_tag#   s    		#r   enabledc                     || _         y r   )r   )r   r&   s     r   $set_capture_exception_code_variablesz1ContextScope.set_capture_exception_code_variables&   s
    07-r   mask_patternsc                     || _         y r   )r   )r   r)   s     r    set_code_variables_mask_patternsz-ContextScope.set_code_variables_mask_patterns)   s
    ,9)r   ignore_patternsc                     || _         y r   )r   )r   r,   s     r   "set_code_variables_ignore_patternsz/ContextScope.set_code_variables_ignore_patterns,   s
    .=+r   c                     | j                   S r   )r   r   s    r   
get_parentzContextScope.get_parent/   s    {{r   returnc                     | j                   | j                   S | j                  &| j                  s| j                  j                         S y r   )r   r   r   get_session_idr0   s    r   r4   zContextScope.get_session_id2   s<    ??&??";;"4::;;--//r   c                     | j                   | j                   S | j                  &| j                  s| j                  j                         S y r   )r   r   r   get_distinct_idr0   s    r   r6   zContextScope.get_distinct_id9   s@    '###;;"4::;;..00r   c                     | j                   j                         }| j                  r7| j                  s+| j                  j	                         }|j                  |       |S r   )r   copyr   r   collect_tagsupdate)r   r   new_tagss      r   r9   zContextScope.collect_tags@   sD    yy~~;;tzz {{//1HKK!r   c                     | j                   | j                   S | j                  &| j                  s| j                  j                         S y r   )r   r   r   $get_capture_exception_code_variablesr0   s    r   r=   z1ContextScope.get_capture_exception_code_variablesJ   s@    00<888;;"4::;;CCEEr   c                     | j                   | j                   S | j                  &| j                  s| j                  j                         S y r   )r   r   r    get_code_variables_mask_patternsr0   s    r   r?   z-ContextScope.get_code_variables_mask_patternsQ   s@    ,,8444;;"4::;;??AAr   c                     | j                   | j                   S | j                  &| j                  s| j                  j                         S y r   )r   r   r   "get_code_variables_ignore_patternsr0   s    r   rA   z/ContextScope.get_code_variables_ignore_patternsX   s@    ..:666;;"4::;;AACCr   )NFTN)__name__
__module____qualname__boolr   r   strr   r!   r   r%   r(   listr+   r.   r1   r4   r6   r   r9   r=   r?   rA    r   r   r   r   
   s     #'%)C C !	C
 "C$% %'3 '3 s 8D 8:d :>$ > # d38n htn (4. HTN r   r   posthog_context_stack)default_context_stackr2   c                  *    t         j                         S r   )rK   getrH   r   r   _get_current_contextrN   e   s    r   r   r   r   r   c              #     K   ddl m} t               }t        || ||      }t        j                  |       	 d 	 t        j                  |j                                y# t        $ rB}|j                  r0|j                  r|j                  j                  |         ||        d}~ww xY w# t        j                  |j                                w xY ww)a  
    Create a new context scope that will be active for the duration of the with block.
    Any tags set within this scope will be isolated to this context. Any exceptions raised
    or events captured within the context will be tagged with the context tags.

    Args:
        fresh: Whether to start with a fresh context (default: False).
               If False, inherits tags, identity and session id's from parent context.
               If True, starts with no state
        capture_exceptions: Whether to capture exceptions raised within the context (default: True).
               If True, captures exceptions and tags them with the context tags before propagating them.
               If False, exceptions will propagate without being tagged or captured.
        client: Optional client instance to use for capturing exceptions (default: None).
                If provided, the client will be used to capture exceptions within the context.
                If not provided, the default (global) client will be used. Note that the passed
                client is only used to capture exceptions within the context - other events captured
                within the context via `Client.capture` or `posthog.capture` will still carry the context
                state (tags, identity, session id), but will be captured by the client directly used (or
                the global one, in the case of `posthog.capture`)

    Examples:
        ```python
        # Inherit parent context tags
        with posthog.new_context():
            posthog.tag("request_id", "123")
            # Both this event and the exception will be tagged with the context tags
            posthog.capture("event_name", {"property": "value"})
            raise ValueError("Something went wrong")
        ```
        ```python
        # Start with fresh context (no inherited tags)
        with posthog.new_context(fresh=True):
            posthog.tag("request_id", "123")
            # Both this event and the exception will be tagged with the context tags
            posthog.capture("event_name", {"property": "value"})
            raise ValueError("Something went wrong")
        ```

    Category:
        Contexts
    r   )capture_exceptionN)
posthogrP   rN   r   rK   set	Exceptionr   r   r1   )r   r   r   rP   current_contextnew_contextes          r   rU   rU   i   s     ^ **,O7I6RK{#
5 	;1134  ))!!""44Q7 	 "!$ 	;1134s3   4CA  $C 	B+)=B&&B++B. .%CCr"   r#   c                 B    t               }|r|j                  | |       yy)aD  
    Add a tag to the current context. All tags are added as properties to any event, including exceptions, captured
    within the context.

    Args:
        key: The tag key
        value: The tag value

    Example:
        ```python
        posthog.tag("user_id", "123")
        ```

    Category:
        Contexts
    N)rN   r%   )r"   r#   rT   s      r   tagrX      s$    " +,OU+ r   c                  >    t               } | r| j                         S i S )z
    Get all tags from the current context. Note, modifying
    the returned dictionary will not affect the current context.

    Returns:
        Dict of all tags in the current context

    Category:
        Contexts
    )rN   r9   rT   s    r   get_tagsr[      s#     +,O++--Ir   r   c                 @    t               }|r|j                  |        yy)a  
    Identify the current context with a distinct ID, associating all events captured in this or
    child contexts with the given distinct ID (unless identify_context is called again). This is overridden by
    distinct id's passed directly to posthog.capture and related methods (identify, set etc). Entering a
    fresh context will clear the context-level distinct ID. The distinct-id passed should be uniquely associated
    with one of your users. Events captured outside of a context, or in a context with no associated distinct
    ID, will be assigned a random UUID, and captured as "personless".

    Args:
        distinct_id: The distinct ID to associate with the current context and its children.

    Category:
        Contexts
    N)rN   r!   )r   rT   s     r   identify_contextr]      s"     +,O''4 r   r   c                 @    t               }|r|j                  |        yy)a  
    Set the session ID for the current context, associating all events captured in this or
    child contexts with the given session ID (unless set_context_session is called again).
    Entering a fresh context will clear the context-level session ID.

    Args:
        session_id: The session ID to associate with the current context and its children. See https://posthog.com/docs/data/sessions

    Category:
        Contexts
    N)rN   r   )r   rT   s     r   set_context_sessionr_      s"     +,O&&z2 r   c                  <    t               } | r| j                         S y)z
    Get the session ID for the current context.

    Returns:
        The session ID if set, None otherwise

    Category:
        Contexts
    N)rN   r4   rZ   s    r   get_context_session_idra      s!     +,O--//r   c                  <    t               } | r| j                         S y)z
    Get the distinct ID for the current context.

    Returns:
        The distinct ID if set, None otherwise

    Category:
        Contexts
    N)rN   r6   rZ   s    r   get_context_distinct_idrc     s!     +,O..00r   r&   c                 @    t               }|r|j                  |        yy)zJ
    Set whether code variables are captured for the current context.
    N)rN   r(   )r&   rT   s     r   ,set_capture_exception_code_variables_contextre     s"     +,O<<WE r   r)   c                 @    t               }|r|j                  |        yy)zg
    Variable names matching these patterns will be masked with *** when capturing code variables.
    N)rN   r+   )r)   rT   s     r   (set_code_variables_mask_patterns_contextrg      s"     +,O88G r   r,   c                 @    t               }|r|j                  |        yy)zj
    Variable names matching these patterns will be ignored completely when capturing code variables.
    N)rN   r.   )r,   rT   s     r   *set_code_variables_ignore_patterns_contextri   )  s"     +,O::?K r   c                  <    t               } | r| j                         S y r   )rN   r=   rZ   s    r   ,get_capture_exception_code_variables_contextrk   2  s    *,OCCEEr   c                  <    t               } | r| j                         S y r   )rN   r?   rZ   s    r   (get_code_variables_mask_patterns_contextrm   9  s    *,O??AAr   c                  <    t               } | r| j                         S y r   )rN   rA   rZ   s    r   *get_code_variables_ignore_patterns_contextro   @  s    *,OAACCr   F.)boundc                 0     dt         dt         f fd}|S )a  
    Decorator that creates a new context for the function. Simply wraps
    the function in a with posthog.new_context(): block.

    Args:
        fresh: Whether to start with a fresh context (default: False)
        capture_exceptions: Whether to capture and track exceptions with posthog error tracking (default: True)

    Example:
        @posthog.scoped()
        def process_payment(payment_id):
            posthog.tag("payment_id", payment_id)
            posthog.tag("payment_method", "credit_card")

            # This event will be captured with tags
            posthog.capture("payment_started")
            # If this raises an exception, it will be captured with tags
            # and then re-raised
            some_risky_function()

    Category:
        Contexts
    funcr2   c                 V     ddl m}  |        fd       }t        t        |      S )Nr   )wrapsc                  Z    t              5   | i |cd d d        S # 1 sw Y   y xY w)N)r   r   )rU   )argskwargsr   r   rs   s     r   wrapperz*scoped.<locals>.decorator.<locals>.wrapperf  s0    5=OP -T,V,- - -s   !*)	functoolsru   r	   rp   )rs   ru   ry   r   r   s   `  r   	decoratorzscoped.<locals>.decoratorc  s+    #	t	- 
	- Awr   )rp   )r   r   r{   s   `` r   scopedr|   J  s    2   a   r   )FTN)FT)$contextvars
contextlibr   typingr   r   r   r   r   r	   r
   posthog.clientr   r   
ContextVarrK   __annotations__rN   rE   rU   rF   rX   r[   r]   r_   ra   rc   re   rG   rg   ri   rk   rm   ro   rp   r|   rH   r   r   <module>r      s    % N N N%S Sl BXAWAWTB&&x'=> 
 h|4   #!%>5>5>5 X>5 >5B,S , , ,,$sCx. "5# 5$ 5(3C 3D 3"  #  F$ F4 FHD HT HL L Lhtn (4. HTN  CxS)*#$ #D #r   