B
     ¸h}>  ã               @   sÚ   d dl m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Zd dl	Z	d dl
Z
yd dlmZ W n  ek
r„   d dlmZ Y nX d dlmZ d dlmZ d dlmZ dZd	Zd
ZG dd„ deƒZG dd„ deƒZdS )é    )Úwith_statementN)ÚStringIO)ÚBytesIO)Úminidom)Ú	ProxyType)ÚWebDriverExceptionzwebdriver.xpizwebdriver_prefs.jsonzfxdriver@googlecode.comc               @   s   e Zd ZdZdS )ÚAddonFormatErrorz3Exception for not well-formed add-on manifest filesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úgC:\Users\sanjo\AppData\Local\Qlobot\Launcher\ext_packages\selenium\webdriver\firefox\firefox_profile.pyr   -   s   r   c               @   sô   e Zd ZdZdZd*dd„Zdd„ Zefdd„Zd	d
„ Z	e
dd„ ƒZe
dd„ ƒZejdd„ ƒZe
dd„ ƒZejdd„ ƒZe
dd„ ƒZejdd„ ƒZe
dd„ ƒZejdd„ ƒZe
dd„ ƒZdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd+d&d'„Zd(d)„ ZdS ),ÚFirefoxProfileZWEBDRIVER_ANONYMOUS_PROFILENc          	   C   s  t js8ttj tj t¡t¡ƒ}t	 
|¡t _W dQ R X t t jd ¡| _d| _|| _d| _| jdkrr|  ¡ | _n`t ¡ | _tj | jd¡}tj| j|t ddd¡d || _t | jd	¡ |  tj | jd
¡¡ tj | jd¡| _tj | jd
¡| _tj | j¡rt | jd¡ dS )a‰  
        Initialises a new instance of a Firefox Profile

        :args:
         - profile_directory: Directory of profile that you want to use. If a
           directory is passed in it will be cloned and the cloned directory
           will be used by the driver when instantiated.
           This defaults to None and will create a new
           directory when object is created.
        NZmutableTzwebdriver-py-profilecopyzparent.lockÚlockz.parentlock)Úignoreií  zuser.jsÚ
extensionsi¤  )r   ÚDEFAULT_PREFERENCESÚopenÚosÚpathÚjoinÚdirnameÚ__file__ÚWEBDRIVER_PREFERENCESÚjsonÚloadÚcopyÚdeepcopyÚdefault_preferencesÚnative_events_enabledÚprofile_dirÚ
tempfolderÚ_create_tempfolderÚtempfileÚmkdtempÚshutilÚcopytreeÚignore_patternsÚchmodÚ_read_existing_userjsÚextensionsDirÚ	userPrefsÚisfile)ÚselfZprofile_directoryZdefault_prefsZnewprofr   r   r   Ú__init__5   s,    



zFirefoxProfile.__init__c             C   s   || j |< dS )zB
        sets the preference that we want in the profile.
        N)r   )r.   ÚkeyÚvaluer   r   r   Úset_preferenceZ   s    zFirefoxProfile.set_preferencec             C   s   |   |¡ d S )N)Ú_install_extension)r.   Ú	extensionr   r   r   Úadd_extension`   s    zFirefoxProfile.add_extensionc             C   s6   x$t jd  ¡ D ]\}}|| j|< qW |  | j¡ d S )NÚfrozen)r   r   Úitemsr   Ú_write_user_prefs)r.   r0   r1   r   r   r   Úupdate_preferencesc   s    z!FirefoxProfile.update_preferencesc             C   s   | j S )zI
        Gets the profile directory that is currently being used
        )r!   )r.   r   r   r   r   j   s    zFirefoxProfile.pathc             C   s   | j S )z<
        Gets the port that WebDriver is working on
        )Ú_port)r.   r   r   r   Úportq   s    zFirefoxProfile.portc          	   C   sp   t |tƒstdƒ‚y$t|ƒ}|dk s,|dkr4tdƒ‚W n  ttfk
rV   tdƒ‚Y nX || _|  d| j¡ dS )zA
        Sets the port that WebDriver will be running on
        zPort needs to be an integeré   iÿÿ  z)Port number must be in the range 1..65535Zwebdriver_firefox_portN)Ú
isinstanceÚintr   Ú
ValueErrorÚ	TypeErrorr:   r2   )r.   r;   r   r   r   r;   x   s    
c             C   s
   | j d S )NÚ webdriver_accept_untrusted_certs)r   )r.   r   r   r   Úaccept_untrusted_certsˆ   s    z%FirefoxProfile.accept_untrusted_certsc             C   s    |dkrt dƒ‚|  d|¡ d S )N)TFz%Please pass in a Boolean to this callrA   )r   r2   )r.   r1   r   r   r   rB   Œ   s    c             C   s
   | j d S )NÚ!webdriver_assume_untrusted_issuer)r   )r.   r   r   r   Úassume_untrusted_cert_issuer’   s    z+FirefoxProfile.assume_untrusted_cert_issuerc             C   s    |dkrt dƒ‚|  d|¡ d S )N)TFz%Please pass in a Boolean to this callrC   )r   r2   )r.   r1   r   r   r   rD   –   s    c             C   s
   | j d S )NÚwebdriver_enable_native_events)r   )r.   r   r   r   r       s    z$FirefoxProfile.native_events_enabledc             C   s    |dkrt dƒ‚|  d|¡ d S )N)TFz%Please pass in a Boolean to this callrE   )r   r2   )r.   r1   r   r   r   r    ¡   s    c       	      C   s”   |   ¡  tƒ }t |dtj¡}t| jƒd }xJt | j¡D ]:\}}}x.|D ]&}tj 	||¡}| 
|||d… ¡ qJW q:W | ¡  t | ¡ ¡ d¡S )z
        A zipped, base64 encoded string of profile directory
        for use with remote WebDriver JSON wire protocol
        Úwr<   NzUTF-8)r9   r   ÚzipfileÚZipFileÚZIP_DEFLATEDÚlenr   r   Úwalkr   ÚwriteÚcloseÚbase64Ú	b64encodeÚgetvalueÚdecode)	r.   ÚfpÚzippedZ	path_rootÚbaseÚdirsÚfilesZfyleÚfilenamer   r   r   Úencoded§   s    
zFirefoxProfile.encodedc             C   s¼   dd l }|jdtdd |d kr(tdƒ‚|jtjkr8d S |  d|jd ¡ |jtjkrž|  d|j	¡ |  
d	|j¡ |  
d
|j¡ |  
d|j¡ |  
d|j¡ n|jtjkr¸|  d|j¡ d S )Nr   zUThis method has been deprecated. Please pass in the proxy object to the Driver Objecté   )Ú
stacklevelzproxy can not be Noneznetwork.proxy.typeZff_valueznetwork.proxy.no_proxies_onÚftpÚhttpÚsslÚsocksznetwork.proxy.autoconfig_url)ÚwarningsÚwarnÚDeprecationWarningr?   Ú
proxy_typer   ZUNSPECIFIEDr2   ZMANUALÚno_proxyÚ_set_manual_proxy_preferenceZ	ftp_proxyZ
http_proxyZ	ssl_proxyZsocks_proxyZPACZproxy_autoconfig_url)r.   Úproxyr_   r   r   r   Ú	set_proxy¸   s"    
zFirefoxProfile.set_proxyc             C   sZ   |d ks|dkrd S |  d¡}|  d| |d ¡ t|ƒdkrV|  d| t|d ƒ¡ d S )NÚ ú:znetwork.proxy.%sr   r<   znetwork.proxy.%s_port)Úsplitr2   rJ   r>   )r.   r0   ZsettingZhost_detailsr   r   r   rd   Ï   s    
z+FirefoxProfile._set_manual_proxy_preferencec             C   s   t  ¡ S )zJ
        Creates a temp folder to store User.js and the extension
        )r$   r%   )r.   r   r   r   r#   Ø   s    z!FirefoxProfile._create_tempfolderc          	   C   sJ   t | jdƒ4}x,| ¡ D ] \}}| d|t |¡f ¡ qW W dQ R X dS )zB
        writes the current user prefs dictionary to disk
        rF   zuser_pref("%s", %s);
N)r   r,   r7   rL   r   Údumps)r.   Z
user_prefsÚfr0   r1   r   r   r   r8   Þ   s    z FirefoxProfile._write_user_prefsc             C   s²   dd l }t d¡}y†t|ƒt}xl|D ]d}t ||¡}y t | d¡¡| j| d¡< W q$ t	k
r†   | 
d| d¡ | d¡ ¡ Y q$X q$W W d Q R X W n t	k
r¬   Y nX d S )Nr   zuser_pref\("(.*)",\s(.*)\)rY   r<   z5(skipping) failed to json.loads existing preference: )r_   ÚreÚcompiler   Úsearchr   ÚloadsÚgroupr   Ú	Exceptionr`   )r.   Zuserjsr_   ZPREF_RErk   ZusrZmatchesr   r   r   r*   æ   s    


 2z$FirefoxProfile._read_existing_userjsTc          
   C   sÒ  |t krtj tj t¡t ¡}d}d}| d¡rtjdtj 	|¡d  d}t
 |d¡}x´| ¡ D ]¨}| d¡ržtj tj ||¡¡sœt tj ||¡¡ qdtj tj tj ||¡¡¡sØt tj tj ||¡¡¡ | |¡}ttj ||¡dƒ}| |¡ W dQ R X qdW |}|}|  |¡}	|	 d	¡}
|
s>td
| ƒ‚tj | j|
¡}|s |	d s |r tj | j¡sŽt | j¡ t | jd¡ t ||d ¡ ntj |¡s¾tj||dd |rÎt |¡ dS )a	  
            Installs addon from a filepath, url
            or directory of addons in the profile.
            - path: url, absolute path to .xpi, or directory of addons
            - unpack: whether to unpack unless specified otherwise in the install.rdf
        Nz.xpiÚ.éÿÿÿÿ)ÚsuffixÚrÚ/ÚwbÚidz#The addon id could not be found: %sÚunpackií  T)Úsymlinks)ÚWEBDRIVER_EXTr   r   r   r   r   Úendswithr$   r%   ri   rG   rH   ÚnamelistÚisdirÚmakedirsÚreadr   rL   Ú_addon_detailsÚgetÚAssertionErrorr+   Úexistsr)   r&   r   r'   Úrmtree)r.   Zaddonry   ZtmpdirZxpifileÚcompressed_fileÚnameÚdatark   Zaddon_detailsZaddon_idÚ
addon_pathr   r   r   r3   ÷   s>    



z!FirefoxProfile._install_extensionc          
   C   s¨  dddddœ}dd„ }dd„ }dd	„ }t j |¡s>td
| ƒ‚yÊt |¡rŒz4t |d¡}d| ¡ krr|| d¡ƒS | d¡}W d| 	¡  X nzt j 
|¡rút j |d¡}t j |¡rÐt|dƒ}	||	 ¡ ƒS Q R X tt j |d¡dƒ}	|	 ¡ }W dQ R X ntd| ƒ‚W n> ttfk
rF }
 ztt|
ƒt ¡ d ƒ‚W dd}
~
X Y nX yät |¡}||dƒ}||dƒ}| |d ¡ d¡}|dkr–| d¡ d¡}x>|jD ]4}|j |d¡}|| ¡ krž| |||ƒi¡ qžW | d¡dkr*xBt|jjƒD ]2}|j |¡}|j|d krô| d|j i¡ qôW W n: t!k
rf }
 ztt|
ƒt ¡ d ƒ‚W dd}
~
X Y nX t"|d tƒrŒ|d  #¡ dk|d< | d¡dkr¤tdƒ‚|S )a­  
        Returns a dictionary of details about the addon.

        :param addon_path: path to the add-on directory or XPI

        Returns::

            {'id':      u'rainbow@colors.org', # id of the addon
             'version': u'1.4',                # version of the addon
             'name':    u'Rainbow',            # name of the addon
             'unpack':  False }                # whether to unpack the addon
        NF)rx   ry   r‡   Úversionc             S   s`   | j j}d}xNt|jƒD ]@}| |¡j|krd| |¡jkr| |¡j d¡d d }P qW |S )Nrg   rh   r<   )ZdocumentElementÚ
attributesÚrangeÚlengthÚitemr1   r‡   ri   )ÚdocÚurlr‹   Ú	namespaceÚir   r   r   Úget_namespace_id<  s    z7FirefoxProfile._addon_details.<locals>.get_namespace_idc             S   s:   g }x&| j D ]}|j|jkr| |j¡ qW d |¡ ¡ S )z'Retrieve the text value of a given noderg   )Ú
childNodesZnodeTypeZ	TEXT_NODEÚappendrˆ   r   Ústrip)ÚelementÚrcÚnoder   r   r   Úget_textG  s
    z/FirefoxProfile._addon_details.<locals>.get_textc             S   sf   t  | ¡}y|d d d }W n0 tk
rN   |d  dd¡d |d  }Y nX ||d |d d	d
œS )zOExtracts the details from the contents of a WebExtensions `manifest.json` file.ZapplicationsZgeckorx   r‡   ú rg   ú@rŠ   F)rx   rŠ   r‡   ry   )r   ro   ÚKeyErrorÚreplace)ÚcontentÚmanifestrx   r   r   r   Úparse_manifest_jsonO  s    
"z:FirefoxProfile._addon_details.<locals>.parse_manifest_jsonzAdd-on path does not exist: %sru   zmanifest.jsonzinstall.rdfz1Add-on path is neither an XPI nor a directory: %srY   z#http://www.mozilla.org/2004/em-rdf#z+http://www.w3.org/1999/02/22-rdf-syntax-ns#ZDescriptionr   rg   rx   ry   ÚtruezAdd-on id could not be found.)$r   r   r„   ÚIOErrorrG   Ú
is_zipfilerH   r}   r€   rM   r~   r   r   r   r   ÚstrÚsysÚexc_infor   ZparseStringZgetElementsByTagNamerŽ   r”   ZnodeNamerž   ÚkeysÚupdater‚   rŒ   r‹   r   r‡   r1   rq   r=   Úlower)r.   r‰   Údetailsr“   rš   r¡   r†   r    Zmanifest_json_filenamerk   Úer   ZemZrdfÚdescriptionr™   Úentryr’   Z	attributer   r   r   r   '  sd    
(



(zFirefoxProfile._addon_details)N)T)r	   r
   r   ZANONYMOUS_PROFILE_NAMEr   r/   r2   r{   r5   r9   Úpropertyr   r;   ÚsetterrB   rD   r    rX   rf   rd   r#   r8   r*   r3   r   r   r   r   r   r   1   s.   
%	
0r   )Ú
__future__r   rN   r   r   r   rl   r&   r¦   r$   rG   Ú	cStringIOr   r   ÚImportErrorÚioZxml.domr   Zselenium.webdriver.common.proxyr   Zselenium.common.exceptionsr   r{   r   ZEXTENSION_NAMErq   r   Úobjectr   r   r   r   r   Ú<module>   s*   