
    pe~              
           d dl 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ee   dee   fdZdee   dee   fdZdedefdZ	 dd	eeee   f   d
ededee   fdZdd
edededefdZy)    N)AnyIterableIteratorListMatchPatternUnioncast)replace_entitiesxreturnc                 *    t        t        |             S )a  flatten(sequence) -> list
    Returns a single, flat list which contains all elements retrieved
    from the sequence and all recursively contained sub-sequences
    (iterables).
    Examples:
    >>> [1, 2, [3,4], (5,6)]
    [1, 2, [3, 4], (5, 6)]
    >>> flatten([[[1,2,3], (42,None)], [4,5], [6], 7, (8,9,10)])
    [1, 2, 3, 42, None, 4, 5, 6, 7, 8, 9, 10]
    >>> flatten(["foo", "bar"])
    ['foo', 'bar']
    >>> flatten(["foo", ["baz", 42], "bar"])
    ['foo', 'baz', 42, 'bar']
    )listiflattenr   s    ,lib/python3.12/site-packages/parsel/utils.pyflattenr      s         c              #   d   K   | D ]%  }t        |      rt        |      E d{    "| ' y7 w)zZiflatten(sequence) -> Iterator
    Similar to ``.flatten()``, but returns iterator insteadN)_is_listliker   )r   els     r   r   r      s4       r{""H	"s   0.0c                 L    t        | d      xr t        | t        t        f       S )ai  
    >>> _is_listlike("foo")
    False
    >>> _is_listlike(5)
    False
    >>> _is_listlike(b"foo")
    False
    >>> _is_listlike([b"foo"])
    True
    >>> _is_listlike((b"foo",))
    True
    >>> _is_listlike({})
    True
    >>> _is_listlike(set())
    True
    >>> _is_listlike((x for x in range(3)))
    True
    >>> _is_listlike(range(5))
    True
    __iter__)hasattr
isinstancestrbytesr   s    r   r   r   "   s$    * 1j!E*Qe*E&EEr   regextextr   c                    t        | t              r$t        j                  | t        j                        } d| j
                  v r>	 t        t        t           | j                  |            j                  d      }||gng }n| j                  |      }t        |      }|s|S |D cg c]  }t        |ddg       c}S # t        $ r g }Y 9w xY wc c}w )aZ  Extract a list of 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
    extractltamp)keep)r   r   recompileUNICODE
groupindexr
   r   searchgroupAttributeErrorfindallr   w3lib_replace_entities)r   r   r   	extractedstringsss         r   extract_regexr1   :   s     %

5"**-E$$$	CU3Zd);<BB9MI &/%:ykG --%gGCJKa"1D%=9KK  	G	 Ls   5B> %C>CCwidthsuffixc                     t        |       |k  r| S |t        |      kD  r| d|t        |      z
   |z   S |dk\  r|t        |      |z
  d S t        d      )z2Truncate the given text to fit in the given width.Nr   z%width must be equal or greater than 0)len
ValueError)r   r2   r3   s      r   shortenr7   W   se    
4yEs6{)ec&k)*V33zc&kE)+,,
<
==r   )T)z...)r%   typingr   r   r   r   r   r   r	   r
   
w3lib.htmlr   r-   r   r   boolr   r   r1   intr7    r   r   <module>r=      s    	 M M M Ax} c $ (3- FC FD F2 JNLgcl"#L+.LBFL	#YL:># >c >3 >3 >r   