
NXc           @  s  d  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	 d d l
 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 d d
 l m Z d d l  m! Z! m" Z" m# Z# d d l$ m% Z% d d l& m' Z' m( Z( d j)   Z* d j)   Z+ e, e d  r}d g e _- n  d Z. e d    Z/ d e0 f d     YZ1 d e j2 f d     YZ3 d e f d     YZ4 d d d  Z5 d   Z6 d S(   s,   A base class for a configurable application.i(   t   print_functionN(   t   deepcopy(   t   defaultdict(   t	   decorator(   t   Configurablet   SingletonConfigurable(   t   KVArgParseConfigLoadert   PyFileConfigLoadert   Configt   ArgumentErrort   ConfigFileNotFoundt   JSONFileConfigLoader(   t   Unicodet   Listt   Enumt   Dictt   Instancet
   TraitError(   t   import_item(   t   indentt   wrap_paragraphst   dedent(   t	   py3compat(   t   string_typest	   iteritemss   
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`.
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(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   catch_config_errorA   s    	
t   ApplicationErrorc           B  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyR(   T   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   superR+   t   format(   t   selft   record(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyR2   c   s    	(   R)   R*   t   __doc__t   loggingt   WARNR-   R.   R2   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyR+   W   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 d7 d e j d e d d Z d   Z e Z e d d e d d Z d   Z e d d e d d Z d   Z e e j   Z! d   Z" e# i d d 6 Z$ e#   Z% d   Z& e#   Z' e d d  e Z( e e  Z) d!   Z* d"   Z+ e, d8 d#   Z. d$   Z/ d%   Z0 d&   Z1 d'   Z2 d(   Z3 e4 d)  Z5 d*   Z6 d+   Z7 d,   Z8 d-   Z9 d.   Z: e, d8 d/   Z; d0   Z< e, d8 d1   Z= e> d8 d8 d2   Z? e, d8 d3   Z@ d4   ZA d d5  ZB e> d8 d6   ZC RS(9   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(   R3   t   seent   ct   parent(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   _classes_inc_parents   s    	u   0.0i    i
   i   i   i(   i2   t   DEBUGt   INFOR7   t   ERRORt   CRITICALt   default_valueR    t   helps#   Set the log level by value or name.c         C  s>   t  | t  r* t t |  } | |  _ n  |  j j |  d S(   s+   Adjust the log level when log_level is set.N(   t
   isinstanceR   t   getattrR6   t	   log_levelR   t   setLevel(   R3   t   namet   oldt   new(    (    sT   /data/av2000/mvv/env_mvv/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)sc         C  s   |  j  d |  j |  j  d  S(   Nt
   log_format(   t   _log_format_changedRQ   (   R3   RM   RN   RO   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   _log_datefmt_changed   s    s#   [%(name)s]%(highlevel)s %(message)ss   The Logging format templatec         C  s<   |  j  j d } |  j d | d |  j  } | j |  d S(   s0   Change the log formatter when log_format is set.i    t   fmtt   datefmtN(   R   t   handlerst   _log_formatter_clst   log_datefmtt   setFormatter(   R3   RM   RN   RO   t   _log_handlert   _log_formatter(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyRR      s    c         C  s   t  j |  j j  } | j |  j  t | _ | } x- | rc | j rJ | S| j sW Pq7 | j	 } q7 Wt
 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   wRT   RU   (   R6   t	   getLoggert	   __class__R)   RL   RK   t   Falset	   propagateRV   RA   t   syst
   executablet   endswitht   StreamHandlert   opent   ost   devnullRW   RQ   RX   RY   t
   addHandler(   R3   R   t   _logRZ   R[   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   _log_default   s"    				s   Application.log_levels	   log-levelc         C  s   x t  |  D] \ } } t |  d k sA t d | | f   t | d t t f  sp t d | | f   t | d t  s t d | | f   q Wd S(   s   ensure flags dict is validi   s   Bad flag: %r:%si    i   N(   R   t   lent   AssertionErrorRI   t   dictR   R   (   R3   RM   RN   RO   t   keyt   value(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   _flags_changed   s    (/s(   traitlets.config.application.Applicationt
   allow_nonec         K  s?   t  j |  |  |  j |  j k r; |  j j d |  j  n  d  S(   Ni    (   R   t   __init__R^   R:   t   insert(   R3   R%   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyRr      s    c         C  s@   t  j |  | | |  |  j j d  |  j j t |   d  S(   Ns   Config changed:(   R   t   _config_changedR   R   t   repr(   R3   RM   RN   RO   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyRt      s    c         C  s   |  j  |  d S(   sM   Do the basic steps to configure me.

        Override in subclasses.
        N(   t   parse_command_line(   R3   R   (    (    sT   /data/av2000/mvv/env_mvv/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(   R3   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyRz     s    c         C  sB  |  j  s d Sg  } i  } x9 |  j D]. } x% | j   d  D] } | | | j <q: Wq# Wx t |  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   aliasesR:   R<   R)   R   t   splitt   class_traitst   Truet   class_get_trait_helpt
   splitlinest   replaceRk   t   extendt   printRf   t   linesept   join(   R3   t   linest	   classdictt   clsR@   t   aliast   longnamet	   classnamet	   traitnamet   traitRH   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   print_alias_help  s"    	
"%c         C  s   |  j  s d Sg  } xq t |  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   -(   t   flagsR   Rk   t   appendR   R   t   stripR   Rf   R   R   (   R3   R   t   mt   cfgRH   t   prefix(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   print_flag_help(  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   (   R   R|   R   Rk   R   t   option_descriptionR   Rf   R   R   R   R   (   R3   R   t   p(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   print_options5  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 t |  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   Rk   R   t   subcommand_descriptionR2   RM   R   R   R   R   R   Rf   R   R   (   R3   R   R   t   subcR   RH   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   print_subcommandsC  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   R:   R   R   t   keyvalue_descriptiont   class_print_helpt   print_examples(   R3   R:   t   help_classesR   R   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyR   V  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   .0R@   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pys	   <genexpr>w  s   (   R   RB   (   R3   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   document_config_optionsr  s    c         C  s/   x( t  |  j  D] } t |  t   q Wd S(   s"   Print the application description.N(   R   t   descriptionR   (   R3   R   (    (    sT   /data/av2000/mvv/env_mvv/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   (   R3   (    (    sT   /data/av2000/mvv/env_mvv/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(   R3   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   print_version  s    c         C  s)   t  |  j  } | j |  | |  _ d S(   s2   Fire the traits events when the config is updated.N(   R   R    t   merge(   R3   R    t	   newconfig(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   update_config  s    c         C  so   |  j  j |  \ } } t | t  r6 t |  } n  |  j j   | j d |  j  |  _	 |  j	 j
 |  d S(   s"   Initialize a subcommand with argv.R    N(   R   t   getRI   R   R   R^   t   clear_instancet   instanceR    Rx   Rw   (   R3   R   R   Rx   RH   (    (    sT   /data/av2000/mvv/env_mvv/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 t |  j  D]f \ } } | j d d  \ } } | | }	 t	 |	  d k r |	 d } n  d j
 | | g  | | <qq Wi  }
 x t |  j  D]x \ } \ } } i  } xM t |  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   listR:   R)   R<   R   R   R|   R}   Rk   R   R   (   R3   t   mro_treeR   t   clsnameRA   R|   R   t	   cls_traitR   t   childrenR   Rn   t   flagdictRH   t   newflagt   subdict(    (    sT   /data/av2000/mvv/env_mvv/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  } | j   } |  j |  | j |  _ d S(   s!   Parse the command line arguments.i   i    RH   s   -hs   ^\w(\-?\w)*$s   --c         3  s   |  ] } |   k Vq d  S(   N(    (   R   t   x(   t   interpreted_argv(    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pys	   <genexpr>  s    s
   --help-alls   --helps	   --versions   -VR   R|   R   R   N(   s   -hs
   --help-alls   --help(   Ry   Ra   R   R   t   cast_unicodeR   Rk   t   ret   matchR   t   indext
   ValueErrort   anyR   R!   R   R   R   R   t   load_configR   t
   extra_args(	   R3   R   t   argR   t   subargvR   R|   t   loaderR    (    (   R   sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyRv     s0    %!

c   	      c  sb  t  | t  s | g } n  x:| d d d  D]%} |  j | d d | d | } | rp | j d | |  n  |  j | d d | d | } | r | j d | |  n  d } x | | g D] } y | j   } WnO t k
 r n[ t k
 r%| j	 p | } | rB| j
 d	 | d
 t qBn X| rB| j d | j	  n  | r | Vq q Wq/ Wt  d S(   se   Load config files (py,json) by filename and path.

        yield each config object in turn.
        Nis   .pyt   pathR   s/   Attempting to load config file %s.py in path %ss   .jsons1   Attempting to load config file %s.json in path %ss&   Exception while loading config file %st   exc_infos   Loaded config file: %s(   RI   R   t   python_config_loader_classR   t   json_config_loader_classRy   R   R
   t	   Exceptiont   full_filenamet   errorR   t   StopIteration(	   R   t   basefilenameR   R   t   pyloadert
   jsonloaderR    R   t   filename(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   _load_config_files  s2    	c         C  s   t  j j |  \ } } g  } x@ |  j | d | d |  j D]  } | j |  |  j |  q= Wt |  d k r | d j | d  } | r |  j j	 d j
 | t j | d d   q n  d S(	   s'   Load config files by filename and path.R   R   i   i    sZ   Collisions detected in {0}.py and {0}.json config files. {0}.json has higher priority: {1}R   i   N(   Rf   R   t   splitextR   R   R   R   Rk   t
   collisionst   warningR2   t   jsont   dumps(   R3   R   R   t   extt   loadedR    R   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   load_config_file+  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   
(   RM   R   RB   t   class_config_sectionR   (   R3   R   R   (    (    sT   /data/av2000/mvv/env_mvv/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   RM   Ra   R!   (   R3   t   exit_status(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyR!   D  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   Rw   Rz   (   R   R   R%   R#   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   launch_instanceH  s    (   i    i
   i   i   i(   i2   s   DEBUGs   INFOs   WARNs   ERRORRF   N(D   R)   R*   R5   R   RM   R   R   R   R   R   R   R   R   R   R:   RB   R   R   R   R   R6   R7   R   RK   RP   R+   RW   RX   RS   RQ   RR   R   t   LoggerR   Rj   R   R|   R   Rp   R   Rx   R   Rr   Rt   R'   Ry   Rw   Rz   R   R   R   R   R_   R   R   R   R   R   R   R   R   Rv   t   classmethodR   R   R   R!   R   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyR8   k   sz   																												
	-*'	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   R_   (   RM   t   configurablet   set_helpt
   unset_helpR   R   t   settert   unsetter(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   boolean_flagV  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(   R8   t   initializedR   R    R   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt
   get_configw  s    (7   R5   t
   __future__R    R   R6   Rf   R   Ra   t   copyR   t   collectionsR   R   t   traitlets.config.configurableR   R   t   traitlets.config.loaderR   R   R   R	   R
   R   t   traitlets.traitletsR   R   R   R   R   R   t   ipython_genutils.importstringR   t   ipython_genutils.textR   R   R   t   ipython_genutilsR   t   ipython_genutils.py3compatR   R   R   R   R   t   hasattrR   R   R'   R   R(   t	   FormatterR+   R8   R   R   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/traitlets/config/application.pyt   <module>   s8   .. !