ó
Ò:\c           @  s  d  Z  d d l m Z d d l Z d d l Z d d l j 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 m Z e
 j d d k Z e rË d d l Z n  d	 d d
 „  ƒ  YZ d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s   
This module provides user configuration file management features.

It's based on the ConfigParser module (present in the standard library).
iÿÿÿÿ(   t   print_functionN(   t   get_home_dir(   t   configparser(   t   is_text_stringt   to_text_stringi    t   2t	   NoDefaultc           B  s   e  Z d  Z RS(   s   NoDefault object.(   t   __name__t
   __module__t   __doc__(    (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyR   -   s   t   DefaultsConfigc           B  sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s&   Class used to save defaults to a file.c         C  s&   t  j j |  ƒ | |  _ | |  _ d S(   së   
        Class used to save defaults to a file.

        Parameters
        -----------
        name: str
            Name of the configuration.
        subfolder: str
            Path to folder location of configuration file.
        N(   t   cpt   ConfigParsert   __init__t   namet	   subfolder(   t   selfR   R   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyR   9   s    	c         C  sd  |  j  rŒ | j d j d t j ƒ ƒ xT |  j  j ƒ  D]C \ } } t | ƒ j d d ƒ } | j d j d | d | ƒ ƒ q5 W| j d ƒ n  xÑ |  j D]Æ } | j d j d | ƒ ƒ x— |  j | j ƒ  D]‚ \ } } | d k rç qÉ n  | d k	 s|  j
 |  j k r5t | ƒ } d	 j | | j d d ƒ f ƒ } n  | j d
 j | ƒ ƒ qÉ W| j d ƒ q– Wd S(   st   Private write method for Python 2.

        The one from configparser fails for non-ascii Windows accounts.
        s   [{section}]
t   sections   
s   
	s   {key} = {value}
t   keyt   valueR   s    = s   {0}
N(   t	   _defaultst   writet   formatR   t   DEFAULTSECTt   itemst   strt   replacet	   _sectionst   Nonet   _optcret   OPTCRER   t   join(   R   t   fpR   R   t   write_valueR   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   _writeH   s"    	 $c         C  sy   |  j  | ƒ s |  j | ƒ n  t | ƒ s: t | ƒ } n  | r\ t d j | | | ƒ ƒ n  t j j |  | | | ƒ d S(   s   Private set method.s   {section}[ {option} ] = {value}N(	   t   has_sectiont   add_sectionR   t   reprt   printR   R   R   t   set(   R   R   t   optionR   t   verbose(    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   _set`   s    c           s«   ˆ  j  ƒ  } ‡  f d †  } y | | ƒ Wn{ t k
 r¦ y: t j | ƒ r[ t j | ƒ n  t j d ƒ | | ƒ Wq§ t k
 r¢ } t	 d ƒ t	 d ƒ | ‚ q§ Xn Xd S(   s*   Save config into the associated .ini file.c           sf   t  r7 t j |  d d d ƒ } ˆ  j | ƒ Wd  QXn+ t |  d d d ƒ } ˆ  j | ƒ Wd  QXd  S(   Nt   wt   encodings   utf-8(   t   PY2t   codecst   openR"   R   (   t   fnamet
   configfile(   R   (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   _write_fileq   s
    gš™™™™™©?s(   Failed to write user configuration file.s   Please submit a bug report.N(
   t   filenamet   IOErrort   ospt   isfilet   ost   removet   timet   sleept	   ExceptionR&   (   R   R0   R2   t   e(    (   R   s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   _savem   s    

c         C  s†   t  j t ƒ  |  j ƒ } d |  j k r< t  j | d ƒ } n  y t j | ƒ Wn t j k
 rc n Xt  j | d j |  j ƒ ƒ } | S(   s6   Create a .ini filename located in user home directory.t   defaultss   {0}.ini(	   R5   R   R   R   R   R7   t   makedirst   errorR   (   R   t   foldert   ini_file(    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyR3   †   s    c         C  sL   xE | D]= \ } } x. | D]& } | | } |  j  | | | t ƒ q Wq Wd S(   s   Set default configuration.N(   R*   t   False(   R   R>   R   t   optionsR(   t	   new_value(    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   set_defaults•   s    
(	   R   R   R	   R   R"   R*   R=   R3   RF   (    (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyR
   6   s   					t
   UserConfigc           B  s  e  Z d  Z d Z d e d d e e e d „ Z e d „  ƒ Z	 e d „  ƒ Z
 d d „ Z d e d „ Z d „  Z d	 „  Z e d
 „  ƒ Z e d „ Z d „  Z d „  Z d „  Z e e d d „ Z d „  Z d „  Z e d „ Z d „  Z e e d „ Z d „  Z d „  Z RS(   sƒ   
    User configuration class.

    Note that 'get' and 'set' arguments number and type differ from the
    overriden methods.
    t   mainc	         C  sý  t  j |  | | ƒ | r d n d |  _ | d k	 rd t j d | ƒ d k rd t d j | ƒ ƒ ‚ n  t | t	 ƒ rˆ |  j
 | f g } n  | |  _ | d k	 r° |  j d t ƒ n  |  j ƒ  }	 | rö y t j |	 d j |	 ƒ ƒ Wqö t k
 rò qö Xn  | rù|  j ƒ  |  j | ƒ }
 |  j | | | ƒ |  j | ƒ |  j |
 ƒ k rÝ| rƒy  t j |	 d j |	 |
 ƒ ƒ Wqƒt k
 rqƒXn  |  j | |
 ƒ | s·|  j | ƒ |  j |
 ƒ k rÇ|  j |
 ƒ n  |  j | d t ƒn  | d k rù|  j ƒ  qùn  d S(	   s¹  
        User configuration class.

        Parameters
        ----------
        name: str
            Name of the configuration.
        defaults: dict
            dictionnary containing options *or* list of tuples (section_name,
            options).
        version: str
            Version of the configuration file (X.Y.Z format).
        subfolder: str
            Configuration file will be saved in HOME/subfolder/name.ini.
        i   i    s   ^(\d+).(\d+).(\d+)$s9   Version number {0} is incorrect - must be in X.Y.Z formatt   saves   {0}.baks   {0}-{1}.bakN(   R
   R   t   rawR   t   ret   matcht
   ValueErrorR   t
   isinstancet   dictt   DEFAULT_SECTION_NAMER>   t   reset_to_defaultsRC   R3   t   shutilt   copyfileR4   t   load_from_init   get_versiont   _UserConfig__save_new_defaultst   _minort   _UserConfig__update_defaultst   _majort&   _UserConfig__remove_deprecated_optionst   set_versiont   set_as_defaults(   R   R   R>   t   loadt   versionR   t   backupt   raw_modet   remove_obsoleteR0   t   old_ver(    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyR   ª   sH    	
c         C  s   |  |  j  d ƒ  S(   s*   Return major component in config versions.t   .(   t   find(   t   _t(    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyRY   ù   s    c         C  s   |  |  j  d ƒ  S(   s*   Return minor component in config versions.Rc   (   t   rfind(   Re   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyRW   þ   s    s   0.0.0c         C  s   |  j  |  j d | ƒ S(   s0   Return configuration (not application!) version.R^   (   t   getRP   (   R   R^   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyRU     s    c         C  s    |  j  |  j d | d | ƒd S(   s-   Set configuration (not application!) version.R^   RI   N(   R'   RP   (   R   R^   RI   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyR[     s    c         C  s¹   y” t  rz |  j ƒ  } t j | ƒ r“ y/ t j | d d ƒ } |  j | ƒ Wd QXWqw t k
 rs t d | ƒ qw Xq“ n |  j	 |  j ƒ  d d ƒWn t
 j k
 r´ t d ƒ n Xd S(   s*   Load config from the associated .ini file.R,   s   utf-8Ns   Failed reading files*   Warning: File contains no section headers.(   R-   R3   R5   R6   R.   R/   t   readfpR4   R&   t   readR   t   MissingSectionHeaderError(   R   R0   t	   conf_file(    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyRT     s    c         C  sX   t  j ƒ  } t j |  j ƒ  ƒ } t j | d ƒ } | j t j | d | d ƒ ƒ | S(   s   Read old defaults.R>   s	   defaults-s   .ini(   R   R   R5   t   dirnameR3   R   Ri   (   R   t   old_versiont   old_defaultst   path(    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   __load_old_defaults  s
    !c         C  sL   t  d d | d | ƒ } t j | j ƒ  ƒ sH | j |  ƒ | j ƒ  n  d S(   s   Save new defaults.R   s	   defaults-R   N(   R
   R5   R6   R3   RF   R=   (   R>   t   new_versionR   t   new_defaults(    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   __save_new_defaults#  s    
	c   
      C  s¸   |  j  | ƒ } x¢ | D]š \ } } x‹ | D]ƒ } | | } y | j | | ƒ }	 Wn# t j t j f k
 rt d }	 n X|	 d k s“ t | ƒ |	 k r) |  j | | | | ƒ q) q) Wq Wd S(   s*   Update defaults after a change in version.N(   t   _UserConfig__load_old_defaultsRg   R   t   NoSectionErrort   NoOptionErrorR   R   R*   (
   R   R>   Rm   R)   Rn   R   RD   R(   RE   t	   old_value(    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   __update_defaults.  s    

c         C  s²   |  j  | ƒ } xœ | j ƒ  D]Ž } x… | j | d |  j ƒD]k \ } } |  j | | ƒ t k r; |  j | | ƒ t |  j | d |  j ƒƒ d k r¦ |  j | ƒ q¦ q; q; Wq Wd S(   s<   Remove options present in the .ini file but not in defaults.RJ   i    N(	   Rt   t   sectionsR   RJ   t   get_defaultR   t   remove_optiont   lent   remove_section(   R   Rm   Rn   R   R(   t   _(    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   __remove_deprecated_options<  s    %$c         C  s   t  j |  j ƒ  ƒ d S(   s&   Remove .ini file associated to config.N(   R7   R8   R3   (   R   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   cleanupF  s    c         C  ss   g  |  _  xc |  j ƒ  D]U } i  } x0 |  j | d |  j ƒD] \ } } | | | <q; W|  j  j | | f ƒ q Wd S(   s%   Set defaults from the current config.RJ   N(   R>   Ry   R   RJ   t   append(   R   R   t   secdictR(   R   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyR\   J  s    	%c         C  s}   xc |  j  D]X \ } } | d k s. | | k r
 x1 | D]& } | | } |  j | | | | ƒ q5 Wq
 q
 W| ry |  j ƒ  n  d S(   s   Reset config to Default values.N(   R>   R   R*   R=   (   R   RI   R)   R   t   secRD   R(   R   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyRQ   S  s    
!c         C  sR   | d k r |  j } n t | ƒ s3 t d ƒ ‚ n  t | ƒ sN t d ƒ ‚ n  | S(   s1   Private method to check section and option types.s#   Argument 'section' must be a strings"   Argument 'option' must be a stringN(   R   RP   R   t   RuntimeError(   R   R   R(   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   __check_section_option]  s    c         C  sT   |  j  | | ƒ } x; |  j D], \ } } | | k r | | k r | | Sq Wt Sd S(   sw   
        Get Default value for a given (section, option).

        -> useful for type checking in 'get' method
        N(   t!   _UserConfig__check_section_optionR>   R   (   R   R   R(   Rƒ   RD   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyRz   g  s
    c         C  s™  |  j  | | ƒ } |  j | ƒ sO | t k r? t j | ƒ ‚ qO |  j | ƒ n  |  j | | ƒ sœ | t k r‚ t j | | ƒ ‚ qœ |  j | | | ƒ | Sn  t j	 j
 |  | | d |  j ƒ} |  j | | ƒ } t | t ƒ rð t j | ƒ } n¥ t | t ƒ rt | ƒ } n‡ t | t ƒ r,t | ƒ } ni t rnt | ƒ rny | j d ƒ } Wqnt t f k
 rjqnXn  y t j | ƒ } Wn t k
 r”n X| S(   sÇ   
        Get an option.

        section=None: attribute a default section name
        default: default value (if not specified, an exception
        will be raised if option doesn't exist)
        RJ   s   utf-8(   R†   R#   R   R   Ru   R$   t
   has_optionRv   R'   R   Rg   RJ   Rz   RN   t   boolt   astt   literal_evalt   floatt   intR-   R   t   decodet   UnicodeEncodeErrort   UnicodeDecodeErrorR;   (   R   R   R(   t   defaultR   t   default_value(    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyRg   t  s8    !c         C  sI   |  j  | | ƒ } x0 |  j D]% \ } } | | k r | | | <q q Wd S(   s   
        Set Default value for a given (section, option).

        -> called when a new (section, option) is set and no default exists
        N(   R†   R>   (   R   R   R(   R‘   Rƒ   RD   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   set_default¡  s    c         C  s  |  j  | | ƒ } |  j | | ƒ } | t k rm t rQ t | ƒ rQ t | ƒ } n  | } |  j | | | ƒ n  t | t ƒ r‹ t | ƒ } nW t | t	 ƒ r© t	 | ƒ } n9 t | t
 ƒ rÇ t
 | ƒ } n t | ƒ sâ t | ƒ } n  |  j | | | | ƒ | r|  j ƒ  n  d S(   sX   
        Set an option.

        section=None: attribute a default section name
        N(   R†   Rz   R   R-   R   R%   R’   RN   Rˆ   R‹   RŒ   R*   R=   (   R   R   R(   R   R)   RI   R‘   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyR'   ¬  s$    c         C  s!   t  j j |  | ƒ |  j ƒ  d S(   s4   Remove the specified section from the configuration.N(   R   R   R}   R=   (   R   R   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyR}   É  s    c         C  s$   t  j j |  | | ƒ |  j ƒ  d S(   s7   Remove the specified option from the specified section.N(   R   R   R{   R=   (   R   R   R(   (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyR{   Î  s    N(   R   R   R	   RP   R   t   TrueRC   R   t   staticmethodRY   RW   RU   R[   RT   Rt   RV   RX   RZ   R€   R\   RQ   R†   Rz   R   Rg   R’   R'   R}   R{   (    (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyRG       s8   F			
			
	
	-		(    (   R	   t
   __future__R    R‰   R7   t   os.pathRo   R5   RK   RR   t   sysR9   t   anaconda_navigator.config.baseR   t"   anaconda_navigator.utils.py3compatR   R   R   R   R^   R-   R.   R   R   R
   RG   (    (    (    s=   lib/python2.7/site-packages/anaconda_navigator/config/user.pyt   <module>   s"   	j