ó
‹²,]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 m Z m Z m Z d d l m Z d d l m Z d d l m Z m Z m 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 for Spyder

It's based on the ConfigParser module (present in the standard library).
iÿÿÿÿ(   t   print_functionN(   t   get_conf_patht   get_home_dirt   get_module_source_path(   t   check_version(   t   configparser(   t   PY2t   is_text_stringt   to_text_stringt	   NoDefaultc           B  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyR	   &   s   t   DefaultsConfigc           B  sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   sT   
    Class used to save defaults to a file and as base class for
    UserConfig
    c         C  sE   t  r t j j |  ƒ n t j j |  d d  ƒ| |  _ | |  _ d  S(   Nt   interpolation(   R   t   cpt   ConfigParsert   __init__t   Nonet   namet	   subfolder(   t   selfR   R   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyR   2   s
    	c         C  sA  |  j  rv | j d t j ƒ xF |  j  j ƒ  D]5 \ } } | j d | t | ƒ j d d ƒ f ƒ q- W| j d ƒ n  xÄ |  j D]¹ } | j d | ƒ x’ |  j | j ƒ  D]} \ } } | d k rÉ q« n  | d k	 sç |  j	 |  j
 k rt | ƒ } d j | | j d d ƒ f ƒ } n  | j d | ƒ q« W| j d ƒ q€ Wd S(	   sz   
        Private write method for Python 2
        The one from configparser fails for non-ascii Windows accounts
        s   [%s]
s   %s = %s
s   
s   
	R
   s    = s   %s
N(   t	   _defaultst   writeR   t   DEFAULTSECTt   itemst   strt   replacet	   _sectionsR   t   _optcret   OPTCRER   t   join(   R   t   fpt   keyt   valuet   section(    (    s1   lib/python2.7/site-packages/spyder/config/user.pyt   _write:   s    	- $c         C  sw   |  j  | ƒ s |  j | ƒ n  t | ƒ s: t | ƒ } n  | rZ t d | | | f ƒ n  t j j |  | | | ƒ d S(   s$   
        Private set method
        s   %s[ %s ] = %sN(   t   has_sectiont   add_sectionR   t   reprt   printR   R   t   set(   R   R"   t   optionR!   t   verbose(    (    s1   lib/python2.7/site-packages/spyder/config/user.pyt   _setO   s    c           s¥   ˆ  j  ƒ  } ‡  f d †  } y | | ƒ Wnu t k
 r  y: t j | ƒ r[ t j | ƒ n  t j d ƒ | | ƒ Wq¡ t k
 rœ } t	 d ƒ t	 | ƒ 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(   R   t   codecst   openR#   R   (   t   fnamet
   configfile(   R   (    s1   lib/python2.7/site-packages/spyder/config/user.pyt   _write_filec   s
    gš™™™™™©?sO   Failed to write user configuration file to disk, with the exception shown belowN(
   t   filenamet   EnvironmentErrort   ospt   isfilet   ost   removet   timet   sleept	   ExceptionR'   (   R   R0   R2   t   e(    (   R   s1   lib/python2.7/site-packages/spyder/config/user.pyt   _save[   s    

c         C  s`   t  |  d d ƒ |  _ t  |  d d ƒ |  _ |  j d k rR |  j d k rR |  j ƒ  S|  j ƒ  Sd S(   s2   Defines the name of the configuration file to use.t	   _filenamet
   _root_pathN(   t   getattrR   R>   R?   t   _filename_globalt   _filename_projects(   R   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyR3   z   s
    
c         C  s   t  j |  j |  j ƒ S(   s·   Create a .ini filename located in the current project directory.
        This .ini files stores the specific project preferences for each
        project created with spyder.
        (   R5   R   R?   R>   (   R   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRB   †   s    c         C  sœ   |  j  d k r/ t j t ƒ  d |  j ƒ } | St ƒ  } d |  j k r{ t j | d ƒ } t j | ƒ s{ t j	 | ƒ q{ n  t j | d |  j ƒ } | Sd S(   s}   Create a .ini filename located in user home directory.
        This .ini files stores the global spyder preferences.
        s   .%s.init   defaultss   %s.iniN(
   R   R   R5   R   R   R   R   t   isdirR7   t   mkdir(   R   t   config_filet   folder(    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRA      s    	c         C  sL   xE | D]= \ } } x. | D]& } | | } |  j  | | | t ƒ q Wq Wd  S(   N(   R+   t   False(   R   RC   R"   t   optionsR)   t	   new_value(    (    s1   lib/python2.7/site-packages/spyder/config/user.pyt   set_defaultsž   s    
(   R
   R   t   __doc__R   R#   R+   R=   R3   RB   RA   RK   (    (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyR   -   s   							t
   UserConfigc           B  sé   e  Z d  Z d Z d e d d e e e d „ Z d d „ Z d e d „ Z	 d „  Z
 d „  Z 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¥  
    UserConfig class, based on ConfigParser
    name: name of the config
    defaults: dictionnary containing options
              *or* list of tuples (section_name, options)
    version: version of the configuration file (X.Y.Z format)
    subfolder: configuration file will be saved in %home%/subfolder/%name%.ini
    
    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	 r_ t j d | ƒ d  k r_ t d | ƒ ‚ n  t | t ƒ rƒ |  j	 | f g } n  | |  _
 | d  k	 r« |  j d t ƒ n  |  j ƒ  }	 | rì y t j |	 d |	 ƒ Wqì t k
 rè qì Xn  | r|  j ƒ  |  j | ƒ }
 d „  } d „  } |  j | | | ƒ | | ƒ | |
 ƒ k rü| rƒy t j |	 d	 |	 |
 f ƒ Wqƒt k
 rqƒXn  t |
 d
 d ƒ r¨|  j d t ƒ n |  j | |
 ƒ | sÖ| | ƒ | |
 ƒ k ræ|  j |
 ƒ n  |  j | d t ƒn  | d  k r|  j ƒ  qn  d  S(   Ni   i    s   ^(\d+).(\d+).(\d+)$s8   Version number %r is incorrect - must be in X.Y.Z formatt   saves   %s.bakc         S  s   |  |  j  d ƒ  S(   Nt   .(   t   find(   t   _t(    (    s1   lib/python2.7/site-packages/spyder/config/user.pyt   <lambda>Ì   t    c         S  s   |  |  j  d ƒ  S(   NRP   (   t   rfind(   RR   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRS   Í   RT   s	   %s-%s.baks   2.4.0t   <(   R   R   t   rawR   t   ret   matcht
   ValueErrort
   isinstancet   dictt   DEFAULT_SECTION_NAMERC   t   reset_to_defaultsRH   R3   t   shutilt   copyfilet   IOErrort   load_from_init   get_versiont   _save_new_defaultsR   t   _update_defaultst   _remove_deprecated_optionst   set_versiont   set_as_defaults(   R   R   RC   t   loadt   versionR   t   backupt   raw_modet   remove_obsoleteR0   t   old_vert   _majort   _minor(    (    s1   lib/python2.7/site-packages/spyder/config/user.pyR   µ   sH    	
		s   0.0.0c         C  s   |  j  |  j d | ƒ S(   s/   Return configuration (not application!) versionRj   (   t   getR]   (   R   Rj   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRc   ä   s    c         C  s    |  j  |  j d | d | ƒd S(   s,   Set configuration (not application!) versionRj   RO   N(   R(   R]   (   R   Rj   RO   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRg   è   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   readfpRa   R'   t   readR   t   MissingSectionHeaderError(   R   R0   R1   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRb   ì   s    c         C  sy   t  j ƒ  } t | d d ƒ r- t d ƒ } n t j |  j ƒ  ƒ } t j | d ƒ } | j t j | d | d ƒ ƒ | S(   s   Read old defaultss   3.0.0s   <=t   spyderRC   s	   defaults-s   .ini(	   R   R   R   R   R5   t   dirnameR3   R   Rs   (   R   t   old_versiont   old_defaultst   path(    (    s1   lib/python2.7/site-packages/spyder/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 defaultsR   s	   defaults-R   N(   R   R5   R6   R3   RK   R=   (   R   RC   t   new_versionR   t   new_defaults(    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRd     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 versionN(   Rz   Rq   R   t   NoSectionErrort   NoOptionErrorR   R   R+   (
   R   RC   Rw   R*   Rx   R"   RI   R)   RJ   t	   old_value(    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRe     s    

c         C  sÚ   |  j  | ƒ } xÄ | j ƒ  D]¶ } x­ | j | d |  j ƒD]“ \ } } |  j | | ƒ t k r; yH |  j | | ƒ t |  j | d |  j ƒƒ d k r¦ |  j | ƒ n  WqÎ t	 j
 k
 rÊ |  j | ƒ qÎ Xq; q; Wq Wd S(   sW   
        Remove options which are present in the .ini file but not in defaults
        RW   i    N(   Rz   t   sectionsR   RW   t   get_defaultR	   t   remove_optiont   lent   remove_sectionR   R}   (   R   Rw   Rx   R"   R)   t   _(    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRf   !  s    %$c         C  s   t  j |  j ƒ  ƒ d S(   s7   
        Remove .ini file associated to config
        N(   R7   R8   R3   (   R   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyt   cleanup0  s    c         C  ss   g  |  _  xc |  j ƒ  D]U } i  } x0 |  j | d |  j ƒD] \ } } | | | <q; W|  j  j | | f ƒ q Wd S(   s6   
        Set defaults from the current config
        RW   N(   RC   R€   R   RW   t   append(   R   R"   t   secdictR)   R!   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRh   6  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(   s0   
        Reset config to Default values
        N(   RC   R   R+   R=   (   R   RO   R*   R"   t   secRI   R)   R!   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyR^   A  s    
!c         C  sR   | d k r |  j } n t | ƒ s3 t d ƒ ‚ n  t | ƒ sN t d ƒ ‚ n  | S(   sB   
        Private method to check section and option types
        s#   Argument 'section' must be a strings"   Argument 'option' must be a stringN(   R   R]   R   t   RuntimeError(   R   R"   R)   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyt   _check_section_optionM  s    c         C  sW   |  j  | | ƒ } x> |  j D]/ \ } } | | k r | | k rK | | Sq q Wt Sd S(   su   
        Get Default value for a given (section, option)
        -> useful for type checking in 'get' method
        N(   R‹   RC   R	   (   R   R"   R)   R‰   RI   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyR   Y  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 | ƒ } n´ t | ƒ r³t ràyU | j d ƒ } y( t j | ƒ } t | ƒ rw| } n  Wn t t f k
 r‘n XWq°t t f k
 r¬q°Xqàn- y t j | ƒ } Wn t t f 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)
        RW   s   utf-8(   R‹   R$   R	   R   R}   R%   t
   has_optionR~   R(   R   Rq   RW   R   R[   t   boolt   astt   literal_evalt   floatt   intR   R   t   decodet   SyntaxErrorRZ   t   UnicodeEncodeErrort   UnicodeDecodeError(   R   R"   R)   t   defaultR!   t   default_valueRJ   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRq   f  sF    !
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‹   RC   (   R   R"   R)   R—   R‰   RI   (    (    s1   lib/python2.7/site-packages/spyder/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(   sV   
        Set an option
        section=None: attribute a default section name
        N(   R‹   R   R	   R   R   R&   R˜   R[   R   R   R‘   R+   R=   (   R   R"   R)   R!   R*   RO   R—   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyR(   ¤  s$    c         C  s!   t  j j |  | ƒ |  j ƒ  d  S(   N(   R   R   R„   R=   (   R   R"   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyR„   ¿  s    c         C  s$   t  j j |  | | ƒ |  j ƒ  d  S(   N(   R   R   R‚   R=   (   R   R"   R)   (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyR‚   Ã  s    N(   R
   R   RL   R]   R   t   TrueRH   R   Rc   Rg   Rb   Rz   Rd   Re   Rf   R†   Rh   R^   R‹   R   R	   Rq   R˜   R(   R„   R‚   (    (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyRM   ¨   s,   		-								4	
	(    (   RL   t
   __future__R    RŽ   R7   t   os.pathRy   R5   RX   R_   R9   t   spyder.config.baseR   R   R   t   spyder.utils.programsR   t   spyder.py3compatR   R   R   R   R   R.   R	   R   R   RM   (    (    (    s1   lib/python2.7/site-packages/spyder/config/user.pyt   <module>   s    {