o
    Rh                     @   s   U d dl Z d dlmZ d dlmZmZmZmZmZ e j	di gdZ
e j	e ed< deeef 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ededef dZdddZdS )    N)contextmanager)AnyCallableDictTypeVarcastposthog_context_stack)default_context_stackreturnc                   C   s   t  d S )N)r
   get r   r   P/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/posthog/scopes.py_get_current_context
   s   r   FTc              
   c   s    ddl m} t  }t }| r|i g n||g }t|}z!zdV  W n ty: } z|r5||  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 from parent context.
               If True, starts with no tags.
        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)	posthogr   r   copyr
   r   set	Exceptionreset)freshcapture_exceptionsr   current_tagscurrent_stack	new_stacktokener   r   r   new_context   s    


r   keyvaluec                 C   s   |t  | < 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)r   )r   r    r   r   r   tag>   s   r!   c                   C   s
   t   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
    )r   r   r   r   r   r   get_tagsL   s   
r"   c                   C   s   t    dS )z&Clear all tags in the current context.N)r   clearr   r   r   r   
clear_tagsW   s   r$   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   )r   )argskwargs)r   r   r'   r   r   wrapperx   s   $z*scoped.<locals>.decorator.<locals>.wrapper)	functoolsr(   r   r%   )r'   r(   r+   r   r   )r'   r   	decoratoru   s   
zscoped.<locals>.decorator)r%   )r   r   r.   r   r-   r   scoped_   s   
r/   )FT)r   N)contextvars
contextlibr   typingr   r   r   r   r   
ContextVarr
   list__annotations__strr   r   r!   r"   r$   r%   r/   r   r   r   r   <module>   s   
 /
