
i^c           @  sq  d  Z  d d l m 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 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 m Z m Z m Z m Z m Z m  Z  m! Z! m" Z" d d
 l# m$ Z$ d d l% m& Z& m' Z' m( Z( d d l) m* Z* d d l+ Z+ d j,   Z- d j,   Z. e/ e	 d  rd g e	 _0 n  d Z1 e j2 j3 d d  Z4 e4 j5   d d h k re6 Z7 n4 e4 j5   d d d h k re8 Z7 n e9 d e4   e d    Z: d e; f d     YZ< d e j= f d     YZ> d e f d     YZ? d d d  Z@ d    ZA d S(!   s,   A base class for a configurable application.i(   t   print_function(   t   deepcopyN(   t   defaultdictt   OrderedDict(   t	   decorator(   t   Configurablet   SingletonConfigurable(   t   KVArgParseConfigLoadert   PyFileConfigLoadert   Configt   ArgumentErrort   ConfigFileNotFoundt   JSONFileConfigLoader(
   t   Boolt   Unicodet   Listt   Enumt   Dictt   Instancet
   TraitErrort   observet   observe_compatt   default(   t   import_item(   t   indentt   wrap_paragraphst   dedent(   t	   py3compats   
Arguments that take values are actually convenience aliases to full
Configurables, whose aliases are listed on the help line. For more information
on full configurables, see '--help-all'.
s   
Parameters are set from command-line arguments of the form:
`--Class.trait=value`.
This line is evaluated in Python, so simple expressions are allowed, e.g.::
`--C.a='range(3)'` For setting C.a=[0,1,2].
t   argvt    sp   
Subcommands are launched as `{app} cmd [args]`. For information on using
subcommand 'cmd', do: `{app} cmd -h`.
t-   TRAITLETS_APPLICATION_RAISE_CONFIG_FILE_ERRORt   1t   truet   0t   falses   Unsupported value for environment variable: 'TRAITLETS_APPLICATION_RAISE_CONFIG_FILE_ERROR' is set to '%s' which is none of  {'0', '1', 'false', 'true', ''}.c         O  s   y |  | | |  SWnl t  t f k
 r } | j   | j j d  | j j t |   | j j d | j  | j d  n Xd S(   s  Method decorator for catching invalid config (Trait/ArgumentErrors) during init.

    On a TraitError (generally caused by bad config), this will print the trait's
    message, and exit the app.

    For use on init methods, to prevent invoking excepthook on invalid input.
    s-   Bad config encountered during initialization:s   Config at the time: %si   N(	   R   R
   t
   print_helpt   logt   fatalt   strt   debugt   configt   exit(   t   methodt   appt   argst   kwargst   e(    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   catch_config_errorM   s    	
t   ApplicationErrorc           B  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyR0   `   s   t   LevelFormatterc           B  s&   e  Z d  Z e j Z d Z d   Z RS(   s  Formatter with additional `highlevel` record

    This field is empty if log level is less than highlevel_limit,
    otherwise it is formatted with self.highlevel_format.

    Useful for adding 'WARNING' to warning messages,
    without adding 'INFO' to info, etc.
    s    %(levelname)s |c         C  sG   | j  |  j k r( |  j | j | _ n	 d | _ t t |   j |  S(   NR   (   t   levelnot   highlevel_limitt   highlevel_formatt   __dict__t	   highlevelt   superR3   t   format(   t   selft   record(    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyR:   p   s    	(   R1   R2   t   __doc__t   loggingt   WARNR5   R6   R:   (    (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyR3   d   s   	t   Applicationc           B  s  e  Z d  Z e d  Z e d  Z e e  Z e e  Z e e  Z e	 Z
 e Z e   Z g  Z d   Z e d  Z e   Z e e  Z e d= d e j d d j d e  Z e d  e d     Z e Z  e d d d j d e  Z! e d d d j d e  Z" e d d  e d     Z# e$ d  d    Z% e& i d d  6 Z' e&   Z( e d!  e d"     Z) e&   Z* e+ d# d$ e Z, e e    Z- e+ e. d> i  d d% Z/ e   Z0 d&   Z1 e d  e d'     Z2 e3 d? d(   Z5 d)   Z6 d*   Z7 d+   Z8 d,   Z9 d-   Z: e; d.  Z< d/   Z= d0   Z> d1   Z? d2   Z@ e3 d? d3   ZA d4   ZB e3 d? d5   ZC eD d? d? e; d6   ZE eF d7    ZG e3 d? d8   ZH d9   ZI d:   ZJ d d;  ZK eD d? d<   ZL RS(@   s8   A singleton application with full configuration support.u   applicationu   This is an application.c         c  sn   t    } x^ |  j D]S } xJ t | j    D]6 } t | t  r, | | k r, | j |  | Vq, q, Wq Wd S(   s   Iterate through configurable classes, including configurable parents

        Children should always be after parents, and each class should only be
        yielded once.
        N(   t   sett   classest   reversedt   mrot
   issubclassR   t   add(   R;   t   seent   ct   parent(    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   _classes_inc_parents   s    	u   0.0i    i
   i   i   i(   i2   t   DEBUGt   INFOR?   t   ERRORt   CRITICALt   default_valuet   helps#   Set the log level by value or name.R(   t	   log_levelc         C  sJ   | j  } t | t j  r6 t t |  } | |  _ n  |  j j |  d S(   s+   Adjust the log level when log_level is set.N(	   t   newt
   isinstancet   sixt   string_typest   getattrR>   RQ   R$   t   setLevel(   R;   t   changeRR   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   _log_level_changed   s
    	s   %Y-%m-%d %H:%M:%Ss:   The date format used by logging formatters for %(asctime)ss#   [%(name)s]%(highlevel)s %(message)ss   The Logging format templatet   log_datefmtt
   log_formatc         C  s?   |  j  j d } |  j d |  j d |  j  } | j |  d S(   s0   Change the log formatter when log_format is set.i    t   fmtt   datefmtN(   R$   t   handlerst   _log_formatter_clsR[   RZ   t   setFormatter(   R;   RX   t   _log_handlert   _log_formatter(    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   _log_format_changed   s    R$   c         C  s   t  j |  j j  } | j |  j  t | _ | } x- | rc | j rJ | S| j sW Pq7 | j	 } q7 Wt
 j r t
 j j d  r t  j t t j d   } n t  j   } |  j d |  j d |  j  } | j |  | j |  | S(   s
  Start logging for this application.

        The default is to log to stderr using a StreamHandler, if no default
        handler already exists.  The log level starts at logging.WARN, but this
        can be adjusted by setting the ``log_level`` attribute.
        s   pythonw.exet   wR\   R]   (   R>   t	   getLoggert	   __class__R1   RW   RQ   t   Falset	   propagateR^   RI   t   syst
   executablet   endswitht   StreamHandlert   opent   ost   devnullR_   R[   RZ   R`   t
   addHandler(   R;   R$   t   _logRa   Rb   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   _log_default   s"    				s   Application.log_levels	   log-levelt   flagsc         C  s   | j  } x | j   D] \ } } t |  d k sJ t d | | f   t | d t t f  sy t d | | f   t | d t j  s t d | | f   q Wd S(   s   ensure flags dict is validi   s   Bad flag: %r:%si    i   N(	   RR   t   itemst   lent   AssertionErrorRS   t   dictR	   RT   RU   (   R;   RX   RR   t   keyt   value(    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   _flags_changed   s
    	(/s(   traitlets.config.application.Applicationt
   allow_nones   The subset of our configuration that came from the command-line

        We re-load this configuration after loading config files,
        to ensure that it maintains highest priority.
        c         K  sm   t  j |  |  |  j } | |  j k ri |  j | j k rP | g |  j | _ qi |  j j d |  j  n  d  S(   Ni    (   R   t   __init__Rf   RB   t   insert(   R;   R-   t   cls(    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyR|     s    	c         C  sC   t  t |   j |  |  j j d  |  j j t | j   d  S(   Ns   Config changed:(   R9   R@   t   _config_changedR$   R'   t   reprRR   (   R;   RX   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyR     s    c         C  s   |  j  |  d S(   sM   Do the basic steps to configure me.

        Override in subclasses.
        N(   t   parse_command_line(   R;   R   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt
   initialize#  s    c         C  s    |  j  d k	 r |  j  j   Sd S(   sA   Start the app mainloop.

        Override in subclasses.
        N(   t   subappt   Nonet   start(   R;   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyR   ,  s    c         C  sB  |  j  s d Sg  } i  } x9 |  j D]. } x% | j   d  D] } | | | j <q: Wq# Wx |  j  j   D] \ } } | j d d  \ } } | | } | j d t  | }	 | j |	  j	   }
 |
 d j
 | |  d | |
 d <t |  d k r|
 d j
 d | d	 |  |
 d <n  | j |
  qe Wt t j j |   d S(
   s!   Print the alias part of the help.Nit   .i   R(   i    s    (%s)s   --%s=s   -%s (   t   aliasesRB   RD   R1   Rt   t   splitt   class_traitst   Truet   class_get_trait_helpt
   splitlinest   replaceRu   t   extendt   printRn   t   linesept   join(   R;   t   linest	   classdictR~   RH   t   aliast   longnamet	   classnamet	   traitnamet   traitRP   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   print_alias_help4  s"    	
"%c         C  s   |  j  s d Sg  } xq |  j  j   D]` \ } \ } } t |  d k rM d n d } | j | |  | j t t | j      q# Wt t j	 j
 |   d S(   s    Print the flag part of the help.Ni   s   --t   -(   Rs   Rt   Ru   t   appendR   R   t   stripR   Rn   R   R   (   R;   R   t   mt   cfgRP   t   prefix(    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   print_flag_helpN  s    	"#c         C  s   |  j  r |  j r d  Sd g } | j d t | d   | j d  x1 t |  j  D]  } | j |  | j d  qY Wt t j j	 |   |  j
   |  j   t   d  S(   Nt   OptionsR   i    R   (   Rs   R   R   Ru   R   t   option_descriptionR   Rn   R   R   R   R   (   R;   R   t   p(    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   print_options[  s    	

c         C  s  |  j  s d Sd g } | j d t | d   | j d  x@ t |  j j d |  j   D]  } | j |  | j d  q] WxX |  j  j   D]G \ } \ } } | j |  | r | j t t	 | j
      q q W| j d  t t j j |   d S(   s&   Print the subcommand part of the help.Nt   SubcommandsR   i    R   R+   (   t   subcommandsR   Ru   R   t   subcommand_descriptionR:   t   nameRt   R   R   R   R   Rn   R   R   (   R;   R   R   t   subcR~   RP   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   print_subcommandsi  s    		"&c         C  s   |  j    |  j   |  j   | r |  j } | r| t d  t d  t   x+ t |  j  D] } t |  t   q^ Wn  x3 | D] } | j   t   q Wn t d  t   |  j   d S(   s   Print the help for each Configurable class in self.classes.

        If classes=False (the default), only flags and aliases are printed.
        s   Class parameterss   ----------------s4   To see all available configurables, use `--help-all`N(	   t   print_descriptionR   R   RB   R   R   t   keyvalue_descriptiont   class_print_helpt   print_examples(   R;   RB   t   help_classesR   R~   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyR#   |  s$    


	




c         C  s   d j  d   |  j   D  S(   sw   Generate rST format documentation for the config options this application

        Returns a multiline string.
        s   
c         s  s   |  ] } | j    Vq d  S(   N(   t   class_config_rst_doc(   t   .0RH   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pys	   <genexpr>  s   (   R   RJ   (   R;   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   document_config_options  s    c         C  s/   x( t  |  j  D] } t |  t   q Wd S(   s"   Print the application description.N(   R   t   descriptionR   (   R;   R   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyR     s    
c         C  sQ   |  j  rM t d  t d  t   t t t |  j  j      t   n  d S(   s   Print usage and examples.

        This usage string goes at the end of the command line help string
        and should contain examples of the application's usage.
        t   Exampless   --------N(   t   examplesR   R   R   R   (   R;   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyR     s    	

c         C  s   t  |  j  d S(   s   Print the version string.N(   R   t   version(   R;   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   print_version  s    c         C  so   |  j  j |  \ } } t | t j  r9 t |  } n  |  j j   | j d |   |  _	 |  j	 j
 |  d S(   s"   Initialize a subcommand with argv.RI   N(   R   t   getRS   RT   RU   R   Rf   t   clear_instancet   instanceR   R   (   R;   R   R   R   RP   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   initialize_subcommand  s    c         C  sw  t  t  } xL |  j D]A } | j } x/ | j   d d !D] } | | j j |  q9 Wq Wi  } xw |  j j   D]f \ } } | j d d  \ } } | | }	 t	 |	  d k r |	 d } n  d j
 | | g  | | <qq Wi  }
 x |  j j   D]x \ } \ } } i  } xM | j   D]? \ } } | | }	 t	 |	  d k rK|	 d } n  | | | <qW| | f |
 | <q W|
 | f S(   s~  flatten flags and aliases, so cl-args override as expected.

        This prevents issues such as an alias pointing to InteractiveShell,
        but a config file setting the same trait in TerminalInteraciveShell
        getting inappropriate priority over the command-line arg.

        Only aliases with exactly one descendent in the class list
        will be promoted.

        i   iR   i    (   R   t   listRB   R1   RD   R   R   Rt   R   Ru   R   Rs   (   R;   t   mro_treeR~   t   clsnameRI   R   R   t	   cls_traitR   t   childrenRs   Rx   t   flagdictRP   t   newflagt   subdict(    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   flatten_flags  s,    	
"
c      	     s  | d k r t j d n | } g  | D] } t j |  ^ q& |  _ | rn | d d k rn | d d g } n  |  j r t |  d k r | d | d } } t j d |  r | |  j k r |  j	 | |  Sn  y | | j
 d     Wn t k
 r|   n Xt   f d   d D  rB|  j d   k  |  j d  n  d
   k sZd   k rt|  j   |  j d  n  |  j   \ } } t d | d | d | d |  j  } t | j    |  _ |  j |  j  | j |  _ d S(   s!   Parse the command line arguments.i   i    RP   s   -hs   ^\w(\-?\w)*$s   --c         3  s   |  ] } |   k Vq d  S(   N(    (   R   t   x(   t   interpreted_argv(    s;   lib/python2.7/site-packages/traitlets/config/application.pys	   <genexpr>  s    s
   --help-alls   --helps	   --versions   -VR   R   Rs   R$   N(   s   -hs
   --help-alls   --help(   R   Ri   R   R   t   cast_unicodeR   Ru   t   ret   matchR   t   indext
   ValueErrort   anyR#   R)   R   R   R   R$   R   t   load_configt
   cli_configt   update_configt
   extra_args(   R;   R   t   argR   t   subargvRs   R   t   loader(    (   R   s;   lib/python2.7/site-packages/traitlets/config/application.pyR     s0    %!

c         c  s  t  | t  s | g } n  x| d d d  D]} |  j | d d | d | } | r| | j d | | pu t j    n  |  j | d d | d | } g  } g  } xD| | g D]6}	 d }
 y |	 j   }
 Wn[ t	 k
 r ng t
 k
 r-|	 j p | } | r  n  | rJ| j d | d	 t qJn X| rJ| j d
 |	 j  n  |
 r xi t | |  D]X \ } } | j |
  } | r`| r`| j d j | |	 j t j | d d   q`q`W|
 |	 j f V| j |
  | j |	 j  q q Wq/ Wd S(   se   Load config files (py,json) by filename and path.

        yield each config object in turn.
        Nis   .pyt   pathR$   s   Looking for %s in %ss   .jsons&   Exception while loading config file %st   exc_infos   Loaded config file: %ssM   Collisions detected in {0} and {1} config files. {1} has higher priority: {2}R   i   (   RS   R   t   python_config_loader_classR'   Rn   t   getcwdt   json_config_loader_classR   R   R   t	   Exceptiont   full_filenamet   errorR   t   zipt
   collisionst   warningR:   t   jsont   dumpsR   (   R~   t   basefilenameR   R$   t   raise_config_file_errorst   pyloadert
   jsonloadert   loadedt	   filenamesR   R(   t   filenamet   earlier_configR   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   _load_config_files  sB    "	)c         C  s   |  j  S(   s$   Currently loaded configuration files(   t   _loaded_config_files(   R;   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   loaded_config_filesO  s    c         C  s   t  j j |  \ } } t   } xd |  j | d | d |  j d |  j D]; \ } } | j |  | |  j k rI |  j j	 |  qI qI W| j |  j
  |  j |  d S(   s'   Load config files by filename and path.R   R$   R   N(   Rn   R   t   splitextR	   R   R$   R   t   mergeR   R   R   R   (   R;   R   R   t   extt
   new_configR(   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   load_config_fileT  s    	c         #  s   t  d   |  j   D      f d    xH t ru   j   } t   f d     j   D      | k r. Pq. q. Wx(   j   D] \ } } | r | Vq q Wd S(   s[  
        Yields only classes with own traits, and their subclasses.

        Thus, produced sample config-file will contain all classes
        on which a trait-value may be overridden:

        - either on the class owning the trait,
        - or on its subclasses, even if those subclasses do not define
          any traits themselves.
        c         s  s-   |  ]# } | t  | j d  t   f Vq d S(   R(   N(   t   boolt   class_own_traitsR   (   R   R~   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pys	   <genexpr>o  s   c           s   t    f d   |  j D  S(   Nc         3  s%   |  ] } |   k o   | Vq d  S(   N(    (   R   t   b(   t   cls_to_config(    s;   lib/python2.7/site-packages/traitlets/config/application.pys	   <genexpr>t  s    (   R   t	   __bases__(   R~   (   R   (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   is_any_parent_includeds  s    c         3  s-   |  ]# \ } } | | p!   |  f Vq d  S(   N(    (   R   R~   t   inc_yes(   R   (    s;   lib/python2.7/site-packages/traitlets/config/application.pys	   <genexpr>{  s   N(   R   RJ   R   t   copyRt   (   R;   t   to_incl_origt   clR   (    (   R   R   s;   lib/python2.7/site-packages/traitlets/config/application.pyt   _classes_in_config_sampled  s    		c         C  sT   d |  j  g } | j d  x' |  j   D] } | j | j    q* Wd j |  S(   s/   generate default config file from Configurabless   # Configuration file for %s.R   s   
(   R   R   R   t   class_config_sectionR   (   R;   R   R~   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   generate_config_file  s
    c         C  s(   |  j  j d |  j  t j |  d  S(   Ns   Exiting application: %s(   R$   R'   R   Ri   R)   (   R;   t   exit_status(    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyR)     s    c         K  s*   |  j  |   } | j |  | j   d S(   s   Launch a global instance of this Application

        If a global instance already exists, this reinitializes and starts it
        N(   R   R   R   (   R~   R   R-   R+   (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   launch_instance  s    (   i    i
   i   i   i(   i2   RK   RL   R?   RM   RN   (    N(M   R1   R2   R=   R   R   R   R   R   R   R   R   R   R   R   RB   RJ   R   R   R   R   R   R   R   R>   R?   t   tagR   RQ   R   R   RY   R3   R_   RZ   R[   Rc   R   Rr   R   R   Rs   Rz   R   R   R   R   R	   R   R   R|   R   R/   R   R   R   R   R   R   R   Rg   R#   R   R   R   R   R   R   R   t   classmethodR   t   propertyR   R   R   R   R)   R   (    (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyR@   x   s   																													-*/	 	c         C  s   | p d | } | p d | } | j  d  \ } } i i t | 6| 6} i i t | 6| 6} i | | f |  6| | f d |  6S(   s  Helper for building basic --trait, --no-trait flags.

    Parameters
    ----------

    name : str
        The name of the flag.
    configurable : str
        The 'Class.trait' string of the trait to be set/unset with the flag
    set_help : unicode
        help string for --name flag
    unset_help : unicode
        help string for --no-name flag

    Returns
    -------

    cfg : dict
        A dict with two keys: 'name', and 'no-name', for setting and unsetting
        the trait, respectively.
    s   set %s=Trues   set %s=FalseR   s   no-(   R   R   Rg   (   R   t   configurablet   set_helpt
   unset_helpR~   R   t   settert   unsetter(    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   boolean_flag  s    c           C  s$   t  j   r t  j   j St   Sd S(   s|   Get the config object for the global Application instance, if there is one

    otherwise return an empty config object
    N(   R@   t   initializedR   R(   R	   (    (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt
   get_config  s    (B   R=   t
   __future__R    R   R   R   R>   Rn   R   Ri   t   collectionsR   R   R   t   traitlets.config.configurableR   R   t   traitlets.config.loaderR   R   R	   R
   R   R   t   traitlets.traitletsR   R   R   R   R   R   R   R   R   R   t   ipython_genutils.importstringR   t   ipython_genutils.textR   R   R   t   ipython_genutilsR   RT   R   R   R   t   hasattrR   R   t   environR   t   _envvart   lowerR   R   Rg   R   R/   R   R0   t	   FormatterR3   R@   R  R
  (    (    (    s;   lib/python2.7/site-packages/traitlets/config/application.pyt   <module>   sF   .F			  (!