
    	h'                       d Z ddlmZ ddlZddlZddl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 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 d	dlmZmZ  G d de          Z G d d          Ze efZ!d#dZ" G d d          Z# G d de          Z ee           G d d                      Z$ ee           G d d                      Z%d$d Z& G d! d"          Z'dS )%z
Processors and tools specific to the `Twisted <https://twisted.org/>`_
networking engine.

See also :doc:`structlog's Twisted support <twisted>`.
    )annotationsN)AnyCallableSequenceTextIOlog)Failure)ILogObservertextFromEventDict)implementer   )BoundLoggerBase)_BUILTIN_DEFAULT_PROCESSORS)until_not_interrupted)JSONRenderer)	EventDictWrappedLoggerc                  &    e Zd ZdZd
ddZd
dd	ZdS )BoundLoggera  
    Twisted-specific version of `structlog.BoundLogger`.

    Works exactly like the generic one except that it takes advantage of
    knowing the logging methods in advance.

    Use it like::

        configure(
            wrapper_class=structlog.twisted.BoundLogger,
        )

    Nevent
str | Nonekwr   returnc                      | j         d|fi |S )zG
        Process event and call ``log.msg()`` with the result.
        msg_proxy_to_loggerselfr   r   s      ^/var/www/html/web-builder-api.evdpl.com/venv/lib/python3.11/site-packages/structlog/twisted.pyr   zBoundLogger.msg/   !     %t$UE88R888    c                      | j         d|fi |S )zG
        Process event and call ``log.err()`` with the result.
        errr   r   s      r!   r%   zBoundLogger.err5   r"   r#   N)r   r   r   r   r   r   )__name__
__module____qualname____doc__r   r%    r#   r!   r   r       sP         9 9 9 9 99 9 9 9 9 9 9r#   r   c                      e Zd ZdZddZdS )	LoggerFactoryz
    Build a Twisted logger when an *instance* is called.

    >>> from structlog import configure
    >>> from structlog.twisted import LoggerFactory
    >>> configure(logger_factory=LoggerFactory())
    argsr   r   r   c                    t           S )z
        Positional arguments are silently ignored.

        :rvalue: A new Twisted logger.

        .. versionchanged:: 0.4.0
            Added support for optional positional arguments.
        r   )r    r.   s     r!   __call__zLoggerFactory.__call__E   s	     
r#   N)r.   r   r   r   r'   r(   r)   r*   r0   r+   r#   r!   r-   r-   <   s2         	 	 	 	 	 	r#   r-   	eventDictr   r   tuple[Any, Any, EventDict]c                   |                      dd          }|                      dd          }|                      dd          }t          |t                    r$t          |t                    rt          d          |r$t          |t                    rt          d          t          |t                    st          |t                    r|pd}|}t          |t                    r|}|s%t          j                    dk    rt                      }t          |t                    r$t          |t                    st          |          }||| fS )	z
    Removes all possible *_why*s and *_stuff*s, analyzes exc_info and returns
    a tuple of ``(_stuff, _why, eventDict)``.

    **Modifies** *eventDict*!
    _stuffN_whyr   z3Both _stuff and event contain an Exception/Failure.z!Both `_why` and `event` supplied.error)NNN)	pop
isinstance_FAIL_TYPES
ValueErrorstrsysexc_infor
   BaseException)r2   r5   r6   r   s       r!   _extractStuffAndWhyr@   T   sD    ]]8T**F==&&DMM'4((E&+&& P:e[+I+I PNOOO  >
5#&& ><=== fk** z%/M/M w%  clnn(::: &-(( !FG1L1L !4""r#   c                  *    e Zd ZdZddZdd
ZddZdS )ReprWrappera  
    Wrap a string and return it as the ``__repr__``.

    This is needed for ``twisted.python.log.err`` that calls `repr` on
    ``_stuff``:

    >>> repr("foo")
    "'foo'"
    >>> repr(ReprWrapper("foo"))
    'foo'

    Note the extra quotes in the unwrapped example.
    stringr<   r   Nonec                    || _         d S r&   rC   )r    rC   s     r!   __init__zReprWrapper.__init__   s    r#   otherobjectboolc                L    t          || j                  o| j        |j        k    S )z5
        Check for equality, just for tests.
        )r9   	__class__rC   )r    rH   s     r!   __eq__zReprWrapper.__eq__   s&    
 udn--M$+2M	
r#   c                    | j         S r&   rF   )r    s    r!   __repr__zReprWrapper.__repr__   s
    {r#   N)rC   r<   r   rD   )rH   rI   r   rJ   )r   r<   )r'   r(   r)   r*   rG   rM   rO   r+   r#   r!   rB   rB   y   sZ            
 
 
 
     r#   rB   c                      e Zd ZdZdd
ZdS )r   a  
    Behaves like `structlog.processors.JSONRenderer` except that it formats
    tracebacks and failures itself if called with ``err()``.

    .. note::

        This ultimately means that the messages get logged out using ``msg()``,
        and *not* ``err()`` which renders failures in separate lines.

        Therefore it will break your tests that contain assertions using
        `flushLoggedErrors
        <https://docs.twisted.org/en/stable/api/
        twisted.trial.unittest.SynchronousTestCase.html#flushLoggedErrors>`_.

    *Not* an adapter like `EventAdapter` but a real formatter.  Also does *not*
    require to be adapted using it.

    Use together with a `JSONLogObserverWrapper`-wrapped Twisted logger like
    `plainJSONStdOutLogger` for pure-JSON logs.
    loggerr   namer<   r2   r   r   $tuple[Sequence[Any], dict[str, Any]]c                    t          |          \  }}}|dk    rH||d<   t          |t                    r-|                    d          |d<   |                                 n||d<   t          t          j        | |||                    fddifS )Nr%   r   verbose)detail	exception
_structlogT)r@   r9   r
   getTracebackcleanFailurerB   GenericJSONRendererr0   r    rQ   rR   r2   r5   r6   s         r!   r0   zJSONRenderer.__call__   s     #6i"@"@i5==!%Ig&'** &)/)<)<I)<)N)N	+&##%%%!%Ig '0fdI   4 	
 		
r#   N)rQ   r   rR   r<   r2   r   r   rS   r1   r+   r#   r!   r   r      s2         *
 
 
 
 
 
r#   r   c                  "    e Zd ZdZddZdd	Zd
S )PlainFileLogObserverz
    Write only the plain message without timestamps or anything else.

    Great to just print JSON to stdout where you catch it with something like
    runit.

    Arguments:

        file: File to print to.

    .. versionadded:: 0.2.0
    filer   r   rD   c                6    |j         | _        |j        | _        d S r&   )write_writeflush_flush)r    r_   s     r!   rG   zPlainFileLogObserver.__init__   s    jjr#   r2   r   c                x    t          | j        t          |          dz              t          | j                   d S )N
)r   rb   r   rd   r    r2   s     r!   r0   zPlainFileLogObserver.__call__   sD    Ki((	
 	
 	

 	dk*****r#   N)r_   r   r   rD   )r2   r   r   rD   r'   r(   r)   r*   rG   r0   r+   r#   r!   r^   r^      sF         ! ! ! !+ + + + + +r#   r^   c                  "    e Zd ZdZddZdd
ZdS )JSONLogObserverWrappera  
    Wrap a log *observer* and render non-`JSONRenderer` entries to JSON.

    Arguments:

        observer (ILogObserver):
            Twisted log observer to wrap.  For example
            :class:`PlainFileObserver` or Twisted's stock `FileLogObserver
            <https://docs.twisted.org/en/stable/api/
            twisted.python.log.FileLogObserver.html>`_

    .. versionadded:: 0.2.0
    observerr   r   rD   c                    || _         d S r&   )	_observer)r    rk   s     r!   rG   zJSONLogObserverWrapper.__init__   s    !r#   r2   r   r<   c                    d|vr@t          j        t          |          |                    d          d          f|d<   d|d<   |                     |          S )NrX   system)r   ro   messageT)jsondumpsr   getrm   rg   s     r!   r0   zJSONLogObserverWrapper.__call__   sq    y((
!2%" " #,--"9"9	  	$Ii  '+Il#~~i(((r#   N)rk   r   r   rD   )r2   r   r   r<   rh   r+   r#   r!   rj   rj      sF         " " " ") ) ) ) ) )r#   rj   c                 N    t          t          t          j                            S )a  
    Return a logger that writes only the message to stdout.

    Transforms non-`JSONRenderer` messages to JSON.

    Ideal for JSONifying log entries from Twisted plugins and libraries that
    are outside of your control::

        $ twistd -n --logger structlog.twisted.plainJSONStdOutLogger web
        {"event": "Log opened.", "system": "-"}
        {"event": "twistd 13.1.0 (python 2.7.3) starting up.", "system": "-"}
        {"event": "reactor class: twisted...EPollReactor.", "system": "-"}
        {"event": "Site starting on 8080", "system": "-"}
        {"event": "Starting factory <twisted.web.server.Site ...>", ...}
        ...

    Composes `PlainFileLogObserver` and `JSONLogObserverWrapper` to a usable
    logger.

    .. versionadded:: 0.2.0
    )rj   r^   r=   stdoutr+   r#   r!   plainJSONStdOutLoggerrv     s    , ""6sz"B"BCCCr#   c                  &    e Zd ZdZ	 dddZddZdS )EventAdapterax  
    Adapt an ``event_dict`` to Twisted logging system.

    Particularly, make a wrapped `twisted.python.log.err
    <https://docs.twisted.org/en/stable/api/twisted.python.log.html#err>`_
    behave as expected.

    Arguments:

        dictRenderer:
            Renderer that is used for the actual log message. Please note that
            structlog comes with a dedicated `JSONRenderer`.

    **Must** be the last processor in the chain and requires a *dictRenderer*
    for the actual formatting as an constructor argument in order to be able to
    fully support the original behaviors of ``log.msg()`` and ``log.err()``.
    NdictRenderer5Callable[[WrappedLogger, str, EventDict], str] | Noner   rD   c                .    |pt           d         | _        d S )N)r   _dictRenderer)r    ry   s     r!   rG   zEventAdapter.__init__3  s    
 *L-H-Lr#   rQ   r   rR   r<   r2   r   r   c                    |dk    r4t          |          \  }}}||d<   d||                     |||          dfS |                     |||          S )Nr%   r   r+   )r5   r6   )r@   r}   r\   s         r!   r0   zEventAdapter.__call__:  sx     5==
 ':)&D&D#FD)!%Ig $ ..vtYGG   !!&$	:::r#   r&   )ry   rz   r   rD   )rQ   r   rR   r<   r2   r   r   r   rh   r+   r#   r!   rx   rx      sU         * M M M M M; ; ; ; ; ;r#   rx   )r2   r   r   r3   )r   rj   )(r*   
__future__r   rq   r=   typingr   r   r   r   twisted.pythonr	   twisted.python.failurer
   twisted.python.logr   r   zope.interfacer   _baser   _configr   _utilsr   
processorsr   r[   r   r   r   r-   r?   r:   r@   rB   r^   rj   rv   rx   r+   r#   r!   <module>r      s    # " " " " "  



 2 2 2 2 2 2 2 2 2 2 2 2       * * * * * * > > > > > > > > & & & & & & " " " " " " 0 0 0 0 0 0 ) ) ) ) ) ) ; ; ; ; ; ; , , , , , , , ,9 9 9 9 9/ 9 9 98       * g&"# "# "# "#J       <-
 -
 -
 -
 -
& -
 -
 -
` \+ + + + + + + +6 \ )  )  )  )  )  )  )  )FD D D D2-; -; -; -; -; -; -; -; -; -;r#   