a
    ¾Ê.c’8  ã                   @   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ZdZdZG dd	„ d	eƒZG d
d„ dƒZdS )é    N)ÚBytesIO)Úminidom)Ú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
   úd/var/www/brookimports/venv/lib/python3.9/site-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dd„ Zdd„ Zdd„ Zd$dd „Zd!d"„ ZdS )%ÚFirefoxProfileZWEBDRIVER_ANONYMOUS_PROFILENc                 C   s2  t jdtdd tjs\ttj tj 	t
¡t¡ƒ}t |¡t_W d  ƒ n1 sR0    Y  t tjd ¡| _|| _d| _| jsŒ|  ¡ | _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.
        zAfirefox_profile has been deprecated, please use an Options objecté   )Ú
stacklevelNZmutablezwebdriver-py-profilecopyzparent.lockÚlockz.parentlock)Úignoreéí  zuser.jsÚ
extensionsi¤  )ÚwarningsÚwarnÚDeprecationWarningr   ÚDEFAULT_PREFERENCESÚopenÚosÚpathÚjoinÚdirnameÚ__file__ÚWEBDRIVER_PREFERENCESÚjsonÚloadÚcopyÚdeepcopyÚdefault_preferencesÚprofile_dirZ
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__/   s8    ÿÿ*ÿ

ÿ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_preferenceU   s    zFirefoxProfile.set_preferencec                 C   s   |   |¡ d S )N)Ú_install_extension)r/   Ú	extensionr
   r
   r   Úadd_extension[   s    zFirefoxProfile.add_extensionc                 C   s<   t jd  ¡ D ]\}}|| jvr|| j|< q|  | j¡ d S )NÚfrozen)r   r   Úitemsr"   Ú_write_user_prefsr2   r
   r
   r   Úupdate_preferences^   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   i   s    zFirefoxProfile.pathc                 C   s   | j S )z<
        Gets the port that WebDriver is working on
        )Ú_portr=   r
   r
   r   Úportp   s    zFirefoxProfile.portc              	   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 )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>   r5   )r/   r?   r
   r
   r   r?   w   s    
c                 C   s
   | j d S )NÚ webdriver_accept_untrusted_certsr1   r=   r
   r
   r   Úaccept_untrusted_certs‡   s    z%FirefoxProfile.accept_untrusted_certsc                 C   s    |dvrt dƒ‚|  d|¡ d S )N©TFú%Please pass in a Boolean to this callrE   ©r   r5   ©r/   r4   r
   r
   r   rF   ‹   s    c                 C   s
   | j d S )NÚ!webdriver_assume_untrusted_issuerr1   r=   r
   r
   r   Úassume_untrusted_cert_issuer‘   s    z+FirefoxProfile.assume_untrusted_cert_issuerc                 C   s    |dvrt dƒ‚|  d|¡ d S )NrG   rH   rK   rI   rJ   r
   r
   r   rL   •   s    c           	      C   sŒ   |   ¡  tƒ }t |dtj¡}t| jƒd }t | j¡D ]6\}}}|D ]&}tj 	||¡}| 
|||d… ¡ qFq8| ¡  t | ¡ ¡ d¡S )z
        A zipped, base64 encoded string of profile directory
        for use with remote WebDriver JSON wire protocol
        Úwr@   NzUTF-8)r<   r   ÚzipfileÚZipFileÚZIP_DEFLATEDÚlenr   r   Úwalkr   ÚwriteÚcloseÚbase64Ú	b64encodeÚgetvalueÚdecode)	r/   ÚfpZzippedZ	path_rootÚbaseÚdirsÚfilesZfyleÚfilenamer
   r
   r   Úencodedœ   s    zFirefoxProfile.encodedc                 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   s`   t | jdƒ@}| ¡ D ]&\}}| d|› dt |¡› d¡ qW d  ƒ n1 sR0    Y  dS )zB
        writes the current user prefs dictionary to disk
        rM   zuser_pref("z", z);
N)r   r-   r:   rS   r   Údumps)r/   Z
user_prefsÚfr3   r4   r
   r
   r   r;   ³   s    z FirefoxProfile._write_user_prefsc                 C   s¾   dd l }t d¡}z”t|ƒx}|D ]b}t ||¡}z t | d¡¡| j| d¡< W q" t	y‚   | 
d| d¡ | d¡ ¡ Y q"0 q"W d   ƒ n1 sš0    Y  W n t	y¸   Y n0 d S )Nr   zuser_pref\("(.*)",\s(.*)\)r   r@   z7(skipping) failed to json.loads existing preference: %s)r   ÚreÚcompiler   Úsearchr   ÚloadsÚgroupr"   Ú	Exceptionr   )r/   Zuserjsr   ZPREF_REr`   ÚusrÚmatchesr
   r
   r   r+   »   s     

 ÿÿ0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¡}| ¡ D ]¾}| d¡rœtj tj ||¡¡sšt tj ||¡¡ qbtj tj tj ||¡¡¡sÖt tj tj ||¡¡¡ | |¡}ttj ||¡dƒ}| |¡ W d  ƒ qb1 s0    Y  qb|}|}|  |¡}	|	 d	¡}
|
sPJ 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Úunpackr   T)Úsymlinks)ÚWEBDRIVER_EXTr   r   r   r   r   Úendswithr%   r&   ÚsplitrN   rO   ÚnamelistÚisdirÚmakedirsÚreadr   rS   Ú_addon_detailsÚgetr,   Úexistsr*   r'   r    r(   Úrmtree)r/   Zaddonrp   ZtmpdirZxpifileÚcompressed_fileÚnameÚdatar`   Zaddon_detailsZaddon_idÚ
addon_pathr
   r
   r   r6   Ì   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¢zHt |d¡}d| ¡ v r€|| d¡ƒW | 	¡  W S | d¡}W | 	¡  n
| 	¡  0 n¬t j 
|¡rBt j |d¡}t j |¡rt|ƒ}	||	 ¡ ƒW  d  ƒ W S 1  sú0    Y  tt j |d¡ƒ}	|	 ¡ }W d  ƒ n1 s60    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 ]4}|j |d¡}|| ¡ v râ| |||ƒi¡ qâ| d¡sdt|jjƒD ]2}|j |¡}|j|d kr0| d|j i¡ q0W 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)ro   rp   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Úitemr4   r~   rt   )Ú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 noder‚   )Ú
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 )zOExtracts the details from the contents of a WebExtensions `manifest.json` file.ZapplicationsZgeckoro   r~   ú r‚   ú@r   F)ro   r   r~   rp   )r   rd   ÚKeyErrorÚreplace)ÚcontentÚmanifestro   r
   r
   r   Úparse_manifest_json$  s    
"üz:FirefoxProfile._addon_details.<locals>.parse_manifest_jsonzAdd-on path does not exist: %srl   zmanifest.jsonzinstall.rdfz1Add-on path is neither an XPI nor a directory: %sr   z#http://www.mozilla.org/2004/em-rdf#z+http://www.w3.org/1999/02/22-rdf-syntax-ns#ÚDescriptionr   r‚   ro   rp   ÚtruezAdd-on id could not be found.)$r   r   r{   ÚOSErrorrN   Ú
is_zipfilerO   ru   rx   rT   rv   r   r   r–   r   ÚstrÚsysÚexc_infor   ÚparseStringZgetElementsByTagNamer‡   r   ZnodeNamer—   ÚkeysÚupdaterz   r…   r„   r†   r~   r4   rf   rA   Úlower)r/   r€   ÚdetailsrŒ   r“   rš   r}   r™   Zmanifest_json_filenamer`   Úerˆ   ZemÚrdfÚdescriptionr’   Úentryr‹   Ú	attributer
   r
   r   ry   ü   sj    ü
ü
.*,



,zFirefoxProfile._addon_details)N)T)r   r   r   ZANONYMOUS_PROFILE_NAMEr   r0   r5   rr   r8   r<   Úpropertyr   r?   ÚsetterrF   rL   r^   r$   r;   r+   r6   ry   r
   r
   r
   r   r   +   s6   
&








0r   )rU   r    r   r   ra   r'   r    r%   r   rN   Úior   Zxml.domr   Zselenium.common.exceptionsr   rr   r   ZEXTENSION_NAMErf   r   r   r
   r
   r
   r   Ú<module>   s"   