a
    Šxd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 )ÚAddonFormatErrorz4Exception for not well-formed add-on manifest files.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r
   r
   úb/var/www/html/Ranjet/env/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œdd„ƒZe
dd„ ƒZejddœdd„ƒZe
dd„ ƒZejddœdd„ƒZe
edœ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   s6  t jdtdd tjs`ttj tj 	t
¡t¡dd}t |¡t_W d  ƒ n1 sV0    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¡r2t | 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é   )Ú
stacklevelúutf-8©ÚencodingNZ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__.   s4    
ÿÿ*
ÿzFirefoxProfile.__init__c                 C   s   || j |< dS )z0sets the preference that we want in the profile.N©r%   ©r2   ÚkeyÚvaluer
   r
   r   Úset_preferenceU   s    zFirefoxProfile.set_preferencec                 C   s   |   |¡ d S )N)Ú_install_extension)r2   Ú	extensionr
   r
   r   Úadd_extensionY   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_prefsr5   r
   r
   r   Úupdate_preferences\   s    
z!FirefoxProfile.update_preferencesc                 C   s   | j S )z8Gets the profile directory that is currently being used.)r&   ©r2   r
   r
   r   r   g   s    zFirefoxProfile.pathc                 C   s   | j S )z+Gets the port that WebDriver is working on.)Ú_portr@   r
   r
   r   Úportl   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Ú	TypeErrorrA   r8   )r2   rB   r
   r
   r   rB   q   s    
c                 C   s
   | j d S )NÚ webdriver_accept_untrusted_certsr4   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 callrI   ©r   r8   ©r2   r7   r
   r
   r   rJ   ƒ   s    c                 C   s
   | j d S )NÚ!webdriver_assume_untrusted_issuerr4   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 )NrK   rL   rO   rM   rN   r
   r
   r   rP      s    c           	   	   C   s¤   |   ¡  tƒ }t |dtj¡b}t| jƒd }t | j¡D ]6\}}}|D ]&}tj 	||¡}| 
|||d… ¡ qHq:W d  ƒ n1 s†0    Y  t | ¡ ¡ d¡S )znA zipped, base64 encoded string of profile directory for use with
        remote WebDriver JSON wire protocol.ÚwrD   NzUTF-8)r?   r   ÚzipfileÚZipFileÚZIP_DEFLATEDÚlenr   r   Úwalkr   ÚwriteÚbase64Ú	b64encodeÚgetvalueÚdecode)	r2   ÚfpZzippedZ	path_rootÚbaseÚ_ÚfilesZfyleÚfilenamer
   r
   r   Úencoded”   s    6zFirefoxProfile.encodedc                 C   s   t  ¡ S )z9Creates a temp folder to store User.js and the extension.)r(   r)   r@   r
   r
   r   r'   ¢   s    z!FirefoxProfile._create_tempfolderc              
   C   sd   t | jddd@}| ¡ D ]&\}}| d|› dt |¡› d¡ qW d  ƒ n1 sV0    Y  dS )z1writes the current user prefs dictionary to disk.rQ   r   r   zuser_pref("z", z);
N)r   r0   r=   rW   r!   Údumps)r2   Z
user_prefsÚfr6   r7   r
   r
   r   r>   ¦   s    z FirefoxProfile._write_user_prefsc                 C   sº   t  d¡}z˜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  W n ty´   Y n0 d S )Nzuser_pref\("(.*)",\s(.*)\)r   r   r   rD   z5(skipping) failed to json.loads existing preference: )ÚreÚcompiler   Úsearchr!   ÚloadsÚgroupr%   Ú	Exceptionr   r   )r2   ZuserjsZpref_patternrc   ZusrÚmatchesr
   r
   r   r.   ¬   s    

 ÿ.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	¡}
|
sRJ 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 )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: Úunpackr   T)Úsymlinks)ÚWEBDRIVER_EXTr   r   r   r   r   Úendswithr(   r)   ÚsplitrR   rS   ÚnamelistÚisdirÚmakedirsÚreadr   rW   Ú_addon_detailsÚgetr/   Úexistsr-   r*   r#   r+   Úrmtree)r2   Zaddonrr   ZtmpdirZxpifileÚcompressed_fileÚnameÚdatarc   Zaddon_detailsZaddon_idÚ
addon_pathr
   r
   r   r9   ¼   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 
|¡rLt 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 s@0    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¡slt|jjƒD ]2}|j |¡}|j|d kr8| d|ji¡ q8W 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)rq   rr   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Ú ú:rD   )ZdocumentElementÚ
attributesÚrangeÚlengthÚitemr7   r€   rv   )Ú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geckorq   r€   ú r„   ú@rƒ   F)rq   rƒ   r€   rr   )r!   rg   ÚKeyErrorÚreplace)ÚcontentÚmanifestrq   r
   r
   r   Úparse_manifest_json  s    
"üz:FirefoxProfile._addon_details.<locals>.parse_manifest_jsonzAdd-on path does not exist: rn   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„   rq   rr   ÚtruezAdd-on id could not be found.)#r   r   r}   ÚOSErrorrR   Ú
is_zipfilerS   rw   rz   Úcloserx   r   r   r˜   r   ÚstrÚsysÚexc_infor   ÚparseStringZgetElementsByTagNamer‰   r   ÚnodeNamer™   Úupdater|   r‡   r†   rˆ   r€   r7   ri   rE   Úlower)r2   r‚   ÚdetailsrŽ   r•   rœ   r   r›   Zmanifest_json_filenamerc   ÚerŠ   ÚemÚrdfÚdescriptionr”   Úentryr   Ú	attributer
   r
   r   r{   ì   sb    
ü.*,




,zFirefoxProfile._addon_details)N)T)r   r   r   ZANONYMOUS_PROFILE_NAMEr   r3   r8   rt   r;   r?   Úpropertyr   rB   ÚsetterrJ   rP   r¢   ra   r'   r>   r.   r9   r{   r
   r
   r
   r   r   *   s6   
'




0r   )rX   r#   r!   r   rd   r*   r£   r(   r   rR   Úior   Úxml.domr   Zselenium.common.exceptionsr   rt   r    ZEXTENSION_NAMEri   r   r   r
   r
   r
   r   Ú<module>   s"   