ϪfdZddlmZddlmZddlmZmZmZddl m Z eZ GddZ d e d d fd Z d ed e d efdZd e d efdZy )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)AnyDictOptional)LogEventc<eZdZdZd dZdedeedeedefdZy) 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&td|_y)z/ Initialize a L{KeyFlattener}. cy)Nrr9/usr/lib/python3/dist-packages/twisted/logger/_flatten.pyz'KeyFlattener.__init__..srN)rkeys)selfs r__init__zKeyFlattener.__init__s%0 $: r fieldName formatSpec conversionc|d}|d}dj|||}|j|xxdz cc<|j|}|dk7r|dt|j|zz }|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})rrrr/)formatrstr)rrrrresultns rflatKeyzKeyFlattener.flatKey s  J  J8??!!@  &Q IIf  6 cC & 122 2F r)r N)__name__ __module__ __qualname____doc__rrrr rrrr r s9 ; *23-EMc] rr eventr Nc|jddyd|vr|d}ni}t}tj|dD]\}}}}| |dk7rd}|j |||}|j ||d}||vr=|j dr|dd}d } nd } tj |d |} | d } |dk(rt} nt} | r| } | | } | ||<| ||<|r||d<yy) 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_flattenedrsrz()TFrr) getr aFormatterparser endswith get_fieldreprr)r%fields keyFlattener literalTextrrr flattenedKey structuredKeycallitfield fieldValueconversionFunctionflattenedValues r flattenEventr<@s> yyt$,%'>L:D:J:J l;#+6 Y J     J#++Iz:N $,,Y BG 6 !    d #!#2IFF$$YE:1X  !% !$  #J+J7-| *}G#+J!'orr8ct}tjd|zdz\\}}}}|J|j|||}d|vr 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<)r8r%r3r4rrrkeys r extractFieldrA{su >L9C9I9I e c:65k9j*     y*j ACe#U  !# &&rc|d}t}g}tj|dD]O\}}}}|j|||j |||xsd}|jt ||Qdj |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 rjoin) r% fieldValuesr3r*r4rrrr@s r flatFormatrFs(K>L A:D:J:J l;,6 Y J   &&y*j>OCPC HHSS)* +, 771:r)r$ collectionsrstringrtypingrrr _interfacesr r-r r<rrArFrrrrKsn  $&&! [ ))X8(8(T8(v''H''>h3r