U
    
W[                     @   sZ   d Z ddlmZ ddlmZ ddlmZ e ZG dd deZ	dd Z
d	d
 Z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.
    )	Formatter)defaultdict)unicodec                   @   s    e Zd ZdZdd Z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}.
    c                 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>       z'KeyFlattener.__init__.<locals>.<lambda>N)r   keys)selfr   r   r   __init__   s    zKeyFlattener.__init__c                 C   sV   dj ||pd|pdd}| j|  d7  < | j| }|dkrR|dt| j|  7 }|S )a  
        Compute a string key for a given field/format/conversion.

        @param fieldName: A format field name.
        @type fieldName: L{str}

        @param formatSpec: A format spec.
        @type formatSpec: L{str}

        @param conversion: A format field conversion type.
        @type conversion: L{str}

        @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.
        @rtype: L{str}
        z%{fieldName}!{conversion}:{formatSpec} )	fieldName
formatSpec
conversion   /)formatr
   str)r   r   r   r   resultnr   r   r   flatKey!   s    
zKeyFlattener.flatKeyN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   r   c                 C   s  |  dddkrdS d| kr&| d }ni }t }t| d D ]\}}}}|dkrTq>|dkr`d}||||}|||d}||krq>|dr|dd }d	}	nd
}	t|d| }
|
d }|dkrt}nt}|	r| }||}|||< |||< q>|r|| d< 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.
    @type event: L{dict}
    
log_formatNlog_flattenedrsr   z()TFr   r   )	getr   
aFormatterparser   endswithZ	get_fieldreprr   )eventZfieldskeyFlattenerliteralTextr   r   r   ZflattenedKeyZstructuredKeyZcallitfieldZ
fieldValueZconversionFunctionZflattenedValuer   r   r   flattenEventC   s@    	


r*   c                 C   sL   t  }td|  d \\}}}}||||}d|k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 L{unicode}.
    @type field: L{str} (native string)

    @param event: A log event.
    @type event: L{dict}

    @return: A value extracted from the field.
    @rtype: L{object}

    @raise KeyError: if the field is not found in the given event.
    {}r   )r   r"   r#   r   r*   )r)   r&   r'   r(   r   r   r   keyr   r   r   extractField   s    
r.   c           
      C   sr   | d }g }t  }t| d }|D ]B\}}}}|| |dk	r$||||pPd}	|t||	  q$d|S )z
    Format an event which has been flattened with L{flattenEvent}.

    @param event: A logging event.
    @type event: L{dict}

    @return: A formatted string.
    @rtype: L{unicode}
    r   r   Nr   r   )r   r"   r#   appendr   r   join)
r&   ZfieldValuesr   r'   ZformatFieldsr(   r   r   r   r-   r   r   r   
flatFormat   s    

  r1   N)r   stringr   collectionsr   Ztwisted.python.compatr   r"   objectr   r*   r.   r1   r   r   r   r   <module>   s   /=