509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
Ç�aWÉa  ã               @   s¼   d Z ddlZddlZddlmZmZmZ ejd dk r@eZ	e
ZneZ	eZddd„ZG dd„ deeƒZG d	d
„ d
eƒZejdƒjZejdƒjZejdƒjZG dd„ deƒZG dd„ deƒZdS )a  
    cssselect.xpath
    ===============

    Translation of parsed CSS selectors to XPath expressions.


    :copyright: (c) 2007-2012 Ian Bicking and contributors.
                See AUTHORS for more details.
    :license: BSD, see LICENSE for more details.

é    N)ÚparseÚparse_seriesÚSelectorErroré   c             C   s   |j ddƒjdƒ}t| ||ƒS )NÚasciiÚreplace)ÚencodeÚdecodeÚgetattr)ÚobjÚnameÚdefault© r   ú/usr/lib/python3.6/xpath.pyÚ_unicode_safe_getattr   s    r   c               @   s   e Zd ZdZdS )ÚExpressionErrorz3Unknown or unsupported selector (eg. pseudo-class).N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   #   s   r   c               @   sF   e Zd Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Z	dS )Ú	XPathExprÚ Ú*Fc             C   s   || _ || _|| _d S )N)ÚpathÚelementÚ	condition)Úselfr   r   r   Zstar_prefixr   r   r   Ú__init__+   s    zXPathExpr.__init__c             C   s,   t | jƒt | jƒ }| jr(|d| j 7 }|S )Nz[%s])Ú_unicoder   r   r   )r   r   r   r   r   Ú__str__0   s    zXPathExpr.__str__c             C   s   d| j j| f S )Nz%s[%s])Ú	__class__r   )r   r   r   r   Ú__repr__6   s    zXPathExpr.__repr__c             C   s"   | j rd| j |f | _ n|| _ | S )Nz%s and (%s))r   )r   r   r   r   r   Úadd_condition9   s    zXPathExpr.add_conditionc             C   s.   | j dkrd S | jdtj| j ƒ ƒ d| _ d S )Nr   zname() = %s)r   r"   ÚGenericTranslatorÚxpath_literal)r   r   r   r   Úadd_name_test@   s
    
zXPathExpr.add_name_testc             C   s   |  j d7  _ dS )ze
        Append '*/' to the path to keep the context constrained
        to a single parent.
        z*/N)r   )r   r   r   r   Úadd_star_prefixH   s    zXPathExpr.add_star_prefixc             C   s:   t | ƒ| }|jdkr ||j7 }|| _|j| _|j| _| S )Nz*/)r   r   r   r   )r   ZcombinerÚotherr   r   r   r   ÚjoinO   s    

zXPathExpr.joinN)r   r   r   F)
r   r   r   r   r   r!   r"   r%   r&   r(   r   r   r   r   r   )   s   
r   z('+)z^[a-zA-Z_][a-zA-Z0-9_.-]*$z^[^ \t\r\n\f]+$c            	   @   sº  e Zd ZdZdddddœZddd	d
dddddœZdZdZdZdZ	dZ
eZdfdd„Zdgdd„Zdd„ Zedd„ ƒZdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zdhd7d8„Zd9d:„ Z d;d<„ Z!d=d>„ Z"d?d@„ Z#dAdB„ Z$dCdD„ Z%dEdF„ Z&dGdH„ Z'dIdJ„ Z(dKdL„ Z)dMdN„ Z*dOdP„ Z+dQdR„ Z,dSdT„ Z-e-Z.e-Z/e-Z0e-Z1e-Z2e-Z3e-Z4e-Z5e-Z6dUdV„ Z7dWdX„ Z8dYdZ„ Z9d[d\„ Z:d]d^„ Z;d_d`„ Z<dadb„ Z=dcdd„ Z>deS )ir#   z¤
    Translator for "generic" XML documents.

    Everything is case-sensitive, no assumption is made on the meaning
    of element names and attribute names.

    Z
descendantZchildZdirect_adjacentZindirect_adjacent)ú ú>ú+ú~ÚexistsZequalsZincludesZ	dashmatchZprefixmatchZsuffixmatchZsubstringmatchZ	different)r-   ú=z~=z|=z^=z$=z*=z!=Úidzxml:langFúdescendant-or-self::c                s   dj ‡ ‡fdd„t|ƒD ƒƒS )uµ  Translate a *group of selectors* to XPath.

        Pseudo-elements are not supported here since XPath only knows
        about "real" elements.

        :param css:
            A *group of selectors* as an Unicode string.
        :param prefix:
            This string is prepended to the XPath expression for each selector.
            The default makes selectors scoped to the context nodeâ€™s subtree.
        :raises:
            :class:`SelectorSyntaxError` on invalid selectors,
            :class:`ExpressionError` on unknown/unsupported selectors,
            including pseudo-elements.
        :returns:
            The equivalent XPath 1.0 expression as an Unicode string.

        z | c             3   s   | ]}ˆj |ˆ d d�V  qdS )T)Útranslate_pseudo_elementsN)Úselector_to_xpath)Ú.0Úselector)Úprefixr   r   r   ú	<genexpr>¾   s   z1GenericTranslator.css_to_xpath.<locals>.<genexpr>)r(   r   )r   Zcssr5   r   )r5   r   r   Úcss_to_xpath«   s    zGenericTranslator.css_to_xpathc             C   s`   t |ddƒ}|std|f ƒ‚| j|ƒ}t|| jƒs8t‚|rP|jrP| j||jƒ}|pVdt|ƒ S )u4  Translate a parsed selector to XPath.


        :param selector:
            A parsed :class:`Selector` object.
        :param prefix:
            This string is prepended to the resulting XPath expression.
            The default makes selectors scoped to the context nodeâ€™s subtree.
        :param translate_pseudo_elements:
            Unless this is set to ``True`` (as :meth:`css_to_xpath` does),
            the :attr:`~Selector.pseudo_element` attribute of the selector
            is ignored.
            It is the caller's responsibility to reject selectors
            with pseudo-elements, or to account for them somehow.
        :raises:
            :class:`ExpressionError` on unknown/unsupported selectors.
        :returns:
            The equivalent XPath 1.0 expression as an Unicode string.

        Zparsed_treeNz"Expected a parsed selector, got %rr   )	r
   Ú	TypeErrorÚxpathÚ
isinstanceÚxpathexpr_clsÚAssertionErrorÚpseudo_elementÚxpath_pseudo_elementr   )r   r4   r5   r1   Ztreer9   r   r   r   r2   Â   s    

z#GenericTranslator.selector_to_xpathc             C   s   t dƒ‚dS )z�Translate a pseudo-element.

        Defaults to not supporting pseudo-elements at all,
        but can be overridden by sub-classes.

        z"Pseudo-elements are not supported.N)r   )r   r9   r=   r   r   r   r>   á   s    z&GenericTranslator.xpath_pseudo_elementc             C   sL   t | ƒ} d| krd|  } n.d| kr,d|  } nddjdd„ t| ƒD ƒƒ } | S )	Nú'z'%s'ú"z"%s"z
concat(%s)ú,c             S   s$   g | ]}|rd |krdpd| ‘qS )r?   z"%s"z'%s'r   )r3   Úpartr   r   r   ú
<listcomp>ó   s   z3GenericTranslator.xpath_literal.<locals>.<listcomp>)r   r(   Úsplit_at_single_quotes)Úsr   r   r   r$   ê   s    

zGenericTranslator.xpath_literalc             C   s:   t |ƒj}t| d|jƒ  dƒ}|dkr2td| ƒ‚||ƒS )z%Translate any parsed selector object.zxpath_%sNz%s is not supported.)Útyper   r
   Úlowerr   )r   Zparsed_selectorZ	type_nameÚmethodr   r   r   r9   ø   s
    
zGenericTranslator.xpathc             C   s4   | j |j }t| d| ƒ}|| j|jƒ| j|jƒƒS )zTranslate a combined selector.zxpath_%s_combinator)Úcombinator_mappingÚ
combinatorr
   r9   r4   Úsubselector)r   ZcombinedrJ   rH   r   r   r   Úxpath_combinedselector  s    z(GenericTranslator.xpath_combinedselectorc             C   sD   | j |jƒ}| j |jƒ}|jƒ  |jr6|jd|j ƒS |jdƒS d S )Nznot(%s)Ú0)r9   r4   rK   r%   r   r"   )r   Znegationr9   Z	sub_xpathr   r   r   Úxpath_negation
  s    z GenericTranslator.xpath_negationc             C   sB   d|j jddƒ }t| |dƒ}|s0td|j  ƒ‚|| j|jƒ|ƒS )z$Translate a functional pseudo-class.zxpath_%s_functionú-Ú_Nz!The pseudo-class :%s() is unknown)r   r   r   r   r9   r4   )r   ÚfunctionrH   r   r   r   Úxpath_function  s    z GenericTranslator.xpath_functionc             C   s@   d|j jddƒ }t| |dƒ}|s0td|j  ƒ‚|| j|jƒƒS )zTranslate a pseudo-class.zxpath_%s_pseudorO   rP   NzThe pseudo-class :%s is unknown)Zidentr   r   r   r9   r4   )r   ZpseudorH   r   r   r   Úxpath_pseudo  s    zGenericTranslator.xpath_pseudoc             C   s¤   | j |j }t| d| ƒ}| jr,|jjƒ }n|j}t|ƒ}|jr\d|j|f }|oZt|jƒ}|rjd| }nd| j|ƒ }| j	rŠ|j
jƒ }n|j
}|| j|jƒ||ƒS )z Translate an attribute selector.zxpath_attrib_%sz%s:%sú@zattribute::*[name() = %s])Úattribute_operator_mappingÚoperatorr
   Úlower_case_attribute_namesÚattribrG   Úis_safe_nameÚ	namespacer$   Úlower_case_attribute_valuesÚvaluer9   r4   )r   r4   rV   rH   r   ÚsaferX   r\   r   r   r   Úxpath_attrib'  s     
zGenericTranslator.xpath_attribc             C   s   | j |jƒ}| j|d|jƒS )zTranslate a class selector.z@class)r9   r4   Úxpath_attrib_includesÚ
class_name)r   Zclass_selectorr9   r   r   r   Úxpath_class=  s    zGenericTranslator.xpath_classc             C   s   | j |jƒ}| j|d|jƒS )zTranslate an ID selector.z@id)r9   r4   Úxpath_attrib_equalsr/   )r   Zid_selectorr9   r   r   r   Ú
xpath_hashD  s    zGenericTranslator.xpath_hashc             C   sh   |j }|sd}d}nt|ƒ}| jr*|jƒ }|jrLd|j|f }|oJt|jƒ}| j|d�}|sd|jƒ  |S )z'Translate a type or universal selector.r   Tz%s:%s)r   )r   rY   Úlower_case_element_namesrG   rZ   r;   r%   )r   r4   r   r]   r9   r   r   r   Úxpath_elementI  s    zGenericTranslator.xpath_elementc             C   s   |j d|ƒS )z;right is a child, grand-child or further descendant of leftz/descendant-or-self::*/)r(   )r   ÚleftÚrightr   r   r   Úxpath_descendant_combinator`  s    z-GenericTranslator.xpath_descendant_combinatorc             C   s   |j d|ƒS )z#right is an immediate child of leftú/)r(   )r   rf   rg   r   r   r   Úxpath_child_combinatord  s    z(GenericTranslator.xpath_child_combinatorc             C   s   |j d|ƒ}|jƒ  |jdƒS )z)right is a sibling immediately after leftz/following-sibling::zposition() = 1)r(   r%   r"   )r   rf   rg   r9   r   r   r   Ú xpath_direct_adjacent_combinatorh  s    z2GenericTranslator.xpath_direct_adjacent_combinatorc             C   s   |j d|ƒS )z1right is a sibling after left, immediately or notz/following-sibling::)r(   )r   rf   rg   r   r   r   Ú"xpath_indirect_adjacent_combinatorn  s    z4GenericTranslator.xpath_indirect_adjacent_combinatorTc       	      C   s  yt |jƒ\}}W n" tk
r4   td|j ƒ‚Y nX |rB|jƒ  |jƒ  |dkrl|r^d| }|jd| ƒS |r|| }| }|dkr�t| ƒ}n
d|  }|dkr²d||f g}ng }|dkrÎ|jd| ƒ n|dk rè|rè|jd	| ƒ d
j	|ƒ}|�r|j|ƒ |S )NzInvalid series: '%r'r   zlast() - %szposition() = %sz+%sé   z(position() %s) mod %s = 0zposition() >= %szposition() < (last() %s)z and )
r   Ú	argumentsÚ
ValueErrorr   r%   r&   r"   ÚstrÚappendr(   )	r   r9   rQ   Úlastr%   ÚaÚbZb_negÚexprr   r   r   Úxpath_nth_child_functionu  s8    


z*GenericTranslator.xpath_nth_child_functionc             C   s   | j ||dd�S )NT)rr   )rv   )r   r9   rQ   r   r   r   Úxpath_nth_last_child_functionž  s    z/GenericTranslator.xpath_nth_last_child_functionc             C   s"   |j dkrtdƒ‚| j||dd�S )Nr   z"*:nth-of-type() is not implementedF)r%   )r   r   rv   )r   r9   rQ   r   r   r   Úxpath_nth_of_type_function¡  s
    
z,GenericTranslator.xpath_nth_of_type_functionc             C   s$   |j dkrtdƒ‚| j||ddd�S )Nr   z"*:nth-of-type() is not implementedTF)rr   r%   )r   r   rv   )r   r9   rQ   r   r   r   Úxpath_nth_last_of_type_function¨  s
    

z1GenericTranslator.xpath_nth_last_of_type_functionc             C   sB   |j ƒ dgdgfkr"td|j ƒ‚|jd j}|jd| j|ƒ ƒS )NÚSTRINGÚIDENTz9Expected a single string or ident for :contains(), got %rr   zcontains(., %s))Úargument_typesr   rn   r\   r"   r$   )r   r9   rQ   r\   r   r   r   Úxpath_contains_function¯  s    
z)GenericTranslator.xpath_contains_functionc             C   sB   |j ƒ dgdgfkr"td|j ƒ‚|jd j}|jd| j|ƒ ƒS )Nrz   r{   z5Expected a single string or ident for :lang(), got %rr   zlang(%s))r|   r   rn   r\   r"   r$   )r   r9   rQ   r\   r   r   r   Úxpath_lang_functionº  s    
z%GenericTranslator.xpath_lang_functionc             C   s
   |j dƒS )Nznot(parent::*))r"   )r   r9   r   r   r   Úxpath_root_pseudoÆ  s    z#GenericTranslator.xpath_root_pseudoc             C   s   |j ƒ  |jƒ  |jdƒS )Nzposition() = 1)r&   r%   r"   )r   r9   r   r   r   Úxpath_first_child_pseudoÉ  s    z*GenericTranslator.xpath_first_child_pseudoc             C   s   |j ƒ  |jƒ  |jdƒS )Nzposition() = last())r&   r%   r"   )r   r9   r   r   r   Úxpath_last_child_pseudoÎ  s    z)GenericTranslator.xpath_last_child_pseudoc             C   s$   |j dkrtdƒ‚|jƒ  |jdƒS )Nr   z"*:first-of-type is not implementedzposition() = 1)r   r   r&   r"   )r   r9   r   r   r   Úxpath_first_of_type_pseudoÓ  s
    
z,GenericTranslator.xpath_first_of_type_pseudoc             C   s$   |j dkrtdƒ‚|jƒ  |jdƒS )Nr   z!*:last-of-type is not implementedzposition() = last())r   r   r&   r"   )r   r9   r   r   r   Úxpath_last_of_type_pseudoÚ  s
    
z+GenericTranslator.xpath_last_of_type_pseudoc             C   s   |j ƒ  |jƒ  |jdƒS )Nz
last() = 1)r%   r&   r"   )r   r9   r   r   r   Úxpath_only_child_pseudoá  s    z)GenericTranslator.xpath_only_child_pseudoc             C   s   |j dkrtdƒ‚|jdƒS )Nr   z!*:only-of-type is not implementedz
last() = 1)r   r   r"   )r   r9   r   r   r   Úxpath_only_of_type_pseudoæ  s    
z+GenericTranslator.xpath_only_of_type_pseudoc             C   s
   |j dƒS )Nznot(*) and not(string-length()))r"   )r   r9   r   r   r   Úxpath_empty_pseudoì  s    z$GenericTranslator.xpath_empty_pseudoc             C   s
   |j dƒS )z:Common implementation for pseudo-classes that never match.rM   )r"   )r   r9   r   r   r   Úpseudo_never_matchesï  s    z&GenericTranslator.pseudo_never_matchesc             C   s   | s
t ‚|j|ƒ |S )N)r<   r"   )r   r9   r   r\   r   r   r   Úxpath_attrib_existsÿ  s    

z%GenericTranslator.xpath_attrib_existsc             C   s   |j d|| j|ƒf ƒ |S )Nz%s = %s)r"   r$   )r   r9   r   r\   r   r   r   rb     s    z%GenericTranslator.xpath_attrib_equalsc             C   s<   |r |j d||| j|ƒf ƒ n|j d|| j|ƒf ƒ |S )Nznot(%s) or %s != %sz%s != %s)r"   r$   )r   r9   r   r\   r   r   r   Úxpath_attrib_different  s    z(GenericTranslator.xpath_attrib_differentc             C   s:   t |ƒr,|jd||| jd| d ƒf ƒ n
|jdƒ |S )Nz:%s and contains(concat(' ', normalize-space(%s), ' '), %s)r)   rM   )Úis_non_whitespacer"   r$   )r   r9   r   r\   r   r   r   r_     s    
z'GenericTranslator.xpath_attrib_includesc          	   C   s,   |j d||| j|ƒ|| j|d ƒf ƒ |S )Nz'%s and (%s = %s or starts-with(%s, %s))rO   )r"   r$   )r   r9   r   r\   r   r   r   Úxpath_attrib_dashmatch  s
    
z(GenericTranslator.xpath_attrib_dashmatchc             C   s.   |r |j d||| j|ƒf ƒ n
|j dƒ |S )Nz%s and starts-with(%s, %s)rM   )r"   r$   )r   r9   r   r\   r   r   r   Úxpath_attrib_prefixmatch#  s
    
z*GenericTranslator.xpath_attrib_prefixmatchc             C   s:   |r,|j d|||t|ƒd | j|ƒf ƒ n
|j dƒ |S )Nz/%s and substring(%s, string-length(%s)-%s) = %srm   rM   )r"   Úlenr$   )r   r9   r   r\   r   r   r   Úxpath_attrib_suffixmatch+  s    "
z*GenericTranslator.xpath_attrib_suffixmatchc             C   s.   |r |j d||| j|ƒf ƒ n
|j dƒ |S )Nz%s and contains(%s, %s)rM   )r"   r$   )r   r9   r   r\   r   r   r   Úxpath_attrib_substringmatch5  s
    
z-GenericTranslator.xpath_attrib_substringmatchN)r0   )r0   F)FT)?r   r   r   r   rI   rU   Zid_attributeÚlang_attributerd   rW   r[   r   r;   r7   r2   r>   Ústaticmethodr$   r9   rL   rN   rR   rS   r^   ra   rc   re   rh   rj   rk   rl   rv   rw   rx   ry   r}   r~   r   r€   r�   r‚   rƒ   r„   r…   r†   r‡   Úxpath_link_pseudoZxpath_visited_pseudoZxpath_hover_pseudoZxpath_active_pseudoZxpath_focus_pseudoZxpath_target_pseudoÚxpath_enabled_pseudoÚxpath_disabled_pseudoÚxpath_checked_pseudorˆ   rb   r‰   r_   r‹   rŒ   rŽ   r�   r   r   r   r   r#   g   sŒ   
 
			 
(
	
r#   c               @   sF   e Zd ZdZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚHTMLTranslatoraê  
    Translator for (X)HTML documents.

    Has a more useful implementation of some pseudo-classes based on
    HTML-specific element names and attribute names, as described in
    the `HTML5 specification`_. It assumes no-quirks mode.
    The API is the same as :class:`GenericTranslator`.

    .. _HTML5 specification: http://www.w3.org/TR/html5/links.html#selectors

    :param xhtml:
        If false (the default), element names and attribute names
        are case-insensitive.

    ZlangFc             C   s   || _ |sd| _d| _d S )NT)Úxhtmlrd   rW   )r   r—   r   r   r   r   R  s    zHTMLTranslator.__init__c             C   s
   |j dƒS )NzŠ(@selected and name(.) = 'option') or (@checked and (name(.) = 'input' or name(.) = 'command')and (@type = 'checkbox' or @type = 'radio')))r"   )r   r9   r   r   r   r•   Y  s    z#HTMLTranslator.xpath_checked_pseudoc             C   sP   |j ƒ dgdgfkr"td|j ƒ‚|jd j}|jd| j| j|jƒ d ƒf ƒS )Nrz   r{   z5Expected a single string or ident for :lang(), got %rr   z‡ancestor-or-self::*[@lang][1][starts-with(concat(translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '-'), %s)]rO   )r|   r   rn   r\   r"   r�   r$   rG   )r   r9   rQ   r\   r   r   r   r~   a  s    
z"HTMLTranslator.xpath_lang_functionc             C   s
   |j dƒS )NzA@href and (name(.) = 'a' or name(.) = 'link' or name(.) = 'area'))r"   )r   r9   r   r   r   r’   o  s    z HTMLTranslator.xpath_link_pseudoc             C   s
   |j dƒS )Na¥  
        (
            @disabled and
            (
                (name(.) = 'input' and @type != 'hidden') or
                name(.) = 'button' or
                name(.) = 'select' or
                name(.) = 'textarea' or
                name(.) = 'command' or
                name(.) = 'fieldset' or
                name(.) = 'optgroup' or
                name(.) = 'option'
            )
        ) or (
            (
                (name(.) = 'input' and @type != 'hidden') or
                name(.) = 'button' or
                name(.) = 'select' or
                name(.) = 'textarea'
            )
            and ancestor::fieldset[@disabled]
        )
        )r"   )r   r9   r   r   r   r”   v  s    z$HTMLTranslator.xpath_disabled_pseudoc             C   s
   |j dƒS )Na'  
        (
            @href and (
                name(.) = 'a' or
                name(.) = 'link' or
                name(.) = 'area'
            )
        ) or (
            (
                name(.) = 'command' or
                name(.) = 'fieldset' or
                name(.) = 'optgroup'
            )
            and not(@disabled)
        ) or (
            (
                (name(.) = 'input' and @type != 'hidden') or
                name(.) = 'button' or
                name(.) = 'select' or
                name(.) = 'textarea' or
                name(.) = 'keygen'
            )
            and not (@disabled or ancestor::fieldset[@disabled])
        ) or (
            name(.) = 'option' and not(
                @disabled or ancestor::optgroup[@disabled]
            )
        )
        )r"   )r   r9   r   r   r   r“   ’  s    z#HTMLTranslator.xpath_enabled_pseudoN)F)r   r   r   r   r�   r   r•   r~   r’   r”   r“   r   r   r   r   r–   ?  s   
r–   )N)r   ÚsysÚreZcssselect.parserr   r   r   Úversion_infoZ
basestringZ_basestringZunicoder   rp   r   ÚRuntimeErrorr   Úobjectr   ÚcompileÚsplitrD   ÚmatchrY   rŠ   r#   r–   r   r   r   r   Ú<module>   s&   
1   [