a
    5ñ»j°5  ã                   @   s®   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d dl	Z	d dl
mZ d dlmZ d dlmZ d dlmZ dZedƒG dd	„ d	eƒƒZG d
d„ dƒZdS )é    N)ÚBytesIO)Úminidom)Ú
deprecated)ÚWebDriverExceptionzwebdriver_prefs.jsonú/Addons must be added after starting the sessionc                   @   s   e Zd ZdZdS )ÚAddonFormatErrorz4Exception for not well-formed add-on manifest files.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úPE:\soccer\ml_env\Lib\site-packages\selenium/webdriver/firefox/firefox_profile.pyr   &   s   r   c                   @   s  e Zd ZdZd"dd„Zdd„ Zedƒd#dd„ƒZd	d
„ Ze	dd„ ƒZ
e	edƒdd„ ƒƒZejedƒddœdd„ƒƒZe	edƒdd„ ƒƒZejedƒddœdd„ƒƒZe	edƒdd„ ƒƒZejedƒddœdd„ƒƒZe	edœdd„ƒZdd„ Zedƒd$dd„ƒZedƒd d!„ ƒZdS )%ÚFirefoxProfileNc                 C   sÞ   i | _ |rLtj t ¡ d¡}tj||t ddd¡d� || _	t 
| j	d¡ nŽt ¡ | _	tjs¦ttj tj t¡t¡dd��}t |¡t_W d	  ƒ n1 sœ0    Y  t tjd
 ¡| _ tjd  ¡ D ]\}}|| j |< qÆ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.
        zwebdriver-py-profilecopyzparent.lockÚlockz.parentlock)Úignoreéí  úutf-8©ÚencodingNZmutableÚfrozen)Ú_desired_preferencesÚosÚpathÚjoinÚtempfileÚmkdtempÚshutilÚcopytreeÚignore_patternsÚ_profile_dirÚchmodr   ÚDEFAULT_PREFERENCESÚopenÚdirnameÚ__file__ÚWEBDRIVER_PREFERENCESÚjsonÚloadÚcopyÚdeepcopyÚitems)ÚselfZprofile_directoryZnewprofZdefault_prefsÚkeyÚvaluer   r   r   Ú__init__.   s$    
ÿ
ÿ*zFirefoxProfile.__init__c                 C   s   || j |< dS )z0Sets the preference that we want in the profile.N©r   )r+   r,   r-   r   r   r   Úset_preferenceM   s    zFirefoxProfile.set_preferencer   c                 C   s   |   |¡ d S )N)Ú_install_extension)r+   Ú	extensionr   r   r   Úadd_extensionQ   s    zFirefoxProfile.add_extensionc              
   C   s–   t j | jd¡}t j |¡r2t  |d¡ |  |¡ t|ddd��B}| j 	¡ D ]&\}}| 
d|› dt |¡› d�¡ qLW d	  ƒ n1 sˆ0    Y  d	S )
z&Writes the desired user prefs to disk.zuser.jsi¤  Úwr   r   zuser_pref("z", z);
N)r   r   r   r   Úisfiler    Ú_read_existing_userjsr"   r   r*   Úwriter&   Údumps)r+   Z
user_prefsÚfr,   r-   r   r   r   Úupdate_preferencesU   s    
z!FirefoxProfile.update_preferencesc                 C   s   | j S )z8Gets the profile directory that is currently being used.)r   ©r+   r   r   r   r   a   s    zFirefoxProfile.pathz'The port is stored in the Service classc                 C   s   | j S )z+Gets the port that WebDriver is working on.)Ú_portr;   r   r   r   Úportf   s    zFirefoxProfile.port)Úreturnc              	   C   sn   t |tƒstdƒ‚z$t|ƒ}|dk s,|dkr4tdƒ‚W n ttfyT   tdƒ‚Y n0 || _|  d| j¡ dS )z0Sets 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<   r0   )r+   r=   r   r   r   r=   l   s    
z8Allowing untrusted certs is toggled in the Options classc                 C   s
   | j d S )NÚ webdriver_accept_untrusted_certsr/   r;   r   r   r   Úaccept_untrusted_certs{   s    z%FirefoxProfile.accept_untrusted_certsc                 C   s"   t |tƒstdƒ‚|  d|¡ d S )Nú%Please pass in a Boolean to this callrD   ©r@   Úboolr   r0   ©r+   r-   r   r   r   rE   €   s    
c                 C   s
   | j d S )NÚ!webdriver_assume_untrusted_issuerr/   r;   r   r   r   Úassume_untrusted_cert_issuer‡   s    z+FirefoxProfile.assume_untrusted_cert_issuerc                 C   s"   t |tƒstdƒ‚|  d|¡ d S )NrF   rJ   rG   rI   r   r   r   rK   Œ   s    
c           	   	   C   s®   | j r|  ¡  tƒ }tj|dtjdd��b}t| jƒd }t 	| j¡D ]6\}}}|D ]&}tj 
||¡}| |||d… ¡ qRqDW d  ƒ n1 s�0    Y  t | ¡ ¡ d¡S )z]Updates preferences and creates a zipped, base64 encoded string of
        profile directory.r4   F)Ústrict_timestampsr?   NzUTF-8)r   r:   r   ÚzipfileÚZipFileÚZIP_DEFLATEDÚlenr   r   Úwalkr   r7   Úbase64Ú	b64encodeÚgetvalueÚdecode)	r+   ÚfpZzippedZ	path_rootÚbaseÚ_ÚfilesZfyleÚfilenamer   r   r   Úencoded”   s    6zFirefoxProfile.encodedc                 C   s¢   t  d¡}t|dd��x}|D ]b}| |¡}z t | d¡¡| j| d¡< W q ty|   t	 
d| d¡| d¡ › �¡ Y q0 qW d  ƒ n1 s”0    Y  dS )zRReads existing preferences and adds them to desired preference
        dictionary.zuser_pref\("(.*)",\s(.*)\)r   r   é   r?   z5(skipping) failed to json.loads existing preference: N)ÚreÚcompiler"   Úsearchr&   ÚloadsÚgroupr   Ú	ExceptionÚwarningsÚwarn)r+   ZuserjsZpref_patternr9   ÚusrÚmatchesr   r   r   r6   £   s    

 ÿz$FirefoxProfile._read_existing_userjsTc              	   C   sÎ  d}d}|  d¡�r
tjdtj |¡d  d�}t |d¡}| ¡ 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  ƒ qD1 sö0    Y  qD|}|}|  |¡}	|	 d	¡}
|
�s2J d
|› �ƒ‚tj 
| jd¡}tj 
||
¡}|�sœ|	d �sœ|�rœtj |¡�sŠt |¡ t |d¡ t ||d ¡ ntj |¡�sºtj||dd� |�rÊt |¡ dS )zñ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: Ú
extensionsÚunpackr   T)Úsymlinks)Úendswithr   r   r   r   ÚsplitrM   rN   ÚnamelistÚisdirr   Úmakedirsr#   Úreadr"   r7   Ú_addon_detailsÚgetr   Úexistsr    r   r(   r   Úrmtree)r+   Zaddonro   ZtmpdirZxpifileÚcompressed_fileÚnameÚdatar9   Zaddon_detailsZaddon_idZextensions_dirÚ
addon_pathr   r   r   r1   ±   s<    

*


z!FirefoxProfile._install_extensionc              
   C   sô  dddddœ}dd„ }dd„ }dd	„ }t j |¡s@td
|› �ƒ‚�z&t |¡r°t |d¡�D}d| ¡ v r†|| d¡ƒW  d  ƒ W S | d¡}W d  ƒ n1 s¤0    Y  n¶t j 	|¡�rXt j 
|d¡}t j |¡�rt|dd��}	||	 ¡ ƒW  d  ƒ W S 1 �s0    Y  tt j 
|d¡dd��}	|	 ¡ }W d  ƒ n1 �sL0    Y  ntd|› �ƒ‚W n@ ttf�y¨ }
 z"tt|
ƒt ¡ d ƒ‚W Y d}
~
n
d}
~
0 0 zÐt |¡}||dƒ}||dƒ}| |d ¡ d¡}|�sô| d¡ d¡}|jD ]0}|j |d¡}||v �rú| |||ƒi¡ �qú| d¡�sxt|jjƒD ]2}|j |¡}|j|d k�rD| d|ji¡ �qDW n< t�y¶ }
 z"tt|
ƒt ¡ d ƒ‚W Y d}
~
n
d}
~
0 0 t |d tƒ�rÜ|d  !¡ dk|d< | d¡�sð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)rm   ro   r|   Úversionc                 S   s^   | j j}d}t|jƒD ]B}| |¡j|krd| |¡jv r| |¡j d¡d d } qZq|S )NÚ ú:r?   )ZdocumentElementÚ
attributesÚrangeÚlengthÚitemr-   r|   rr   )ÚdocÚurlr‚   Ú	namespaceÚir   r   r   Úget_namespace_idð   s    z7FirefoxProfile._addon_details.<locals>.get_namespace_idc                 S   s6   g }| j D ]}|j|jkr
| |j¡ q
d |¡ ¡ S )z(Retrieve the text value of a given node.r€   )Ú
childNodesZnodeTypeZ	TEXT_NODEÚappendr}   r   Ústrip)ÚelementÚrcÚnoder   r   r   Úget_textû   s
    
z/FirefoxProfile._addon_details.<locals>.get_textc                 S   sd   t  | ¡}z|d d d }W n. tyL   |d  dd¡d |d  }Y n0 ||d |d d	d
œS )z[Extracts the details from the contents of a WebExtensions
            `manifest.json` file.ZapplicationsZgeckorm   r|   ú r€   ú@r   F)rm   r   r|   ro   )r&   r`   ÚKeyErrorÚreplace)ÚcontentÚmanifestrm   r   r   r   Úparse_manifest_json  s    
"üz:FirefoxProfile._addon_details.<locals>.parse_manifest_jsonzAdd-on path does not exist: rj   zmanifest.jsonzinstall.rdfr   r   z/Add-on path is neither an XPI nor a directory: r\   z#http://www.mozilla.org/2004/em-rdf#z+http://www.w3.org/1999/02/22-rdf-syntax-ns#ÚDescriptionr   r€   rm   ro   ÚtruezAdd-on id could not be found.)"r   r   ry   ÚOSErrorrM   Ú
is_zipfilerN   rs   rv   rt   r   r"   r”   r   ÚstrÚsysÚexc_infor   ZparseStringZgetElementsByTagNamer…   r‹   ZnodeNamer•   Úupdaterx   rƒ   r‚   r„   r|   r-   rb   r@   Úlower)r+   r~   ÚdetailsrŠ   r‘   r˜   r{   r—   Zmanifest_json_filenamer9   Úer†   ZemZrdfÚdescriptionr�   Úentryr‰   Ú	attributer   r   r   rw   à   sZ    
*.*,




,zFirefoxProfile._addon_details)N)N)T)r   r	   r
   r!   r.   r0   r   r3   r:   Úpropertyr   r=   ÚsetterrE   rK   r�   r[   r6   r1   rw   r   r   r   r   r   +   sB   

.r   )rR   r(   r&   r   r]   r   rž   r   rc   rM   Úior   Zxml.domr   Ztyping_extensionsr   Zselenium.common.exceptionsr   r%   rb   r   r   r   r   r   r   Ú<module>   s"   