U
    ˆj‚  ã                   @   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e
d	d
œdd„Zee
edœdd„Ze
ed
œ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d„Zeee ee edœ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}.
    N)Úreturnc                 C   s   t dd„ ƒ| _dS )z/
        Initialize a L{KeyFlattener}.
        c                   S   s   dS )Nr   © r   r   r   ú;/tmp/pip-unpacked-wheel-j5h_c8_q/twisted/logger/_flatten.pyÚ<lambda>   ó    z'KeyFlattener.__init__.<locals>.<lambda>N)r   Úkeys)Úselfr   r   r   Ú__init__   s    zKeyFlattener.__init__)Ú	fieldNameÚ
formatSpecÚ
conversionr
   c                 C   sf   |dkrd}|dkrd}dj |||d�}| j|  d7  < | j| }|dkrb|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)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r	      s     þr	   N)Úeventr
   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r†q>| d¡r¢|dd… }d	}	nd
}	t |d| ¡}
|
d }|dkrÊt}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.
    Ú
log_formatNÚlog_flattenedÚrÚsr   z()éþÿÿÿTFr   r   )	Úgetr	   Ú
aFormatterÚparser   ÚendswithÚ	get_fieldÚreprr   )r    ÚfieldsÚkeyFlattenerÚliteralTextr   r   r   ZflattenedKeyZstructuredKeyZcallitÚfieldZ
fieldValueZconversionFunctionZflattenedValuer   r   r   ÚflattenEvent@   sB    
ÿ

r0   )r/   r    r
   c                 C   sX   t ƒ }t d|  d ¡\\}}}}|dk	s.t‚| |||¡}d|krLt|ƒ |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(   ÚAssertionErrorr   r0   )r/   r    r-   r.   r   r   r   Úkeyr   r   r   ÚextractField{   s    
ÿr5   c           	      C   sn   | d }t ƒ }g }t | d ¡D ]B\}}}}| |¡ |dk	r | |||pLd¡}| 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    ZfieldValuesr-   r$   r.   r   r   r   r4   r   r   r   Ú
flatFormatš   s    ÿ
r8   )r   Úcollectionsr   Ústringr   Útypingr   r   r   Z_interfacesr   r'   r	   r0   r   r5   r8   r   r   r   r   Ú<module>   s   ,;