o
    "Th                     @   s$  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	 G dd dZ
e jdddZe jee
  ed< d	ee
 fd
dZed'ddZdeded	dfddZd	eeef fddZd(ddZded	dfddZded	dfddZd	ee fddZd	ee fd d!Zed"ed#ef d$Zd'd%d&ZdS ))    N)contextmanager)OptionalAnyCallableDictTypeVarcastc                   @   s   e Zd Z			ddedefddZdefd	d
ZdefddZdedefddZ	dd Z
dee fddZdee fddZdeeef fddZdS )ContextScopeNFTfreshcapture_exceptionsc                 C   s(   || _ || _|| _d | _d | _i | _d S N)parentr
   r   
session_iddistinct_idtags)selfr   r
   r    r   O/home/air/segue/gemini/back/venv/lib/python3.10/site-packages/posthog/scopes.py__init__   s   
zContextScope.__init__r   c                 C   
   || _ d S r   )r   )r   r   r   r   r   set_session_id      
zContextScope.set_session_idr   c                 C   r   r   )r   )r   r   r   r   r   set_distinct_id   r   zContextScope.set_distinct_idkeyvaluec                 C   s   || j |< d S r   )r   )r   r   r   r   r   r   add_tag   s   zContextScope.add_tagc                 C   s   | j S r   )r   r   r   r   r   
get_parent   s   zContextScope.get_parentreturnc                 C   .   | j d ur| j S | jd ur| js| j S d S r   )r   r   r
   get_session_idr   r   r   r   r        
   

zContextScope.get_session_idc                 C   r   r   )r   r   r
   get_distinct_idr   r   r   r   r"   '   r!   zContextScope.get_distinct_idc                 C   s.   | j  }| jr| js| j }|| |S r   )r   copyr   r
   collect_tagsupdate)r   r   new_tagsr   r   r   r$   .   s
   


zContextScope.collect_tags)NFT)__name__
__module____qualname__boolr   strr   r   r   r   r   r   r    r"   r   r$   r   r   r   r   r	      s     
r	   posthog_context_stack)default_context_stackr   c                   C   s   t  S r   )r.   getr   r   r   r   _get_current_context>   s   r0   FTc              
   c   s    ddl m} t }t|| |}t| z$zdV  W n ty/ } z|jr*||  d}~ww W t|  dS t|  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.

    Examples:
        # 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")

        # 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")

    r   )capture_exceptionN)	posthogr1   r0   r	   r.   set	Exceptionr   r   )r
   r   r1   current_contextnew_contexter   r   r   r6   B   s   

"r6   r   r   c                 C   s   t  }|r|| | dS dS )z
    Add a tag to the current context.

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

    Example:
        posthog.tag("user_id", "123")
    N)r0   r   )r   r   r5   r   r   r   tagq   s   r8   c                  C   s   t  } | r	|  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
    )r0   r$   r5   r   r   r   get_tags   s   r:   c                  C   s   t  } | r| j  dS dS )zAClear all tags in the current context. Does not clear parent tagsN)r0   r   clearr9   r   r   r   
clear_tags   s   r<   r   c                 C      t  }|r||  dS dS )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.

    Args:
        distinct_id: The distinct ID to associate with the current context and its children.
    N)r0   r   )r   r5   r   r   r   identify_context   s   
r>   r   c                 C   r=   )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
    N)r0   r   )r   r5   r   r   r   set_context_session   s   	r?   c                  C      t  } | r	|  S dS )zq
    Get the session ID for the current context.

    Returns:
        The session ID if set, None otherwise
    N)r0   r    r9   r   r   r   get_context_session_id      rA   c                  C   r@   )zs
    Get the distinct ID for the current context.

    Returns:
        The distinct ID if set, None otherwise
    N)r0   r"   r9   r   r   r   get_context_distinct_id   rB   rC   F.)boundc                    s   dt dt f fd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()
    funcr   c                    s.   ddl m} |  fdd}tt|S )Nr   )wrapsc                     s>   t  d | i |W  d    S 1 sw   Y  d S )N)r
   r   )r6   )argskwargs)r   r
   rF   r   r   wrapper   s   $z*scoped.<locals>.decorator.<locals>.wrapper)	functoolsrG   r   rD   )rF   rG   rJ   r   r
   )rF   r   	decorator   s   
zscoped.<locals>.decorator)rD   )r
   r   rM   r   rL   r   scoped   s   
rN   )FT)r   N)contextvars
contextlibr   typingr   r   r   r   r   r   r	   
ContextVarr.   __annotations__r0   r6   r+   r8   r:   r<   r>   r?   rA   rC   rD   rN   r   r   r   r   <module>   s&   
  3.
