
    Vh                         d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
  e       Z G d d      Zd	e
d
dfdZded	e
d
efdZd	e
d
efdZy)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                   <    e Zd ZdZd	dZdedee   dee   defdZy)
KeyFlattenerz
    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                 &    t        d       | _        y)z/
        Initialize a L{KeyFlattener}.
        c                       y)Nr    r       G/home/dcms/DCMS/lib/python3.12/site-packages/twisted/logger/_flatten.py<lambda>z'KeyFlattener.__init__.<locals>.<lambda>   s    r   N)r   keys)selfs    r   __init__zKeyFlattener.__init__   s     %0	$:	r   	fieldName
formatSpec
conversionc                     |d}|d}dj                  |||      }| j                  |xx   dz  cc<   | j                  |   }|dk7  r|dt        | j                  |         z   z  }|S )a  
        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   flatKeyzKeyFlattener.flatKey    s     JJ8??!! @ 

 			&QIIf6cC		& 1222Fr   )r   N)__name__
__module____qualname____doc__r   r   r   r    r   r   r   r   r      s9    
;*23-EMc]	r   r   eventr   Nc                    | j                  dd      yd| v r| d   }ni }t               }t        j                  | d         D ]  \  }}}}||dk7  rd}|j	                  |||      }|j	                  ||d      }||v r=|j                  d      r|dd }d	}	nd
}	t        j                  |d|       }
|
d   }|dk(  rt        }nt        }|	r |       } ||      }|||<   |||<    |r|| d<   yy)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   flattenEventr<   @   s>    yyt$,%'>L:D:J:Jl; #+6Y
J J#++Iz:N$,,Y
BG6!d#!#2IFF$$YE:1X
!%!$#J+J7-| *}G#+J !'o r   r8   c                     t               }t        j                  d| z   dz         \  \  }}}}|J |j                  |||      }d|vrt	        |       |d   |   S )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   r-   r.   r    r<   )r8   r%   r3   r4   r   r   r   keys           r   extractFieldrA   {   su      >L9C9I9Iec:65k9j*    


y*j
ACe#U!#&&r   c                    | d   }t               }g }t        j                  | d         D ]O  \  }}}}|j                  |       ||j	                  |||xs d      }|j                  t        ||                Q dj                  |      S )z
    Format an event which has been flattened with L{flattenEvent}.

    @param event: A logging event.

    @return: A formatted string.
    r(   r'   r*   r   )r   r-   r.   appendr    r   join)	r%   fieldValuesr3   r*   r4   r   r   r   r@   s	            r   
flatFormatrF      s     (K>L
A:D:J:Jl; ,6Y
J 	
 &&y*j>OCPCHHSS)*+, 771:r   )r$   collectionsr   stringr   typingr   r   r   _interfacesr	   r-   r   r<   r   rA   rF   r   r   r   <module>rK      sn   
 $  & & ![
) )X8( 8(T 8(v' 'H ' '>h 3 r   