HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/twisted/logger/__pycache__/_flatten.cpython-38.pyc
U

�b��@s�dZddlmZddlmZddlmZmZmZddl	m
Z
e�ZGdd�d�Ze
d	d
�dd�Z
ee
ed
�dd�Ze
ed
�dd�Zd	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:eZdZdZdd�dd�Zeeeeeed�dd�ZdS)	�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)�returncCstdd��|_dS)z/
        Initialize a L{KeyFlattener}.
        cSsdS)Nr�rrr�9/usr/lib/python3/dist-packages/twisted/logger/_flatten.py�<lambda>�z'KeyFlattener.__init__.<locals>.<lambda>N)r�keys)�selfrrr�__init__szKeyFlattener.__init__)�	fieldName�
formatSpec�
conversionr
cCsf|dkrd}|dkrd}dj|||d�}|j|d7<|j|}|dkrb|dt|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})rrrr�/)�formatr�str)rrrr�result�nrrr�flatKey s�
zKeyFlattener.flatKey)�__name__�
__module__�__qualname__�__doc__rrrrrrrrr	s�r	N)�eventr
cCs|�dd�dkrdSd|kr&|d}ni}t�}t�|d�D]�\}}}}|dkrTq>|dkr`d}|�|||�}|�||d�}||kr�q>|�d�r�|dd�}d	}	nd
}	t�|d|�}
|
d}|dkr�t}nt}|	r�|�}||�}
|
||<|||<q>|r�||d<dS)
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�srz()���TFrr)	�getr	�
aFormatter�parser�endswith�	get_field�reprr)r �fields�keyFlattener�literalTextrrrZflattenedKeyZ
structuredKeyZcallit�fieldZ
fieldValueZconversionFunctionZflattenedValuerrr�flattenEvent@sB
�

r0)r/r r
cCsXt�}t�d|d�\\}}}}|dk	s.t�|�|||�}d|krLt|�|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(�AssertionErrorrr0)r/r r-r.rrr�keyrrr�extractField{s
�r5c	Csn|d}t�}g}t�|d�D]B\}}}}|�|�|dk	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(�appendrr�join)	r ZfieldValuesr-r$r.rrrr4rrr�
flatFormat�s�
r8)r�collectionsr�stringr�typingrrr�_interfacesrr'r	r0rr5r8rrrr�<module>s,;