HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/logger/__pycache__/_json.cpython-38.pyc
U


W[U'�@sdZddlZddlmZddlmZmZddlmZddl	m
Z
ddlmZdd	l
mZdd
lmZddlmZmZddlmZe�Zd
d�Zdd�Zdd�Zdd�ed�dd�dd�fdd�ed�eefgZedd�eD��Zdd�Zdd�Zd d!�Zd"d#�Z d*d%d&�Z!d+d(d)�Z"dS),zA
Tools for saving and loading log events in a structured format.
�N)�
NamedConstant)�dumps�loads)�UUID�)�flattenEvent��FileLogObserver)�LogLevel)�Logger)�unicode�_PY3)�FailurecCs t|��t|jj|jjd�d�S)a
    Convert a failure to a JSON-serializable data structure.

    @param failure: A failure to serialize.
    @type failure: L{Failure}

    @return: a mapping of strings to ... stuff, mostly reminiscent of
        L{Failure.__getstate__}
    @rtype: L{dict}
    )�
__module__�__name__)�type)�dict�__getstate__rrr)Zfailure�r�6/usr/lib/python3/dist-packages/twisted/logger/_json.py�
failureAsJSONs��rcCsPt|t�rtt|�St|t�r4tdd�|��D��St|t�rH|�d�S|SdS)a�
    On Python 2, we really need native strings in a variety of places;
    attribute names will sort of work in a __dict__, but they're subtly wrong;
    however, printing tracebacks relies on I/O to containers that only support
    bytes.  This function converts _all_ native strings within a
    JSON-deserialized object to bytes.

    @param obj: An object to convert to bytes.
    @type obj: L{object}

    @return: A string of UTF-8 bytes.
    @rtype: L{bytes}
    css"|]\}}t|�t|�fVqdS�N)�asBytes)�.0�k�vrrr�	<genexpr>AszasBytes.<locals>.<genexpr>�utf-8N)�
isinstance�list�maprr�itemsr�encode)�objrrrr0s




rcCsZttdd�}|dkr t�t�}n|t�}ts4t|�}|d}t|dd|�|d<||_|S)a#
    Load a L{Failure} from a dictionary deserialized from JSON.

    @param failureDict: a JSON-deserialized object like one previously returned
        by L{failureAsJSON}.
    @type failureDict: L{dict} mapping L{unicode} to attributes

    @return: L{Failure}
    @rtype: L{Failure}
    �__new__Nrrr)�getattrr�typesZInstanceTyper
rr�__dict__)ZfailureDictZ
newFailure�fZtypeInforrr�failureFromJSONIs
r)cCst|t�ott|jd�|kSr)rrr%r
�name��levelrrr�<lambda>is
r-z$02E59486-F24D-46AD-8224-3ACDF2A5732AcCst|jd�S)N)r*)rr*r+rrrr-n�cCstt|dd�S)Nr*)r%r
r+rrrr-or.cCs
t|t�Sr)rr)�orrrr-sr.z$E76887E2-20ED-49BF-A8F8-BA25CC586F2DcCsg|]\}}}}||f�qSrr)r�	predicate�uuid�saver�loaderrrr�
<listcomp>{s
r4cCs d|krtt|d�|�S|S)aR
    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.
    @type aDict: L{dict}

    @return: C{aDict} itself, or the object represented by C{aDict}
    @rtype: L{object}
    �__class_uuid__)�uuidToLoaderr)ZaDictrrr�objectLoadHook�s
r7cCs>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.
    @type pythonObject: L{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)ZpythonObjectr0r1r2r3�resultrrr�objectSaveHook�s
r;cCsZttkrttddd�}ndd�}t|dd�}t|�t|f|�}t|t�sVt|dd�S|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.
    @type event: L{dict} with arbitrary keys and values

    @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.
    @rtype: L{unicode}
    �charmapT)�default�encoding�skipkeyscSst|t�r|�d�St|�S)z�
            Serialize an object not otherwise serializable by L{dumps}.

            @param unencodable: An unencodable object.
            @return: C{unencodable}, serialized
            r<)r�bytes�decoder;)Zunencodablerrrr=�s

zeventAsJSON.<locals>.default)r=r?r�replace)r@r9rr;rrrr)�event�kwr=r:rrr�eventAsJSON�s
rEcCst|td�}|S)z�
    Decode a log event from JSON.

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

    @return: A reconstructed version of the log event.
    @rtype: L{dict}
    )Zobject_hook)rr7)Z	eventTextZloadedrrr�
eventFromJSON�s
rF�cst|�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{u"\n"}).  C{RS} is a record separator.  By default, this is a single
    RS character (C{u"\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{unicode} data.  Otherwise, UTF-8 L{bytes} will be used.
    @type outFile: L{io.IOBase}

    @param recordSeparator: The record separator to use.
    @type recordSeparator: L{unicode}

    @return: A file log observer.
    @rtype: L{FileLogObserver}
    csd��t|��S)Nz{0}{1}
)�formatrE)rC��recordSeparatorrrr-�r.z%jsonFileLogObserver.<locals>.<lambda>r)ZoutFilerJrrIr�jsonFileLogObserver�s
�rK�c#s�dd�}dd��|dkr:||�d��}|dkr4|}qFd}n||�}d}|dkrXd	}�}n�fd
d�}t|�}|�|�}|s�t|�dkr�||�}|dk	r�|Vq�|||�7}|�|�}	|	dd
�D]&}
t|
�dkr�||
�}|dk	r�|Vq�|	d
}qldS)a�
    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{unicode} or UTF-8 L{bytes}.
    @type inFile: iterable of lines

    @param recordSeparator: The expected record separator.
        If L{None}, attempt to automatically detect the record separator from
        one of C{u"\x1e"} or C{u""}.
    @type recordSeparator: L{unicode}

    @param bufferSize: The size of the read buffer used while reading from
        C{inFile}.
    @type bufferSize: integer

    @return: Log events as read from C{inFile}.
    @rtype: iterable of L{dict}
    cSst|�tkr|S|�d�SdS)Nr)rr@r")�srrrrsz&eventsFromJSONLogFile.<locals>.asBytescSstzt|��d�}Wn(tk
r:tjdt|�d�YdSXz
t|�WStk
rntjdt|�d�YdSXdS)Nrz2Unable to decode UTF-8 for JSON record: {record!r}��recordz&Unable to read JSON record: {record!r})r@rA�UnicodeDecodeError�log�errorrF�
ValueError)rO�textrrr�eventFromBytearrays �
�z1eventsFromJSONLogFile.<locals>.eventFromBytearrayNr�r.�
cs.|dtd�kr�|�Stjdt|�d�dS)N����
z0Unable to read truncated JSON record: {record!r}rN)�ordrQrRr@rN�rUrr�eventFromRecordDs�z.eventsFromJSONLogFile.<locals>.eventFromRecordrrX)�read�	bytearray�len�split)ZinFilerJZ
bufferSizer�firstr\�bufferZnewDatarCZrecordsrOrr[r�eventsFromJSONLogFiles:


rc)rG)NrL)#�__doc__r&Z
constantlyrZjsonrrr1rZ_flattenrZ_filer	Z_levelsr
Z_loggerrZtwisted.python.compatrr
Ztwisted.python.failurerrQrrr)r8rr6r7r;rErFrKrcrrrr�<module>sD ����(