o
    b                     @   s   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 dZd	e
d
dfddZded	e
d
efddZd	e
d
efddZdS )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                   @   s<   e Zd ZdZdddZdedee dee defd	d
ZdS )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                 C   s   t dd | _dS )z/
        Initialize a L{KeyFlattener}.
        c                   S   s   dS )Nr    r   r   r   9/usr/lib/python3/dist-packages/twisted/logger/_flatten.py<lambda>   s    z'KeyFlattener.__init__.<locals>.<lambda>N)r   keys)selfr   r   r   __init__   s   zKeyFlattener.__init__	fieldName
formatSpec
conversionc                 C   sf   |du rd}|du rd}dj |||d}| j|  d7  < | j| }|dkr1|dt| j|  7 }|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.
        N z%{fieldName}!{conversion}:{formatSpec})r   r   r   r   /)formatr   str)r   r   r   r   resultnr   r   r   flatKey    s   
zKeyFlattener.flatKey)r
   N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r	      s    
r	   eventr
   Nc                 C   s  |  dddu r
dS d| v r| d }ni }t }t| d D ]Y\}}}}|du r*q|dkr0d}||||}|||d}||v rCq|drQ|dd }d	}	nd
}	t|d| }
|
d }|dkret}nt}|	rl| }||}|||< |||< q|r|| d< dS dS )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flattenedValuer   r   r   flattenEvent@   sD   


r5   r1   c                 C   sX   t  }td|  d \\}}}}|dus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.
    {}Nr!   )r	   r&   r'   r   r5   )r1   r   r,   r-   r   r   r   keyr   r   r   extractField{   s   
r9   c           	      C   sn   | d }t  }g }t| d D ]!\}}}}|| |dur1||||p&d}|t||  qd|S )z
    Format an event which has been flattened with L{flattenEvent}.

    @param event: A logging event.

    @return: A formatted string.
    r!   r    Nr#   r   )r	   r&   r'   appendr   r   join)	r   fieldValuesr,   r#   r-   r   r   r   r8   r   r   r   
flatFormat   s   

r=   )r   collectionsr   stringr   typingr   r   r   _interfacesr   r&   r	   r5   r   r9   r=   r   r   r   r   <module>   s   ,;