o
    hC                     @  s  d Z ddlmZ ddlZddl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mZmZmZmZmZmZmZ dd	lmZ dd
lmZ ddlmZmZmZmZ zddlZW n eyc   dZY nw zddlZW n eyu   dZY nw zddl Z ddl!m"Z" ddl#m$Z$ W n ey   dZ Y nw g dZ%ej&dkZ'dZ(dZ)d=ddZ*edurej+j,Z,ej+j-Z-ej+j.Z.ej/j0Z0ej/j1Z1ej/j2Z2ej/j3Z3ej/j4Z4ej/j5Z5ej6j0Z7ndZ,dZ-dZ.dZ0dZ1dZ2dZ3d Z4d!Z5d"Z7e' peduZ8e8Z9G d#d$ d$eZ:ee:ee: f Z;G d%d& d&Z<G d'd( d(Z=d>d.d/Z>eG d0d1 d1Z?e? Z@	 d>d2d3ZAe dur-e@ZBn
edur5eAZBne>ZBG d4d5 d5ZCeD ZEd?d;d<ZFdS )@zw
Helpers that make development with *structlog* more pleasant.

See also the narrative documentation in `development`.
    )annotationsN)	dataclass)StringIO)
ModuleType)AnyIterableLiteralProtocolSequenceTextIOTypeUnion   )_format_exception)_figure_out_exc_info)	EventDictExceptionRendererExcInfoWrappedLogger)Console)	Traceback)ConsoleRendererplain_tracebackrich_tracebackbetter_tracebackwin32z1{who} requires the {package} package installed.     sstrlengthintreturnc                 C  s(   |t |  }| d|dkr|  S d  S )z&
    Pads *s* to length *length*.
     r   len)r   r   missing r&   C/var/www/html/ai/venv/lib/python3.10/site-packages/structlog/dev.py_padE   s   r(   z[0mz[1mz[2mz[31mz[34mz[36mz[35mz[33mz[32mz[41mc                   @  sv   e Zd ZU ded< ded< ded< ded< ded< ded< ded< ded	< ded
< ded< ded< ded< ded< dS )_Stylesr   resetbrightlevel_criticallevel_exceptionlevel_error
level_warn
level_infolevel_debuglevel_notset	timestamplogger_namekv_keykv_valueN)__name__
__module____qualname____annotations__r&   r&   r&   r'   r)   o   s   
 r)   c                   @  s@   e Zd ZeZeZeZeZ	eZ
eZeZeZeZeZeZeZeZdS )_ColorfulStylesN)r7   r8   r9   	RESET_ALLr*   BRIGHTr+   REDr,   r-   r.   YELLOWr/   GREENr0   r1   RED_BACKr2   DIMr3   BLUEr4   CYANr5   MAGENTAr6   r&   r&   r&   r'   r;          r;   c                   @  s@   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZdS )_PlainStyles N)r7   r8   r9   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r&   r&   r&   r'   rG      rF   rG   sior   exc_infor   Nonec                 C  s   |  dt|  dS )a   
    "Pretty"-print *exc_info* to *sio* using our own plain formatter.

    To be passed into `ConsoleRenderer`'s ``exception_formatter`` argument.

    Used by default if neither Rich nor *better-exceptions* are present.

    .. versionadded:: 21.2.0
    
N)writer   rI   rJ   r&   r&   r'   r      s   
r   c                   @  s   e Zd ZU dZdZded< dZded< dZd	ed
< dZded< dZ	ded< dZ
d	ed< dZd	ed< dZded< dZd	ed< dZd	ed< dZded< dZded< dZded< d%d#d$ZdS )&RichTracebackFormatterag  
    A Rich traceback renderer with the given options.

    Pass an instance as `ConsoleRenderer`'s ``exception_formatter`` argument.

    See :class:`rich.traceback.Traceback` for details on the arguments.

    If a *width* of -1 is passed, the terminal width is used. If the width
    can't be determined, fall back to 80.

    .. versionadded:: 23.2.0
    	truecolorz:Literal['auto', 'standard', '256', 'truecolor', 'windows']color_systemTboolshow_localsd   r    
max_framesNz
str | NonethemeF	word_wrap   extra_lineswidthindent_guides
   locals_max_lengthP   locals_max_stringlocals_hide_dunderlocals_hide_sunderr&   zSequence[str | ModuleType]suppressrI   r   rJ   r   r!   rK   c                 C  sv   | j dkrtd\| _ }|d t|| jdtj|| j	| j
| j| j| j| j | j| j| j| j| j| jd d S )N)r^   r   rL   )filerQ   )rS   rU   rV   rW   rY   rZ   r[   r]   r_   r`   ra   rb   )rZ   shutilget_terminal_sizerM   r   rQ   printr   from_exceptionrS   rU   rV   rW   rY   r[   r]   r_   r`   ra   rb   )selfrI   rJ   _r&   r&   r'   __call__   s(   

zRichTracebackFormatter.__call__rI   r   rJ   r   r!   rK   )r7   r8   r9   __doc__rQ   r:   rS   rU   rV   rW   rY   rZ   r[   r]   r_   r`   ra   rb   rk   r&   r&   r&   r'   rO      s"   
 
rO   c                 C  s   |  ddtj|   dS )a	  
    Pretty-print *exc_info* to *sio* using the *better-exceptions* package.

    To be passed into `ConsoleRenderer`'s ``exception_formatter`` argument.

    Used by default if *better-exceptions* is installed and Rich is absent.

    .. versionadded:: 21.2.0
    rL   rH   N)rM   joinbetter_exceptionsformat_exceptionrN   r&   r&   r'   r      s   
r   c                	   @  sR   e Zd ZdZeedddedddf	d%ddZd&ddZd'd!d"Z	e
d(d)d#d$ZdS )*r   a  
    Render ``event_dict`` nicely aligned, possibly in colors, and ordered.

    If ``event_dict`` contains a true-ish ``exc_info`` key, it will be rendered
    *after* the log line. If Rich_ or better-exceptions_ are present, in colors
    and with extra context.

    Arguments:

        pad_event: Pad the event to this many characters.

        colors:
            Use colors for a nicer output. `True` by default. On Windows only
            if Colorama_ is installed.

        force_colors:
            Force colors even for non-tty destinations. Use this option if your
            logs are stored in a file that is meant to be streamed to the
            console. Only meaningful on Windows.

        repr_native_str:
            When `True`, `repr` is also applied to native strings (i.e. unicode
            on Python 3 and bytes on Python 2). Setting this to `False` is
            useful if you want to have human-readable non-ASCII output on
            Python 2.  The ``event`` key is *never* `repr` -ed.

        level_styles:
            When present, use these styles for colors. This must be a dict from
            level names (strings) to Colorama styles. The default can be
            obtained by calling `ConsoleRenderer.get_default_level_styles`

        exception_formatter:
            A callable to render ``exc_infos``. If Rich_ or better-exceptions_
            are installed, they are used for pretty-printing by default (rich_
            taking precedence). You can also manually set it to
            `plain_traceback`, `better_traceback`, an instance of
            `RichTracebackFormatter` like `rich_traceback`, or implement your
            own.

        sort_keys: Whether to sort keys when formatting. `True` by default.

        event_key:
            The key to look for the main log message. Needed when you rename it
            e.g. using `structlog.processors.EventRenamer`.

        timestamp_key:
            The key to look for timestamp of the log message. Needed when you
            rename it e.g. using `structlog.processors.EventRenamer`.

    Requires the Colorama_ package if *colors* is `True` **on Windows**.

    .. _Colorama: https://pypi.org/project/colorama/
    .. _better-exceptions: https://pypi.org/project/better-exceptions/
    .. _Rich: https://pypi.org/project/rich/

    .. versionadded:: 16.0.0
    .. versionadded:: 16.1.0 *colors*
    .. versionadded:: 17.1.0 *repr_native_str*
    .. versionadded:: 18.1.0 *force_colors*
    .. versionadded:: 18.1.0 *level_styles*
    .. versionchanged:: 19.2.0
       Colorama now initializes lazily to avoid unwanted initializations as
       ``ConsoleRenderer`` is used by default.
    .. versionchanged:: 19.2.0 Can be pickled now.
    .. versionchanged:: 20.1.0
       Colorama does not initialize lazily on Windows anymore because it breaks
       rendering.
    .. versionchanged:: 21.1.0
       It is additionally possible to set the logger name using the
       ``logger_name`` key in the ``event_dict``.
    .. versionadded:: 21.2.0 *exception_formatter*
    .. versionchanged:: 21.2.0
       `ConsoleRenderer` now handles the ``exc_info`` event dict key itself. Do
       **not** use the `structlog.processors.format_exc_info` processor
       together with `ConsoleRenderer` anymore! It will keep working, but you
       can't have customize exception formatting and a warning will be raised
       if you ask for it.
    .. versionchanged:: 21.2.0
       The colors keyword now defaults to True on non-Windows systems, and
       either True or False in Windows depending on whether Colorama is
       installed.
    .. versionadded:: 21.3.0 *sort_keys*
    .. versionadded:: 22.1.0 *event_key*
    .. versionadded:: 23.2.0 *timestamp_key*
    FNTeventr3   	pad_eventr    colorsrR   force_colorsrepr_native_strlevel_stylesStyles | Noneexception_formatterr   	sort_keys	event_keyr   timestamp_keyc
                 C  s   |r+t r(td u r| jj}
ttj|
d dd|r$t  tjdd nt  t	}nt
}|| _|| _|d u r>| || _n|| _| jD ]}| j|  |j7  < qDtt| j dd d| _|| _|| _|| _|| _|	| _d S )	Nz with `colors=True`colorama)whopackageF)stripc                 S  s   t | S )Nr#   )er&   r&   r'   <lambda>  s    z*ConsoleRenderer.__init__.<locals>.<lambda>)key)_IS_WINDOWSr|   	__class__r7   SystemError_MISSINGformatdeinitinitr;   rG   _styles
_pad_eventget_default_level_styles_level_to_colorr+   r$   maxkeys_longest_level_repr_native_str_exception_formatter
_sort_keys
_event_key_timestamp_key)ri   rr   rs   rt   ru   rv   rx   ry   rz   r{   	classnamestylesr   r&   r&   r'   __init__e  s>   

zConsoleRenderer.__init__valr   r!   c                 C  s(   | j du r	t|S t|tr|S t|S )zj
        Determine representation of *val* depending on its type &
        self._repr_native_str.
        T)r   repr
isinstancer   )ri   r   r&   r&   r'   _repr  s
   

zConsoleRenderer._reprloggerr   name
event_dictr   c                   s  t  } jd }|d ur|jjt| jj d   dd }|d urA|dj	|d t
|j jj d   jd }t|tsQt|} r`t
|jjj d }n|jj7 }|jj|   dd }|d u r dd }|d ur|djj jj | jj d   dd }	 d	d }
 d
d }  }jrt|}|d fdd|D  |	d ur|d|	  |s|
d ur|d |rt|}|dkr|| | S |
d urjturtjddd |d|
  | S )Nr"   level[rH   z] r   r4   stack	exceptionrJ   c                 3  sF    | ]}j j| j j d  j j  |  j j V  qdS )=N)r   r5   r*   r6   r   ).0r   r   ri   r&   r'   	<genexpr>  s"    
z+ConsoleRenderer.__call__.<locals>.<genexpr>rL   zR

===============================================================================
)NNNzQRemove `format_exc_info` from your processor chain if you want pretty exceptions.   )
stacklevel)r   popr   rM   r   r3   r   r*   r   getr(   r   r   r   r   r+   r4   r   r   sortedrn   r   r   r   warningswarngetvalue)ri   r   r   r   rI   tsr   rq   r4   r   excrJ   event_dict_keysr&   r   r'   rk     s   
	
	


zConsoleRenderer.__call__c              	   C  s2   | rt nt}|j|j|j|j|j|j|j|jdS )a  
        Get the default styles for log levels

        This is intended to be used with `ConsoleRenderer`'s ``level_styles``
        parameter.  For example, if you are adding custom levels in your
        home-grown :func:`~structlog.stdlib.add_log_level` you could do::

            my_styles = ConsoleRenderer.get_default_level_styles()
            my_styles["EVERYTHING_IS_ON_FIRE"] = my_styles["critical"] renderer
            = ConsoleRenderer(level_styles=my_styles)

        Arguments:

            colors:
                Whether to use colorful styles. This must match the *colors*
                parameter to `ConsoleRenderer`. Default: `True`.
        )criticalr   errorr   warninginfodebugnotset)	r;   rG   r,   r-   r.   r/   r0   r1   r2   )rs   r   r&   r&   r'   r     s   z(ConsoleRenderer.get_default_level_styles)rr   r    rs   rR   rt   rR   ru   rR   rv   rw   rx   r   ry   rR   rz   r   r{   r   )r   r   r!   r   )r   r   r   r   r   r   r!   r   )T)rs   rR   r!   r   )r7   r8   r9   rm   _EVENT_WIDTH_has_colorsdefault_exception_formatterr   r   rk   staticmethodr   r&   r&   r&   r'   r     s     X
8
[r   r   r   method_namer   r   c                 C  s(   |dks| dttur|S d|d< |S )z
    Set ``event_dict["exc_info"] = True`` if *method_name* is ``"exception"``.

    Do nothing if the name is different or ``exc_info`` is already set.
    r   rJ   T)r   	_SENTINEL)r   r   r   r&   r&   r'   set_exc_info)  s
   	r   )r   r   r   r    r!   r   rl   )r   r   r   r   r   r   r!   r   )Grm   
__future__r   re   sysr   dataclassesr   ior   typesr   typingr   r   r   r	   r
   r   r   r   _framesr   
processorsr   r   r   r   r   r|   ImportErrorro   richrich.consoler   rich.tracebackr   __all__platformr   r   r   r(   Styler<   r=   rB   Forer>   rC   rD   rE   r?   r@   BackrA   r   _use_colorsr)   Stylesr;   rG   r   rO   r   r   r   r   objectr   r   r&   r&   r&   r'   <module>   s   (

	

7


  