
     f6                         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dS )    N)BytesIO)minidom)
deprecated)WebDriverExceptionzwebdriver_prefs.json/Addons must be added after starting the sessionc                       e Zd ZdZdS )AddonFormatErrorz4Exception for not well-formed add-on manifest files.N)__name__
__module____qualname____doc__     m/var/www/api.educacionweb.es/myenv/lib/python3.11/site-packages/selenium/webdriver/firefox/firefox_profile.pyr	   r	   &   s        >>>>r   r	   c                   N   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dS )FirefoxProfileNc           	      0   i | _         |rt          j                            t	          j                    d          }t          j        ||t          j        ddd                     || _	        t          j
        | j	        d           d	S t	          j                    | _	        t          j        st          t          j                            t          j                            t                    t                     d          5 }t#          j        |          t          _        d	d	d	           n# 1 swxY w Y   t'          j        t          j        d
                   | _         t          j        d                                         D ]\  }}|| j         |<   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.
        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__.   s    %'! 	7gll8#3#5#57QRRGO!763I-Y_an3o3o    !(DHT&..... ( 0 2 2D!5 RGLL!:!:<QRR]d   R"9==9Q9QN6R R R R R R R R R R R R R R R
 )-n6XYb6c(d(dD%,@JPPRR 7 7
U16)#..7 7s   D..D25D2c                     || j         |<   dS )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                 0    |                      |           d S N)_install_extension)r1   	extensions     r   add_extensionzFirefoxProfile.add_extensionQ   s    	*****r   c           
         t           j                            | j        d          }t           j                            |          r*t          j        |d           |                     |           t          |dd          5 }| j        	                                D ]3\  }}|
                    d| dt          j        |           d           4	 d	d	d	           d	S # 1 swxY w Y   d	S )
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<   W\\$"3Y??
7>>*%% 	3HZ'''&&z222*cG444 	G"7==?? G G
UEcEEdj.?.?EEEFFFFG	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	Gs   AC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     zr   returnc                 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 )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     $$$ 	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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                 z    t          |t                    st          d          |                     d|           d S )N%Please pass in a Boolean to this callrW   rR   boolr   r:   r1   r6   s     r   rX   z%FirefoxProfile.accept_untrusted_certs   sC     %&& 	N$%LMMM>FFFF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                 z    t          |t                    st          d          |                     d|           d S )NrZ   r_   r[   r]   s     r   r`   z+FirefoxProfile.assume_untrusted_cert_issuer   sC     %&& 	N$%LMMM?GGGGGr   c           	      0   | j         r|                                  t                      }t          j        |dt          j        d          5 }t          | j                  dz   }t          j	        | j                  D ]I\  }}}|D ]@}t          j        
                    ||          }|                    |||d                    AJ	 ddd           n# 1 swxY w Y   t          j        |                                                              d          S )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   sY    $ 	&##%%%YY_Rg&:eTTT 	AX^DI*I"$'$)"4"4 A Aa! A AD!w||D$77HLL8IJJ+?@@@@AA	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A ..55g>>>s   A;CCCc           
         t          j        d          }t          |d          5 }|D ]}|                    |          }	 t	          j        |                    d                    | j        |                    d          <   [# t          $ rC t          j
        d|                    d          |                    d          z               Y w xY w	 ddd           dS # 1 swxY w Y   dS )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   sP    z"?@@&7+++ 	q  &--c22BF*W]][\M]M]B^B^D-gmmA.>.>??    MuPWP]P]^_P`P`cjcpcpqrcscsPsuu    		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s7   C"ABC"A
CC"CC""C&)C&Tc           	         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
|             t          j        
                    | j        d          }t          j        
                    ||
          }|sk|	d         sc|rat          j                            |          s)t          j        |           t          j        |d           t+          j        ||dz              n6t          j                            |          st+          j        ||d           |rt+          j        |           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: 
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   s    >>&!! 	%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**DDDUDDDDD d&7FFW\\.(;;
 	BmH5 	B' 	B7>>.11 0N+++///Kf!455557>>*-- BzDAAAA  	"M&!!!!!	" 	"s   3HH	H	c                    ddddd}d }d }d }t           j                            |          st          d|           	 t	          j        |          r	 t	          j        |d          }d	|                                v r2 ||                    d	                    |	                                 S |                    d
          }|	                                 n+# |	                                 w xY wt           j        
                    |          rt           j                            |d	          }t           j                            |          rFt          |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|           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 ]A}|j                            |d          }||v r |                    | ||          i           B|                    d          s`t7          |j        j                  D ]F}|j                            |          }|j        |dz   k    r|                    d|j        i           GnF# t@          $ r9}
t          t          |
          t!          j                    d                   d}
~
ww xY wtC          |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 :rP   )documentElement
attributesrangelengthitemr6   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 node.r   )
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 )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     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: 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   closer   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N		 		 			' 	' 	'	 	 	 w~~j)) 	GEEEFFF	>!*-- ^,&-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 =4wGGG =12216688<<= = = = = = = = = = = = = = = = "',,z=AAGTTT (XY vvxxH( ( ( ( ( ( ( ( ( ( ( ( ( ( ( \PZ\\]]]" 	> 	> 	>"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G##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6A2H (FH FH F2H G-!H -G11H 4G15H I4IID?N 
O$4OO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 7 7 7>/ / / ZABB+ + + CB+G G G ! ! X! Z9::  ;: X 
[Z9::B B B ;: [B ZJKKM M LK XM "ZJKKG G G LK #"G
 ZJKKN N LK XN "(ZJKKH H H LK )(H ? ? ? ? X?   ZABB," ," ," CB,"\ ZABBm m CBm m m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      s-  $    				 				  



                ( ( ( ( ( ( 9 9 9 9 9 9.  =>>? ? ? ? ?y ? ? ?>?c c c c c c c c c cr   