
    G@d(                        d Z ddlZddlZddlZddlmZ ddlZddlZddl	Z	ddl
mZmZ ddlmZmZmZmZmZmZ ddlmZmZ ddlmZ ddlmZmZmZmZmZ ddlm Z  dd	l!m"Z" dd
l#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ 	 ddl,m-Z-m.Z. n# e/$ r dxZ-Z.Y nw xY w ej0                    Z1ej2        3                    dd          4                    d          Z5d e5D             Z5ej6        Z7 G d d          Z8d Z9d Z:d Z;d Z<d Z=d Z>d Z?d Z@d ZAdS )z-Utility functions for the Spyder application.    N)QCoreApplicationQt)QColorQIconQPaletteQPixmapQPainterQImage)QApplicationQSplashScreen)QSvgRenderer)DEVget_conf_pathget_debug_levelrunning_in_mac_apprunning_under_pytest)CONF)DAFSA)get_image_path)running_installer_test)QStylePalette)file_uriqapplication)QQuickWindowQSGRendererInterfaceSPYDER_FILTER_LOG ,c                 6    g | ]}|                                 S  strip).0fs     0lib/python3.11/site-packages/spyder/app/utils.py
<listcomp>r&   ,   s     000a		000    c                       e Zd ZdZd Zd ZdS )Spyz
    This is used to inject a 'spy' object in the internal console
    namespace to inspect Spyder internals.

    Attributes:
        app       Reference to main QApplication object
        window    Reference to spyder.MainWindow widget
    c                 "    || _         || _        d S Nappwindow)selfr-   r.   s      r%   __init__zSpy.__init__;   s    r'   c                     t          | j                                                  d t          | j                  D             z   S )Nc                 *    g | ]}|d          dk    |S )r   _r    r#   xs     r%   r&   zSpy.__dir__.<locals>.<listcomp>A   s!    ???q1Q43;;;;;r'   )list__dict__keysdir	__class__)r/   s    r%   __dir__zSpy.__dir__?   sB    T]''))**??C//???@ 	Ar'   N)__name__
__module____qualname____doc__r0   r;   r    r'   r%   r)   r)   2   sA           A A A A Ar'   r)   c                     t           j        dk    r~t          j        t          j        d          } t          j        |           sdS d t          j        |           D             }|r(t          t          j        | |d                             S nt          j	        }d|d         |d         fz  } t          j        | d          }t          j
        |          rt          |          S dS )	zc
    Return Python documentation path
    (Windows: return the PythonXX.chm path if available)
    ntDocNc                 <    g | ]}t          j        d |          |S )z(?i)Python[0-9]{3,6}.chm)rematch)r#   paths     r%   r&   z'get_python_doc_path.<locals>.<listcomp>M   s@     F F Ft"=tDDFd F F Fr'   r   z/usr/share/doc/python%d.%d/html   z
index.html)osnameospjoinsysprefixisdirlistdirr   version_infoisfile)doc_path
python_chmvinf
python_docs       r%   get_python_doc_pathrV   D   s    
 
w$8CJ..y"" 	FF Frz(';'; F F F
 	?CHXz!}==>>>	? 4Qa7II(L11J
z* $
###$ $r'   c                    | dk    rGt          j        t          j                   t           t	          j        t          j                   dS dS | dk    rGt          j        t          j                   t           t	          j        t          j	                   dS dS | dk    rEt          j        t          j
                   t          "t	          j        t          j	                   dS dS dS )zh
    Set the OpenGL implementation used by Spyder.

    See spyder-ide/spyder#7447 for the details.
    softwareNdesktopgles)r   setAttributer   AA_UseSoftwareOpenGLr   setSceneGraphBackendr   SoftwareAA_UseDesktopOpenGLOpenGLAA_UseOpenGLES)options    r%   set_opengl_implementationrc   Y   s     %b&=>>>#-.B.KLLLLL $#	9		%b&<===#-.B.IJJJJJ $#	6		%b&7888#-.B.IJJJJJ 
	##r'   c                 x   | j         st                      dk    rt          j        t          j        d}|t                               }d}| j                            d          }d |D             }|t          z   }d |D             }t          j                    g}t          j
        d         }|                    t          j        |d	                     d
 |dgk    r't          |          dk    rt          |          fdt          j        |          } G fddt          j                  } |            }	t"                              |           |D ]]}
|
                    |	           |
                    |           |
                    |           t"                              |
           \dS dS )z3Setup logging with cli options defined by the user.r   )      z5%(asctime)s [%(levelname)s] [%(name)s] -> %(message)sr   c                 6    g | ]}|                                 S r    r!   r4   s     r%   r&   z!setup_logging.<locals>.<listcomp>u   s     >>>17799>>>r'   c                     g | ]
}|d k    |S )r   r    r4   s     r%   r&   z!setup_logging.<locals>.<listcomp>w   s    AAAb1r'   SPYDER_DEBUG_FILEzw+)filenamemodec                     dS )NTr    )r5   s    r%   <lambda>zsetup_logging.<locals>.<lambda>   s    t r'   r   c                 6                         | d          d uS )NT)stop_on_prefix)lookup)r5   dafsas    r%   rm   zsetup_logging.<locals>.<lambda>   s    ELL4L$H$H+/%0 r'   c                       e Zd ZdZ fdZdS )#setup_logging.<locals>.ModuleFilterz,Filter messages based on module name prefix.c                 $     |j                   S r+   )rI   )r/   record
match_funcs     r%   filterz*setup_logging.<locals>.ModuleFilter.filter   s    !z&+...r'   N)r<   r=   r>   r?   rw   )rv   s   r%   ModuleFilterrs      s4        >>/ / / / / / /r'   rx   N)
debug_infor   loggingINFODEBUG
filter_logsplitFILTER_NAMESStreamHandlerrH   environappendFileHandlerlenr   	FormatterFilterroot_loggersetLevel	addFiltersetFormatter
addHandler)cli_optionslevels	log_level
log_formatconsole_filtershandlersfilepath	formatterrx   rw   handlerrq   rv   s              @@r%   setup_loggingr   m   s    $,!2!2Q!6!6\gm44?,,-	L
%066s;;>>o>>>)L8AAoAAA)++,:12===	
 	
 	
 $^
rd""s?';';a'?'?/**E1 1 1 1J %j11		/ 	/ 	/ 	/ 	/ 	/ 	/7> 	/ 	/ 	/ Y''' 	, 	,Gf%%%  +++Y'''""7++++I "7!6@	, 	,r'   c                     t          j        d          } t          j        t          j        t          d          d                    }|D ]c}|                     |          }|Jt          |                    d                    }t          j
        |          st          j        |           dt          j        d         }t          j        |          rt          j        |           dS dS )z Delete previous debug log files.z.*_.*_(\d+)[.]loglsp_logsz*.logNrG   ri   )rD   compileglobrJ   rK   r   rE   intgrouppsutil
pid_existsrH   remover   exists)regexfilesr$   rE   pid
debug_files         r%   delete_debug_log_filesr      s    J+,,EIch}Z88'BBCCE  Aekk!nn%%C$S)) 	!/0J
z* 
	* r'   c                 D    dg}t           s||vrt          |           dS dS )av  
    Qt warning messages are intercepted by this handler.

    On some operating systems, warning messages might be displayed
    even if the actual message does not apply. This filter adds a
    blacklist for messages that are being printed for no apparent
    reason. Anything else will get printed in the internal console.

    In DEV mode, all messages are printed.
    z<QMainWidget::resizeDocks: all sizes need to be larger than 0N)r   print)msg_typemsg_log_context
msg_string	BLACKLISTs       r%   qt_message_handlerr      s?     	GI  j	))j *)r'   c                  "   t                      st          ddt          j                  } |                     d           t	          |           }t          t          d                    }|                    |           |                                 t          j
        |           }|                    dddd          }t          |          }|                                }|                    d           |                    |           nd}|S )zCreate splash screen.i  i  r   splash   N)r   r
   Format_ARGB32_Premultipliedfillr	   r   r   renderendr   	fromImagecopyr   fontsetPixelSizesetFont)imagepainterrendererpmr   splash_fonts         r%   create_splash_screenr      s    !! sC!CDD

15//x 8 899   u%%WWQ3$$r""kkmm  $$${####Mr'   c                     t           j        }t          |          }|                                 }|                    t
          j        t
          j        |           |                     |           dS )zZ
    Fix color for links.

    This was taken from QDarkstyle, which is MIT licensed.
    N)	r   COLOR_ACCENT_4r   palettesetColorr   NormalLink
setPalette)r-   colorqcolorapp_palettes       r%   set_links_colorr      sX     (EE]]F++--K(-@@@NN;r'   c                  n   t                      t          t          d                    }                     |            t	          d          r                    d            G fddt                    }ddlm} ||_        g fd}|t          _
        d }|t          _        d	gt          _        S )
z&Create application and patch sys.exit.spydersetDesktopFileNamec                   4    e Zd ZdZ fdZed             ZdS ),create_application.<locals>.FakeQApplicationzSpyder's fake QApplicationc                     } d S r+   r    )r/   argsr-   s     r%   r0   z5create_application.<locals>.FakeQApplication.__init__   s    DDDr'   c                      dS )z5Do nothing because the Qt mainloop is already runningNr    r    r'   r%   exec_z2create_application.<locals>.FakeQApplication.exec_   s	     Dr'   N)r<   r=   r>   r?   r0   staticmethodr   )r-   s   r%   FakeQApplicationr      sO        ((	 	 	 	 	 
	 	 
	 	 	r'   r   r   )	QtWidgetsc                     d S r+   r    )args    r%   fake_sys_exitz)create_application.<locals>.fake_sys_exit   s    r'   c                 l    t          j        | ||           t                      rt          d          d S )NrG   )rL   __excepthook__r   
SystemExit)type_valuetbacks      r%   spy_excepthookz*create_application.<locals>.spy_excepthook   s=    5%///!## 	  Q--	  	 r'   r   )r   r   r   setWindowIconhasattrr   r   qtpyr   rL   exit
excepthookargv)app_iconr   r   r   r   r-   s        @r%   create_applicationr      s     ..C ^H--..Hh s()) )x(((      <    -I     CH      $CN tCHJr'   c                     | ||          }	 |                                  nA# t          $ r4 |j        +	 |j                                         n# t          $ r Y nw xY w w xY w|                                 |                                 |                                 |j        rE|j                            i            |j                            dt          ||                     t          j                     t          j        dk    rt          j        t           j        d           t%                      rJ|j                            |j                   d|_        t/          |d          r|r|j        |z   }n|j        }|r|D ]}|                    |           |j                            |j                   t7                      s|                                 |S )a  
    Create and show Spyder's main window and start QApplication event loop.

    Parameters
    ----------
    WindowClass: QMainWindow
        Subclass to instantiate the Window.
    app: QApplication
        Instance to start the application.
    splash: QSplashScreen
        Splash screen instamce.
    options: argparse.Namespace
        Command line options passed to Spyder
    args: list
        List of file names passed to the Spyder executable in the
        command line.
    N)	namespacespyr,   darwinT_pending_file_open)setupBaseExceptionconsoleexit_interpreterpre_visible_setupshowpost_visible_setupstart_interpreterset_namespace_itemr)   r   notify_all_observersrL   platformr   r[   r   AA_DontShowIconsInMenusr   sig_open_external_fileconnectopen_external_file_has_startedr   r   focusChangedchange_last_focused_widgetr   r   )WindowClassr-   r   optionsr   mainas          r%   create_windowr    s   & ;vw''D

   <#--////     	IIKKK| J&&&444''ss4/H/H/HIII 	 |x%b&@$GGG  ."**4+BCCC3,-- 	. .-4-  ' 	' 	'A##A&&&& T<===!! 		Ks,   # A!AA!
AA!AA!)Br?   r   rz   rH   os.pathrF   rJ   rD   rL   r   qtpy.QtCorer   r   
qtpy.QtGuir   r   r   r   r	   r
   qtpy.QtWidgetsr   r   
qtpy.QtSvgr   spyder.config.baser   r   r   r   r   spyder.config.managerr   !spyder.utils.external.dafsa.dafsar   spyder.utils.image_path_managerr   spyder.utils.installersr   spyder.utils.paletter   spyder.utils.qthelpersr   r   qtpy.QtQuickr   r   	Exception	getLoggerr   r   getr~   r   r   ORIGINAL_SYS_EXITr)   rV   rc   r   r   r   r   r   r   r  r    r'   r%   <module>r     s   4 3   				       				 



  , , , , , , , , I I I I I I I I I I I I I I I I 6 6 6 6 6 6 6 6 # # # # # #              ' & & & & & 3 3 3 3 3 3 : : : : : : : : : : : : . . . . . . 9 9 9 9 9 9 9 9/????????? / / /*..L'''/  g!!z~~1266<<SAA00<000 H A A A A A A A A$$ $ $*K K K(&, &, &,R     $  .     , , ,^B B B B Bs   <B 	BB