
    3 d=$                     .   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 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mZ eeeefZd Z d Z!d Z"ddZ#d Z$d Z%d Z&e
d             Z'd Z( ed          Z)d Z*d Z+dS )z.Helper functions which don't fit anywhere else    N)deque)contextmanager)partial)import_module)iter_modulesreplace_entities)Item)LocalWeakReferencedCache)ScrapyDeprecationWarning)flatten
to_unicodec                 ^    | g S t          | t                    st          | d          r| S | gS )zConvert an argument to an iterable. The argument can be a None, single
    value, or an iterable.

    Exception: if arg is a dict, [arg] will be returned
    N__iter__)
isinstance_ITERABLE_SINGLE_VALUEShasattr)args    1lib/python3.11/site-packages/scrapy/utils/misc.pyarg_to_iterr      sA      	c233 Z8P8P 
5L    c                    t          | t                    s0t          |           r| S t          dt	          |                      	 |                     d          }n!# t          $ r t          d|  d          w xY w| d|         | |dz   d         }}t          |          }	 t          ||          }n$# t          $ r t          d| d| d	          w xY w|S )
aR  Load an object given its absolute object path, and return it.

    The object can be the import path of a class, function, variable or an
    instance, e.g. 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware'.

    If ``path`` is not a string, but is a callable object, such as a class or
    a function, then return it as is.
    z:Unexpected argument type, expected string or object, got: .zError loading object 'z': not a full pathN   zModule 'z#' doesn't define any object named '')r   strcallable	TypeErrortyperindex
ValueErrorr   getattrAttributeError	NameError)pathdotmodulenamemodobjs         r   load_objectr+   %   s*    dC   
D>> 	KXDQUJJXX
 
 	
Lkk# L L LJ$JJJKKKL :tC!GIIDF


CWc4   W W WU6UUdUUUVVVW Js   A A;%B6 6!Cc                 *   g }t          |           }|                    |           t          |d          r\t          |j                  D ]G\  }}}| dz   |z   }|r|t          |          z  }#t          |          }|                    |           H|S )zLoads a module and all its submodules from the given module path and
    returns them. If *any* module throws an exception while importing, that
    exception is thrown back.

    For example: walk_modules('scrapy.utils')
    __path__r   )r   appendr   r   r-   walk_modules)r%   modsr)   _subpathispkgfullpathsubmods           r   r/   r/   F   s     D


CKKsJ $!-cl!;!; 	$ 	$AwczG+H $X...&x00F####Kr   utf-8c                    t          j        dt          d           t          | t                    rt          j        | t
          j                  } 	 |                     |          	                    d          g}n%# t          $ r |                     |          }Y nw xY wt          |          }t          |t                    rd |D             S fd|D             S )ac  Extract a list of unicode strings from the given text/encoding using the following policies:

    * if the regex contains a named group called "extract" that will be returned
    * if the regex contains multiple numbered groups, all those will be returned (flattened)
    * if the regex doesn't contain any group the entire regex matching is returned
    zHscrapy.utils.misc.extract_regex has moved to parsel.utils.extract_regex.   
stacklevelextractc                 4    g | ]}t          |d dg          S ltamp)keepr   ).0ss     r   
<listcomp>z!extract_regex.<locals>.<listcomp>s   s*    IIIA $777IIIr   c                 R    g | ]#}t          t          |          d dg          $S r=   )r	   r   )rA   rB   encodings     r   rC   z!extract_regex.<locals>.<listcomp>t   sB       JKAx00e}EEE  r   )warningswarnr   r   r   recompileUNICODEsearchgroup	Exceptionfindallr   )regextextrE   stringss     ` r   extract_regexrR   \   s    MR     % .
5"*--&<<%%++I667 & & &--%%&gG$ JIIIIII   OV   s   )A= =BBc                     t          j                    }	 |                     d          }|sn|                    |           .|                                S )zCalculate the md5 checksum of a file-like object without reading its
    whole content in memory.

    >>> from io import BytesIO
    >>> md5sum(BytesIO(b'file content to hash'))
    '784406af91dd5a54fbb9c84c2236595a'
    Ti  )hashlibmd5readupdate	hexdigest)filemds      r   md5sumr\   y   sR     	AIIdOO 			
 ;;==r   c                 ^    | duo)d|                      dd                                          v S )z3Return True if link rel attribute has nofollow typeNnofollow, )replacesplit)rels    r   rel_has_nofollowrd      s0    d?JzS[[c-B-B-H-H-J-JJJr   c                    ||t          d          |j        }|r$t          | d          r | j        |g|R i |}d}n.t          | d          r | j        |g|R i |}d}n
 | |i |}d}|t          | j         d| d          |S )a  Construct a class instance using its ``from_crawler`` or
    ``from_settings`` constructors, if available.

    At least one of ``settings`` and ``crawler`` needs to be different from
    ``None``. If ``settings `` is ``None``, ``crawler.settings`` will be used.
    If ``crawler`` is ``None``, only the ``from_settings`` constructor will be
    tried.

    ``*args`` and ``**kwargs`` are forwarded to the constructors.

    Raises ``ValueError`` if both ``settings`` and ``crawler`` are ``None``.

    .. versionchanged:: 2.2
       Raises ``TypeError`` if the resulting instance is ``None`` (e.g. if an
       extension has not been implemented correctly).
    Nz-Specify at least one of settings and crawler.from_crawlerfrom_settings__new__r   z returned None)r!   settingsr   rf   rg   r   __qualname__)objclsri   crawlerargskwargsinstancemethod_names          r   create_instancerq      s    "  $ 	NLMMM#  76>22  &6&w@@@@@@$		)	)  '6'B4BBB6BB%64*6** N6.LLLLLMMMOr   c               +   X  K   d | D             }t           j                            |            	 dV  |                                D ]$\  }}|t           j        |= |t           j        |<   %dS # |                                D ]$\  }}|t           j        |= |t           j        |<   %w xY w)z{Temporarily set environment variables inside the context manager and
    fully restore previous environment afterwards
    c                 N    i | ]"}|t           j                            |          #S  )osenvironget)rA   ks     r   
<dictcomp>zset_environ.<locals>.<dictcomp>   s(    999QArz~~a((999r   N)ru   rv   rW   items)rn   original_envrx   vs       r   set_environr}      s       :9&999LJf" &&(( 	" 	"DAq "JqMM !
1		" 	"L&&(( 	" 	"DAq "JqMM !
1		"s   A. .;B)c              #      K   t          | g          }d}|rb|                                } t          | t          j                  r|r3d}|                    t          j        |                      | V  |`dS dS )zrSimilar to ``ast.walk``, but walks only function body and skips nested
    functions defined within the node.
    FTN)r   popleftr   astFunctionDefextenditer_child_nodes)nodetodowalked_func_defs      r   walk_callabler      s       $==DO
 ||~~dCO,, 	# "OC(..///


      r      )limitc                    | t           v rt           |          S d }t          j        |           r| }t          |t                    r|j        }t          |t                    t          j        |          }t          j        d          }|	                    d|          }|
                    |          }|r,t          j	        d|                    d           d|          }t          j        |          }t          |          D ]@}t          |t          j                  r$ ||          sdt           | <   t           |          c S Adt           | <   t           |          S )z
    Returns True if a callable is a generator function which includes a
    'return' statement with a value different than None, False otherwise
    c                 ^    | j         }|d u p"t          |t          j                  o|j         d u S )N)valuer   r   NameConstant)return_noder   s     r   returns_nonez4is_generator_with_return_value.<locals>.returns_none   s5    !TMXZs/?@@XU[TXEX	
r   z	(^[\t ]+) 
r   TF)_generator_callbacks_cacheinspectisgeneratorfunctionr   r   func	getsourcerH   rI   submatchrL   r   parser   Return)	r   r   r   srcpatterncoder   treer   s	            r   is_generator_with_return_valuer      sb   
 -- 4)(33
 
 
 "8,, <w'' 	9D w'' 	 %%*\**{{2s##c"" 	=6/u{{1~~//t<<Dy!$'' 	< 	<D$
++ <LL4F4F <7;*841(;;;;+0x(%h//r   c           
         	 t          |          r.t          j        d| j        j         d|j         dd           d
S d
S # t
          $ r; | j        j        dz   |j        z   }t          j        d| d| d| d	d           Y d
S w xY w)z
    Logs a warning if a callable is a generator function and includes
    a 'return' statement with a value different than None
    zThe "r   a&  " method is a generator and includes a "return" statement with a value different than None. This could lead to unexpected behaviour. Please see https://docs.python.org/3/reference/simple_stmts.html#the-return-statement for details about the semantics of the "return" statement within generatorsr8   r9   z$Unable to determine whether or not "z" is a generator with a return value. This will not prevent your code from working, but it prevents Scrapy from detecting potential issues in your implementation of "zu". Please, report this in the Scrapy issue tracker (https://github.com/scrapy/scrapy/issues), including the code of ""N)r   rF   rG   	__class____name__IndentationError)spiderr   callable_names      r   #warn_on_generator_with_return_valuer      s   

)(33 	M^(1 ^ ^H4E ^ ^ ^
      	 	  	
 	
 	
(1C7(:KK7= 7 7;H7 7 '4	7 7 7
 	
 	
 	
 	
 	
 	
 	
	
s   ;A ABB)r6   ),__doc__r   rT   r   ru   rH   rF   collectionsr   
contextlibr   	functoolsr   	importlibr   pkgutilr   
w3lib.htmlr	   scrapy.itemr
   scrapy.utils.datatypesr   scrapy.utils.deprecater   scrapy.utils.pythonr   r   dictr   bytesr   r   r+   r/   rR   r\   rd   rq   r}   r   r   r   r   rt   r   r   <module>r      s   4 4 



   				 				        % % % % % %       # # # # # #             ' ' ' ' ' '       ; ; ; ; ; ; ; ; ; ; ; ; 3 3 3 3 3 3 3 3c50 
 
 
  B  ,   :  "K K K
     F " " ""    65C@@@ "0 "0 "0J
 
 
 
 
r   