
    7VfR>                         d dl Z d dlZd dlmZm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
  G d de      Zej                  ej                   z  dfdZd Zy# e$ r eZ
Y 5w xY w)    N)LockThread)JSONDecodeErrorc                   4   e Zd ZdZdej
                  j                  dd      rdndz   ZdZe	d        Z
	 	 ddZd Ze	d	        Zd
 Zd Zd Zd Zd Zed        Zed        Zej,                  d        Zed        Zej,                  d        Zd Zd Zd Zy)	BaseScope )--disable-gpuz--allow-file-access-from-filesz--disable-breakpadz--disable-dev-shm-usageLAMBDA_RUNTIME_DIRN)z--single-processc                 4    | j                   | j                  z   S )a~  
        Get tuple containing the default chromium arguments that will be passed to chromium if not overridden.

        chromium arguments can be overridden in the Scope constructor using the chromium_args argument, or they
        can be overridden by assigning a tuple to the chromium_args property of an already constructed Scope instance

        :return: tuple of str
        )_default_chromium_args_scope_chromium_args)clss    \/var/www/html/software/conda/envs/catlas/lib/python3.12/site-packages/kaleido/scopes/base.pydefault_chromium_argszBaseScope.default_chromium_args$   s     ))C,D,DDD    c                    |du r| j                         }n|du rd}|du r|D cg c]
  }|dk7  s	| }}t        |      | _        t        j                         | _        d | _        d | _        t               | _	        y c c}w )NTFr   r	   )
r   tuple_chromium_argsioBytesIO
_std_error_std_error_thread_procr   
_proc_lock)selfdisable_gpuchromium_argsargs       r   __init__zBaseScope.__init__0   s    
 D  668Me#M %,9TSSO=SSTMT#M2 **,!%
& Us
   
A<A<c                 $    | j                          y N)_shutdown_kaleidor   s    r   __del__zBaseScope.__del__G   s     r   c           	      j   t         j                  j                  t         j                  j                  t         j                  j                  t         j                  j	                  t
                          dd      }t        j                         dk(  r|dz  }t         j                  j                  |      r|}|S t        d      }|jt         j                  j                  dt         j                        }|j                  t         j                  d      }t        dj!                  ||            |S )	N
executablekaleidoWindowsz.cmdPATHz
    aC  
The kaleido executable is required by the kaleido Python library, but it was not included
in the Python package and it could not be found on the system PATH.

Searched for included kaleido executable at:
    {vendored_executable_path} 

Searched for executable 'kaleido' on the following system PATH:
    {formatted_path}
)vendored_executable_pathformatted_path)ospathjoindirnameabspath__file__platformsystemexistswhichenvirongetdefpathreplacepathsep
ValueErrorformat)r   r*   executable_pathr-   r+   s        r   r=   zBaseScope.executable_pathJ   s    #%77<<GGOOBGGOOBGGOOH,EFG$
  ??	)$.$77>>23 7O0 ' $I.O&zz~~fbjj9!%bjj(!C  !&1I'5 !   r   c                 l   | j                         | j                  g}| j                  D ]m  }t        | |      }|du rd|j	                  dd      z   }n4|du s|0d|j	                  dd      z   dz   t        t        |            z   }|j                  |       o |j                  | j                         |S )z
        Build list of kaleido command-line arguments based on current values of
        the properties specified by self._scope_flags and self.chromium_args

        :return: list of flags
        Tz--_-F=)
r=   
scope_name_scope_flagsgetattrr9   reprstrappendextendr   )r   	proc_argskvflags        r   _build_proc_argszBaseScope._build_proc_argst   s     ))+T__=	"" 
	#Aa ADyaiiS11eqy aiiS11C7$s1v,FT"
	# 	++,r   c                     	 | j                   @| j                   j                  j                         }| j                  j	                  |       nyN)z
        Write standard-error of subprocess to the _std_error StringIO buffer.
        Intended to be called once in a background thread
        N)r   stderrreadliner   writer   vals     r   _collect_standard_errorz!BaseScope._collect_standard_error   sE    
 zz%jj''002%%c*  r   c           	         | j                   | j                   j                         | j                  5  | j                   | j                   j                         | j                   | j                   j                          t	        j
                         | _        | j                         }t        j                  |t        j                  t        j                  t        j                  t        j                  dk(        | _         | j                  | j                  j                         sPt        | j                         | _        | j                  j#                  d       | j                  j%                          | j                   j&                  j)                         j+                  d      }|sd| j-                         z   }t/        |      t1        j2                  |      }|j5                  dd	      d	k7  r5| j                   j                          t/        |j5                  d
d            ddd       yy# 1 sw Y   yxY w)z`
        Launch the kaleido subprocess if it is not already running and in a good state
        Nwin32)stdinstdoutrO   shell)targetTutf-8z3Failed to start Kaleido subprocess. Error stream:

coder   messagez"Failed to start Kaleido subprocess)r   pollr   waitr   r   r   rM   
subprocessPopenPIPEsysr2   r   is_aliver   rT   	setDaemonstartrX   rP   decode_get_decoded_std_errorr;   jsonloadsr7   )r   rI   startup_response_stringr]   startup_responses        r   _ensure_kaleidozBaseScope._ensure_kaleido   s    ::!2!> (t::%):)Fzz-

) ')jjlDO !% 5 5 7I!+!1!1!(oo))!llg5"DJ --5T=S=S=\=\=^17t?[?[1\...88>..446 /3jj.?.?.H.H.J.Q.QRY.Z+2S 779:   )11+/::6M+N(+//:a? JJOO-",-=-A-A)Mq-r"ssQ(t (t "?(t (ts   G9H77I c                 X   d}	 t         j                  j                  }| j                  j	                         j                  |      }|?	 t        j                  d      }| j                  j	                         j                  |      }|d}|S # t        $ r Y Rw xY w# t        $ r Y  w xY w)zK
        Attempt to decode standard error bytes stream to a string
        NFz1Failed to decode Chromium's standard error stream)	rc   rO   encodingr   getvaluerg   	Exceptionlocalegetpreferredencoding)r   std_err_strro   s      r   rh   z BaseScope._get_decoded_std_error   s     	zz**H//224;;HEK !66u="oo668??I MK  		  s$   AB 
>B 	BB	B)(B)c                 J   | j                   | j                  5  | j                   b| j                   j                         $| j                   j                  j	                          	 | j                   j                  d       d| _         ddd       yy#  Y xY w# 1 sw Y   yxY w)z_
        Shut down the kaleido subprocess, if any, and self the _proc property to None
        Ng       @)timeout)r   r   r^   rW   closer_   r#   s    r   r"   zBaseScope._shutdown_kaleido   s     ::! &::)zz(0 

((..0


4 "&DJ#& & "& &s$   AB%BBBBB"c                     t         r!   )NotImplementedErrorr#   s    r   rB   zBaseScope.scope_name   s    !!r   c                     d| j                   v S )zT If True, asks chromium to disable GPU hardware acceleration with --disable-gpu flagr	   )r   r#   s    r   r   zBaseScope.disable_gpu  s     $"4"444r   c                     | j                   D cg c]
  }|dk7  s	| }}|r|j                  d       t        |      | _         y c c}w )Nr	   )r   rG   r   )r   rS   r   new_argss       r   r   zBaseScope.disable_gpu  sC    #'#5#5PC9OCPPOOO,"8_ Qs
   
AAc                     | j                   S r!   )r   r#   s    r   r   zBaseScope.chromium_args  s    """r   c                 D    t        |      | _        | j                          y r!   )r   r   r"   rR   s     r   r   zBaseScope.chromium_args  s    #Cj r   c                 ,    t        j                  |      S r!   )ri   dumpsrR   s     r   _json_dumpszBaseScope._json_dumps  s    zz#r   c                 
   | j                          | j                  t        ||            j                  d      }| j                  5  t        j                         | _        | j                  j                  j                  |       | j                  j                  j                  dj                  d             | j                  j                  j                          | j                  j                  j                         }ddd       j                  d      }|sd| j                         z   }t!        |      	 t#        j$                  |      }|S # 1 sw Y   RxY w# t&        $ r t)        dt+        |      z           w xY w)aS  
        Transform input data using the current scope, returning dict response with error code
        whether successful or not.

        :param data: JSON-serializable object to be converted
        :param kwargs: Transform arguments for scope
        :return: Dict of response from Kaleido executable, whether successful or not
        )datar[   
Nz!Transform failed. Error stream:

zInvalid JSON: )rm   r   dictencoder   r   r   r   r   rW   rQ   flushrX   rP   rg   rh   r;   ri   rj   r   printrE   )r   r   kwargsexport_specresponseresponse_stringr]   s          r   _perform_transformzBaseScope._perform_transform  sA    	 &&tF'>?FFwO __ 	4 jjlDO JJ"";/JJ""4;;w#78JJ""$zz((113H	4 #//'29//12  W%%	zz/2H
 /	4 	4&  	"T/%::;	s   B;E=E  E "Fc                      | j                   |fi |}|j                  dd      }|dk7  r.|j                  dd      }t        dj	                  ||            |j                  dd      }|j                         S )a  
        Transform input data using the current scope

        Subclasses should provide a more helpful docstring

        :param data: JSON-serializable object to be converted
        :param kwargs: Transform arguments for scope
        :return: Transformed value as bytes
        r\   r   r]   Nz2Transform failed with error code {code}: {message})r\   r]   result)r   popr7   r;   r<   r   )r   r   r   r   r\   r]   
img_strings          r   	transformzBaseScope.transformE  s     +4**4:6: ||FA&19ll9d3GDKKw L   \\(D1
  ""r   )TT)__name__
__module____qualname__rC   r,   r6   r7   r   r   classmethodr   r   r$   r=   rM   rT   rm   rh   r"   propertyrB   r   setterr   r   r   r   r   r   r   r   r      s    L "$0Dd!KQS
 	E 	E !.! ' 'R2/tb.&4 " " 5 5 - - # # ! !(T#r   r   c                 h    d }t         j                  j                         r | |      r S y|.t         j                  j	                  dt         j
                        }|sy|j                  t         j                        }t        j                  dk(  rt         j                  |vr |j                  dt         j                         t         j                  j	                  dd      j                  t         j                        }t         fd|D              r g}n|D cg c]  } |z   	 }}n g}t               }|D ]m  }t         j                  j                  |      }	|	|vs'|j                  |	       |D ]1  }
t         j                  j!                  ||
      } |||      s-|c c S  o yc c}w )	a  
    Backport (unmodified) of shutil.which command from Python 3.6
    Remove this when Python 2 support is dropped

    Given a command, mode, and a PATH string, return the path which
    conforms to the given mode on the PATH, or None if there is no such
    file.

    `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
    of os.environ.get("PATH"), or can be overridden with a custom search
    path.
    c                     t         j                  j                  |       xr8 t        j                  | |      xr  t         j                  j	                  |        S r!   )r,   r-   r4   accessisdir)fnmodes     r   _access_checkz which_py2.<locals>._access_checkp  s;    ww~~b!SbiiD&9S"''--PRBS>SSr   Nr)   rV   r   PATHEXT c              3   x   K   | ]1  }j                         j                  |j                                3 y wr!   )lowerendswith).0extcmds     r   	<genexpr>zwhich_py2.<locals>.<genexpr>  s(     DSsyy{##CIIK0Ds   7:)r,   r-   r/   r6   r7   r8   splitr:   rc   r2   curdirinsertanysetnormcaseaddr.   )r   r   r-   r   pathextfilesr   seendirnormdirthefilenames   `           r   	which_py2r   `  sa    T 
wwsd#J|zz~~fbjj1::bjj!D
||wyyD KK299% **..B/55bjjA
 DGDDEE*123S3Y2E2 5D  ''""3'$HHW   ww||C1 t,K 	   3s    F/c                 h    t         j                  dkD  rddl}|j                  |       S t	        |       S )a  
    Return the absolute path of the input executable string, based on the
    user's current PATH variable.

    This is a wrapper for shutil.which that is compatible with Python 2.

    Parameters
    ----------
    cmd: str
        String containing the name of an executable on the user's path.

    Returns
    -------
    str or None
        String containing the absolute path of the executable, or None if
        the executable was not found.

    )   r   r   N)rc   version_infoshutilr5   r   )r   r   s     r   r5   r5     s.    & & ||C  ~r   )r`   ri   	threadingr   r   r   r,   rc   rr   r2   r   ImportErrorr;   objectr   F_OKX_OKr   r5   r   r   r   <module>r      sj      " 	 	 
  !$
L# L#`
 "'') >Bk  ! O!s   A A"!A"