
    h                         S r SSKJr  SSKJr  SSKJrJrJr  SSK	J
r
  \" 5       r " S S5      rS	\
S
S4S jrS\S	\
S
\4S jrS	\
S
\4S jrg)z
Code related to "flattening" events; that is, extracting a description of all
relevant fields from the format string and persisting them for later
examination.
    )defaultdict)	Formatter)AnyDictOptional   )LogEventc                   H    \ rS rSrSrSS jrS\S\\   S\\   S\4S	 jrS
r	g)KeyFlattener   z
A L{KeyFlattener} computes keys for the things within curly braces in
PEP-3101-style format strings as parsed by L{string.Formatter.parse}.
returnNc                 &    [        S 5      U l        g)z
Initialize a L{KeyFlattener}.
c                      g)Nr    r       ^/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/logger/_flatten.py<lambda>'KeyFlattener.__init__.<locals>.<lambda>   s    r   N)r   keys)selfs    r   __init__KeyFlattener.__init__   s     %0	$:	r   	fieldName
formatSpec
conversionc                     Uc  SnUc  SnSR                  UUUS9nU R                  U==   S-  ss'   U R                  U   nUS:w  a  US[        U R                  U   5      -   -  nU$ )aZ  
Compute a string key for a given field/format/conversion.

@param fieldName: A format field name.
@param formatSpec: A format spec.
@param conversion: A format field conversion type.

@return: A key specific to the given field, format and conversion, as
    well as the occurrence of that combination within this
    L{KeyFlattener}'s lifetime.
 z%{fieldName}!{conversion}:{formatSpec})r   r   r   r   /)formatr   str)r   r   r   r   resultns         r   flatKeyKeyFlattener.flatKey    s     JJ8??!! @ 

 			&QIIf6cC		& 1222Fr   )r   )r   N)
__name__
__module____qualname____firstlineno____doc__r   r    r   r#   __static_attributes__r   r   r   r   r      s9    
;*23-EMc]	r   r   eventr   Nc                    U R                  SS5      c  gSU ;   a  U S   nO0 n[        5       n[        R                  U S   5       H  u  p4pVUc  M  US:w  a  SnUR	                  XEU5      nUR	                  XES5      nXq;   a  M>  UR                  S5      (       a  USS nS	n	OS
n	[        R                  USU 5      n
U
S   nUS:X  a  [        nO[        nU	(       a  U" 5       nU" U5      nXU'   XU'   M     U(       a  XS'   gg)z
Flatten the given event by pre-associating format fields with specific
objects and callable results in a L{dict} put into the C{"log_flattened"}
key in the event.

@param event: A logging event.

log_formatNlog_flattenedrsr   z()TFr   r   )	getr   
aFormatterparser#   endswith	get_fieldreprr    )r+   fieldskeyFlattenerliteralTextr   r   r   flattenedKeystructuredKeycallitfield
fieldValueconversionFunctionflattenedValues                 r   flattenEventrB   @   s,    yyt$,%'>L:D:J:Jl;6
 J#++I:N$,,YBG!d##!#2IFF$$YE:1X
!%!$#J+J7-| *}G;J !'o r   r>   c                     [        5       n[        R                  SU -   S-   5      u  u  p4pVUc   eUR                  XEU5      nSU;  a  [	        U5        US   U   $ )a  
Extract a given format field from the given event.

@param field: A string describing a format field or log key.  This is the
    text that would normally fall between a pair of curly braces in a
    format string: for example, C{"key[2].attribute"}.  If a conversion is
    specified (the thing after the C{"!"} character in a format field) then
    the result will always be str.
@param event: A log event.

@return: A value extracted from the field.

@raise KeyError: if the field is not found in the given event.
{}r.   )r   r3   r4   r#   rB   )r>   r+   r9   r:   r   r   r   keys           r   extractFieldrG   {   so      >L9C9I9Iec:65kj    


yj
ACe#U!#&&r   c                 $   U S   n[        5       n/ n[        R                  U S   5       HS  u  pEpgUR                  U5        Uc  M  UR	                  XVU=(       d    S5      nUR                  [        X   5      5        MU     SR                  U5      $ )z~
Format an event which has been flattened with L{flattenEvent}.

@param event: A logging event.

@return: A formatted string.
r.   r-   r0   r   )r   r3   r4   appendr#   r    join)	r+   fieldValuesr9   r0   r:   r   r   r   rF   s	            r   
flatFormatrL      s     (K>L
A:D:J:Jl;6
 	
 &&yj>OCPCHHS)*+; 771:r   )r)   collectionsr   stringr   typingr   r   r   _interfacesr	   r3   r   rB   r    rG   rL   r   r   r   <module>rQ      sn   
 $  & & ![
) )X8( 8(T 8(v' 'H ' '>h 3 r   