
    Vh               
          d Z ddlmZmZ ddlmZmZmZmZm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ  e       Zeeef   ZdedefdZ dedefdZ!d  ed      d d fd  ed      e e!fgZ"e"D  ci c]
  \  } }}}|| c}}}} Z#dede$fdZ%de$defdZ&dedefdZ'dedefdZ(	 d'd ee   d!edefd"Z)	 	 d(d$ee   d!e
e   d%e*de	e   fd&Z+y#c c}}}} w ))zA
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returnc                     t        | j                         t        | j                  j                  | j                  j                              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__r   r   r   )r   s    D/home/dcms/DCMS/lib/python3.12/site-packages/twisted/logger/_json.pyfailureAsJSONr       s?     ||..\\**
     failureDictc                     t        j                  t               }| d   }t        |d   d|      | d<   |j                  |        |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}
    r   r    )r   __new__r   __setstate__)r"   ftypeInfos      r   failureFromJSONr)   .   sE     	 A6"Hx
3RBKNN;Hr!   c                 `    t        | t              xr t        t        | j                  d       | u S N)
isinstancer   getattrr   namelevels    r   <lambda>r1   @   s+    um, =%**d3u< r!   z$02E59486-F24D-46AD-8224-3ACDF2A5732Ac                 .    t        | j                        S )N)r.   )r   r.   r/   s    r   r1   r1   E   s    d

+ r!   c                 *    t        t        | d   d       S )Nr.   )r-   r   r/   s    r   r1   r1   F   s    ghft< r!   c                 "    t        | t              S r+   )r,   r   )os    r   r1   r1   I   s    *Q( r!   z$E76887E2-20ED-49BF-A8F8-BA25CC586F2DaDictc                 B    d| v rt        t        | 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   )r6   s    r   objectLoadHookr:   T   s,     5 D'7!89:5AALr!   pythonObjectc                 n    t         D ])  \  }}}} ||       s ||       }t        |      |d<   |c S  ddiS )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.
    r8   unpersistableT)	classInfostr)r;   	predicateuuidsaverloaderresults         r   objectSaveHookrE   d   sO     +4 &	4\"<(F'*4yF#$M	
 T""r!   eventc                 n    dt         dt        t        t        f   fd}t	        |        t        | |d      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.
    unencodabler   c                 Z    t        | t              r| j                  d      S t        |       S )z
        Serialize an object not otherwise serializable by L{dumps}.

        @param unencodable: An unencodable object.

        @return: C{unencodable}, serialized
        charmap)r,   bytesdecoderE   )rH   s    r   defaultzeventAsJSON.<locals>.default   s*     k5)%%i00k**r!   T)rM   skipkeys)objectr   JSONDictr?   r   r   )rF   rM   s     r   eventAsJSONrQ   y   s7    
+V 
+hm(< 
+ $77r!   	eventTextc                 @    t        t        t        | t                    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)r   rP   r   r:   )rR   s    r   eventFromJSONrU      s     %	~FGGr!   outFilerecordSeparatorc                 "    t        | fd      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.
    c                 $     t        |        dS )N
)rQ   )rF   rW   s    r   r1   z%jsonFileLogObserver.<locals>.<lambda>   s    /!2;u3E2FbI r!   r   )rV   rW   s    `r   jsonFileLogObserverr[      s    , I r!   NinFile
bufferSizec              #     K   dt         dt        fd}dt        dt        t           fd|" || j                  d            }|dk(  r|}nd	}n
 ||      }d	}|d	k(  rd
}}ndt        dt        t           ffd}t        |      }	 | j                  |      }|st        |      dkD  r ||      }	|	|	 y| ||      z  }|j                  |      }
|
dd D ]   }t        |      dkD  s ||      }	|	|	 " |
d   }zw)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}.
    sr   c                 H    t        | t              r| S | j                  d      S )Nutf-8)r,   rK   encode)r_   s    r   asBytesz&eventsFromJSONLogFile.<locals>.asBytes   s     aH88G$$r!   recordc                    	 t        |       j                  d      }	 t        |      S # t        $ r# t        j	                  dt        |              Y y w xY w# t        $ r# t        j	                  dt        |              Y y w xY w)Nra   z2Unable to decode UTF-8 for JSON record: {record!r}rd   z&Unable to read JSON record: {record!r})rK   rL   UnicodeDecodeErrorlogerrorrU   
ValueError)rd   texts     r   eventFromBytearrayz1eventsFromJSONLogFile.<locals>.eventFromBytearray   s    	=''0D	 && " 	IIDV}   	  	II>uV}IU	s    ( 
A )AA)BBNr      r!      
c                 x    | d   t        d      k(  r |       S t        j                  dt        |              y )NrZ   z0Unable to read truncated JSON record: {record!r}rf   )ordrh   ri   rK   )rd   rl   s    r   eventFromRecordz.eventsFromJSONLogFile.<locals>.eventFromRecord   s>    bzSY&)&11		F =   r!   r   rp   )r   rK   	bytearrayr
   r   readlensplit)r\   rW   r]   rc   firstrecordSeparatorBytesrr   buffernewDatarF   recordsrd   rl   s               @r   eventsFromJSONLogFiler|      sE    (%6 %e %9 (1C   A'G#(  $'   '7s"$,	I 	(82D 	 uF
++j)6{Q'/$K''"",,34crl 	 F6{Q'/$K		  ' s   C'D+
D6D))Ni   ),__doc__jsonr   r   typingr   r   r   r   r	   r
   r   r   rA   r   
constantlyr   twisted.python.failurer   _filer   _flattenr   _interfacesr   _levelsr   _loggerr   rh   r?   rP   r    r)   r>   r9   rO   r:   rE   rQ   rU   r[   intr|   )r@   rA   rB   rC   s   0000r   <module>r      s  
  I I I  $ * " " !  h S>7 x $ g $	
 	34+< 	)34		& FOOO!A)T5&fO( v  # #H #*8x 8C 8>HS HX H .4W'*: &*_sG_c]_ _ h	_[ Ps   C-
