o
    ҷh7                     @   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
   ]/var/www/html/venv/lib/python3.10/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Ze
dd Zejd%ddZe
dd Zejd%ddZe
defddZdd Zdd Zdd Zd&d d!Zd"d# ZdS )'FirefoxProfileWEBDRIVER_ANONYMOUS_PROFILENc                 C   s(  t js(ttjtjttdd}t	
|t _W d   n1 s#w   Y  tt jd | _|| _d| _| js@|  | _n0t | _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rt| jd dS 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.
        utf-8encodingNmutablezwebdriver-py-profilecopyzparent.locklockz.parentlock)ignore  zuser.js
extensionsi  )r   DEFAULT_PREFERENCESopenospathjoindirname__file__WEBDRIVER_PREFERENCESjsonloadcopydeepcopydefault_preferencesprofile_dir
tempfolder_create_tempfoldertempfilemkdtempshutilcopytreeignore_patternschmod_read_existing_userjsextensionsDir	userPrefsisfile)selfprofile_directorydefault_prefsnewprofr
   r
   r   __init__.   s2   

zFirefoxProfile.__init__c                 C   s   || j |< dS )z0Sets the preference that we want in the profile.Nr"   r0   keyvaluer
   r
   r   set_preferenceR   s   zFirefoxProfile.set_preferencec                 C   s   |  | d S N)_install_extension)r0   	extensionr
   r
   r   add_extensionV   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_prefsr6   r
   r
   r   update_preferencesY   s
   

z!FirefoxProfile.update_preferencesc                 C      | j S )z8Gets the profile directory that is currently being used.)r#   r0   r
   r
   r   r   d      zFirefoxProfile.pathc                 C   rB   )z+Gets the port that WebDriver is working on.)_portrC   r
   r
   r   porti   rD   zFirefoxProfile.portreturnc              	   C   sj   t |ts	tdzt|}|dk s|dkrtdW n ttfy(   tdw || _| 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..65535webdriver_firefox_portN)
isinstanceintr   
ValueError	TypeErrorrE   r9   )r0   rF   r
   r
   r   rF   n   s   
c                 C   
   | j d S )N webdriver_accept_untrusted_certsr5   rC   r
   r
   r   accept_untrusted_certs|      
z%FirefoxProfile.accept_untrusted_certsc                 C   "   t |ts	td| d| d S )N%Please pass in a Boolean to this callrO   rJ   boolr   r9   r0   r8   r
   r
   r   rP      s   
c                 C   rN   )N!webdriver_assume_untrusted_issuerr5   rC   r
   r
   r   assume_untrusted_cert_issuer   rQ   z+FirefoxProfile.assume_untrusted_cert_issuerc                 C   rR   )NrS   rW   rT   rV   r
   r
   r   rX      s   
c           	   	   C   s   |    t }t|dtj1}t| jd }t| jD ]\}}}|D ]}tj	||}|
|||d  q$qW d   n1 sCw   Y  t| dS )znA zipped, base64 encoded string of profile directory for use with
        remote WebDriver JSON wire protocol.wrH   NzUTF-8)rA   r   zipfileZipFileZIP_DEFLATEDlenr   r   walkr   writebase64	b64encodegetvaluedecode)	r0   fpzipped	path_rootbase_filesfylefilenamer
   r
   r   encoded   s   zFirefoxProfile.encodedc                 C   s   t  S )z9Creates a temp folder to store User.js and the extension.)r&   r'   rC   r
   r
   r   r%      s   z!FirefoxProfile._create_tempfolderc              
   C   sf   t | jddd!}| D ]\}}|d| dt| d qW d   dS 1 s,w   Y  dS )z1Writes the current user prefs dictionary to disk.rY   r   r   zuser_pref("z", z);
N)r   r.   r?   r_   r   dumps)r0   
user_prefsfr7   r8   r
   r
   r   r@      s
    "z FirefoxProfile._write_user_prefsc                 C   s   t d}zNt|dd=}|D ]0}||}zt|d| j|d< W q ty?   t	
d|d|d   Y qw W d    W d S 1 sLw   Y  W d S  ty]   Y d S w )Nzuser_pref\("(.*)",\s(.*)\)r   r      rH   z5(skipping) failed to json.loads existing preference: )recompiler   searchr   loadsgroupr"   	Exceptionwarningswarn)r0   userjspref_patternro   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rtjdtj	|d  d}t
|d}| D ]^}|drMtjtj||sLttj|| q0tjtjtj||sjttjtj|| ||}ttj||d}|| W d   n1 sw   Y  q0|}|}| |}	|	d	}
|
sJ d
| tj| j|
}|s|	d s|rtj| jst| j t| jd t||d  ntj|stj||dd |rt| dS dS )zInstalls 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'   splitrZ   r[   namelistisdirmakedirsreadr   r_   _addon_detailsgetr-   existsr+   r(   r    r)   rmtree)r0   addonr   tmpdirxpifilecompressed_filenamedataro   addon_detailsaddon_id
addon_pathr
   r
   r   r;      sF   




z!FirefoxProfile._install_extensionc              
   C   s  ddddd}dd }dd }dd	 }t j|s td
| zt|rPz$t|d}d| v r@||dW |	  W S |d}W |	  n\|	  w t j
|rt j|d}t j|rt|dd}	||	 W  d   W S 1 s{w   Y  tt j|ddd}	|	 }W d   n1 sw   Y  ntd| W n ttfy }
 ztt|
t d d}
~
ww zet|}||d}||d}||d d}|s|dd}|jD ]}|j|d}||v r||||i q|ds&t|jjD ]}|j|}|j|d kr$|d|ji qW n t y? }
 ztt|
t d d}
~
ww t!|d trR|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)r   r   r   versionc                 S   s`   | j j}d}t|jD ]"}||j|kr-d||jv r-||jdd d } |S q|S )N :rH   )documentElement
attributesrangelengthitemr8   r   r   )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   )
childNodesnodeType	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y&   |d ddd |d  }Y nw ||d |d d	d
S )z[Extracts the details from the contents of a WebExtensions
            `manifest.json` file.applicationsgeckor   r    r   @r   F)r   r   r   r   )r   rt   KeyErrorreplace)contentmanifestr   r
   r
   r   parse_manifest_json  s   
 z:FirefoxProfile._addon_details.<locals>.parse_manifest_jsonzAdd-on path does not exist: r   zmanifest.jsonzinstall.rdfr   r   z/Add-on path is neither an XPI nor a directory: rp   z#http://www.mozilla.org/2004/em-rdf#z+http://www.w3.org/1999/02/22-rdf-syntax-ns#Descriptionr   r   r   r   truezAdd-on id could not be found.)#r   r   r   OSErrorrZ   
is_zipfiler[   r   r   closer   r   r   r   r   strsysexc_infor   parseStringgetElementsByTagNamer   r   nodeNamer   updater   r   r   r   r   r8   rv   rJ   lower)r0   r   detailsr   r   r   r   r   manifest_json_filenamero   er   emrdfdescriptionr   entryr   	attributer
   r
   r   r      st   

"




zFirefoxProfile._addon_detailsr:   )rG   N)T)r   r   r   ANONYMOUS_PROFILE_NAMEr   r4   r9   r   r=   rA   propertyr   rF   setterrP   rX   r   rl   r%   r@   r,   r;   r   r
   r
   r
   r   r   *   s8    
$




0r   )r`   r    r   r   rq   r(   r   r&   rw   rZ   ior   xml.domr   selenium.common.exceptionsr   r   r   EXTENSION_NAMErv   r   r   r
   r
   r
   r   <module>   s$   