
    8h7                         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	e      Z G d
 d      Zy)    N)BytesIO)minidom)WebDriverExceptionzwebdriver.xpizwebdriver_prefs.jsonzfxdriver@googlecode.comc                       e Zd ZdZy)AddonFormatErrorz4Exception for not well-formed add-on manifest files.N)__name__
__module____qualname____doc__     /home/stella.sw7ft.com/public_html/site_rebuilder/venv/lib/python3.12/site-packages/selenium/webdriver/firefox/firefox_profile.pyr   r   &   s    >r   r   c                      e Zd ZdZdZddZd ZefdZd Z	e
d        Ze
d        Zej                  dd
       Ze
d        Zej                  dd       Ze
d        Zej                  dd       Ze
d	efd       Zd Zd Zd ZddZd Zy)FirefoxProfileWEBDRIVER_ANONYMOUS_PROFILENc           	         t         j                  swt        t        j                  j                  t        j                  j                  t              t              d      5 }t        j                  |      t         _        ddd       t        j                  t         j                  d         | _        || _        d| _        | j                  s| j!                         | _        nt#        j$                         | _        t        j                  j                  | j                  d      }t'        j(                  | j                  |t'        j*                  ddd      	       || _        t        j,                  | j                  d
       | j/                  t        j                  j                  | j                  d             t        j                  j                  | j                  d      | _        t        j                  j                  | j                  d      | _        t        j                  j5                  | j2                        r!t        j,                  | j2                  d       yy# 1 sw Y   x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.
        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newprofs       r   __init__zFirefoxProfile.__init__.   s    11RWW__X68MNY` N59YY}5M2N
 $(==1S1ST]1^#_ ,#668D&..0DOggll4??4NOGOO  '&2H2HX^`m2n  'DHHT%%u-&&rww||D4D4Di'PQWW\\$*:*:LId&6&6	B77>>$..)HHT^^U+ *+N Ns   !I&&I0c                 "    || j                   |<   y)z0Sets the preference that we want in the profile.Nr'   r5   keyvalues      r   set_preferencezFirefoxProfile.set_preferenceR   s    (-  %r   c                 &    | j                  |       y N)_install_extension)r5   	extensions     r   add_extensionzFirefoxProfile.add_extensionV   s    	*r   c                     t         j                  d   j                         D ]#  \  }}|| j                  vs|| j                  |<   % | j	                  | j                         y )Nfrozen)r   r   itemsr'   _write_user_prefsr<   s      r   update_preferencesz!FirefoxProfile.update_preferencesY   s_    (<<XFLLN 	6JC $22205((-	6 	t778r   c                     | j                   S )z8Gets the profile directory that is currently being used.)r(   r5   s    r   r   zFirefoxProfile.pathd   s     r   c                     | j                   S )z+Gets the port that WebDriver is working on.)_portrK   s    r   portzFirefoxProfile.porti   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	TypeErrorrM   r?   )r5   rN   s     r   rN   zFirefoxProfile.portn   s     $$$%BCC	Dt9Dax4%<()TUU ( 
4djjA I& 	D$%BCC	Ds    A" "A=c                      | j                   d   S )N webdriver_accept_untrusted_certsr;   rK   s    r   accept_untrusted_certsz%FirefoxProfile.accept_untrusted_certs|   s    ''(JKKr   c                 ^    t        |t              st        d      | j                  d|       y )N%Please pass in a Boolean to this callrX   rS   boolr   r?   r5   r>   s     r   rY   z%FirefoxProfile.accept_untrusted_certs   s)    %&$%LMM>Fr   c                      | j                   d   S )N!webdriver_assume_untrusted_issuerr;   rK   s    r   assume_untrusted_cert_issuerz+FirefoxProfile.assume_untrusted_cert_issuer   s    ''(KLLr   c                 ^    t        |t              st        d      | j                  d|       y )Nr[   r`   r\   r^   s     r   ra   z+FirefoxProfile.assume_untrusted_cert_issuer   s)    %&$%LMM?Gr   c           	         | j                          t               }t        j                  |dt        j                        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)znA zipped, base64 encoded string of profile directory for use with
        remote WebDriver JSON wire protocol.wrQ   NzUTF-8)rI   r   zipfileZipFileZIP_DEFLATEDlenr   r   walkr   writebase64	b64encodegetvaluedecode)	r5   fpzipped	path_rootbase_filesfylefilenames	            r   encodedzFirefoxProfile.encoded   s     	!Y__Rg&:&:; 	AvDII*I"$''$))"4 Aa! AD!ww||D$7HLL8IJ+?@AA	A .55g>>	A 	As    A=C88Dc                 *    t        j                         S )z9Creates a temp folder to store User.js and the extension.)r+   r,   rK   s    r   r*   z!FirefoxProfile._create_tempfolder   s    !!r   c           
          t        | j                  dd      5 }|j                         D ]0  \  }}|j                  d| dt	        j
                  |       d       2 	 ddd       y# 1 sw Y   yxY w)z1Writes the current user prefs dictionary to disk.rd   r   r   zuser_pref("z", z);
N)r   r3   rG   rj   r#   dumps)r5   
user_prefsfr=   r>   s        r   rH   z FirefoxProfile._write_user_prefs   sp    $..#8 	GA(..0 G
U+cU#djj.?-@EFG	G 	G 	Gs   AA''A0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# t        $ r Y y w xY w)Nzuser_pref\("(.*)",\s(.*)\)r   r      rQ   z5(skipping) failed to json.loads existing preference: )recompiler   searchr#   loadsgroupr'   	Exceptionwarningswarn)r5   userjspref_patternr|   usrmatchess         r   r1   z$FirefoxProfile._read_existing_userjs   s    zz"?@	fw/ 1 C*11#6GEIZZPWP]P]^_P`Ea00q1AB 
 %  ST[TaTabcTdgngtgtuvgwTwSxy   		sS   C CA B<C?C AC
CCCCC C 	C('C(c           	      "   |t         k(  rEt        j                  j                  t        j                  j	                  t
              t               }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(                  |
      }|s|	d   s|rt        j                  j+                  | j(                        s?t        j                  | j(                         t        j,                  | j(                  d       t/        j0                  ||dz          n7t        j                  j+                  |      st/        j2                  ||d       |rt/        j4                  |       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: unpackr   T)symlinks)WEBDRIVER_EXTr   r   r   r    r!   endswithr+   r,   splitre   rf   namelistisdirmakedirsreadr   rj   _addon_detailsgetr2   existsr0   r-   r%   r.   rmtree)r5   addonr   tmpdirxpifilecompressed_filenamedatar|   addon_detailsaddon_id
addon_paths               r   rB   z!FirefoxProfile._install_extension   sl    M!GGLL!:MJE>>&!%%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 WW\\$"4"4h?
mH5'77>>$"4"45D../++U3KKf!4577>>*-zDA MM&! -& &s   0NN	c                    ddddd}d }d }d }t         j                  j                  |      st        d|       	 t	        j
                  |      rr	 t	        j                  |d      }d	|j                         v r' ||j                  d	            |j                          S |j                  d
      }|j                          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.                  j1                  |d      }||v s$|j3                  | ||      i       > |j5                  d      sot7        |j8                  j:                        D ]M  }|j8                  j+                  |      }|j<                  |dz   k(  s1|j3                  d|j>                  i       O tC        |d   t              r|d   jE                         dk(  |d<   |j5                  d      st        d      |S # j                          w 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 :rQ   )documentElement
attributesrangelengthitemr>   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: r~   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   OSErrorre   
is_zipfilerf   r   r   closer   r   r   r   r   strsysexc_infor   parseStringgetElementsByTagNamer   r   nodeNamer   updater   r   r   r   r   r>   r   rS   lower)r5   r   detailsr   r   r   r   r   manifest_json_filenamer|   er   emrdfdescriptionr   entryr   	attributes                      r   r   zFirefoxProfile._addon_details   sa    N			'	 ww~~j)8EFF	>!!*-,&-ooj#&FO&/*B*B*DD2?3G3G3XY $))+  /33MBH#))+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[ $))+= =( ( " 	>"3q63<<>!+<==	>.  	>"3q63<<>!+<==	>s   M >L& M $L& 5A<M 1L;	M +M <MM %BN A;N =N &L88M ;M M MM N$*NN	O*O		OrA   )rO   N)T)r   r	   r
   ANONYMOUS_PROFILE_NAMEr   r9   r?   r   rD   rI   propertyr   rN   setterrY   ra   r   rw   r*   rH   r1   rB   r   r   r   r   r   r   *   s   :!,H. '4 +9       
[[B B L L ""G #G
 M M "((H )H ? ? ?"G ."`mr   r   )rk   r%   r#   r   r   r-   r   r+   r   re   ior   xml.domr   selenium.common.exceptionsr   r   r"   EXTENSION_NAMEr   r   r   r   r   r   <module>r      sV   $    	 	  
      9. *?y ?l lr   