
    3 d                     p   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	 d dl
mZmZmZmZmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ defd
Zd Z	 	 ddeeej        f         deeeej        f                  defdZddZddZddee         fdZ d Z!	 	 ddee         deee                  fdZ"dS )    N)ConfigParser)
itemgetter)Path)AnyDictListOptionalUnion)ScrapyDeprecationWarning
UsageError)BaseSettings)update_classpath)without_none_valuesc                    fdfd}d }t          |t          t          f          r/ |            t          |          fd|D                       S ||                     |            ||            t           ||                     } d t          |                                 t          d                    D             S )	z<Compose a component list from a { class: order } dictionary.c                     t          fd| D                       t          |           k    rt          d| d          d S )Nc                 &    h | ]} |          S  r   .0cconverts     1lib/python3.11/site-packages/scrapy/utils/conf.py	<setcomp>zBbuild_component_list.<locals>._check_components.<locals>.<setcomp>   s!    ---q

---    Some paths in 8 convert to the same object, please update your settings)len
ValueError)complistr   s    r   _check_componentsz/build_component_list.<locals>._check_components   se    ----H---..#h--? 	. . . .  	 	r   c                    t          | t                    rt                      }|                                 D ]\  }}|                     |          }|                     |                    |k    r2t	          dt          |                                           d          |                     |          ||           |S  |            fd|                                 D             S )Nr   r   )priorityc                 .    i | ]\  }} |          |S r   r   )r   kvr   s      r   
<dictcomp>z;build_component_list.<locals>._map_keys.<locals>.<dictcomp>)   s'    ;;;$!Q

A;;;r   )
isinstancer   itemsgetpriorityr   listkeysset)compdictcompbsr$   r%   prior    r   s        r   	_map_keysz'build_component_list.<locals>._map_keys   s   h-- 	!^^F (( 	= 	=1++A..%%ggajj11T9 =$>hmmoo)>)> > > >   JJwwqzz1tJ<<<<M(###;;;;(..*:*:;;;;r   c                     |                                  D ]7\  }}|0t          |t          j                  st	          d| d| d          8dS )zDFail if a value in the components dict is not a real number or None.NzInvalid value z for component z., please provide a real number or None instead)r(   r'   numbersRealr   )r-   namevalues      r   _validate_valuesz.build_component_list.<locals>._validate_values+   s    #>>++ 	 	KD% E7<)H)H  CU C C4 C C C  	 	r   c              3   .   K   | ]} |          V  d S )Nr   r   s     r   	<genexpr>z'build_component_list.<locals>.<genexpr>6   s+      771GGAJJ777777r   Nc                     g | ]\  }}|S r   r   )r   r$   r%   s      r   
<listcomp>z(build_component_list.<locals>.<listcomp>=   s    FFF$!QAFFFr      )key)	r'   r*   tupletypeupdater   sortedr(   r   )r-   customr   r0   r6   r    s     `  @r   build_component_listrB      s       < < < < < <"   &4-(( 8&!!!tF||7777777777  X"99X#6#677HFF&!1!1z!}}EEEFFFFr   c                 4    t          d | D                       S )zSConvert a list of arguments like ['arg1=val1', 'arg2=val2', ...] to a
    dict
    c              3   B   K   | ]}|                     d d          V  dS )=r;   N)split)r   xs     r   r8   z"arglist_to_dict.<locals>.<genexpr>D   s.      11AQ111111r   )dict)arglists    r   arglist_to_dictrJ   @   s!     11111111r   .pathprevpathreturnc                    |"t          |           t          |          k    rdS t          |                                           } | dz  }|                                rt          |          S t	          | j        |           S )zkReturn the path to the closest scrapy.cfg file by traversing the current
    directory and its parents
    N 
scrapy.cfg)strr   resolveexistsclosest_scrapy_cfgparent)rL   rM   cfgfiles      r   rU   rU   G   sz      D		S]] : r::D\!G~~ 7||dk4000r   defaultTc                 `   t                      }|                    d|           r#|                    d|           t          j        d<   t                      }|rRt          t          |          j                  }|r1|t          j
        vr%t          j
                            |           dS dS dS dS )zInitialize environment to use command-line tool from inside a project
    dir. This sets the Scrapy settings module and modifies the Python path to
    be able to locate the project module.
    settingsSCRAPY_SETTINGS_MODULEN)
get_config
has_optiongetosenvironrU   rR   r   rV   sysrL   append)projectset_syspathcfgclosestprojdirs        r   init_envrh   W   s    
 ,,C
~~j'** L/2wwz7/K/K
+, ""G %d7mm*++ 	%7#(2 	%HOOG$$$$$% %	% 	% 	% 	%r   c                 j    t          |           }t                      }|                    |           |S )z(Get Scrapy config file as a ConfigParser)get_sourcesr   read)use_closestsourcesre   s      r   r\   r\   f   s/    +&&G
..CHHWJr   c                 j   t           j                            d          p t          d                                          }ddt          t          |          dz            t          t          d                                                    g}| r!|                    t                                 |S )NXDG_CONFIG_HOMEz	~/.configz/etc/scrapy.cfgzc:\scrapy\scrapy.cfgrQ   z~/.scrapy.cfg)r_   r`   r^   r   
expanduserrR   rb   rU   )rl   xdg_config_homerm   s      r   rj   rj   n   s    

())KT+->->-I-I-K-K  	D!!L011D!!,,..//	G  -)++,,,Nr   c                 T   |                                  }|                    d|                    d                     |                    d|d                    |                    d|                    d          pd            |                    d|                    d                     |                    d	|d
                    |                    di            |d         |                    dd            n)|                    d|                    d                     |S )Nbatch_item_countFEED_EXPORT_BATCH_ITEM_COUNTencodingFEED_EXPORT_ENCODINGfieldsFEED_EXPORT_FIELDSstore_emptyFEED_STORE_EMPTY
uri_paramsFEED_URI_PARAMSitem_export_kwargsFEED_EXPORT_INDENTindent)copy
setdefaultgetintgetdictorlistgetbool)feedrZ   outs      r   *feed_complete_default_values_from_settingsr   }   s   
))++CNN%x7U'V'VWWWNN:x(>?@@@NN8X334HIIQTRRRNN=("2"23E"F"FGGGNN<*;!<===NN',,,$% Hx&&&&x1E!F!FGGGJr   outputoverwrite_outputc                    t          |                     d                                                    fd}d}|r&|rt          d          |rt          d          |}d}|rWt	          |          dk    r5 ||           d}t          j        |t          d	
           |d         d|iiS t          d          i }|D ]~}	 |                    dd          \  }	}
|	dk    rd}	n:# t          $ r- |}	t          |          j                            dd          }
Y nw xY w ||
           d|
i||	<   |rd||	         d<   |                    |                     d                     |S )z
    Receives feed export params (from the 'crawl' or 'runspider' commands),
    checks for inconsistencies in their quantities and returns a dictionary
    suitable to be used as the FEEDS setting.
    FEED_EXPORTERSc                 V    | vr#t          d|  dt                     d          d S )NzUnrecognized output format 'z'. Set a supported one (z`) after a colon at the end of the output URI (i.e. -o/-O <URI>:<FORMAT>) or as a file extension.)r   r=   )output_formatvalid_output_formatss    r   check_valid_formatz8feed_process_params_from_cli.<locals>.check_valid_format   sY     44 	:} : :(-.B(C(C: : :  	 	r   Fz<Please use only one of -o/--output and -O/--overwrite-outputa'  -t/--output-format is a deprecated command line option and does not work in combination with -O/--overwrite-output. To specify a format please specify it after a colon at the end of the output URI (i.e. -O <URI>:<FORMAT>). Example working in the tutorial: scrapy crawl quotes -O quotes.json:jsonTr;   a  The -t/--output-format command line option is deprecated in favor of specifying the output format within the output URI using the -o/--output or the -O/--overwrite-output option (i.e. -o/-O <URI>:<FORMAT>). See the documentation of the -o or -O option or the following examples for more information. Examples working in the tutorial: scrapy crawl quotes -o quotes.csv:csv   or   scrapy crawl quotes -O quotes.json:json   )
stacklevelr   formatzOThe -t command-line option cannot be used if multiple output URIs are specified:-zstdout:rK   rP   	overwriteFEEDS)r   getwithbaser+   r   r   warningswarnr   rsplitr   r   suffixreplacer?   getdict)rZ   r   r   r   r   r   messageresultelementfeed_urifeed_formatr   s              @r   feed_process_params_from_clir      s    /-.. 
dff      I  	N    	:   "	 
v;;! 	:}---:  M'#;JJJJ1I-899!
 
 	

 )+F 1 1	%$+NN3$:$:!Hk
 3 %$  	@ 	@ 	@Hw--.66sB??KKK	@ 	;'''$k2x 	1,0F8[) MM(""7++,,,Ms   C''4DD)rK   N)rX   T)T)NN)#r2   r_   ra   r   configparserr   operatorr   pathlibr   typingr   r   r   r	   r
   scrapy.exceptionsr   r   scrapy.settingsr   scrapy.utils.deprecater   scrapy.utils.pythonr   rB   rJ   rR   PathLikerU   rh   r\   rj   r   r   r   r   r   <module>r      s    				 



  % % % % % %             3 3 3 3 3 3 3 3 3 3 3 3 3 3 B B B B B B B B ( ( ( ( ( ( 3 3 3 3 3 3 3 3 3 3 3 3 +/8H -G -G -G -G`2 2 2 %(261 1
R[ 
!1uS"+-./1 	1 1 1 1 % % % %    T#Y      $ ,0	O OIO tCy)	O O O O O Or   