
    hg8                         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e          ZdS )    N)BytesIO)minidom)WebDriverExceptionzwebdriver.xpizwebdriver_prefs.jsonzfxdriver@googlecode.comc                       e Zd ZdZdS )AddonFormatErrorz3Exception for not well-formed add-on manifest filesN)__name__
__module____qualname____doc__     c/var/www/fb-scrape/myenv/lib/python3.11/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	             Ze
d
             Zej        d             Ze
d             Zej        d             Ze
d             Zd Zd Zd ZddZd ZdS )FirefoxProfileWEBDRIVER_ANONYMOUS_PROFILENc           	         t          j        dt          d           t          j        st          t          j                            t          j        	                    t                    t                              5 }t          j        |          t          _        ddd           n# 1 swxY w Y   t          j        t          j        d                   | _        || _        d| _        | j        s|                                 | _        nt)          j                    | _        t          j                            | j        d          }t-          j        | j        |t-          j        ddd	          
           || _        t          j        | j        d           |                     t          j                            | j        d                     t          j                            | j        d          | _        t          j                            | j        d          | _        t          j                            | j                  rt          j        | 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.
        zAfirefox_profile has been deprecated, please use an Options object   )
stacklevelN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_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__/   sB    	Y(Q	8 	8 	8 	81 	Nbgll27??8#<#<#8: : ; ; N>K59Y}5M5M2N N N N N N N N N N N N N N N $(=.y9$; $; , 		R#6688D&.00DOgll4?4NOOGOD,g#)#9-Q^#_#_a a a a&DHT%u---&&rw||D4Di'P'PQQQW\\$*:LIId&6	BB7>>$.)) 	,HT^U+++++	, 	,s   =B((B,/B,c                     || j         |<   dS )zB
        sets the preference that we want in the profile.
        Nr)   r7   keyvalues      r   set_preferencezFirefoxProfile.set_preferenceU   s     ). %%%r   c                 0    |                      |           d S N)_install_extension)r7   	extensions     r   add_extensionzFirefoxProfile.add_extension[   s    	*****r   c                     t           j        d                                         D ]\  }}|| j        vr
|| j        |<   |                     | j                   d S )Nfrozen)r   r   itemsr)   _write_user_prefsr>   s      r   update_preferencesz!FirefoxProfile.update_preferences^   sd    (<XFLLNN 	6 	6JC $22205(-t788888r   c                     | j         S )zI
        Gets the profile directory that is currently being used
        )r*   r7   s    r   r    zFirefoxProfile.pathi   s    
 r   c                     | j         S )z<
        Gets the port that WebDriver is working on
        )_portrM   s    r   portzFirefoxProfile.portp   s    
 zr   c                 2   t          |t                    st          d          	 t          |          }|dk     s|dk    rt          d          n$# t          t          f$ r t          d          w xY w|| _        |                     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..65535webdriver_firefox_portN)
isinstanceintr   
ValueError	TypeErrorrO   rA   )r7   rP   s     r   rP   zFirefoxProfile.portw   s    
 $$$ 	D$%BCCC	Dt99Daxx4%<<()TUUU (I& 	D 	D 	D$%BCCC	D
4djAAAAAs   *A !A2c                     | j         d         S )N webdriver_accept_untrusted_certsr=   rM   s    r   accept_untrusted_certsz%FirefoxProfile.accept_untrusted_certs   s    '(JKKr   c                 X    |dvrt          d          |                     d|           d S )NTF%Please pass in a Boolean to this callrY   r   rA   r7   r@   s     r   rZ   z%FirefoxProfile.accept_untrusted_certs   s9    %%$%LMMM>FFFFFr   c                     | j         d         S )N!webdriver_assume_untrusted_issuerr=   rM   s    r   assume_untrusted_cert_issuerz+FirefoxProfile.assume_untrusted_cert_issuer   s    '(KLLr   c                 X    |dvrt          d          |                     d|           d S )Nr\   r]   ra   r^   r_   s     r   rb   z+FirefoxProfile.assume_untrusted_cert_issuer   s9    %%$%LMMM?GGGGGr   c                    |                                   t                      }t          j        |dt          j                  }t          | j                  dz   }t          j        | j                  D ]I\  }}}|D ]@}t          j        	                    ||          }|
                    |||d                    AJ|                                 t          j        |                                                              d          S )z
        A zipped, base64 encoded string of profile directory
        for use with remote WebDriver JSON wire protocol
        wrR   NzUTF-8)rK   r   zipfileZipFileZIP_DEFLATEDlenr    r   walkr!   writeclosebase64	b64encodegetvaluedecode)	r7   fpzipped	path_rootbasedirsfilesfylefilenames	            r   encodedzFirefoxProfile.encoded   s     	!!!YYS'*>??	NNQ&	!#!3!3 	= 	=D$ = =7<<d33Xx	

';<<<<= 	..55g>>>r   c                 (    t          j                    S )zJ
        Creates a temp folder to store User.js and the extension
        )r-   r.   rM   s    r   r,   z!FirefoxProfile._create_tempfolder   s     !!!r   c           
          t          | j        d          5 }|                                D ]3\  }}|                    d|dt	          j        |          d           4	 ddd           dS # 1 swxY w Y   dS )zB
        writes the current user prefs dictionary to disk
        re   zuser_pref("z", z);
N)r   r5   rI   rk   r%   dumps)r7   
user_prefsfr?   r@   s        r   rJ   z FirefoxProfile._write_user_prefs   s     $.#&& 	M!(..00 M M
UCCCE9J9J9J9JKLLLLM	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	Ms   A	A--A14A1c           	         dd l }t          j        d          }	 t          |          5 }|D ]}t          j        ||          }	 t          j        |                    d                    | j        |                    d          <   [# t          $ r?  |j
        d|                    d          z  |                    d          z              Y w xY w	 d d d            d S # 1 swxY w Y   d S # t          $ r Y d S w xY w)Nr   zuser_pref\("(.*)",\s(.*)\)r   rR   z7(skipping) failed to json.loads existing preference: %s)r   recompiler   searchr%   loadsgroupr)   	Exceptionr   )r7   userjsr   PREF_REr~   usrmatchess          r   r3   z$FirefoxProfile._read_existing_userjs   s   *:;;	f K K KC i55GKEIZPWP]P]^_P`P`EaEa0q1A1ABB$ K K K%&_&-mmA&6&6'79@q9I9I'J K K K K KK	KK K K K K K K K K K K K K K K K K K  	 	 	DD	sZ   C. C!ABC!ACC!CC!C. !C%%C. (C%)C. .
C<;C<Tc           	      D   |t           k    rGt          j                            t          j                            t
                    t                     }d}d}|                    d          rt          j        dt          j        	                    |          d         z             }t          j        |d          }|                                D ]}|                    d          rpt          j                            t          j                            ||                    s2t          j        t          j                            ||                     t          j                            t          j                            t          j                            ||                              sOt          j        t          j                            t          j                            ||                               |                    |          }t!          t          j                            ||          d          5 }|                    |           ddd           n# 1 swxY w Y   |}|}|                     |          }	|	                    d	          }
|
sJ d
|z              t          j                            | j        |
          }|sz|	d         sr|rpt          j                            | j                  s3t          j        | j                   t          j        | j        d           t/          j        ||dz              n6t          j                            |          st/          j        ||d           |rt/          j        |           dS 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.   splitrf   rg   namelistisdirmakedirsreadr   rk   _addon_detailsgetr4   existsr2   r/   r'   r0   rmtree)r7   addonr   tmpdirxpifilecompressed_filenamedatar~   addon_detailsaddon_id
addon_paths               r   rD   z!FirefoxProfile._install_extension   s2    M!!GLL!:!:MJJE>>&!! 	%S27==3G3G3K-KLLLF%oeS99O'0022 	& 	&==%% &7==fd)C)CDD @BGLL$>$>???7==fd9S9S)T)TUU QBGOOBGLL4N4N$O$OPPP*//55Dbgll6488$?? &1& & & & & & & & & & & & & & &GE ++E22 $$T**FF>FFFF W\\$"4h??
 	BmH5 	B' 	B7>>$"455 4D.///+U333Kf!455557>>*-- BzDAAAA  	"M&!!!!!	" 	"s   I''I+	.I+	c                    ddddd}d }d }d }t           j                            |          st          d|z            	 t	          j        |          r	 t	          j        |d          }d	|                                v r2 ||                    d	                    |	                                 S |                    d
          }|	                                 n)# |	                                 w xY wt           j        
                    |          rt           j                            |d	          }t           j                            |          rEt          |d          5 }	 ||	                                          cddd           S # 1 swxY w Y   t          t           j                            |d
          d          5 }	|	                                }ddd           n# 1 swxY w Y   nt          d|z            nM# t          t          f$ r9}
t          t          |
          t!          j                    d                   d}
~
ww xY w	 t%          j        |          } ||d          } ||d          }|                    |dz                                 d          }|s(|                    d                              d          }|j        D ]S}|j                            |d          }||                                v r |                    | ||          i           T|                    d          s`t9          |j        j                  D ]F}|j                            |          }|j        |dz   k    r|                    d|j         i           GnF# tB          $ r9}
t          t          |
          t!          j                    d                   d}
~
ww xY wtE          |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)r   r   r   versionc                 4   | j         j        }d}t          |j                  D ]t}|                    |          j        |k    rTd|                    |          j        v r8|                    |          j                            d          d         dz   } nu|S )N :rR   )documentElement
attributesrangelengthitemr@   r   r   )docurlr   	namespaceis        r   get_namespace_idz7FirefoxProfile._addon_details.<locals>.get_namespace_id  s    ,7JI:,--  ??1%%+s22jooa00555$.OOA$6$6$;$A$A#$F$Fq$IC$O	r   c                     g }| j         D ],}|j        |j        k    r|                    |j                   -d                    |                                          S )z'Retrieve the text value of a given noder   )
childNodesnodeType	TEXT_NODEappendr   r!   strip)elementrcnodes      r   get_textz/FirefoxProfile._addon_details.<locals>.get_text  sY    B* ) )=DN22IIdi(((772;;$$&&&r   c                     t          j        |           }	 |d         d         d         }n8# t          $ r+ |d                             dd          dz   |d         z   }Y nw xY w||d         |d         d	d
S )zOExtracts 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    z'**HSn-g6t< S S Sf%--c266<x	?RRS #I. +	  s   + 2A A zAdd-on path does not exist: %sr   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   r   r   truezAdd-on id could not be found.)$r   r    r   IOErrorrf   
is_zipfilerg   r   r   rl   r   r!   r   r   r   strsysexc_infor   parseStringgetElementsByTagNamer   r   nodeNamer   keysupdater   r   r   r   r   r@   r   rT   lower)r7   r   detailsr   r   r   r   r   manifest_json_filenamer~   er   emrdfdescriptionr   entryr   	attributes                      r   r   zFirefoxProfile._addon_details   s    	
 
		 		 			' 	' 	'	 	 	 w~~j)) 	I:ZGHHH	>!*-- `,&-oj#&F&FO&/*B*B*D*DDD22?3G3G3X3XYY $))++++  /33MBBH#))++++O))++++z** 	`)+j/)R)R&7>>"899 =4c:: =a2216688<<= = = = = = = = = = = = = = = = "',,z=AA3GG (1 vvxxH( ( ( ( ( ( ( ( ( ( ( ( ( ( ( QT^^___" 	> 	> 	>"3q663<>>!+<===	>	>%h//C "!#'LMMB""3(UVVC2233FGGLLQOOK N!66}EEJJ1MM#. < <--b"55GLLNN**NNE88D>>#:;;;;;t$$ @{5<== @ @A + 6 ; ;A > >I ~d22io'>??? 	> 	> 	>"3q663<>>!+<===	> gh'-- 	D ' 1 7 7 9 9V CGH {{4   	D"#BCCCs   H
 AC   H
 5C  
H
  C66A1H
 'FH
 FH
 F1H
 
G+H
 +G//H
 2G/3H
 
I4IIEN* *
O-44O((O-rC   )T)r   r	   r
   ANONYMOUS_PROFILE_NAMEr   r;   rA   r   rF   rK   propertyr    rP   setterrZ   rb   ry   r,   rJ   r3   rD   r   r   r   r   r   r   +   s       :#, #, #, #,L. . . '4 + + + +9 9 9     X    X 
[B B [B L L XL "G G #"G
 M M XM "(H H )(H ? ? X? " " "M M M  "." ." ." ."`r r r r rr   r   )rm   r'   r%   r   r   r/   r   r-   r   rf   ior   xml.domr   selenium.common.exceptionsr   r   r$   EXTENSION_NAMEr   r   objectr   r   r   r   <module>r      s  $    				 				  



                9 9 9 9 9 9  . *> > > > >y > > >C C C C CV C C C C Cr   