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__/_json.cpython-38.pyc
U

�b� �@s�dZddlmZmZddlmZmZmZmZm	Z	m
Z
mZmZddl
mZddlmZddlmZddlmZdd	lmZdd
lmZddlmZddlmZe�ZeeefZeed
�dd�Z eed�dd�Z!dd�ed�dd�dd�fdd�ed�e e!fgZ"dd�e"D�Z#ee$d�dd�Z%e$ed�d d!�Z&eed"�d#d$�Z'eed%�d&d'�Z(d1eeeed)�d*d+�Z)d2eee
ee*e	ed.�d/d0�Z+d,S)3zA
Tools for saving and loading log events in a structured format.
�)�dumps�loads)�IO�Any�AnyStr�Dict�Iterable�Optional�Union�cast)�UUID)�
NamedConstant)�Failure���FileLogObserver)�flattenEvent)�LogEvent)�LogLevel)�Logger)�failure�returncCs t|��t|jj|jjd�d�S)z�
    Convert a failure to a JSON-serializable data structure.

    @param failure: A failure to serialize.

    @return: a mapping of strings to ... stuff, mostly reminiscent of
        L{Failure.__getstate__}
    )�
__module__�__name__)�type)�dict�__getstate__rrr)r�r�6/usr/lib/python3/dist-packages/twisted/logger/_json.py�
failureAsJSONs	��r)�failureDictrcCs0t�t�}|d}t|dd|�|d<||_|S)z�
    Load a L{Failure} from a dictionary deserialized from JSON.

    @param failureDict: a JSON-deserialized object like one previously returned
        by L{failureAsJSON}.

    @return: L{Failure}
    rrr)r�__new__r�__dict__)r �fZtypeInforrr�failureFromJSON.s
	
r$cCst|t�ott|jd�|kS�N)�
isinstancer
�getattrr�name��levelrrr�<lambda>@s
r+z$02E59486-F24D-46AD-8224-3ACDF2A5732AcCst|jd�S)N)r()rr(r)rrrr+E�cCstt|dd�S)Nr()r'rr)rrrr+Fr,cCs
t|t�Sr%)r&r)�orrrr+Ir,z$E76887E2-20ED-49BF-A8F8-BA25CC586F2DcCsi|]\}}}}||�qSrr)�.0�	predicate�uuid�saver�loaderrrr�
<dictcomp>Qs
r3)�aDictrcCs d|krtt|d�|�S|S)a#
    Dictionary-to-object-translation hook for certain value types used within
    the logging system.

    @see: the C{object_hook} parameter to L{json.load}

    @param aDict: A dictionary loaded from a JSON object.

    @return: C{aDict} itself, or the object represented by C{aDict}
    �__class_uuid__)�uuidToLoaderr)r4rrr�objectLoadHookTsr7)�pythonObjectrcCs>tD]0\}}}}||�r||�}t|�|d<|SqddiS)a�
    Object-to-serializable hook for certain value types used within the logging
    system.

    @see: the C{default} parameter to L{json.dump}

    @param pythonObject: Any object.

    @return: If the object is one of the special types the logging system
        supports, a specially-formatted dictionary; otherwise, a marker
        dictionary indicating that it could not be serialized.
    r5Z
unpersistableT)�	classInfo�str)r8r/r0r1r2�resultrrr�objectSaveHookds

r<)�eventrcCs.ttttfd�dd�}t|�t||dd�S)a�
    Encode an event as JSON, flattening it if necessary to preserve as much
    structure as possible.

    Not all structure from the log event will be preserved when it is
    serialized.

    @param event: A log event dictionary.

    @return: A string of the serialized JSON; note that this will contain no
        newline characters, and may thus safely be stored in a line-delimited
        file.
    )�unencodablercSst|t�r|�d�St|�S)z�
        Serialize an object not otherwise serializable by L{dumps}.

        @param unencodable: An unencodable object.

        @return: C{unencodable}, serialized
        �charmap)r&�bytes�decoder<)r>rrr�default�s

zeventAsJSON.<locals>.defaultT)rB�skipkeys)�objectr
�JSONDictr:rr)r=rBrrr�eventAsJSONysrF)�	eventTextrcCsttt|td��S)z�
    Decode a log event from JSON.

    @param eventText: The output of a previous call to L{eventAsJSON}

    @return: A reconstructed version of the log event.
    )�object_hook)rrErr7)rGrrr�
eventFromJSON�srI�)�outFile�recordSeparatorrcst|�fdd��S)a�
    Create a L{FileLogObserver} that emits JSON-serialized events to a
    specified (writable) file-like object.

    Events are written in the following form::

        RS + JSON + NL

    C{JSON} is the serialized event, which is JSON text.  C{NL} is a newline
    (C{"\n"}).  C{RS} is a record separator.  By default, this is a single
    RS character (C{"\x1e"}), which makes the default output conform to the
    IETF draft document "draft-ietf-json-text-sequence-13".

    @param outFile: A file-like object.  Ideally one should be passed which
        accepts L{str} data.  Otherwise, UTF-8 L{bytes} will be used.
    @param recordSeparator: The record separator to use.

    @return: A file log observer.
    cs��t|��d�S)N�
)rF)r=�rLrrr+�r,z%jsonFileLogObserver.<locals>.<lambda>r)rKrLrrNr�jsonFileLogObserver�s
�rON�)�inFilerL�
bufferSizerc#sttd�dd�}tttd�dd��|dkrN||�d��}|d	krH|}qZd
}n||�}d
}|d
krld}�}ntttd��fdd
�}t|�}|�|�}|s�t|�dkr�||�}	|	dk	r�|	V�q|||�7}|�|�}
|
dd�D]&}t|�dkr�||�}	|	dk	r�|	Vq�|
d}q�dS)aM
    Load events from a file previously saved with L{jsonFileLogObserver}.
    Event records that are truncated or otherwise unreadable are ignored.

    @param inFile: A (readable) file-like object.  Data read from C{inFile}
        should be L{str} or UTF-8 L{bytes}.
    @param recordSeparator: The expected record separator.
        If L{None}, attempt to automatically detect the record separator from
        one of C{"\x1e"} or C{""}.
    @param bufferSize: The size of the read buffer used while reading from
        C{inFile}.

    @return: Log events as read from C{inFile}.
    )�srcSst|t�r|S|�d�SdS)N�utf-8)r&r@�encode)rSrrr�asBytes�s
z&eventsFromJSONLogFile.<locals>.asBytes)�recordrcSstzt|��d�}Wn(tk
r:tjdt|�d�YdSXz
t|�WStk
rntjdt|�d�YdSXdS)NrTz2Unable to decode UTF-8 for JSON record: {record!r}�rWz&Unable to read JSON record: {record!r})r@rA�UnicodeDecodeError�log�errorrI�
ValueError)rW�textrrr�eventFromBytearray�s�
z1eventsFromJSONLogFile.<locals>.eventFromBytearrayNr�r,�
cs.|dtd�kr�|�Stjdt|�d�dS)N���rMz0Unable to read truncated JSON record: {record!r}rX)�ordrZr[r@rX�r^rr�eventFromRecord�s�z.eventsFromJSONLogFile.<locals>.eventFromRecordrra)rr@�	bytearrayr	r�read�len�split)rQrLrRrV�firstZrecordSeparatorBytesrd�bufferZnewDatar=�recordsrWrrcr�eventsFromJSONLogFile�s:


rl)rJ)NrP),�__doc__�jsonrr�typingrrrrrr	r
rr0r�
constantlyr
�twisted.python.failurer�_filer�_flattenr�_interfacesr�_levelsr�_loggerrrZr:rErr$r9r6rDr7r<rFrIrO�intrlrrrr�<module>sX(�
������