a
    &b
                     @   sL   d dl Z d dlZd dlmZ dd Zdd Zdd Zdd
dZdddZ	dS )    N)replace_entitiesc                 C   s   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x r   +lib/python3.9/site-packages/parsel/utils.pyflatten   s    r	   c                 c   s2   | D ](}t |r&t|D ]
}|V  qq|V  qdS )zZiflatten(sequence) -> Iterator
    Similar to ``.flatten()``, but returns iterator insteadN)_is_listliker	   )r   ZelZel_r   r   r   r      s
    
r   c                 C   s   t | dot| tjtf S )at  
    >>> _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(six.moves.xrange(5))
    True
    __iter__)hasattr
isinstancesixZ	text_typebytesr   r   r   r   r
   #   s    r
   Tc                 C   s   t | tjrt| tj} d| jv rdz| |d}W n t	yN   g }Y qn0 |dur^|gng }n
| 
|}t|}|s~|S dd |D S )ab  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
    extractNc                 S   s   g | ]}t |d dgdqS )ltZamp)Zkeep)w3lib_replace_entities).0sr   r   r   
<listcomp>S       z!extract_regex.<locals>.<listcomp>)r   r   Zstring_typesrecompileUNICODE
groupindexsearchgroupAttributeErrorfindallr	   )Zregextextr   Z	extractedZstringsr   r   r   extract_regex;   s    


r    ...c                 C   s\   t | |kr| S |t |kr4| d|t |  | S |dkrP|t || d S tddS )z2Truncate the given text to fit in the given width.Nr   z%width must be equal or greater than 0)len
ValueError)r   widthsuffixr   r   r   shortenV   s    r&   )T)r!   )
r   r   Z
w3lib.htmlr   r   r	   r   r
   r    r&   r   r   r   r   <module>   s   
