B
     h                 @   sr   d Z ddlZddlmZ ye W n ek
r8   eZY nX dd ZdddZdd	 Z	dddZ
dd Zdd ZdS )z
The Utils methods.
    N)Keysc              C   s<   t  t jt j} | d | d |  d }|   |S )z/
    Determines a free port using sockets.
    )z0.0.0.0r         )socketAF_INETSOCK_STREAMbindlistengetsocknameclose)Zfree_socketport r   \C:\Users\sanjo\AppData\Local\Qlobot\Launcher\ext_packages\selenium\webdriver\common\utils.py	free_port   s    

r   c             C   s   yt | d}W n t jk
r&   dS X d}x\|D ]T\}}}}}d}|rVt||d }|rl|t jkrl|d S |r2|s2|t jkr2|d }q2W |S )a  Resolve a hostname to an IP, preferring IPv4 addresses.

    We prefer IPv4 so that we don't change behavior from previous IPv4-only
    implementations, and because some drivers (e.g., FirefoxDriver) do not
    support IPv6 connections.

    If the optional port number is provided, only IPs that listen on the given
    port are considered.

    :Args:
        - host - A hostname.
        - port - Optional port number.

    :Returns:
        A single IP address, as a string. If any IPv4 address is found, one is
        returned. Otherwise, if any IPv6 address is found, one is returned. If
        neither, then None is returned.

    NTr   )r   getaddrinfogaierroris_connectabler   AF_INET6)hostr   Z	addrinfosipfamily_ZsockaddrZconnectabler   r   r   find_connectable_ip+   s    r   c             C   s*   d| kr|  dsd| |f S d| |f S )zJoins a hostname and port together.

    This is a minimal implementation intended to cope with IPv6 literals. For
    example, _join_host_port('::1', 80) == '[::1]:80'.

    :Args:
        - host - A hostname.
        - port - An integer port.

    :[z[%s]:%dz%s:%d)
startswith)r   r   r   r   r   join_host_portQ   s    r   	localhostc             C   sP   d}z8yt || fd}d}W n t jk
r8   d}Y nX W d|rJ|  X |S )zx
    Tries to connect to the server at port to see if it is running.

    :Args:
     - port - The port to connect.
    Nr   TF)r   create_connectionerrorr   )r   r   Zsocket_resultr   r   r   r   a   s    
r   c             C   sn   yddl m} W n tk
r,   ddl}Y nX y&|d|  }| dkrNdS dS W n tk
rh   dS X dS )z
    Tries to connect to the HTTP server at /status path
    and specified port to see if it responds successfully.

    :Args:
     - port - The port to connect.
    r   )requestNzhttp://127.0.0.1:%s/status   TF)urllibr!   ImportErrorZurllib2urlopengetcode	Exception)r   Zurl_requestresr   r   r   is_url_connectablet   s    r)   c             C   s   g }x~| D ]v}t |tr$|| q
t |tr\t|}xHtt|D ]}|||  qDW q
x"tt|D ]}|||  qjW q
W |S )z7Processes the values that will be typed in the element.)
isinstancer   appendintstrrangelen)valuetypingvalir   r   r   keys_to_typing   s    


r4   )N)r   )__doc__r   Zselenium.webdriver.common.keysr   
basestring	NameErrorr-   r   r   r   r   r)   r4   r   r   r   r   <module>   s   

&
