Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/twisted/logger/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/twisted/logger/__pycache__/_json.cpython-37.pyc

B

:��]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�G/opt/alt/python37/lib64/python3.7/site-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>zutf-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�|kS)N)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�S)N)rr
)�orrrr)sr*z$E76887E2-20ED-49BF-A8F8-BA25CC586F2DcCsg|]\}}}}||f�qSrr)r�	predicate�uuid�saver�loaderrrr�
<listcomp>|sr0cCs 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
r3cCs>x4tD],\}}}}||�r||�}t|�|d<|SqWddiS)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.
    r1Z
unpersistableT)�	classInfo�str)ZpythonObjectr,r-r.r/�resultrrr�objectSaveHook�sr7cCsZttkrttddd�}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
            r8)r�bytes�decoder7)Zunencodablerrrr9�s

zeventAsJSON.<locals>.default)r9r;zutf-8�replace)r<r5rr7rrrr)�event�kwr9r6rrr�eventAsJSON�s
rAcCst|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}
    )�object_hook)rr3)Z	eventText�loadedrrr�
eventFromJSON�s
rD�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}
)�formatrA)r?)�recordSeparatorrrr)�r*z%jsonFileLogObserver.<locals>.<lambda>)r)ZoutFilerGr)rGr�jsonFileLogObserver�srH�c#s�dd�}dd��|dkr:||�d��}|dkr4|}qFd}n||�}d}|dkrXd	}�}n�fd
d�}t|�}x�|�|�}|s�t|�dkr�||�}|dk	r�|VP|||�7}|�|�}	x6|	dd
�D]&}
t|
�dkr�||
�}|dk	r�|Vq�W|	d
}qnWdS)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)Nzutf-8)rr<r)�srrrrsz&eventsFromJSONLogFile.<locals>.asBytescSsnyt|��d�}Wn&tk
r8tjdt|�d�dSXyt|�Stk
rhtjdt|�d�dSXdS)Nzutf-8z2Unable to decode UTF-8 for JSON record: {record!r})�recordz&Unable to read JSON record: {record!r})r<r=�UnicodeDecodeError�log�errorrD�
ValueError)rK�textrrr�eventFromBytearraysz1eventsFromJSONLogFile.<locals>.eventFromBytearrayNr�r*�
cs.|dtd�kr�|�Stjdt|�d�dS)N����
z0Unable to read truncated JSON record: {record!r})rK)�ordrMrNr<)rK)rQrr�eventFromRecordDsz.eventsFromJSONLogFile.<locals>.eventFromRecordrrT)�read�	bytearray�len�split)ZinFilerGZ
bufferSizer�firstrW�bufferZnewDatar?�recordsrKr)rQr�eventsFromJSONLogFiles<



r_)rE)NrI)#�__doc__r#Z
constantlyr�jsonrrr-r�_flattenr�_filerZ_levelsr	Z_loggerr
Ztwisted.python.compatrrZtwisted.python.failurer
rMrrr&r4rr2r3r7rArDrHr_rrrr�<module>s: 
(