
    g5                         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	e             Z G d
 d      Zy)    N)BytesIO)minidom)
deprecated)WebDriverExceptionzwebdriver_prefs.json/Addons must be added after starting the sessionc                       e Zd ZdZy)AddonFormatErrorz4Exception for not well-formed add-on manifest files.N)__name__
__module____qualname____doc__     ]/var/www/html/venv/lib/python3.12/site-packages/selenium/webdriver/firefox/firefox_profile.pyr	   r	   &   s    >r   r	   c                      e Zd ZdZddZd Z ed      dd       Zd Ze	d        Z
e	 ed      d	               Zej                   ed      dd              Ze	 ed      d               Zej                   ed      dd              Ze	 ed      d               Zej                   ed      dd              Ze	d
efd       Zd Z ed      dd       Z ed      d        Zy)FirefoxProfileNc           	      B   i | _         |rt        j                  j                  t	        j
                         d      }t        j                  ||t        j                  ddd             || _	        t        j                  | j                  d       y	t	        j
                         | _	        t        j                  swt        t        j                  j                  t        j                  j                  t              t               d      5 }t#        j$                  |      t        _        d	d	d	       t'        j(                  t        j                  d
         | _         t        j                  d   j+                         D ]  \  }}|| j                   |<    y	# 1 sw Y   mxY w)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encodingN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)selfprofile_directorynewprofdefault_prefskeyvalues         r   __init__zFirefoxProfile.__init__.   s7    %'!ggll8#3#3#57QRGOO!763I3I-Y_an3o !(DHHT&&. ( 0 0 2D!55GGLL!:<QR]d R"9==9QN6R
 )-n6X6XYb6c(dD%,@@JPPR 7
U16))#.7R Rs   
FFc                 "    || j                   |<   y)z0Sets the preference that we want in the profile.Nr   )r1   r5   r6   s      r   set_preferencezFirefoxProfile.set_preferenceM   s    ).!!#&r   r   c                 &    | j                  |       y N)_install_extension)r1   	extensions     r   add_extensionzFirefoxProfile.add_extensionQ   s    	*r   c           
         t         j                  j                  | j                  d      }t         j                  j	                  |      r't        j
                  |d       | j                  |       t        |dd      5 }| j                  j                         D ]0  \  }}|j                  d| dt        j                  |       d       2 	 d	d	d	       y	# 1 sw Y   y	xY w)
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   r0   writer,   dumps)r1   
user_prefsfr5   r6   s        r   update_preferencesz!FirefoxProfile.update_preferencesU   s    WW\\$"3"3Y?
77>>*%HHZ'&&z2*cG4 	G"77==? G
U+cU#djj.?-@EFG	G 	G 	Gs   ?ACC c                     | j                   S )z8Gets the profile directory that is currently being used.)r%   r1   s    r   r   zFirefoxProfile.patha   s        r   z'The port is stored in the Service classc                     | j                   S )z+Gets the port that WebDriver is working on.)_portrJ   s    r   portzFirefoxProfile.portf   s     zzr   returnc                     t        |t              st        d      	 t        |      }|dk  s|dkD  rt        d      	 || _        | j                  d| j
                         y# t        t        f$ r t        d      w xY w)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	TypeErrorrL   r:   )r1   rM   s     r   rM   zFirefoxProfile.portl   s     $$$%BCC	Dt9Dax4%<()TUU ( 
4djjA I& 	D$%BCC	Ds    A" "A=z8Allowing untrusted certs is toggled in the Options classc                      | j                   d   S )N webdriver_accept_untrusted_certsr9   rJ   s    r   accept_untrusted_certsz%FirefoxProfile.accept_untrusted_certs{   s     (()KLLr   c                 ^    t        |t              st        d      | j                  d|       y )N%Please pass in a Boolean to this callrW   rR   boolr   r:   r1   r6   s     r   rX   z%FirefoxProfile.accept_untrusted_certs   s+     %&$%LMM>Fr   c                      | j                   d   S )N!webdriver_assume_untrusted_issuerr9   rJ   s    r   assume_untrusted_cert_issuerz+FirefoxProfile.assume_untrusted_cert_issuer   s     (()LMMr   c                 ^    t        |t              st        d      | j                  d|       y )NrZ   r_   r[   r]   s     r   r`   z+FirefoxProfile.assume_untrusted_cert_issuer   s+     %&$%LMM?Gr   c           	      $   | j                   r| j                          t               }t        j                  |dt        j
                  d      5 }t        | j                        dz   }t        j                  | j                        D ]B  \  }}}|D ]7  }t        j                  j                  ||      }|j                  |||d        9 D 	 ddd       t        j                  |j                               j                  d      S # 1 sw Y   ;xY w)z]Updates preferences and creates a zipped, base64 encoded string of
        profile directory.rA   F)strict_timestampsrP   NzUTF-8)r   rH   r   zipfileZipFileZIP_DEFLATEDlenr   r   walkr   rD   base64	b64encodegetvaluedecode)	r1   fpzipped	path_rootbase_filesfylefilenames	            r   encodedzFirefoxProfile.encoded   s     $$##%Y__Rg&:&:eT 	AX^DII*I"$''$))"4 Aa! AD!ww||D$7HLL8IJ+?@AA	A .55g>>	A 	As   A=DDc           
         t        j                  d      }t        |d      5 }|D ]T  }|j                  |      }	 t	        j
                  |j                  d            | j                  |j                  d      <   V 	 ddd       y# t        $ r< t        j                  d|j                  d      |j                  d      z           Y w xY w# 1 sw Y   yxY w)zRReads existing preferences and adds them to desired preference
        dictionary.zuser_pref\("(.*)",\s(.*)\)r   r      rP   z5(skipping) failed to json.loads existing preference: N)recompiler(   searchr,   loadsgroupr   	Exceptionwarningswarn)r1   userjspref_patternrG   usrmatchess         r   rC   z$FirefoxProfile._read_existing_userjs   s     zz"?@&7+ 	q &--c2BF**W]][\M]B^D--gmmA.>?	 	
 ! MMOPWP]P]^_P`cjcpcpqrcsPsOtu	 	s0   CA B;CAC	CCCCc           	         d}d}|j                  d      rt        j                  dt        j                  j                  |      d   z         }t        j                  |d      }|j                         D ]  }|j                  d      rrt        j                  j                  t        j                  j                  ||            rSt        j                  t        j                  j                  ||             t        j                  j                  t        j                  j                  t        j                  j                  ||                  sPt        j                  t        j                  j                  t        j                  j                  ||                   |j                  |      }t        t        j                  j                  ||      d      5 }|j                  |       ddd        |}|}| j!                  |      }	|	j#                  d	      }
|
s
J d
|        t        j                  j                  | j$                  d      }t        j                  j                  ||
      }|sk|	d   sf|rdt        j                  j'                  |      s+t        j                  |       t        j(                  |d       t+        j,                  ||dz          n7t        j                  j'                  |      st+        j.                  ||d       |rt+        j0                  |       yy# 1 sw Y   xY w)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: 
extensionsunpackr   T)symlinks)endswithr    r!   r   r   splitrd   re   namelistisdirr   makedirsr)   readr(   rD   _addon_detailsgetr%   existsr&   r"   r.   r#   rmtree)r1   addonr   tmpdirxpifilecompressed_filenamedatarG   addon_detailsaddon_idextensions_dir
addon_paths                r   r=   z!FirefoxProfile._install_extension   sE    >>&!%%S277==3G3K-KLF%ooeS9O'002 	&==%77==fd)CDBGGLL$>?77==fd9S)TUBGGOOBGGLL4N$OP*//5Dbggll648$? &1& &	& GE ++E2 $$T*D<UGDDx d&7&7FWW\\.(;
mH5'77>>.1N+/KKf!4577>>*-zDA MM&! /& &s   "L88M	c                 t   ddddd}d }d }d }t         j                  j                  |      st        d|       	 t	        j
                  |      rct	        j                  |d      5 }d	|j                         v r  ||j                  d	            cddd       S |j                  d
      }ddd       nt         j                  j                  |      rt         j                  j                  |d	      }t         j                  j                  |      r-t        |d      5 }	 ||	j                               cddd       S t        t         j                  j                  |d
      d      5 }	|	j                         }ddd       nt        d|       	 t#        j$                        } ||d      } ||d      }|j'                  |dz         j)                  d      }|s |j'                  d      j)                  d      }|j*                  D ]<  }|j,                  j/                  |d      }||v s$|j1                  | ||      i       > |j3                  d      sot5        |j6                  j8                        D ]M  }|j6                  j)                  |      }|j:                  |dz   k(  s1|j1                  d|j<                  i       O tA        |d   t              r|d   jC                         dk(  |d<   |j3                  d      st        d      |S # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# t        t        f$ r/}
t        t        |
      t        j                          d         d}
~
ww xY w# t>        $ r/}
t        t        |
      t        j                          d         d}
~
ww xY w)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                 H   | j                   j                  }d}t        |j                        D ]q  }|j	                  |      j
                  |k(  s"d|j	                  |      j                  v s@|j	                  |      j                  j                  d      d   dz   } |S  |S )N :rP   )documentElement
attributesrangelengthitemr6   r   r   )docurlr   	namespaceis        r   get_namespace_idz7FirefoxProfile._addon_details.<locals>.get_namespace_id   s    ,,77JI:,,- ??1%++s2jooa0555$.OOA$6$;$;$A$A#$Fq$IC$O	 r   c                     g }| j                   D ]7  }|j                  |j                  k(  s|j                  |j                         9 dj                  |      j                         S )z(Retrieve the text value of a given node.r   )
childNodesnodeType	TEXT_NODEappendr   r   strip)elementrcnodes      r   get_textz/FirefoxProfile._addon_details.<locals>.get_text   sU    B** )==DNN2IIdii() 772;$$&&r   c                     t        j                  |       }	 |d   d   d   }||d   |d   d	d
S # t        $ r! |d   j                  dd      dz   |d   z   }Y 6w xY w)z[Extracts the details from the contents of a WebExtensions
            `manifest.json` file.applicationsgeckor   r    r   @r   F)r   r   r   r   )r,   r{   KeyErrorreplace)contentmanifestr   s      r   parse_manifest_jsonz:FirefoxProfile._addon_details.<locals>.parse_manifest_json  s     zz'*HSn-g6t< #I. +	   Sf%--c26<x	?RRSs   / 'AAzAdd-on path does not exist: r   zmanifest.jsonzinstall.rdfr   r   z/Add-on path is neither an XPI nor a directory: rw   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   OSErrorrd   
is_zipfilere   r   r   r   r   r(   r   r	   strsysexc_infor   parseStringgetElementsByTagNamer   r   nodeNamer   updater   r   r   r   r   r6   r}   rR   lower)r1   r   detailsr   r   r   r   r   manifest_json_filenamerG   er   emrdfdescriptionr   entryr   	attributes                      r   r   zFirefoxProfile._addon_details   s_    N			'	 ww~~j)8EFF	>!!*-__Z5 C&/*B*B*DD2?3G3G3XYC C  /33MBH	C C
 z*)+j/)R&77>>"894wG =121668<= = "'',,z=AGT (XY vvxH( (  OPZ|\]]	>%%h/C "#'LMB"3(UVC2233FGLLQOK!66}EJJ1M#.. <--b"5G#NNE8D>#:;	<
 ;;t${55<<= @A + 6 6 ; ;A >I ~~d2ioo'>?@ gh'- ' 1 7 7 9V CGH {{4 "#BCCcC C= =( ( " 	>"3q63<<>!+<==	>.  	>"3q63<<>!+<==	>s   +L> *)L	L> L.A4L> "L$8	L> +L> -L1>L> BM? 2A;M? .M? L!L> $L.)L> 1L;6L> >M<*M77M<?	N7*N22N7r<   )rN   N)T)r
   r   r   r'   r7   r:   r   r?   rH   propertyr   rM   setterrX   r`   r   ru   rC   r=   r   r   r   r   r   r   +   s   7>/ AB+ C+G ! ! 9: ;  
[[9:B ; B JKM L M ""JKG L #G
 JKN L N "((JKH L )H ? ? ? AB," C,"\ ABg Cgr   r   )ri   r.   r,   r   rx   r"   r   r    r~   rd   ior   xml.domr   typing_extensionsr   selenium.common.exceptionsr   r+   r}   r	   r   r   r   r   <module>r      sd   $    	 	  
      ( 9.  =>?y ? ??] ]r   