o
    >h4                     @  s4  d 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 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 d	dlmZ dZdAddZdBddZedfdCddZefdDd d!ZdEd&d'ZG d(d% d%e Z!G d)d* d*e	e"ef Z#dFd.d/Z$dAd0d1Z%dGd4d5Z&dAd6d7Z'd8d8d8efdHd>d?Z(d@S )Iz&
Tools for formatting logging events.
    )annotations)datetime)AnyCallableIteratorMappingOptionalUnioncast)NamedConstant)FixedOffsetTimeZone)Failure)	safe_repr   )
aFormatter
flatFormat)LogEventz%Y-%m-%dT%H:%M:%S%zeventr   returnstrc                 C  s   t | ddddS )aa  
    Formats an event as text, using the format in C{event["log_format"]}.

    This implementation should never raise an exception; if the formatting
    cannot be done, the returned string will describe the event generically so
    that a useful message is emitted regardless.

    @param event: A logging event.

    @return: A formatted string.
    F)includeTracebackincludeTimestampincludeSystem)eventAsText)r    r   v/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/logger/_format.pyformatEvent   s   r   errorBaseExceptionc                 C  sV   zdj | |dW S  ty*   t }ddd |  D }dj t|||d Y S w )z
    Formats an event as text that describes the event generically and a
    formatting error.

    @param event: A logging event.
    @param error: The formatting error.

    @return: A formatted string.
    z)Unable to format event {event!r}: {error})r   r   z, c                 s  s*    | ]\}}d  t|t|fV  qdS )z = N)joinr   ).0keyvaluer   r   r   	<genexpr>C   s
    
z+formatUnformattableEvent.<locals>.<genexpr>zrMESSAGE LOST: unformattable object logged: {error}
Recoverable data: {text}
Exception during formatting:
{failure})r   failuretext)formatr   r   r   itemsr   )r   r   r$   r%   r   r   r   formatUnformattableEvent-   s   


r(   -whenOptional[float]
timeFormatOptional[str]defaultc                 C  s8   |du s| du r
|S t | }t| |}t||S )a  
    Format a timestamp as text.

    Example::

        >>> from time import time
        >>> from twisted.logger import formatTime
        >>>
        >>> t = time()
        >>> formatTime(t)
        u'2013-10-22T14:19:11-0700'
        >>> formatTime(t, timeFormat="%Y/%W")  # Year and week number
        u'2013/42'
        >>>

    @param when: A timestamp.
    @param timeFormat: A time format.
    @param default: Text to return if C{when} or C{timeFormat} is L{None}.

    @return: A formatted time.
    N)r   fromLocalTimeStampDateTimefromtimestampr   strftime)r*   r,   r.   tzr   r   r   r   
formatTimeQ   s
   
r4    Callable[[Optional[float]], str]c                 C  s(   t | |d}|s
dS |dd}|d S )a&  
    Format an event as a line of human-readable text for, e.g. traditional log
    file output.

    The output format is C{"{timeStamp} [{system}] {event}\n"}, where:

        - C{timeStamp} is computed by calling the given C{formatTime} callable
          on the event's C{"log_time"} value

        - C{system} is the event's C{"log_system"} value, if set, otherwise,
          the C{"log_namespace"} and C{"log_level"}, joined by a C{"#"}.  Each
          defaults to C{"-"} is not set.

        - C{event} is the event, as formatted by L{formatEvent}.

    Example::

        >>> from time import time
        >>> from twisted.logger import formatEventAsClassicLogText
        >>> from twisted.logger import LogLevel
        >>>
        >>> formatEventAsClassicLogText(dict())  # No format, returns None
        >>> formatEventAsClassicLogText(dict(log_format="Hello!"))
        u'- [-#-] Hello!\n'
        >>> formatEventAsClassicLogText(dict(
        ...     log_format="Hello!",
        ...     log_time=time(),
        ...     log_namespace="my_namespace",
        ...     log_level=LogLevel.info,
        ... ))
        u'2013-10-22T17:30:02-0700 [my_namespace#info] Hello!\n'
        >>> formatEventAsClassicLogText(dict(
        ...     log_format="Hello!",
        ...     log_time=time(),
        ...     log_system="my_system",
        ... ))
        u'2013-11-11T17:22:06-0800 [my_system] Hello!\n'
        >>>

    @param event: an event.
    @param formatTime: A time formatter

    @return: A formatted event, or L{None} if no output is appropriate.
    )r4   N
z
	)r   replace)r   r4   	eventTextr   r   r   formatEventAsClassicLogTexts   s
   /r9   r!   getterCallable[[str], Any]PotentialCallWrapperc                 C  s8   |  d}|r| dd n| }||}|r| }t|S )at  
    Check to see if C{key} ends with parentheses ("C{()}"); if not, wrap up the
    result of C{get} in a L{PotentialCallWrapper}.  Otherwise, call the result
    of C{get} first, before wrapping it up.

    @param key: The last dotted segment of a formatting key, as parsed by
        L{Formatter.vformat}, which may end in C{()}.

    @param getter: A function which takes a string and returns some other
        object, to be formatted and stringified for a log.

    @return: A L{PotentialCallWrapper} that will wrap up the result to allow
        for subsequent usages of parens to defer execution to log-format time.
    z()N)endswithr<   )r!   r:   callitrealKeyr"   r   r   r   keycall   s   
rA   c                   @  sL   e Zd ZdZdddZdd
dZdddZdddZdddZdddZ	dS )r<   z
    Object wrapper that wraps C{getattr()} so as to process call-parentheses
    C{"()"} after a dotted attribute access.
    wrappedobjectr   Nonec                 C  s
   || _ d S N)_wrapped)selfrB   r   r   r   __init__      
zPotentialCallWrapper.__init__namer   c                   s   t | fddS )Nc                   s   t  j| S rE   )getattrrF   )name_rG   r   r   <lambda>   s    z2PotentialCallWrapper.__getattr__.<locals>.<lambda>)rA   )rG   rJ   r   rM   r   __getattr__   s   z PotentialCallWrapper.__getattr__c                 C  s   | j | }t|S rE   )rF   r<   )rG   rJ   r"   r   r   r   __getitem__   s   
z PotentialCallWrapper.__getitem__format_specc                 C  s   t | j|S rE   )r&   rF   )rG   rQ   r   r   r   
__format__   s   zPotentialCallWrapper.__format__c                 C  
   t | jS rE   )reprrF   rM   r   r   r   __repr__   rI   zPotentialCallWrapper.__repr__c                 C  rS   rE   )r   rF   rM   r   r   r   __str__   rI   zPotentialCallWrapper.__str__N)rB   rC   r   rD   )rJ   r   r   rC   )rQ   r   r   r   )r   r   )
__name__
__module____qualname____doc__rH   rO   rP   rR   rU   rV   r   r   r   r   r<      s    




c                   @  s8   e Zd ZdZdddZdd	d
ZdddZdddZdS )CallMappingz
    Read-only mapping that turns a C{()}-suffix in key names into an invocation
    of the key rather than a lookup of the key.

    Implementation support for L{formatWithCall}.
    
submappingMapping[str, Any]r   rD   c                 C  s
   || _ dS )zo
        @param submapping: Another read-only mapping which will be used to look
            up items.
        N)_submapping)rG   r\   r   r   r   rH      s   
zCallMapping.__init__Iterator[Any]c                 C  rS   rE   )iterr^   rM   r   r   r   __iter__   rI   zCallMapping.__iter__intc                 C  rS   rE   )lenr^   rM   r   r   r   __len__   rI   zCallMapping.__len__r!   r   r   c                 C  s   t || jjS )z|
        Look up an item in the submapping for this L{CallMapping}, calling it
        if C{key} ends with C{"()"}.
        )rA   r^   rP   )rG   r!   r   r   r   rP      s   zCallMapping.__getitem__N)r\   r]   r   rD   )r   r_   )r   rb   )r!   r   r   r   )rW   rX   rY   rZ   rH   ra   rd   rP   r   r   r   r   r[      s    


r[   formatStringmappingr]   c                 C  s   t t| dt|S )a  
    Format a string like L{str.format}, but:

        - taking only a name mapping; no positional arguments

        - with the additional syntax that an empty set of parentheses
          correspond to a formatting item that should be called, and its result
          C{str}'d, rather than calling C{str} on the element directly as
          normal.

    For example::

        >>> formatWithCall("{string}, {function()}.",
        ...                dict(string="just a string",
        ...                     function=lambda: "a function"))
        'just a string, a function.'

    @param formatString: A PEP-3101 format string.
    @param mapping: A L{dict}-like object to format.

    @return: The string with formatted values interpolated.
    r   )r   r   vformatr[   )re   rf   r   r   r   formatWithCall   s   rh   c              
   C  s   z=d| v r
t | W S tttttf  | dd}|du r W dS t|tr&nt|tr1|d}nt	d|t
|| W S  tyT } zt| |W  Y d}~S d}~ww )ae  
    Formats an event as a string, using the format in C{event["log_format"]}.

    This implementation should never raise an exception; if the formatting
    cannot be done, the returned string will describe the event generically so
    that a useful message is emitted regardless.

    @param event: A logging event.

    @return: A formatted string.
    log_flattened
log_formatN zutf-8zLog format must be str, not )r   r
   r   r	   r   bytesget
isinstancedecode	TypeErrorrh   r   r(   )r   r&   er   r   r   _formatEvent  s    


rr   r$   r   c              
   C  sB   z|   }W |S  ty  } zdt| }W Y d}~|S d}~ww )a^  
    Format a failure traceback, assuming UTF-8 and using a replacement
    strategy for errors.  Every effort is made to provide a usable
    traceback, but should not that not be possible, a message and the
    captured exception are logged.

    @param failure: The failure to retrieve a traceback from.

    @return: The formatted traceback.
    z((UNABLE TO OBTAIN TRACEBACK FROM EVENT):N)getTracebackr   r   )r$   	tracebackrq   r   r   r   _formatTraceback5  s   
ru   c                 C  s   t tt | dd}|du r4t tt | dd}|du r!d}n|j}djt t| dd|d}|S zt|}W |S  tyG   d}Y |S w )	a  
    Format the system specified in the event in the "log_system" key if set,
    otherwise the C{"log_namespace"} and C{"log_level"}, joined by a C{"#"}.
    Each defaults to C{"-"} is not set.  If formatting fails completely,
    "UNFORMATTABLE" is returned.

    @param event: The event containing the system specification.

    @return: A formatted string representing the "log_system" key.
    
log_systemN	log_levelr)   z{namespace}#{level}log_namespace)	namespacelevelUNFORMATTABLE)r
   r   r   rm   r   rJ   r&   	Exception)r   systemrz   	levelNamer   r   r   _formatSystemG  s$   	
r   Tr   boolr   r   Callable[[float], str]c           
   	   C  s   t | }|rd| v r| d }t|}d||f}|s|S d}|r1d|tt| dddg}d}	|r@ddt| ddg}	d	j||	|d
S )ah  
    Format an event as text.  Optionally, attach timestamp, traceback, and
    system information.

    The full output format is:
    C{"{timeStamp} [{system}] {event}\n{traceback}\n"} where:

        - C{timeStamp} is the event's C{"log_time"} value formatted with
          the provided C{formatTime} callable.

        - C{system} is the event's C{"log_system"} value, if set, otherwise,
          the C{"log_namespace"} and C{"log_level"}, joined by a C{"#"}.  Each
          defaults to C{"-"} is not set.

        - C{event} is the event, as formatted by L{formatEvent}.

        - C{traceback} is the traceback if the event contains a
          C{"log_failure"} key.  In the event the original traceback cannot
          be formatted, a message indicating the failure will be substituted.

    If the event cannot be formatted, and no traceback exists, an empty string
    is returned, even if includeSystem or includeTimestamp are true.

    @param event: A logging event.
    @param includeTraceback: If true and a C{"log_failure"} key exists, append
        a traceback.
    @param includeTimestamp: If true include a formatted timestamp before the
        event.
    @param includeSystem:  If true, include the event's C{"log_system"} value.
    @param formatTime: A time formatter

    @return: A formatted string with specified options.

    @since: Twisted 18.9.0
    log_failurer6   rk   log_timeN []z{timeStamp}{system}{eventText})	timeStampr}   r8   )rr   ru   r   r
   floatrm   r   r&   )
r   r   r   r   r4   r8   frt   r   r}   r   r   r   r   f  s$   * r   N)r   r   r   r   )r   r   r   r   r   r   )r*   r+   r,   r-   r.   r   r   r   )r   r   r4   r5   r   r-   )r!   r   r:   r;   r   r<   )re   r   rf   r]   r   r   )r$   r   r   r   )r   r   r   r   r   r   r   r   r4   r   r   r   ))rZ   
__future__r   r   r0   typingr   r   r   r   r   r	   r
   
constantlyr   twisted.python._tzhelperr   twisted.python.failurer   twisted.python.reflectr   _flattenr   r   _interfacesr   timeFormatRFC3339r   r(   r4   r9   rA   rC   r<   r   r[   rh   rr   ru   r   r   r   r   r   r   <module>   s<   $

&#
6


"
!