
    h               
          S r SSKJrJr  SSKJrJrJrJrJ	r	J
r
JrJr  SSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  \" 5       r\\\4   rS\S\4S jr S\S\4S jr!S \" S5      S S 4S \" S5      \ \!4/r"\" V VVVs0 s H  u  pp#X_M
     snnnn r#S\S\$4S jr%S\$S\4S jr&S\S\4S jr'S\S\4S jr( S'S \\   S!\S\4S" jjr)  S(S$\\   S!\
\   S%\*S\	\   4S& jjr+g#s  snnnn f ))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                     [        U R                  5       [        U R                  R                  U R                  R                  S9S9$ )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    [/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/logger/_json.pyfailureAsJSONr       s?     ||..\\**
     failureDictc                     [         R                  " [         5      nU S   n[        US   SU5      U S'   UR                  U 5        U$ )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                 j    [        U [        5      =(       a    [        [        U R                  S 5      U L $ N)
isinstancer   getattrr   namelevels    r   <lambda>r1   @   s+    um, =%**d3u<=r!   z$02E59486-F24D-46AD-8224-3ACDF2A5732Ac                 (    [        U R                  S9$ )N)r.   )r   r.   r/   s    r   r1   r1   E   s    d

+r!   c                 *    [        [        U S   S 5      $ )Nr.   )r-   r   r/   s    r   r1   r1   F   s    ghft<r!   c                 "    [        U [        5      $ r+   )r,   r   )os    r   r1   r1   I   s    *Q(r!   z$E76887E2-20ED-49BF-A8F8-BA25CC586F2DaDictc                 H    SU ;   a  [         [        U S   5         " U 5      $ U $ )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                 x    [          H-  u  pp4U" U 5      (       d  M  U" U 5      n[        U5      US'   Us  $    SS0$ )am  
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   sI     +4&	\""<(F'*4yF#$M	 +4
 T""r!   eventc                 h    S[         S[        [        [        4   4S jn[	        U 5        [        XSS9$ )at  
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                 d    [        U [        5      (       a  U R                  S5      $ [        U 5      $ )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   defaulteventAsJSON.<locals>.default   s-     k5))%%i00k**r!   T)rM   skipkeys)objectr   JSONDictr?   r   r   )rF   rM   s     r   eventAsJSONrR   y   s5    
+V 
+hm(< 
+ $77r!   	eventTextc                 :    [        [        [        U [        S95      $ )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   rQ   r   r:   )rS   s    r   eventFromJSONrV      s     %	~FGGr!   outFilerecordSeparatorc                 $   ^ [        U U4S j5      $ )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 [        U 5       S3$ )N
)rR   )rF   rX   s    r   r1   %jsonFileLogObserver.<locals>.<lambda>   s    /!2;u3E2FbIr!   r   )rW   rX   s    `r   jsonFileLogObserverr]      s    , I r!   NinFile
bufferSizec              #   0  ^#    S[         S[        4S jnS[        S[        [           4S jmUc#  U" U R                  S5      5      nUS:X  a  UnOS	nO
U" U5      nS	nUS	:X  a  S
nTnOS[        S[        [           4U4S jjn[        U5      n U R                  U5      nU(       d  [        U5      S:  a  U" U5      n	U	b  U	v   gXs" U5      -  nUR                  U5      n
U
SS  H%  n[        U5      S:  d  M  U" U5      n	U	c  M!  U	v   M'     U
S   nM  7f)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{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                 R    [        U [        5      (       a  U $ U R                  S5      $ )Nutf-8)r,   rK   encode)ra   s    r   asBytes&eventsFromJSONLogFile.<locals>.asBytes   s#    aH88G$$r!   recordc                     [        U 5      R                  S5      n [        U5      $ ! [         a     [        R	                  S[        U 5      S9   g f = f! [         a     [        R	                  S[        U 5      S9   g f = f)Nrc   z2Unable to decode UTF-8 for JSON record: {record!r}rg   z&Unable to read JSON record: {record!r})rK   rL   UnicodeDecodeErrorlogerrorrV   
ValueError)rg   texts     r   eventFromBytearray1eventsFromJSONLogFile.<locals>.eventFromBytearray   s    	=''0D	 && " 	IIDV}   	  	II>uV}IU	s    ( 
A 'AA'A?>A?Nr      r!      
c                 t   > U S   [        S5      :X  a  T" U 5      $ [        R                  S[        U 5      S9  g )Nr[   z0Unable to read truncated JSON record: {record!r}ri   )ordrk   rl   rK   )rg   ro   s    r   eventFromRecord.eventsFromJSONLogFile.<locals>.eventFromRecord   s>    bzSY&)&11		F =   r!   r   rt   )r   rK   	bytearrayr
   r   readlensplit)r^   rX   r_   re   firstrecordSeparatorBytesrv   buffernewDatarF   recordsrg   ro   s               @r   eventsFromJSONLogFiler      s?    (%6 %e %9 (1C   A'G#(  $'   '7s"$,	I 	(82D 	 uF
++j)6{Q'/$K''"",,34crlF6{Q'/$K	 # ' s   C3D:	DD))Ni   ),__doc__jsonr   r   typingr   r   r   r   r	   r
   r   r   rA   r   
constantlyr   twisted.python.failurer   _filer   _flattenr   _interfacesr   _levelsr   _loggerr   rk   r?   rQ   r    r)   r>   r9   rP   r:   rE   rR   rV   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OY!A)5YO( v  # #H #*8x 8C 8>HS HX H .4W'*: &*_sG_c]_ _ h	_[ Ps   C7
