
    3 d1                        d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZmZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZ g d
Z G d d          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z  G d de          Z!dS )zK
Item Exporters are used to export/serialize items into different formats.
    N)Mapping)XMLGenerator)ItemAdapteris_item)ScrapyDeprecationWarning)Item)is_listliketo_bytes
to_unicode)ScrapyJSONEncoder)BaseItemExporterPprintItemExporterPickleItemExporterCsvItemExporterXmlItemExporterJsonLinesItemExporterJsonItemExporterMarshalItemExporterc                   B    e Zd ZdddZddZd Zd Zd Zd Zdd
Z	d	S )r   F	dont_failc                B    || _         |                     ||           d S )Nr   )_kwargs
_configure)selfr   kwargss      0lib/python3.11/site-packages/scrapy/exporters.py__init__zBaseItemExporter.__init__"   s%    )44444    c                 X   |                     dd          | _        |                     dd          | _        |                     dd          | _        |                     dd          | _        |s9|r9t          dd                    |                                                     dS dS )	zConfigure the exporter by popping options from the ``options`` dict.
        If dont_fail is set, it won't raise an exception on unexpected options
        (useful for using with keyword arguments in subclasses ``__init__`` methods)
        encodingNfields_to_exportexport_empty_fieldsFindentzUnexpected options: z, )popr!   r"   r#   r$   	TypeErrorjoinkeys)r   optionsr   s      r   r   zBaseItemExporter._configure&   s    
  J55 ',> E E#*;;/De#L#L kk(D11 	PW 	PN499W\\^^3L3LNNOOO	P 	P 	P 	Pr   c                     t           N)NotImplementedErrorr   items     r   export_itemzBaseItemExporter.export_item2   s    !!r   c                 F    |                     dd           } ||          S )N
serializerc                     | S r+    )xs    r   <lambda>z2BaseItemExporter.serialize_field.<locals>.<lambda>6   s    q r   )getr   fieldnamevaluer1   s        r   serialize_fieldz BaseItemExporter.serialize_field5   s'    YY|[[99
z%   r   c                     d S r+   r3   r   s    r   start_exportingz BaseItemExporter.start_exporting9       r   c                     d S r+   r3   r=   s    r   finish_exportingz!BaseItemExporter.finish_exporting<   r?   r   Nc              #   Z  K   t                    || j        }| j        ,|r                                }n                                }nyt          | j        t                    rB|r| j                                        }nCfd| j                                        D             }n|r| j        }nfd| j        D             }|D ]`}t          |t                    r||}}n|\  }}|v r3	                    |          }| 
                    |||                   }	n|}	||	fV  adS )z^Return the fields to export as an iterable of tuples
        (name, serialized_value)
        Nc              3   .   K   | ]\  }}|v 	||fV  d S r+   r3   ).0r4   yr.   s      r   	<genexpr>z:BaseItemExporter._get_serialized_fields.<locals>.<genexpr>Q   sH        #q!T	F     r   c              3   $   K   | ]
}|v |V  d S r+   r3   )rD   r4   r.   s     r   rF   z:BaseItemExporter._get_serialized_fields.<locals>.<genexpr>X   s,      LLA!t)LaLLLLLLr   )r   r#   r"   field_namesr(   
isinstancer   itemsstrget_field_metar;   )
r   r.   default_valueinclude_empty
field_iter
field_name
item_fieldoutput_field
field_metar:   s
    `        r   _get_serialized_fieldsz'BaseItemExporter._get_serialized_fields?   s      4   	5 4M  	M )!--//

!YY[[

-w77 	M !288::

   '+'<'B'B'D'D  

  M!2

LLLL)>LLL
$ 	& 	&J*c** 6+5zL

+5(
LT! &!00<<
,,ZtJGWXX%%%%%%	& 	&r   F)NN)
__name__
__module____qualname__r   r   r/   r;   r>   rA   rT   r3   r   r   r   r   !   s        $) 5 5 5 5 5
P 
P 
P 
P" " "! ! !    && && && && && &&r   r   c                   $     e Zd Z fdZd Z xZS )r   c                      t                      j        dddi| || _        | j                            d| j                    t          di | j        | _        d S )Nr   Tensure_asciir3   )superr   filer   
setdefaultr!   r   encoderr   r]   r   	__class__s      r   r   zJsonLinesItemExporter.__init__i   sd    22426222	DM0ABBB(884<88r   c                     t          |                     |                    }| j                            |          dz   }| j                            t          || j                             d S N
)dictrT   r_   encoder]   writer
   r!   r   r.   itemdictdatas       r   r/   z!JsonLinesItemExporter.export_itemo   s]    33D99::|""8,,t3	t}5566666r   rV   rW   rX   r   r/   __classcell__ra   s   @r   r   r   h   sG        9 9 9 9 97 7 7 7 7 7 7r   r   c                   6     e Zd Z fdZd Zd Zd Zd Z xZS )r   c                 4    t                      j        dddi| || _        | j        | j        dk    r| j        nd }| j                            d|           | j                            d| j                    t          di | j        | _        d| _	        d S )Nr   Tr   r$   r[   r3   )
r\   r   r]   r$   r   r^   r!   r   r_   
first_item)r   r]   r   json_indentra   s       r   r   zJsonItemExporter.__init__v   s    22426222	
  ;Pt{QPDKKD 	 	+666DM0ABBB(884<88r   c                 L    | j         | j                            d           d S d S )N   
)r$   r]   rg   r=   s    r   _beautify_newlinez"JsonItemExporter._beautify_newline   s0    ; 	#IOOE"""""	# 	#r   c                 b    | j                             d           |                                  d S )N   [)r]   rg   rt   r=   s    r   r>   z JsonItemExporter.start_exporting   s.    	     r   c                 b    |                                   | j                            d           d S )N   ])rt   r]   rg   r=   s    r   rA   z!JsonItemExporter.finish_exporting   s.       	r   c                 R   | j         rd| _         n.| j                            d           |                                  t	          |                     |                    }| j                            |          }| j                            t          || j	                             d S )NF   ,)
rp   r]   rg   rt   re   rT   r_   rf   r
   r!   rh   s       r   r/   zJsonItemExporter.export_item   s    ? 	%#DOOIOOD!!!""$$$33D99::|""8,,	t}5566666r   )	rV   rW   rX   r   rt   r>   rA   r/   rl   rm   s   @r   r   r   u   st            # # #! ! !  7 7 7 7 7 7 7r   r   c                   F     e Zd Z fdZd
dZddZd Zd Zd Zd	 Z	 xZ
S )r   c                     |                     dd          | _        |                     dd          | _         t                      j        di | | j        sd| _        t          || j                  | _        d S )Nitem_elementr.   root_elementrJ   utf-8r!   r3   )r%   r}   r~   r\   r   r!   r   xgr`   s      r   r   zXmlItemExporter.__init__   sw    "JJ~v>>"JJ~w??""6"""} 	$#DMtdm<<<r   Fc                 j    | j         )| j         dk    s|r| j                            d           d S d S d S )Nr   rd   r$   r   
characters)r   new_items     r   rt   z!XmlItemExporter._beautify_newline   sP    ; 	%a 	%8 	%Gt$$$$$	% 	% 	% 	%r      c                 b    | j         r'| j                            d| j         z  |z             d S d S )N r   )r   depths     r   _beautify_indentz XmlItemExporter._beautify_indent   s>    ; 	:GsT[05899999	: 	:r   c                     | j                                          | j                             | j        i            |                     d           d S )NTr   )r   startDocumentstartElementr~   rt   r=   s    r   r>   zXmlItemExporter.start_exporting   sN    T.333-----r   c                    |                      d           | j                            | j        i            |                                  |                     |d          D ]\  }}|                     ||d           |                      d           | j                            | j                   |                     d           d S )Nr   r    )rM      Tr   )r   r   r   r}   rt   rT   _export_xml_field
endElement)r   r.   r9   r:   s       r   r/   zXmlItemExporter.export_item   s    A&&&T.333   66t26NN 	9 	9KD%""4a"8888A&&&4,--------r   c                 v    | j                             | j                   | j                                          d S r+   )r   r   r~   endDocumentr=   s    r   rA   z XmlItemExporter.finish_exporting   s5    4,---r   c                    |                      |           | j                            |i            t          |d          r`|                                  |                                D ] \  }}|                     |||dz              !|                      |           nt          |          rK|                                  |D ]}|                     d||dz              |                      |           nWt          |t                    r| j        
                    |           n'| j        
                    t          |                     | j                            |           |                                  d S )Nr   rJ   r   r:   )r   r   r   hasattrrt   rJ   r   r	   rI   rK   r   r   )r   r9   serialized_valuer   subnamer:   s         r   r   z!XmlItemExporter._export_xml_field   s   E***T2&&&#W-- 	6""$$$"2"8"8":": H H&&wUQY&GGGG!!!....)** 	6""$$$) H H&&wUQY&GGGG!!!....(#.. 	6G/0000Gs#3445554        r   rU   )r   )rV   rW   rX   r   rt   r   r>   r/   rA   r   rl   rm   s   @r   r   r      s        = = = = =% % % %: : : :. . .
. . .  ! ! ! ! ! ! !r   r   c                   D     e Zd Z	 	 	 d
 fd	Zd Zd Zd Zd Zd	 Z xZ	S )r   T,Nc                     t                      j        dddi| | j        sd| _        || _        t	          j        |dd| j        d|          | _        t          j        | j        fi | j	        | _
        d| _        || _        d S )Nr   Tr   Fr   )line_bufferingwrite_throughr!   newlineerrorsr3   )r\   r   r!   include_headers_lineioTextIOWrapperstreamcsvwriterr   
csv_writer_headers_not_written_join_multivalued)r   r]   r   join_multivaluedr   r   ra   s         r   r   zCsvItemExporter.__init__   s     	22426222} 	$#DM$8!& ]
 
 
 *T[AADLAA$(!!1r   c                 N    |                     d| j                  } ||          S Nr1   )r6   _join_if_neededr7   s        r   r;   zCsvItemExporter.serialize_field   s(    YY|T-ABB
z%   r   c                     t          |t          t          f          r+	 | j                            |          S # t
          $ r Y nw xY w|S r+   )rI   listtupler   r'   r&   )r   r:   s     r   r   zCsvItemExporter._join_if_needed   sY    edE]++ 	-225999   s   8 
AAc                    | j         rd| _         |                     |           |                     |dd          }t          |                     d |D                                 }| j                            |           d S )NFr   T)rM   rN   c              3       K   | ]	\  }}|V  
d S r+   r3   )rD   _r4   s      r   rF   z.CsvItemExporter.export_item.<locals>.<genexpr>   s&      %;%;DAqa%;%;%;%;%;%;r   )r   '_write_headers_and_set_fields_to_exportrT   r   
_build_rowr   writerow)r   r.   fieldsvaluess       r   r/   zCsvItemExporter.export_item   s    $ 	?(-D%88>>>,,TSW,XXdoo%;%;F%;%;%;;;<<  (((((r   c              #   l   K   |D ].}	 t          || j                  V  # t          $ r |V  Y +w xY wd S r+   )r   r!   r&   )r   r   ss      r   r   zCsvItemExporter._build_row  sg       	 	A DM222222   	 	s    11c                 `   | j         r| j        s&t          |                                          | _        t	          | j        t
                    r| j                                        }n| j        }t          |                     |                    }| j	        
                    |           d S d S r+   )r   r"   r   rH   rI   r   r   r   r   r   r   )r   r.   r   rows       r   r   z7CsvItemExporter._write_headers_and_set_fields_to_export	  s    $ 		*( H(3D(9(9(E(E(G(G%$/99 /.5577.tv..//CO$$S)))))		* 		*r   )Tr   N)
rV   rW   rX   r   r;   r   r/   r   r   rl   rm   s   @r   r   r      s         "2 2 2 2 2 20! ! !  ) ) )  
* 
* 
* 
* 
* 
* 
*r   r   c                   &     e Zd Zd fd	Zd Z xZS )r      c                 V     t                      j        di | || _        || _        d S Nr3   )r\   r   r]   protocol)r   r]   r   r   ra   s       r   r   zPickleItemExporter.__init__  s2    ""6"""	 r   c                     t          |                     |                    }t          j        || j        | j                   d S r+   )re   rT   pickledumpr]   r   )r   r.   ds      r   r/   zPickleItemExporter.export_item  s;    ,,T2233Aty$-00000r   )r   rk   rm   s   @r   r   r     sL        ! ! ! ! ! !
1 1 1 1 1 1 1r   r   c                   (     e Zd ZdZ fdZd Z xZS )r   a1  Exports items in a Python-specific binary format (see
    :mod:`marshal`).

    :param file: The file-like object to use for exporting the data. Its
                 ``write`` method should accept :class:`bytes` (a disk file
                 opened in binary mode, a :class:`~io.BytesIO` object, etc)
    c                 H     t                      j        di | || _        d S r   r\   r   r]   r`   s      r   r   zMarshalItemExporter.__init__*  +    ""6"""			r   c                 z    t          j        t          |                     |                    | j                   d S r+   )marshalr   re   rT   r]   r-   s     r   r/   zMarshalItemExporter.export_item.  s2    T$55d;;<<diHHHHHr   )rV   rW   rX   __doc__r   r/   rl   rm   s   @r   r   r   !  sX             I I I I I I Ir   r   c                   $     e Zd Z fdZd Z xZS )r   c                 H     t                      j        di | || _        d S r   r   r`   s      r   r   zPprintItemExporter.__init__3  r   r   c                     t          |                     |                    }| j                            t	          t          j        |          dz                        d S rc   )re   rT   r]   rg   r
   pprintpformat)r   r.   ri   s      r   r/   zPprintItemExporter.export_item7  sN    33D99::	!9!9D!@AABBBBBr   rk   rm   s   @r   r   r   2  sN            C C C C C C Cr   r   c                   <     e Zd ZdZd fd	Zd Zd Zd Zd Z xZ	S )	PythonItemExportera<  This is a base class for item exporters that extends
    :class:`BaseItemExporter` with support for nested items.

    It serializes items to built-in Python types, so that any serialization
    library (e.g. :mod:`json` or msgpack_) can be used on top of it.

    .. _msgpack: https://pypi.org/project/msgpack/
    Fc                     |                     dd          | _        t                                          ||           | j        rt	          j        dt                     | j        s	d| _        d S d S )NbinaryTzDPythonItemExporter will drop support for binary export in the futurer   )r%   r   r\   r   warningswarnr   r!   )r   r)   r   ra   s      r   r   zPythonItemExporter._configureF  sw    kk(D117I...; 	MV(   } 	$#DMMM	$ 	$r   c                 N    |                     d| j                  } ||          S r   )r6   _serialize_valuer7   s        r   r;   z"PythonItemExporter.serialize_fieldQ  s(    YY|T-BCC
z%   r   c                 ~    t          |t                    r                     |          S t          |          r"t	                               |                    S t          |          r fd|D             S  j        rt          nt          }t          |t          t          f          r || j                  S |S )Nc                 :    g | ]}                     |          S r3   )r   )rD   vr   s     r   
<listcomp>z7PythonItemExporter._serialize_value.<locals>.<listcomp>[  s'    <<<D))!,,<<<r   r   )rI   r   r/   r   re   _serialize_itemr	   r   r
   r   rK   bytesr!   )r   r:   encode_funcs   `  r   r   z#PythonItemExporter._serialize_valueU  s    eT"" 	+##E***5>> 	5,,U33444u 	=<<<<e<<<<"&+=hh:ec5\** 	>;ut}====r   c              #      K   t          |                                          D ]6\  }}| j        rt          |          n|}||                     |          fV  7d S r+   )r   rJ   r   r
   r   )r   r.   keyr:   s       r   r   z"PythonItemExporter._serialize_itema  so      %d++1133 	4 	4JC#';7(3---CCt,,U3333333	4 	4r   c                     t          |                     |                    }| j        r"t          |                     |                    }|S r+   )re   rT   r   r   )r   r.   results      r   r/   zPythonItemExporter.export_itemf  sG    d11$7788; 	8$..v6677Fr   rU   )
rV   rW   rX   r   r   r;   r   r   r/   rl   rm   s   @r   r   r   <  s         	$ 	$ 	$ 	$ 	$ 	$! ! !
 
 
4 4 4
      r   r   )"r   r   r   r   r   r   r   collections.abcr   xml.sax.saxutilsr   itemadapterr   r   scrapy.exceptionsr   scrapy.itemr   scrapy.utils.pythonr	   r
   r   scrapy.utils.serializer   __all__r   r   r   r   r   r   r   r   r   r3   r   r   <module>r      s    


 				     # # # # # # ) ) ) ) ) ) , , , , , , , , 6 6 6 6 6 6       A A A A A A A A A A 4 4 4 4 4 4	 	 	D& D& D& D& D& D& D& D&N
7 
7 
7 
7 
7, 
7 
7 
7#7 #7 #7 #7 #7' #7 #7 #7L6! 6! 6! 6! 6!& 6! 6! 6!r?* ?* ?* ?* ?*& ?* ?* ?*D1 1 1 1 1) 1 1 1I I I I I* I I I"C C C C C) C C C. . . . .) . . . . .r   