
&9_[c        
   @   s  d  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 Z d d l Z d d l	 m
 Z
 m Z d d l m Z m Z d d l m Z m Z d d l m Z m Z d d l m Z m Z d d	 l m Z d d
 l m Z d d l m Z m Z m Z m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% m& Z& e j' d k re j( j) d d  Z+ e+ rwe j, j- e+ d  g Z. qg  Z. n d d g Z. g  Z/ e j, j- e j0 d d  Z1 e1 e. k re/ j2 e1  n  e j( j) d  Z3 e3 d d h k rd Z4 nS e3 j5   d d h k re6 Z4 n2 e3 j5   d d h k r8e7 Z4 n e j8 d e3  i d d 6d d 6d d 6d d  6d! d" 6Z9 e: d# i i e j; d$ 6d% 6d& f d' i i e j< d$ 6d% 6d( f d) i i e6 d* 6e6 d+ 6d, 6d- f  Z= d. e f d/     YZ> d, e
 f d0     YZ? d S(1   s-  
An application for IPython.

All top-level applications should use the classes in this module for
handling configuration and creating configurables.

The job of an :class:`Application` is to create the master configuration
object and then create the configurable objects, passing the config to them.
iN(   t   deepcopy(   t   Applicationt   catch_config_error(   t   ConfigFileNotFoundt   PyFileConfigLoader(   t   releaset   crashhandler(   t
   ProfileDirt   ProfileDirError(   t   get_ipython_dirt   get_ipython_package_dir(   t   ensure_dir_exists(   t	   py3compat(
   t   Listt   Unicodet   Typet   Boolt   Dictt   Sett   Instancet	   Undefinedt   defaultt   observet   ntt   PROGRAMDATAt   ipythons   /usr/local/etc/ipythons   /etc/ipythont   etct   IPYTHON_SUPPRESS_CONFIG_ERRORSt    t   1t   truet   0t   falses   Unsupported value for environment variable: 'IPYTHON_SUPPRESS_CONFIG_ERRORS' is set to '%s' which is none of  {'0', '1', 'false', 'true', ''}.s   ProfileDir.locations   profile-dirs   BaseIPythonApplication.profilet   profiles"   BaseIPythonApplication.ipython_dirs   ipython-dirs   Application.log_levels	   log-levels(   BaseIPythonApplication.extra_config_filet   configt   debugt	   log_levelR   s8   set log level to logging.DEBUG (maximize logging output)t   quiets;   set log level to logging.CRITICAL (minimize logging output)t   initt   copy_config_filest   auto_createt   BaseIPythonApplications   Initialize profile with default config files.  This is equivalent
            to running `ipython profile create <profile>` prior to startup.
            t   ProfileAwareConfigLoaderc           B   s   e  Z d  Z d d d  Z RS(   s>   A Python file config loader that is aware of IPython profiles.c         C   sb   | d  k	 rF y t j t   |  } Wn t k
 r9 d  SX| j } n  t t |   j | d | S(   Nt   path(	   t   NoneR   t   find_profile_dir_by_nameR	   R   t   locationt   superR*   t   load_subconfig(   t   selft   fnameR+   R!   t   profile_dir(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyR0   [   s    N(   t   __name__t
   __module__t   __doc__R,   R0   (    (    (    s7   lib/python2.7/site-packages/IPython/core/application.pyR*   Y   s   c           B   s  e  Z e d   Z e d  Z e e j  Z e e  Z	 e e
  Z e e g  Z e Z e   Z e   Z e d  d    Z e d  d    Z e e j j e   d d d   Z e e    Z e d  d	    Z e d
 d  j d e   Z! e d  d    Z" e d d
 d j d e   Z# e d  d    Z$ e d
 d  j d e   Z% e d  d    Z& e' Z( e) e d e  Z* e d  d    Z+ e, e' d
 d j d e   Z- e, e' d
 d j d e   Z. e e    Z/ e d  d    Z0 e, e' d
 d j d e   Z1 e, e' d
 d j d e   Z2 e3 e4 j5  Z6 e7 d    Z8 i  Z9 d( d  Z; d    Z< d!   Z= e d  d"    Z> e? d#  Z@ d$   ZA d%   ZB d&   ZC e7 d( d'   ZD RS()   u   ipythonu.   IPython: an enhanced interactive Python shell.t   config_file_namec         C   s   |  j  j d d  d S(   Nt   -t   _u
   _config.py(   t   namet   replace(   R1   (    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   _config_file_name_defaulty   s    c         C   s/   | d | d k r+ |  j  j | d  n  d  S(   Nt   newt   old(   t   config_file_specifiedt   add(   R1   t   change(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   _config_file_name_changed|   s    u   configu   profileu   defaultt   config_file_pathsc         C   s   t  j   g S(   N(   R   t   getcwd(   R1   (    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   _config_file_paths_default   s    t   helps   Path to an extra config file to load.
    
    If specified, load this config file in addition to any other IPython config.
    R"   t   extra_config_filec         C   s`   | d } | d } y |  j  j |  Wn t k
 r; n X|  j j |  |  j  j |  d  S(   NR>   R=   (   t   config_filest   removet
   ValueErrorR?   R@   t   append(   R1   RA   R>   R=   (    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   _extra_config_file_changed   s    

s   The IPython profile to use.R!   c         C   s)   t  j j t   d d | d  |  _ d  S(   Nu   configu   profileR=   (   t   osR+   t   joinR
   t   builtin_profile_dir(   R1   RA   (    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   _profile_changed   s    	s  
        The name of the IPython directory. This directory is used for logging
        configuration (through profiles), history storage, etc. The default
        is usually $HOME/.ipython. This option can also be specified through
        the environment variable IPYTHONDIR.
        t   ipython_dirc         C   s/   t    } |  j i d d 6| d 6| d 6 | S(   NRQ   R:   R>   R=   (   R	   t   _ipython_dir_changed(   R1   t   d(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   _ipython_dir_default   s    		t
   allow_noneR3   c         C   s   |  j  r d  S|  j   |  j S(   N(   t   _in_init_profile_dirt   init_profile_dirR3   (   R1   (    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   _profile_dir_default   s    	
s7   Whether to overwrite existing config files when copyings1   Whether to create profile dir if it doesn't existRH   c         C   s
   |  j  g S(   N(   R7   (   R1   (    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   _config_files_default   s    s,  Whether to install the default config files into the profile dir.
        If a new profile is being created, and IPython contains config files for that
        profile, then they will be staged into the new directory.  Otherwise,
        default config files will be automatically generated.
        s   Create a massive crash report when IPython encounters what may be an
        internal error.  The default is to append a short message to the
        usual tracebackc         K   sO   t  t |   j |   y t j   Wn$ |  j j d  |  j d  n Xd  S(   Ns(   Current working directory doesn't exist.i   (   R/   R)   t   __init__R   RD   t   logt   errort   exit(   R1   t   kwargs(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyRZ      s    c         C   sc   | |  j  k rJ |  j j d j d |   |  j j d j d |   n  t t |   j | |  S(   NsP   Subcommand `ipython {sub}` is deprecated and will be removed in future versions.t   subs4   You likely want to use `jupyter {sub}` in the future(   t   deprecated_subcommandsR[   t   warningt   formatR/   R)   t   initialize_subcommand(   R1   t   subct   argv(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyRc      s    
c         C   s8   |  j  |   |  _ |  j t _ d   } t j |  d S(   s?   Create a crash handler, typically setting sys.excepthook to it.c           S   s   t  j t  _ d  S(   N(   t   syst   __excepthook__t
   excepthook(    (    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   unset_crashhandler   s    N(   t   crash_handler_classt   crash_handlerRh   Rf   t   atexitt   register(   R1   Ri   (    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   init_crash_handler   s    	c         C   s3   |  j  r |  j | | |  St j | | |  Sd S(   s   this is sys.excepthook after init_crashhandler
        
        set self.verbose_crash=True to use our full crashhandler, instead of
        a regular traceback with a short message (crash_handler_lite)
        N(   t   verbose_crashRk   R   t   crash_handler_lite(   R1   t   etypet   evaluet   tb(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyRh      s    	c         C   s  | d } | d } | t  k	 ri t j t j j |  t j    } | t j k ri t j j |  qi n  t j t j j |  t j    } t j j	 |  t
 |  t j j | d  } t j j t   d d d  } t j j |  rt j j |  rt j | |  n  x] d
 D]U } t j j | |  }	 y t
 |	  Wqt k
 rm}
 |  j j d |	 |
  qXqW|  j j d	 |  d  S(   NR>   R=   t   READMEu   configu   profilet
   extensionst   nbextensionss   couldn't create path %s: %ss   IPYTHONDIR set to: %s(   Ru   Rv   (   R   R   t   cast_bytes_py2RM   R+   t   abspathRf   t   getfilesystemencodingRI   RK   R   RN   R
   t   existst   shutilt   copyt   OSErrorR[   R\   R#   (   R1   RA   R>   R=   t   str_oldt   str_patht   readmet
   readme_srcRS   R+   t   e(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyRR   
  s,    


%c         C   s  |  j  j d |  j  d } |  j  j d |  y? | d	 k	 rU t j } | t _ n  t j |  | d |  j Wn$ t k
 r |  j  j d |  n X| d	 k	 r | t _ n  x |  j D] } | s | | k r q n  |  j  j d |  j	  y t j |  | d |  j Wq t k
 rU| |  j
 k r9|  j  j } n |  j  j } | d |  q t k
 r| sn  n  |  j  j d |  j	 d t q Xq Wd	 S(
   sr  Load the config file.

        By default, errors in loading config are handled, and a warning
        printed on screen. For testing, the suppress_errors option is set
        to False, so errors will make tests fail.

        `supress_errors` default value is to be `None` in which case the
        behavior default to the one of `traitlets.Application`.

        The default value can be set :
           - to `False` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '0', or 'false' (case insensitive).
           - to `True` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '1' or 'true' (case insensitive).
           - to `None` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '' (empty string) or leaving it unset.

        Any other value are invalid, and will make IPython exit with a non-zero return code.
        s"   Searching path %s for config filess   ipython_config.pys"   Attempting to load config file: %sR+   s   Config file %s not founds#   Config file not found, skipping: %ss   Error loading config file: %st   exc_infoN(   R[   R#   RC   R,   R   t   raise_config_file_errorst   load_config_fileR   RH   R7   R?   Ra   t	   Exceptiont   True(   R1   t   suppress_errorst   base_configt	   old_valueR7   t   msg(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyR   &  sJ    	c         C   sg  t  |  _ |  j d k	 r d Sd |  j k r+y" t j |  j |  j |  j  } Wn t	 k
 r|  j
 su |  j d k r y" t j |  j |  j |  j  } Wn5 t	 k
 r |  j j d |  j  |  j d  qX|  j j d | j  q(|  j j d |  j  |  j d  q>X|  j j d | j  n|  j j j } y t j | |  j  } Wn t	 k
 r|  j
 ry t j | |  j  } Wn2 t	 k
 r|  j j d	 |  |  j d  qX|  j j d
 |  q
|  j j d |  |  j d  n X|  j j d |  t j j | j  } | j d  r>| d |  _ n  | |  _ |  j j | j  t |  _ d S(   s   initialize the profile dirNs   ProfileDir.locationR   s   Could not create profile: %ri   s   Created profile dir: %rs   Profile %r not found.s   Using existing profile dir: %rs&   Could not create profile directory: %rs   Creating new profile dir: %rs   Profile directory %r not found.t   profile_i   (   R   RV   R3   R,   R"   R   R-   RQ   R!   R   R(   t   create_profile_dir_by_nameR[   t   fatalR]   t   infoR.   R#   t   find_profile_dirt   create_profile_dirRM   R+   t   basenamet
   startswithRC   RK   t   False(   R1   t   pR.   t   dir_name(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyRW   f  sJ    	""		c         C   sO  |  j  j t  |  j  j t  |  j } |  j r |  j } |  j } | r t j	 j
 t j	 j | |   r |  j j d | | |  j j |  j f  |  j j | d | d |  j qK|  j   n t j t j	 j | d   } xg | D]_ } t j	 j |  } |  j j | d | d t r |  j j d | |  j |  j j f  q q Wd S(   s8   [optionally] copy default config files into profile dir.s)   Staging %r from %s into %r [overwrite=%s]R+   t	   overwrites   *.pys"   Staging bundled %s from %s into %rN(   RC   t   extendt   ENV_CONFIG_DIRSt   SYSTEM_CONFIG_DIRSRO   R'   R!   R7   RM   R+   Rz   RN   R[   Ra   R3   R.   R   t   copy_config_filet   stage_default_config_filet   globR   R   (   R1   R+   t   srct   cfgt   filest   fullpath(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   init_config_files  s"    				'"c         C   s   |  j    } t j j |  j j |  j  } |  j sF t j j |  r |  j	 j
 d |  t | d   } | j |  Wd QXn  d S(   sA   auto generate default config file, and stage it into the profile.s"   Generating default config file: %rt   wN(   t   generate_config_fileRM   R+   RN   R3   R.   R7   R   Rz   R[   Ra   t   opent   write(   R1   t   sR2   t   f(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyR     s    c         C   sh   |  j  |  |  j   |  j d  k	 r* d  St |  j  } |  j   |  j   |  j   |  j	 |  d  S(   N(
   t   parse_command_lineRn   t   subappR,   R    R"   RW   R   R   t   update_config(   R1   Re   t	   cl_config(    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt
   initialize  s    



N(E   R4   R5   R   R:   t   descriptionR   t   versionR   t   base_aliasest   aliasest
   base_flagst   flagsR   R   t   classesR*   t   python_config_loader_classR   R?   R7   R   R<   R   RB   RM   R+   RN   R
   RO   RC   RE   t   tagR   RG   RL   R!   RP   RQ   RT   R   RV   R   R3   RX   R   R   R(   RH   RY   R'   Ro   R   R   t   CrashHandlerRj   R   RZ   R`   R,   Rc   Rn   Rh   RR   R   R   RW   R   R   R   (    (    (    s7   lib/python2.7/site-packages/IPython/core/application.pyR)   g   sl   		!			
									@	6			(@   R6   Rl   R|   R    R   t   loggingRM   R{   Rf   t   traitlets.config.applicationR   R   t   traitlets.config.loaderR   R   t   IPython.coreR   R   t   IPython.core.profiledirR   R   t   IPython.pathsR	   R
   t   IPython.utils.pathR   t   IPython.utilsR   t	   traitletsR   R   R   R   R   R   R   R   R   R   R:   t   environt   getR,   t   programdataR+   RN   R   R   t   prefixt   _env_config_dirRK   t   _envvarR   t   lowerR   R   R]   R   t   dictt   DEBUGt   CRITICALR   R*   R)   (    (    (    s7   lib/python2.7/site-packages/IPython/core/application.pyt   <module>
   sf   F					
		