
    Je(                     H   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.  ej`                         Z1ejd                  jg                  dd      ji                  d      Z5e5D  cg c]  } | jm                          c} Z5ejn                  Z8 G d d      Z9d Z:d Z;d Z<d Z=d Z>d Z?d Z@d ZAd ZBy# e/$ r dxZ-Z.Y w xY wc c} w )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                       e Zd ZdZd Zd Zy)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                      || _         || _        y Nappwindow)selfr$   r%   s      2/usr/lib/python3/dist-packages/spyder/app/utils.py__init__zSpy.__init__;   s        c                     t        | j                  j                               t        | j                        D cg c]  }|d   dk7  s| c}z   S c c}w )Nr   _)list__dict__keysdir	__class__)r&   xs     r'   __dir__zSpy.__dir__?   sG    T]]'')*/?q1Q43;?@ 	A?s   AAN)__name__
__module____qualname____doc__r(   r2    r)   r'   r    r    2   s    Ar)   r    c                     t         j                  dk(  rt        j                  t        j
                  d      } t        j                  |       syt        j                  |       D cg c]  }t        j                  d|      r| }}|r?t        t        j                  | |d               S t        j                  }d|d   |d   fz  } t        j                  | d      }t        j                  |      rt        |      S yc c}w )	zc
    Return Python documentation path
    (Windows: return the PythonXX.chm path if available)
    ntDocNz(?i)Python[0-9]{3,6}.chmr   z/usr/share/doc/python%d.%d/html   z
index.html)osnameospjoinsysprefixisdirlistdirrematchr   version_infoisfile)doc_pathpath
python_chmvinf
python_docs        r'   get_python_doc_pathrM   D   s    
 
ww$88CJJ.yy"')zz('; Ft"=tD  F
 FCHHXz!}=>>4Qa7II(L1J
zz*
## Fs   %C>c                    | dk(  rNt        j                  t        j                         t        $t	        j
                  t        j                         yy| dk(  rNt        j                  t        j                         t        $t	        j
                  t        j                         yy| dk(  rNt        j                  t        j                         t        $t	        j
                  t        j                         yyy)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_implementationrZ   Y   s     %%b&=&=>#--.B.K.KL $	9	%%b&<&<=#--.B.I.IJ $	6	%%b&7&78#--.B.I.IJ $ 
r)   c                 ~   | j                   st               dkD  rt        j                  t        j                  d}|t                  }d}| j
                  j                  d      }|D cg c]  }|j                          }}|t        z   }|D cg c]
  }|dk7  s	| }}t        j                         g}t        j                  d   }|j                  t        j                  |d             d	 |dgk7  rt        |      dkD  rt        |      fd
t        j                   |      } G fddt        j"                        }	 |	       }
t$        j'                  |       |D ]J  }|j)                  |
       |j+                  |       |j'                  |       t$        j-                  |       L yyc c}w c c}w )z3Setup logging with cli options defined by the user.r   )      z5%(asctime)s [%(levelname)s] [%(name)s] -> %(message)sr   r   SPYDER_DEBUG_FILEzw+)filenamemodec                      y)NTr7   )r1   s    r'   <lambda>zsetup_logging.<locals>.<lambda>   s    r)   c                 .    j                  | d      d uS )NT)stop_on_prefix)lookup)r1   dafsas    r'   rb   zsetup_logging.<locals>.<lambda>   s    ELL4L$H+/%0 r)   c                       e Zd ZdZ fdZy)#setup_logging.<locals>.ModuleFilterz,Filter messages based on module name prefix.c                 (     |j                         S r"   )r=   )r&   record
match_funcs     r'   filterz*setup_logging.<locals>.ModuleFilter.filter   s    !&++..r)   N)r3   r4   r5   r6   rl   )rk   s   r'   ModuleFilterrh      s    >/r)   rm   N)
debug_infor   loggingINFODEBUG
filter_logsplitstripFILTER_NAMESStreamHandlerr<   environappendFileHandlerlenr   	FormatterFilterroot_loggersetLevel	addFiltersetFormatter
addHandler)cli_optionslevels	log_level
log_formatconsole_filtersr1   handlersfilepath	formatterrm   rl   handlerrf   rk   s               @@r'   setup_loggingr   m   s   !2Q!6\\gmm4?,-	L
%0066s;.=>1779>>)L8&5Ab1AA))+,::12=	
 $
rd"s?';a'?/*E1J %%j1		/7>> 	/ Y' 	,Gf%  +Y'""7+		,A "7 ?As   ,F5
F:F:c                     t        j                  d      } t        j                  t        j                  t        d      d            }|D ][  }| j                  |      }|t        |j                  d            }t        j                  |      rGt        j                  |       ] t        j                  d   }t        j                  |      rt        j                  |       yy)z Delete previous debug log files.z.*_.*_(\d+)[.]loglsp_logsz*.logNr;   r^   )rD   compileglobr>   r?   r   rE   intgrouppsutil
pid_existsr<   removerw   exists)regexfilesfrE   pid
debug_files         r'   delete_debug_log_filesr      s    JJ+,EIIchh}Z8'BCE Aekk!n%C$$S)		! /0J
zz*
		* r)   c                 6    dg}t         s||vrt        |       yy)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                        } | j                  d       t	        |       }t        t        d            }|j                  |       |j                          t        j                  |       }|j                  dddd      }t        |      }|j                         }|j                  d       |j                  |       |S 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!CD

15/x 89 u%WWQ3$r"kkm  ${# M Mr)   c                     t         j                  }t        |      }| j                         }|j	                  t
        j                  t
        j                  |       | j                  |       y)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      sI     ((EE]F++-K(--@NN;r)   c                  0   t               t        t        d            } j                  |        t	        d      rj                  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                   ,    e Zd ZdZ fdZed        Zy),create_application.<locals>.FakeQApplicationzSpyder's fake QApplicationc                 
    } y r"   r7   )r&   argsr$   s     r'   r(   z5create_application.<locals>.FakeQApplication.__init__   s    Dr)   c                       y)z5Do nothing because the Qt mainloop is already runningNr7   r7   r)   r'   exec_z2create_application.<locals>.FakeQApplication.exec_   s     r)   N)r3   r4   r5   r6   r(   staticmethodr   )r$   s   r'   FakeQApplicationr      s    (	 
	 
	r)   r   r   )	QtWidgetsc                      y r"   r7   )args    r'   fake_sys_exitz)create_application.<locals>.fake_sys_exit   s    r)   c                 \    t        j                  | ||       t               rt        d      y )Nr;   )r@   __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   r@   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                     | ||      }	 |j                          |j	                          |j                          |j                          |j                  rC|j                  j                  i        |j                  j                  dt        ||             t        j                          t        j                  dk(  r$t        j                  t         j"                  d       t%               rV|j&                  j)                  |j*                         d|_        t/        |d      r|r|j0                  |z   }n|j0                  }|r|D ]  }|j+                  |        |j2                  j)                  |j4                         t7               s|j9                          |S # t        $ r9 |j                  +	 |j                  j                           # t        $ r Y  w xY w w xY w)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.
    )	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_observersr@   platformr   rR   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

 	IIK||&&&4''ss4/HI 	 ||x%%b&@&@$G ""**4+B+BC3,---4--  	'A##A&	'
 T<<=!		KY  <<#--/ 	 ! s/   F   G"6GG"	GG"GG")Cr6   r   ro   r<   os.pathrI   r>   rD   r@   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}   rw   getrs   ru   rt   r   ORIGINAL_SYS_EXITr    rM   rZ   r   r   r   r   r   r   r   )r   s   0r'   <module>r     s
   4   	  	 
  , I I 6 #  ' 3 : : . 9/?
  g!zz~~126<<SA#/0a	0 HH A A$$*K(&,R $. ,^BM  /*..L'/ 1s   <D D	DD