o
    >h[                     @   s8  d Z ddlmZmZmZmZmZ z
ddlmZ e W n e	y%   dZY nw ddl
mZ ddl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mZmZ d	dlmZ d	dlmZ G dd dejZ G dd dejZ!G dd dejZ"G dd dejZ#G dd dZ$G dd de%Z&G dd dejZ'dS )z+
Test cases for L{twisted.logger._format}.
    )AnyAnyStrDictOptionalcast)tzsetN)Failure)addTZCleanupmktimesetTZ)unittest)SkipTest   )eventAsTextformatEventformatEventAsClassicLogText
formatTimeformatUnformattableEventformatWithCall)LogEvent)LogLevelc                   @   s   e Zd ZdZdededefd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ddZdddZdddZdS )FormattingTestsz5
    Tests for basic event formatting functions.
    	logFormateventreturnc                 K   s$   ||d< t |}| t|t |S )z
        Create a Twisted log event dictionary from C{event} with the given
        C{logFormat} format string, format it with L{formatEvent}, ensure that
        its type is L{str}, and return its result.
        
log_format)r   assertIstypestr)selfr   r   result r!   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/logger/test/test_format.pyformat'   s   zFormattingTests.formatNc                 C   s   |  d| d |  d| d |  d| jddd |  d| jddd	d
 d |  d| d | d| d | jddd}| d| ttd}| d| d | jddd dS )ac  
        L{formatEvent} will format an event according to several rules:

            - A string with no formatting instructions will be passed straight
              through.

            - PEP 3101 strings will be formatted using the keys and values of
              the event as named fields.

            - PEP 3101 keys ending with C{()} will be treated as instructions
              to call that key (which ought to be a callable) before
              formatting.

        L{formatEvent} will always return L{str}, and if given bytes, will
        always treat its format string as UTF-8 encoded.
             abcz{x})xzno, yes.z{not_called}, {called()}.noc                   S      dS )Nyesr!   r!   r!   r!   r"   <lambda>I       z2FormattingTests.test_formatEvent.<locals>.<lambda>)
not_calledcalledu   Sánchezs   SánchezUnable to format events   Snchezs   S{a!s}nchez   )azSb'\xe1'nchezSnchezs   S{a!r}nchezN)assertEqualr#   assertInr   repr)r   maybeResultxe1r!   r!   r"   test_formatEvent2   s   
"z FormattingTests.test_formatEventc                 C   s*   G dd d}|  d| jd| d dS )zq
        L{formatEvent} will format PEP 3101 keys containing C{.}s ending with
        C{()} as methods.
        c                   @   s   e Zd ZdefddZdS )z0FormattingTests.test_formatMethod.<locals>.Worldr   c                 S   r)   )Nworldr!   r   r!   r!   r"   where[   s   z6FormattingTests.test_formatMethod.<locals>.World.whereN)__name__
__module____qualname__r   r<   r!   r!   r!   r"   WorldZ   s    r@   zhello worldzhello {what.where()})whatN)r4   r#   )r   r@   r!   r!   r"   test_formatMethodT   s   z!FormattingTests.test_formatMethodc                 C   s,   G dd dt }| d| jd| d dS )zS
        L{formatEvent} will format subscripts of attributes per PEP 3101.
        c                   @   s*   e Zd ZU edddZeeef ed< dS )z>FormattingTests.test_formatAttributeSubscript.<locals>.Examplebarqux)foobazconfigN)r=   r>   r?   dictrG   r   r   __annotations__r!   r!   r!   r"   Exampleg   s   
  rJ   zbar quxz+{example.config[foo]} {example.config[baz]})exampleN)objectr4   r#   )r   rJ   r!   r!   r"   test_formatAttributeSubscriptb   s   z-FormattingTests.test_formatAttributeSubscriptc                 C   s$   t ddd}t|}| d| dS )z5
        Formatting an event with no format.
           r   )rE   rC   r$   N)rH   r   r4   r   r   r    r!   r!   r"   test_formatEventNoFormatr   s   z(FormattingTests.test_formatEventNoFormatc                 C   s8   t t ddd}t|}| d| | t|| dS )z:
        Formatting an event with a bogus format.
        rN   r   )r   rE   rC   zLog format must be strN)rH   rL   r   r5   r6   rO   r!   r!   r"   test_formatEventWeirdFormat{      z+FormattingTests.test_formatEventWeirdFormatc                 C   s8   t ddd d}t|}| d| | t|| dS )zF
        Formatting an event that's just plain out to get us.
        {evil()}c                   S      dd S NrN   r   r!   r!   r!   r!   r"   r+          z?FormattingTests.test_formatUnformattableEvent.<locals>.<lambda>r   evilr/   N)rH   r   r5   r6   rO   r!   r!   r"   test_formatUnformattableEvent   rR   z-FormattingTests.test_formatUnformattableEventc                 C   sL   ddddd t tt di}t|}| d| | d| | d	| d
S )zR
        Formatting an unformattable event that has an unformattable key.
        r   rS   rX   c                   S   rT   rU   r!   r!   r!   r!   r"   r+      rV   zSFormattingTests.test_formatUnformattableEventWithUnformattableKey.<locals>.<lambda>gurk*MESSAGE LOST: unformattable object logged:Recoverable data:Exception during formatting:N)r   r   Unformattabler   r5   rO   r!   r!   r"   1test_formatUnformattableEventWithUnformattableKey   s   zAFormattingTests.test_formatUnformattableEventWithUnformattableKeyc                 C   sD   t ddd t d}t|}| d| | d| | d| dS )	T
        Formatting an unformattable event that has an unformattable value.
        rS   c                   S   rT   rU   r!   r!   r!   r!   r"   r+      rV   zUFormattingTests.test_formatUnformattableEventWithUnformattableValue.<locals>.<lambda>)r   rX   rZ   r[   r\   r]   N)rH   r^   r   r5   rO   r!   r!   r"   3test_formatUnformattableEventWithUnformattableValue   s   zCFormattingTests.test_formatUnformattableEventWithUnformattableValuec                 C   sP   t ddd dd}t|ttt }| d| | tdd td | d	S )
r`   rS   c                   S   rT   rU   r!   r!   r!   r!   r"   r+      rV   zbFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStop.<locals>.<lambda>okay)r   rX   recoverabler[   rc   z = N)rH   r   r   BaseExceptionr^   r5   r6   rO   r!   r!   r"   @test_formatUnformattableEventWithUnformattableErrorOMGWillItStop   s    zPFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStopr   N)r=   r>   r?   __doc__r   rL   r   r#   r9   rB   rM   rP   rQ   rY   r_   ra   re   r!   r!   r!   r"   r   "   s    

"


	




r   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 )TimeFormattingTestsz.
    Tests for time formatting functions.
    r   Nc                 C   s   t |  d S )N)r	   r;   r!   r!   r"   setUp      zTimeFormattingTests.setUpc                    sf   t du rtddtdtt dtddf fdd}|d	dd
 |ddd |ddd |ddd dS )z
        Default time stamp format is RFC 3339 and offset respects the timezone
        as set by the standard C{TZ} environment variable and L{tzset} API.
        N:Platform cannot change timezone; unable to verify offsets.nameexpectedDSTexpectedSTDr   c                    sD   t |  td} t|| |r td} t|| d S d S )N)	i  rN      r   r   r   r   ro   r   )	i        r   r   r         rN   )r   r
   r4   r   )rl   rm   rn   localSTDlocalDSTr;   r!   r"   testForTimeZone   s   zMTimeFormattingTests.test_formatTimeWithDefaultFormat.<locals>.testForTimeZoneUTC+00z2007-01-31T00:00:00+0000zEST+05EDT,M4.1.0,M10.5.0z2006-06-30T00:00:00-0400z2007-01-31T00:00:00-0500zCEST-01CEDT,M4.1.0,M10.5.0z2006-06-30T00:00:00+0200z2007-01-31T00:00:00+0100zCST+06z2007-01-31T00:00:00-0600)r   r   r   r   )r   rv   r!   r;   r"    test_formatTimeWithDefaultFormat   s>   z4TimeFormattingTests.test_formatTimeWithDefaultFormatc                 C   s(   |  tdd |  tdddd dS )zL
        If C{when} argument is L{None}, we get the default output.
        N-!)defaultr4   r   r;   r!   r!   r"   test_formatTimeWithNoTime   s   z-TimeFormattingTests.test_formatTimeWithNoTimec                 C   s6   t d}| t|ddd | t|dddd dS )zR
        If C{timeFormat} argument is L{None}, we get the default output.
        	i  	         (   /   rN   i  N
timeFormatry   rz   )r   r{   r
   r4   r   r   tr!   r!   r"   test_formatTimeWithNoFormat   s   z/TimeFormattingTests.test_formatTimeWithNoFormatc                 C   s    t d}| t|ddd dS )z2
        Alternate time format in output.
        r~   z%Y/%Wr   z2013/38Nr   r   r!   r!   r"   &test_formatTimeWithAlternateTimeFormat  s   z:TimeFormattingTests.test_formatTimeWithAlternateTimeFormatc                 C   s   |  tdddd dS )z0
        "%f" supported in time format.
        g?x.Az%fr   234560Nr|   r;   r!   r!   r"   test_formatTimePercentF  s   z+TimeFormattingTests.test_formatTimePercentFrf   )
r=   r>   r?   rg   ri   rx   r}   r   r   r   r!   r!   r!   r"   rh      s    


0

rh   c                   @   s~   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ddZ
dddZdddZdddZdddZdS )ClassicLogFormattingTestsz@
    Tests for classic text log event formatting functions.
    r   Nc                 C   sH   t du rtdt|  td td}td|d}| t|d dS )z
        Time is first field.  Default time stamp format is RFC 3339 and offset
        respects the timezone as set by the standard C{TZ} environment variable
        and L{tzset} API.
        Nrk   rw   r~   XYZZYr   log_timez%2013-09-24T11:40:47+0000 [-#-] XYZZY
)r   r   r	   r   r
   rH   r4   r   )r   r   r   r!   r!   r"   test_formatTimeDefault  s   z0ClassicLogFormattingTests.test_formatTimeDefaultc                 C   s:   dt t dtfdd}tddd}| t||dd	 d
S )zc
        Time is first field.  Custom formatting function is an optional
        argument.
        r   r   c                 S   s   d|  dS )N__r!   )r   r!   r!   r"   r   -  rj   zCClassicLogFormattingTests.test_formatTimeCustom.<locals>.formatTimer   i90  r   )r   z__12345__ [-#-] XYZZY
N)r   floatr   rH   r4   r   )r   r   r   r!   r!   r"   test_formatTimeCustom'  s   
z/ClassicLogFormattingTests.test_formatTimeCustomc                 C       t ddd}| t|d dS )z:
        Namespace is first part of second field.
        r   my.namespace)r   log_namespacez- [my.namespace#-] XYZZY
NrH   r4   r   r   r   r!   r!   r"   test_formatNamespace6  
   z.ClassicLogFormattingTests.test_formatNamespacec                 C   s"   t dtjd}| t|d dS )z7
        Level is second part of second field.
        r   )r   	log_levelz- [-#warn] XYZZY
NrH   r   warnr4   r   r   r!   r!   r"   test_formatLevel@  
   z*ClassicLogFormattingTests.test_formatLevelc                 C   r   )z)
        System is second field.
        r   S.Y.S.T.E.M.r   
log_system- [S.Y.S.T.E.M.] XYZZY
Nr   r   r!   r!   r"   test_formatSystemJ  r   z+ClassicLogFormattingTests.test_formatSystemc                 C   s&   t ddtjdd}| t|d dS )B
        System is not supplanted by namespace and level.
        r   r   r   )r   r   r   r   r   Nr   r   r!   r!   r"   test_formatSystemRulzT  s   z/ClassicLogFormattingTests.test_formatSystemRulzc                 C   s"   t dt d}| t|d dS )r   r   r   z- [UNFORMATTABLE] XYZZY
N)rH   r^   r4   r   r   r!   r!   r"   test_formatSystemUnformattablec  r   z8ClassicLogFormattingTests.test_formatSystemUnformattablec                 C   r   )z0
        Formatted event is last field.
        zid:{id}123r   idz- [-#-] id:123
Nr   r   r!   r!   r"   test_formatFormatm  r   z+ClassicLogFormattingTests.test_formatFormatc                 C   s   t dd}| t|d dS )z#
        No format string.
        r   )r   NrH   r   r   r   r!   r!   r"   test_formatNoFormatw  s   
z-ClassicLogFormattingTests.test_formatNoFormatc                 C   s    t ddd}| t|d dS )z&
        Empty format string.
        r$   r   r   Nr   r   r!   r!   r"   test_formatEmptyFormat~  s   z0ClassicLogFormattingTests.test_formatEmptyFormatc                 C   s   t dd}| t|d dS )zO
        If the formatted event has newlines, indent additional lines.
        z"XYZZY
A hollow voice says:
"Plugh")r   z-- [-#-] XYZZY
	A hollow voice says:
	"Plugh"
Nr   r   r!   r!   r"   test_formatFormatMultiLine  s
   
z4ClassicLogFormattingTests.test_formatFormatMultiLinerf   )r=   r>   r?   rg   r   r   r   r   r   r   r   r   r   r   r   r!   r!   r!   r"   r     s    














r   c                   @   s   e Zd ZdZdddZdS )FormatFieldTestsz+
    Tests for format field functions.
    r   Nc                 C   s>   |  tdtddd dd |  tdtdd d	d
 dS )z
        L{formatWithCall} is an extended version of L{str.format} that
        will interpret a set of parentheses "C{()}" at the end of a format key
        to mean that the format key ought to be I{called} rather than
        stringified.
        zHello, {world}. {callme()}.earthc                   S   r)   )Nmayber!   r!   r!   r!   r"   r+     r,   z6FormatFieldTests.test_formatWithCall.<locals>.<lambda>)r:   callmezHello, earth. maybe.zHello, {repr()!r}.c                   S   r)   )Nr6   r!   r!   r!   r!   r"   r+     r,   )r6   zHello, 'repr'.N)r4   r   rH   r;   r!   r!   r"   test_formatWithCall  s   z$FormatFieldTests.test_formatWithCallrf   )r=   r>   r?   rg   r   r!   r!   r!   r"   r     s    r   c                   @   s   e Zd ZdZdefddZdS )r^   z>
    An object that raises an exception from C{__repr__}.
    r   c                 C   s   t dd S rU   )r   r;   r!   r!   r"   __repr__  rj   zUnformattable.__repr__N)r=   r>   r?   rg   r   r   r!   r!   r!   r"   r^     s    r^   c                   @   s   e Zd ZdZdS )CapturedErrorz3
    A captured error for use in format tests.
    N)r=   r>   r?   rg   r!   r!   r!   r"   r     s    r   c                   @   s   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 d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 ddZdS )!EventAsTextTestszi
    Tests for L{eventAsText}, all of which ensure that the
    returned type is UTF-8 decoded text.
    r   Nc                 C   sd   zt d t y   t }Y nw ddi}||d< t|ddd}| t| | | d| dS )	zT
        An event with a C{log_failure} key will have a traceback appended.
        This is a fake errorr   This is a test log messagelog_failureTFincludeTimestampincludeSystemNr   r   r   r5   r   getTracebackr   fr   	eventTextr!   r!   r"   test_eventWithTraceback  s   
z(EventAsTextTests.test_eventWithTracebackc                 C   sd   zt d t y   t }Y nw ddi}||d< t|ddd}| t| | | d| dS )	zu
        An event with an empty C{log_format} key appends a traceback from
        the accompanying failure.
        r   r   r$   r   TFr   Nr   r   r!   r!   r"   "test_formatEmptyEventWithTraceback  s   
z3EventAsTextTests.test_formatEmptyEventWithTracebackc                 C   sv   zt d t y   t }Y nw ddd d}||d< t|ddd	}| |t | t| | | d| d
S )zw
        An event with an unformattable value in the C{log_format} key still
        has a traceback appended.
        r   rS   c                   S   rT   rU   r!   r!   r!   r!   r"   r+     rV   zHEventAsTextTests.test_formatUnformattableWithTraceback.<locals>.<lambda>rW   r   TFr   N)r   r   r   assertIsInstancer   r5   r   r   r!   r!   r"   %test_formatUnformattableWithTraceback  s   
z6EventAsTextTests.test_formatUnformattableWithTracebackc                 C   s   zt d t y   t }Y nw ddddd ttt di}||d< t|d	d
d}| |t | d| | t| | | d| dS )z
        An event with an unformattable value in the C{log_format} key, that
        throws an exception when __repr__ is invoked still has a traceback
        appended.
        r   r   rS   rX   c                   S   rT   rU   r!   r!   r!   r!   r"   r+     rV   zMEventAsTextTests.test_formatUnformattableErrorWithTraceback.<locals>.<lambda>rZ   r   TFr   zMESSAGE LOSTN)	r   r   r   r   r^   r   r   r5   r   r   r!   r!   r"   *test_formatUnformattableErrorWithTraceback  s   
z;EventAsTextTests.test_formatUnformattableErrorWithTracebackc                 C   s<   ddi}t  |d< t|ddd}| |t | d| dS )	zk
        If a traceback cannot be appended, a message indicating this is true
        is appended.
        r   r$   r   TFr   z'(UNABLE TO OBTAIN TRACEBACK FROM EVENT)N)rL   r   r   r   r5   r   r   r   r!   r!   r"   &test_formatEventUnformattableTraceback  s
   
z7EventAsTextTests.test_formatEventUnformattableTracebackc                 C   s2   ddi}t |ddd}| |t | d| dS )zY
        An event with no C{log_failure} key will not have a traceback appended.
        r   r   TFr   N)r   r   r   r5   r   r!   r!   r"   test_formatEventNonCritical  s   z,EventAsTextTests.test_formatEventNonCriticalc                 C   s\   zt d t y   t }Y nw ddi}||d< t|ddd}| d| | d| d	S )
zU
        An exception message with multibyte characters is properly handled.
        u   €r   r   r   TFr   	TracebackNr   r   r   r5   r   r!   r!   r"   test_formatTracebackMultibyte  s   
z.EventAsTextTests.test_formatTracebackMultibytec                 C   s\   zt d t y   t }Y nw ddi}||d< t|ddd}| d| | d	| d
S )zj
        An error raised attempting to decode the UTF still produces a
        valid log message.
        s
   t e s t r   r   r   TFr   r   z.CapturedError(b"\xff\xfet\x00e\x00s\x00t\x00")Nr   r   r!   r!   r"   ,test_formatTracebackHandlesUTF8DecodeFailure*  s   
z=EventAsTextTests.test_formatTracebackHandlesUTF8DecodeFailurec                 C   s^   zt d t y   t }Y nw td}dd|d}||d< t|dddd	}| |d
 dS )zq
        If includeSystem is specified as the only option no timestamp or
        traceback are printed.
        r   r~   ABCDfake_systemr   r   r   r   FTr   includeTracebackr   z[fake_system] ABCDNr   r   r
   r   r4   r   r   r   r   r   r!   r!   r"   test_eventAsTextSystemOnly;  s*   
z+EventAsTextTests.test_eventAsTextSystemOnlyc                 C   s~   t du rtdt|  td ztd ty    t }Y nw td}dd|d}||d	< t|d
ddd}| |d dS )zq
        If includeTimestamp is specified as the only option no system or
        traceback are printed.
        Nrk   rw   r   r~   r   r   r   r   TFr   z2013-09-24T11:40:47+0000 ABCD)	r   r   r	   r   r   r   r
   r   r4   r   r!   r!   r"   test_eventAsTextTimestampOnlyW  s2   
z.EventAsTextTests.test_eventAsTextTimestampOnlyc                 C   s\   zt d t y   t }Y nw td}d|d}||d< t|dddd}| |d	 d
S )z\
        If includeSystem is specified with a missing system [-#-]
        is used.
        r   r~   r   r   r   FTr   z
[-#-] ABCDNr   r   r!   r!   r"   test_eventAsTextSystemMissingy  s(   
z.EventAsTextTests.test_eventAsTextSystemMissingc                 C   sb   zt d t y   t }Y nw td}d|tjdd}||d< t|dddd	}| |d
 dS )z
        If includeSystem is specified with a missing system but
        namespace and level are present they are used.
        r   r~   r   test)r   r   r   r   r   FTr   z[test#info] ABCDNr   r   r
   r   infor   r4   r   r!   r!   r"   .test_eventAsTextSystemMissingNamespaceAndLevel  s,   
z?EventAsTextTests.test_eventAsTextSystemMissingNamespaceAndLevelc                 C   s`   zt d t y   t }Y nw td}d|tjd}||d< t|dddd}| |d	 d
S )zv
        If includeSystem is specified with a missing system but
        level is present, level is included.
        r   r~   r   )r   r   r   r   FTr   z[-#info] ABCDNr   r   r!   r!   r"   &test_eventAsTextSystemMissingLevelOnly  s*   
z7EventAsTextTests.test_eventAsTextSystemMissingLevelOnlyc                 C   s^   dt dtdt fdd}| t t|dtd | t t|dtd | d|d	t d d
S )zX
        C{eventAsText} can handle formats that have classes or types as input.
        r   r   r   c                 [   s   t d| i|ddddS )Nr   Fr   )r   )r   r   r!   r!   r"   getText  s   
z=EventAsTextTests.test_eventAsTextTypeAsInput.<locals>.getTextz{c})cr   z{c.__name__}N)r   r   r4   intRuntimeError)r   r   r!   r!   r"   test_eventAsTextTypeAsInput  s   z,EventAsTextTests.test_eventAsTextTypeAsInputrf   )r=   r>   r?   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r!   r!   r"   r     s     






	



"

r   )(rg   typingr   r   r   r   r   timer   ImportErrortwisted.python.failurer   !twisted.python.test.test_tzhelperr	   r
   r   twisted.trialr   twisted.trial.unittestr   _formatr   r   r   r   r   r   _interfacesr   _levelsr   TestCaser   rh   r   r   r^   	Exceptionr   r   r!   r!   r!   r"   <module>   s0     U 	