+
    Dfj4                    f   R t ^ RIHt ^ RIHt ^ RIHtHtHtH	t	H
t
HtHt ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^RIHtHt ^R	IHt R
tR R ltR R lt]R3R R llt]3R R lltR R lt ! R R] 4      t! ! R R]	]"]3,          4      t#R R lt$R R lt%R R lt&R  R! lt'R"R"R"]3R# R$ llt(R%# )&z&
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%zc                    V ^8  d   QhRRRR/#    eventr   returnstr )formats   "8/usr/lib/python3/dist-packages/twisted/logger/_format.py__annotate__r      s      x C     c                     \        V RRRR7      # )aE  
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   s   &r   formatEventr"      s     	 r   c               $    V ^8  d   QhRRRRRR/# )r   r   r   errorBaseExceptionr   r   r   )r   s   "r   r   r   -   s!     !
 !
H !
] !
s !
r   c                     RP                  WR7      #   \         dP    \        4       pRP                  R T P	                  4        4       4      pRP                  \        T4      Y#R7      u # i ; i)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              3  p   "   T F,  w  rR P                  \        V4      \        V4      34      x  K.  	  R# 5i)z = N)joinr   ).0keyvalues   &  r   	<genexpr>+formatUnformattableEvent.<locals>.<genexpr>C   s3      
+
 JJ	#	%(89::+s   46zrMESSAGE LOST: unformattable object logged: {error}
Recoverable data: {text}
Exception during formatting:
{failure})r$   failuretext)r   r%   r   r(   itemsr   )r   r$   r.   r/   s   &&  r   formatUnformattableEventr1   -   s    
:AA B 
 	
  
 )yy 
#kkm
 
66<f& 7= 7	

s    AA.-A.-c               (    V ^8  d   QhRRRRRRRR/# )r   whenzOptional[float]
timeFormatOptional[str]defaultr   r   r   )r   s   "r   r   r   Q   s0     2 2
22 2 		2r   c                    Ve   V f   V# \         P                  ! V 4      p\        P                  ! W4      p\	        VP                  V4      4      # )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.
)r   fromLocalTimeStampDateTimefromtimestampr   strftime)r4   r5   r7   tzr   s   &&&  r   
formatTimer>   Q   sI    4 T\ 33D9))$38$$Z011r   c               $    V ^8  d   QhRRRRRR/# )r   r   r   r>   z Callable[[Optional[float]], str]r   r6   r   )r   s   "r   r   r   s   s$     3 33!A33r   c                d    \        WR7      pV'       g   R# VP                  RR4      pVR,           # )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.
)r>   N
z
	)r!   replace)r   r>   	eventTexts   && r   formatEventAsClassicLogTextrD   s   s2    ^ E9I!!$/Itr   c               $    V ^8  d   QhRRRRRR/# )r   r*   r   getterzCallable[[str], Any]r   PotentialCallWrapperr   )r   s   "r   r   r      s#     ' ' '2 '7K 'r   c                    V P                  R4      pV'       d   V RR MT pV! V4      pV'       d   V! 4       p\        V4      # )aL  
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)endswithrG   )r*   rF   callitrealKeyr+   s   &&   r   keycallrM      s?     \\$F c#2hcG7OE&&r   c                  b    ] tR t^tRtR R ltR R ltR R ltR R	 ltR
 R lt	R R lt
RtR# )rG   zs
Object wrapper that wraps C{getattr()} so as to process call-parentheses
C{"()"} after a dotted attribute access.
c                    V ^8  d   QhRRRR/# )r   wrappedobjectr   Noner   )r   s   "r   r   !PotentialCallWrapper.__annotate__   s          4  r   c                	    Wn         R # N_wrapped)selfrP   s   &&r   __init__PotentialCallWrapper.__init__   s    r   c                    V ^8  d   QhRRRR/# r   namer   r   rQ   r   )r   s   "r   r   rS      s     J J J Jr   c                	$   a  \        VV 3R  l4      # )c                0   < \        SP                  V 4      # rU   )getattrrW   )name_rX   s   &r   <lambda>2PotentialCallWrapper.__getattr__.<locals>.<lambda>   s    74==%+Hr   )rM   )rX   r]   s   f&r   __getattr__ PotentialCallWrapper.__getattr__   s    tHIIr   c                    V ^8  d   QhRRRR/# r\   r   )r   s   "r   r   rS      s     + + + +r   c                	>    V P                   V,          p\        V4      # rU   )rW   rG   )rX   r]   r+   s   && r   __getitem__ PotentialCallWrapper.__getitem__   s     d##E**r   c                    V ^8  d   QhRRRR/# )r   format_specr   r   r   )r   s   "r   r   rS      s     2 2c 2c 2r   c                	.    \        V P                  V4      # rU   )r   rW   )rX   rk   s   &&r   
__format__PotentialCallWrapper.__format__   s    dmm[11r   c                   V ^8  d   QhRR/# r   r   r   r   )r   s   "r   r   rS      s     # ## #r   c                	,    \        V P                  4      # rU   )reprrW   rX   s   &r   __repr__PotentialCallWrapper.__repr__   s    DMM""r   c                   V ^8  d   QhRR/# rp   r   )r   s   "r   r   rS      s     " " "r   c                	,    \        V P                  4      # rU   )r   rW   rs   s   &r   __str__PotentialCallWrapper.__str__   s    4==!!r   rV   N)__name__
__module____qualname____firstlineno____doc__rY   rd   rh   rm   rt   rx   __static_attributes__r   r   r   rG   rG      s+    
 J+2#" "r   rG   c                  J    ] tR t^tRtR R ltR R ltR R ltR R	 ltR
t	R# )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}.
c                    V ^8  d   QhRRRR/# )r   
submappingMapping[str, Any]r   rR   r   )r   s   "r   r   CallMapping.__annotate__   s     & &#4 & &r   c                    Wn         R# )zW
@param submapping: Another read-only mapping which will be used to look
    up items.
N_submapping)rX   r   s   &&r   rY   CallMapping.__init__   s
    
 &r   c                   V ^8  d   QhRR/# )r   r   zIterator[Any]r   )r   s   "r   r   r      s     & &- &r   c                	,    \        V P                  4      # rU   )iterr   rs   s   &r   __iter__CallMapping.__iter__   s    D$$%%r   c                   V ^8  d   QhRR/# )r   r   intr   )r   s   "r   r   r      s     % % %r   c                	,    \        V P                  4      # rU   )lenr   rs   s   &r   __len__CallMapping.__len__   s    4##$$r   c                    V ^8  d   QhRRRR/# )r   r*   r   r   r   r   )r   s   "r   r   r      s     : :s :s :r   c                @    \        WP                  P                  4      # )zd
Look up an item in the submapping for this L{CallMapping}, calling it
if C{key} ends with C{"()"}.
)rM   r   rh   )rX   r*   s   &&r   rh   CallMapping.__getitem__   s    
 s,,8899r   r   N)
rz   r{   r|   r}   r~   rY   r   r   rh   r   r   r   r   r   r      s     &&%: :r   r   c               $    V ^8  d   QhRRRRRR/# )r   formatStringr   mappingr   r   r   )r   s   "r   r   r      s'     K K K/@ KS Kr   c           	     V    \        \        P                  ! V R\        V4      4      4      # )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   )r   r   s   &&r   formatWithCallr      s#    . z!!,K4HIJJr   c                    V ^8  d   QhRRRR/# r   r   )r   s   "r   r   r     s     2 2 2S 2r   c                    RV 9   d   \        V 4      # \        \        \        \        \
        3,          ,          V P                  RR4      4      pVf   R# \        V\        4      '       d   M7\        V\
        4      '       d   VP                  R4      pM\        RV: 24      h\        W4      #   \         d   p\        Y4      u Rp?# Rp?ii ; i)aI  
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	TypeErrorr   r%   r1   )r   r   es   &  r   _formatEventr     s    2e#e$$huS%Z01599\43PQ> fc""&&]]7+F:6*EFFf,, 2'112s*   B/ A B/ AB/ /C:
C
C
Cc                    V ^8  d   QhRRRR/# )r   r.   r   r   r   r   )r   s   "r   r   r   5  s      g # r   c                ~     V P                  4       pV#   \         d   pR\        T4      ,           p Rp?T# Rp?ii ; i)aB  
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.   	tracebackr   s   &  r   _formatTracebackr   5  sF    H((*	   H>QG	Hs    <7<c                    V ^8  d   QhRRRR/# r   r   )r   s   "r   r   r   G  s       c r   c           	        \        \        \        ,          V P                  RR4      4      pVfr   \        \        \        ,          V P                  RR4      4      pVf   RpMVP
                  pRP                  \        \        V P                  RR4      4      VR7      pV#  \        V4      pV#   \         d    Rp T# i ; i)	as  
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_levelr2   z{namespace}#{level}log_namespace)	namespacelevelUNFORMATTABLE)r
   r   r   r   r   r]   r   	Exception)r   systemr   	levelNames   &   r   _formatSystemr   G  s     (3-<!>?F~Xm,eiiT.JK=I

I&--3		/3 ?@ . 
 M		%[F M  	%$FM	%s   "B/ /C ?C Tc               0    V ^8  d   QhRRRRRRRRRRR	R
/# )r   r   r   r   boolr   r    r>   zCallable[[float], str]r   r   r   )r   s   "r   r   r   f  sD     ? ??? ? 	?
 '? 	?r   c                   \        V 4      pV'       d.   RV 9   d'   V R,          p\        V4      pRP                  WW34      pV'       g   V# RpV'       d8   RP                  V! \        \        V P                  RR4      4      4      R.4      pRp	V'       d   RP                  R\        V 4      RR.4      p	R	P                  VV	VR
7      # )a  
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_failurerA   r   log_timeN []z{timeStamp}{system}{eventText})	timeStampr   rC   )r   r   r(   r
   floatr   r   r   )
r   r   r   r    r>   rC   fr   r   r   s
   &&&&&     r   r!   r!   f  s    T U#IMU2- $Q'	IIy45	IGGZUEIIj$4O(PQSVWX	F#}U3S#>?+22 3  r   N))r~   
__future__r   r   r:   typingr   r   r   r   r   r	   r
   
constantlyr   twisted.python._tzhelperr   twisted.python.failurer   twisted.python.reflectr   _flattenr   r   _interfacesr   timeFormatRFC3339r"   r1   r>   rD   rM   rQ   rG   r   r   r   r   r   r   r!   r   r   r   <module>r      s   
 # ) J J J $ 8 * , , !) (!
L !22F EO3l'."6 "8:'#s(# ::K42D$B "!)3? ?r   