
\[\s                 @   s$  d  Z  d d l Z d d l Z d d l m Z Gd d   d e  Z Gd d   d e  Z Gd d	   d	 e  Z Gd
 d   d e  Z	 Gd d   d e  Z
 Gd d   d e
  Z Gd d   d  Z e   Z e d k r d d   Z e e j  e e j  e e j d j d d   n  d S)a  Provides access to stored IDLE configuration information.

Refer to the comments at the beginning of config-main.def for a description of
the available configuration files and the design implemented to update user
configuration information.  In particular, user configuration choices which
duplicate the defaults will be removed from the user's configuration files,
and if a file becomes empty, it will be deleted.

The contents of the user files may be altered using the Options/Configure IDLE
menu to access the configuration GUI (configDialog.py), or manually.

Throughout this module there is an emphasis on returning useable defaults
when a problem occurs in returning a requested configuration value back to
idle. This is to allow IDLE to continue to function in spite of errors in
the retrieval of config information. When a default is returned instead of
a requested config value, a message is printed to stderr to aid in
configuration problem notification and resolution.
    N)ConfigParserc               @   s   e  Z d  Z d S)InvalidConfigTypeN)__name__
__module____qualname__ r   r   +/usr/lib/python3.4/idlelib/configHandler.pyr      s    r   c               @   s   e  Z d  Z d S)InvalidConfigSetN)r   r   r   r   r   r   r   r	      s    r	   c               @   s   e  Z d  Z d S)InvalidFgBgN)r   r   r   r   r   r   r   r
      s    r
   c               @   s   e  Z d  Z d S)InvalidThemeN)r   r   r   r   r   r   r   r      s    r   c               @   sR   e  Z d  Z d Z d d d  Z d d d d d  Z d d	   Z d
 d   Z d S)IdleConfParserzI
    A ConfigParser specialised for idle configuration file handling
    Nc             C   s&   | |  _  t j |  d | d d d S)zK
        cfgFile - string, fully specified configuration file name
        ZdefaultsstrictFN)filer   __init__)selfcfgFileZcfgDefaultsr   r   r   r   #   s    	zIdleConfParser.__init__Fc             C   sh   |  j  | |  s | S| d k r2 |  j | |  S| d k rN |  j | |  S|  j | | d | Sd S)z
        Get an option value for given section/option or return default.
        If type is specified, return as type.
        boolintrawN)
has_optionZ
getbooleanZgetintget)r   sectionoptiontypedefaultr   r   r   r   Get*   s    zIdleConfParser.Getc             C   s$   |  j  |  r |  j |  Sg  Sd S)z4Return a list of options for given section, else [].N)has_sectionoptions)r   r   r   r   r   GetOptionList8   s    zIdleConfParser.GetOptionListc             C   s   |  j  |  j  d S)z&Load the configuration file from disk.N)readr   )r   r   r   r   Load?   s    zIdleConfParser.Load)r   r   r   __doc__r   r   r   r    r   r   r   r   r      s
   r   c               @   sj   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z	 d d   Z
 d S)IdleUserConfParserzG
    IdleConfigParser specialised for user configuration handling.
    c             C   s#   |  j  |  s |  j |  n  d S)z!If section doesn't exist, add it.N)r   add_section)r   r   r   r   r   
AddSectionH   s    zIdleUserConfParser.AddSectionc             C   s:   x3 |  j    D]% } |  j |  s |  j |  q q Wd S)z)Remove any sections that have no options.N)sectionsr   Zremove_section)r   r   r   r   r   RemoveEmptySectionsM   s    z&IdleUserConfParser.RemoveEmptySectionsc             C   s   |  j    |  j   S)z9Return True if no sections after removing empty sections.)r&   r%   )r   r   r   r   IsEmptyS   s    
zIdleUserConfParser.IsEmptyc             C   s#   |  j  |  r |  j | |  Sd S)zReturn True if option is removed from section, else False.

        False if either section does not exist or did not have option.
        F)r   Zremove_option)r   r   r   r   r   r   RemoveOptionX   s    zIdleUserConfParser.RemoveOptionc             C   s   |  j  | |  rH |  j | |  | k r. d S|  j | | |  d Sn6 |  j |  sg |  j |  n  |  j | | |  d Sd S)zReturn True if option is added or changed to value, else False.

        Add section if required.  False means option already had value.
        FTN)r   r   setr   r#   )r   r   r   valuer   r   r   	SetOptiona   s    zIdleUserConfParser.SetOptionc             C   s,   t  j j |  j  r( t  j |  j  n  d S)z9Remove user config file self.file from disk if it exists.N)ospathexistsr   remove)r   r   r   r   
RemoveFiler   s    zIdleUserConfParser.RemoveFilec             C   s   |  j    sv |  j } y t | d  } Wn. t k
 rX t j |  t | d  } Yn X|  |  j |  Wd QXn
 |  j   d S)zUpdate user configuration file.

        Remove empty sections. If resulting config isn't empty, write the file
        to disk. If config is empty, remove the file from disk if it exists.

        wN)r'   r   openOSErrorr,   unlinkwriter0   )r   Zfnamer   r   r   r   Savew   s    	zIdleUserConfParser.SaveN)r   r   r   r!   r$   r&   r'   r(   r+   r0   r6   r   r   r   r   r"   C   s   	r"   c               @   s]  e  Z d  Z d Z d d   Z d d   Z d d   Z d d d	 d
 d d  Z d d   Z d d   Z	 d d d  Z
 d d   Z d d   Z d d   Z d	 d
 d
 d d  Z d d   Z d d   Z d d    Z d! d"   Z d# d$   Z d% d&   Z d' d(   Z d) d*   Z d+ d,   Z d d- d.  Z d/ d0   Z d1 d2   Z d3 d4   Z d5 d6   Z d S)7IdleConfal  Hold config parsers for all idle config files in singleton instance.

    Default config files, self.defaultCfg --
        for config_type in self.config_types:
            (idle install dir)/config-{config-type}.def

    User config files, self.userCfg --
        for config_type in self.config_types:
        (user home dir)/.idlerc/config-{config-type}.cfg
    c             C   s<   d |  _  i  |  _ i  |  _ i  |  _ |  j   |  j   d  S)Nmain
extensions	highlightkeys)zmainz
extensionsz	highlightzkeys)config_types
defaultCfguserCfgcfgCreateConfigHandlersLoadCfgFiles)r   r   r   r   r      s    				
zIdleConf.__init__c             C   s   t  d k r! t j j t  } n t j j t j d  } |  j   } i  } i  } xS |  j D]H } t j j	 | d | d  | | <t j j	 | d | d  | | <q\ Wx? |  j D]4 } t
 | |  |  j | <t | |  |  j | <q Wd S)z5Populate default and user config parser dictionaries.__main__r   zconfig-z.defz.cfgN)r   r,   r-   dirname__file__abspathsysGetUserCfgDirr<   joinr   r=   r"   r>   )r   ZidleDiruserDirZdefCfgFilesZusrCfgFilesZcfgTyper   r   r   r@      s    		zIdleConf.CreateConfigHandlersc             C   s  d } t  j j d  } | d k r| t  j j |  s| d | d } y t | d t j Wn t k
 ro Yn Xd } q| n  | d k r t  j   } n  t  j j	 | |  } t  j j |  sy t  j
 |  Wqt k
 r
d | d } t | d t j t  YqXn  | S)zfReturn a filesystem directory for storing user config files.

        Creates it if required.
        z.idlerc~z.
 Warning: os.path.expanduser("~") points to
 z,
 but the path does not exist.r   z2
 Warning: unable to create user config directory
z(
 Check path and permissions.
 Exiting!
)r,   r-   
expanduserr.   printrF   stderrr3   getcwdrH   mkdir
SystemExit)r   ZcfgDirrI   warnr   r   r   rG      s*    zIdleConf.GetUserCfgDirNTFc       	   !   C   sQ  y@ |  j  | j | |  r? |  j  | j | | d | d | SWnm t k
 r d | | | |  j  | j | | d | f } y t | d t j Wn t k
 r Yn XYn Xy@ |  j | j | |  r |  j | j | | d | d | SWn t k
 rYn X| rMd | | | f } y t | d t j WqMt k
 rIYqMXn  | S)a  Return a value for configType section option, or default.

        If type is not None, return a value of that type.  Also pass raw
        to the config parser.  First try to return a valid value
        (including type) from a user configuration. If that fails, try
        the default configuration. If that fails, return default, with a
        default of None.

        Warn if either user or default configurations have an invalid value.
        Warn if default is returned and warn_on_default is True.
        r   r   zu
 Warning: configHandler.py - IdleConf.GetOption -
 invalid %r value for configuration option %r
 from section %r: %rr   z
 Warning: configHandler.py - IdleConf.GetOption -
 problem retrieving configuration option %r
 from section %r.
 returning default value: %r)	r>   r   r   
ValueErrorrL   rF   rM   r3   r=   )	r   
configTyper   r   r   r   warn_on_defaultr   warningr   r   r   	GetOption   s4    	#
zIdleConf.GetOptionc             C   s   |  j  | j | | |  d S)z0Set section option to value in user config file.N)r>   r+   )r   rS   r   r   r*   r   r   r   r+      s    zIdleConf.SetOptionc             C   sl   | |  j  k r t d   n  | d k r: |  j | } n( | d k rV |  j | } n t d   | j   S)zReturn sections for configSet configType configuration.

        configSet must be either 'user' or 'default'
        configType must be in self.config_types.
        zInvalid configType specifieduserr   zInvalid configSet specified)r<   r   r>   r=   r	   r%   )r   	configSetrS   	cfgParserr   r   r   GetSectionList  s    zIdleConf.GetSectionListc             C   s   |  j  d j |  r+ |  j d |  } n |  j d |  } | | d } | d k rd | d } n | | d } i | d 6| d	 6} | s | S| d
 k r | d S| d k r | d	 St d   d S)a  Return individual highlighting theme elements.

        fgBg - string ('fg'or'bg') or None, if None return a dictionary
        containing fg and bg colours (appropriate for passing to Tkinter in,
        e.g., a tag_config call), otherwise fg or bg colour only as specified.
        r:   r   rW   z-foregroundZcursorznormal-backgroundz-backgroundZ
foregroundZ
backgroundZfgZbgzInvalid fgBg specifiedN)r=   r   GetThemeDictr
   )r   themeelementZfgBgZ	themeDictZforeZbackr:   r   r   r   GetHighlight  s    zIdleConf.GetHighlightc             C   s  | d k r |  j  d } n( | d k r8 |  j d } n t d   i d d 6d d 6d d	 6d d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d  6d d! 6d d" 6} x | D] } | j | |  sld# | | | | f } y t | d$ t j Wqlt k
 rhYqlXn  | j | | d | | } | | | <qW| S)%a)  Return {option:value} dict for elements in themeName.

        type - string, 'default' or 'user' theme type
        themeName - string, theme name
        Values are loaded over ultimate fallback defaults to guarantee
        that all theme elements are present in a newly created theme.
        rW   r:   r   zInvalid theme type specifiedz#000000znormal-foregroundz#ffffffznormal-backgroundzkeyword-foregroundzkeyword-backgroundzbuiltin-foregroundzbuiltin-backgroundzcomment-foregroundzcomment-backgroundzstring-foregroundzstring-backgroundzdefinition-foregroundzdefinition-backgroundzhilite-foregroundZgrayzhilite-backgroundzbreak-foregroundzbreak-backgroundzhit-foregroundzhit-backgroundzerror-foregroundzerror-backgroundzcursor-foregroundzstdout-foregroundzstdout-backgroundzstderr-foregroundzstderr-backgroundzconsole-foregroundzconsole-backgroundz
 Warning: configHandler.py - IdleConf.GetThemeDict -
 problem retrieving theme element %r
 from theme %r.
 returning default value: %rr   )	r>   r=   r   r   rL   rF   rM   r3   r   )r   r   Z	themeNamerY   r\   r]   rU   Zcolourr   r   r   r[   /  sV    

zIdleConf.GetThemeDictc             C   s   |  j  d d d d d S)z.Return the name of the currently active theme.r8   Themenamer    )rV   )r   r   r   r   CurrentThemeo  s    zIdleConf.CurrentThemec             C   s   |  j  d d d d d S)z0Return the name of the currently active key set.r8   ZKeysr`   r   ra   )rV   )r   r   r   r   CurrentKeyss  s    zIdleConf.CurrentKeysc       	      C   s  |  j  |  j d d   } |  j  |  j d d   } x* | D]" } | | k r= | j |  q= q= W| rg  } x | D] } |  j d | d d d d d rv | s | r | r d } n d	 } |  j d | | d d d d d
 d r| j |  qq| j |  qv qv W| S| Sd S)zReturn extensions in default and user config-extensions files.

        If active_only True, only return active (enabled) extensions
        and optionally only editor or shell extensions.
        If active_only False, return all extensions.
        r   r9   rW   enableTr   r   Zenable_editorZenable_shellrT   FN)RemoveKeyBindNamesrZ   appendrV   )	r   active_onlyZeditor_onlyZ
shell_onlyZextnsZ	userExtnsextnactiveExtnsr   r   r   r   GetExtensionsw  s.    			zIdleConf.GetExtensionsc             C   sq   | } g  } x6 | D]. } | j  d  r | j | j |   q q W| j d d  x | D] } | | =q\ W| S)z:Return extnNameList with keybinding section names removed.	_bindings_cfgBindingsreverseT)rk   rl   )endswithrf   indexsort)r   ZextnNameListnamesZkbNameIndiciesr`   ro   r   r   r   re     s    zIdleConf.RemoveKeyBindNamesc             C   sd   d } d | d } xI |  j  d d  D]5 } x, |  j |  D] } | | k r= | } q= q= Wq' W| S)zReturn the name of the extension binding virtualEvent, or None.

        virtualEvent - string, name of the virtual event to test for,
                       without the enclosing '<< >>'
        Nz<<z>>rg   r   )rj   GetExtensionKeys)r   virtualEventZextNameZvEventrh   eventr   r   r   GetExtnNameForEvent  s    zIdleConf.GetExtnNameForEventc       	      C   s   | d } |  j    } i  } |  j d j |  r~ |  j d j |  } x3 | D]( } d | d } | | } | | | <qO Wn  | S)a  Return dict: {configurable extensionName event : active keybinding}.

        Events come from default config extension_cfgBindings section.
        Keybindings come from GetCurrentKeySet() active key dict,
        where previously used bindings are disabled.
        rl   r9   z<<z>>)GetCurrentKeySetr=   r   r   )	r   extensionNamekeysNameZ
activeKeysextKeys
eventNames	eventNamert   bindingr   r   r   rr     s    

zIdleConf.GetExtensionKeysc             C   s   | d } i  } |  j  d j |  r |  j  d j |  } xJ | D]? } |  j d | | d d j   } d | d } | | | <qC Wn  | S)a  Return dict {configurable extensionName event : keybinding list}.

        Events come from default config extension_cfgBindings section.
        Keybindings list come from the splitting of GetOption, which
        tries user config before default config.
        rl   r9   r   ra   z<<z>>)r=   r   r   rV   split)r   rw   rx   ry   rz   r{   r|   rt   r   r   r   Z__GetRawExtensionKeys  s    
!zIdleConf.__GetRawExtensionKeysc             C   s   | d } |  j  |  } |  j d j |  r |  j d j |  } xJ | D]? } |  j d | | d d j   } d | d } | | | <qL Wn  | S)a  Return dict {extensionName event : active or defined keybinding}.

        Augment self.GetExtensionKeys(extensionName) with mapping of non-
        configurable events (from default config) to GetOption splits,
        as in self.__GetRawExtensionKeys.
        rk   r9   r   ra   z<<z>>)rr   r=   r   r   rV   r}   )r   rw   Z	bindsNameZextBindsrz   r{   r|   rt   r   r   r   GetExtensionBindings  s    
!zIdleConf.GetExtensionBindingsc             C   s5   | d d  } |  j  d | | d d j   } | S)zReturn the keybinding list for keySetName eventStr.

        keySetName - name of key binding set (config-keys section).
        eventStr - virtual event, including brackets, as in '<<event>>'.
           r;   r   ra   )rV   r}   )r   
keySetNameZeventStrr{   r|   r   r   r   GetKeyBinding  s    !zIdleConf.GetKeyBindingc             C   st   |  j  |  j    } t j d k rp xI | j   D]8 \ } } d d   | D } | | k r1 | | | <q1 q1 Wn  | S)z/Return CurrentKeys with 'darwin' modifications.darwinc             S   s"   g  |  ] } | j  d  d   q S)z<Alt-z<Option-)replace).0xr   r   r   
<listcomp>  s   	 z-IdleConf.GetCurrentKeySet.<locals>.<listcomp>)	GetKeySetrc   rF   platformitems)r   resultkvZv2r   r   r   rv     s    zIdleConf.GetCurrentKeySetc             C   s   |  j  |  } |  j d d  } xh | D]` } |  j |  } | r( xB | D]7 } | | | j   k rs d | | <n  | | | | <qJ Wq( q( W| S)zReturn event-key dict for keySetName core plus active extensions.

        If a binding defined in an extension is already in use, the
        extension binding is disabled by being set to ''
        rg      ra   )GetCoreKeysrj   _IdleConf__GetRawExtensionKeysvalues)r   r   ZkeySetri   rh   ry   rt   r   r   r   r   	  s    zIdleConf.GetKeySetc             C   s   d | d |  j    k S)zReturn True if the virtual event is one of the core idle key events.

        virtualEvent - string, name of the virtual event to test for,
                       without the enclosing '<< >>'
        z<<z>>)r   )r   rs   r   r   r   IsCoreBinding  s    zIdleConf.IsCoreBindingc             C   s  i1 d d g d 6d d g d 6d d g d	 6d
 d g d 6d g d 6d g d 6d g d 6d g d 6d g d 6d g d 6d g d 6d g d 6d g d 6d g d 6d g d  6d! g d" 6d# g d$ 6d% g d& 6d' g d( 6d) g d* 6d+ g d, 6d- g d. 6d/ g d0 6d1 g d2 6d3 g d4 6d5 g d6 6d7 g d8 6d9 g d: 6d; g d< 6d= g d> 6d? d@ g dA 6dB g dC 6dD g dE 6dF g dG 6dH g dI 6dJ g dK 6dL g dM 6dN dO g dP 6dQ g dR 6dS g dT 6dU g dV 6dW g dX 6dY g dZ 6d[ g d\ 6d] g d^ 6d_ g d` 6da g db 6dc g dd 6de g df 6} | rxy | D]n } |  j  | |  } | r:| | | <qdg | | | | f } y t | dh t j Wqt k
 r|YqXqWn  | S)ia  Return dict of core virtual-key keybindings for keySetName.

        The default keySetName None corresponds to the keyBindings base
        dict. If keySetName is not None, bindings from the config
        file(s) are loaded _over_ these defaults, so if there is a
        problem getting any core binding there will be an 'ultimate last
        resort fallback' to the CUA-ish bindings defined here.
        z<Control-c>z<Control-C>z<<copy>>z<Control-x>z<Control-X>z<<cut>>z<Control-v>z<Control-V>z	<<paste>>z<Control-a>z<Home>z<<beginning-of-line>>z<Control-l>z<<center-insert>>z<Control-q>z<<close-all-windows>>z<Alt-F4>z<<close-window>>z<<do-nothing>>z<Control-d>z<<end-of-file>>z<F1>z<<python-docs>>z
<Shift-F1>z<<python-context-help>>z<Alt-n>z<<history-next>>z<Alt-p>z<<history-previous>>z<<interrupt-execution>>z<F6>z<<view-restart>>z<Control-F6>z<<restart-shell>>z<Alt-c>z<<open-class-browser>>z<Alt-m>z<<open-module>>z<Control-n>z<<open-new-window>>z<Control-o>z<<open-window-from-file>>z<Control-j>z<<plain-newline-and-indent>>z<Control-p>z<<print-window>>z<Control-y>z<<redo>>z<Escape>z<<remove-selection>>z<Alt-Shift-S>z<<save-copy-of-window-as-file>>z<Alt-s>z<<save-window-as-file>>z<Control-s>z<<save-window>>z<Alt-a>z<<select-all>>z<Control-slash>z<<toggle-auto-coloring>>z<Control-z>z<<undo>>z<Control-g>z<F3>z<<find-again>>z<Alt-F3>z<<find-in-files>>z<Control-F3>z<<find-selection>>z<Control-f>z<<find>>z<Control-h>z<<replace>>z<Alt-g>z<<goto-line>>z<Key-BackSpace>z<<smart-backspace>>z<Key-Return>z<Key-KP_Enter>z<<newline-and-indent>>z	<Key-Tab>z<<smart-indent>>z<Control-Key-bracketright>z<<indent-region>>z<Control-Key-bracketleft>z<<dedent-region>>z<Alt-Key-3>z<<comment-region>>z<Alt-Key-4>z<<uncomment-region>>z<Alt-Key-5>z<<tabify-region>>z<Alt-Key-6>z<<untabify-region>>z<Alt-Key-t>z<<toggle-tabs>>z<Alt-Key-u>z<<change-indentwidth>>z<Control-Key-BackSpace>z<<del-word-left>>z<Control-Key-Delete>z<<del-word-right>>z
 Warning: configHandler.py - IdleConf.GetCoreKeys -
 problem retrieving key binding for event %r
 from key set %r.
 returning default value: %rr   )r   rL   rF   rM   r3   )r   r   ZkeyBindingsrt   r|   rU   r   r   r   r   &  s|    	









































zIdleConf.GetCoreKeysc       	      C   s  g  } | d k r" |  j  d } n( | d k r> |  j d } n t d   | j d  } x | D] } | j d | d d } | j d  d k r d } d } n/ | j d  } | d	 j   } | d j   } | r` | r` | j | | | f  q` q` W| j	 d
 d d    | S)a  Return list of extra help sources from a given configSet.

        Valid configSets are 'user' or 'default'.  Return a list of tuples of
        the form (menu_item , path_to_help_file , option), or return the empty
        list.  'option' is the sequence number of the help resource.  'option'
        values determine the position of the menu items on the Help menu,
        therefore the returned list must be sorted by 'option'.

        rW   r8   r   zInvalid configSet specifiedZ	HelpFiles;r   ra   r   keyc             S   s   |  d S)Nr   r   )r   r   r   r   <lambda>  s    z1IdleConf.GetExtraHelpSourceList.<locals>.<lambda>)
r>   r=   r	   r   r   findr}   striprf   rp   )	r   rX   ZhelpSourcesrY   r   r   r*   ZmenuItemZhelpPathr   r   r   GetExtraHelpSourceLists  s&    
	zIdleConf.GetExtraHelpSourceListc             C   s    |  j  d  |  j  d  } | S)zReturn a list of the details of all additional help sources.

        Tuples in the list are those of GetExtraHelpSourceList.
        r   rW   )r   )r   ZallHelpSourcesr   r   r   GetAllExtraHelpSourcesList  s    z#IdleConf.GetAllExtraHelpSourcesListc             C   s:   x3 |  j  D]( } |  j  | j   |  j | j   q
 Wd S)zLoad all configuration files.N)r=   r    r>   )r   r   r   r   r   rA     s    zIdleConf.LoadCfgFilesc             C   s)   x" |  j  D] } |  j  | j   q
 Wd S)z2Write all loaded user configuration files to disk.N)r>   r6   )r   r   r   r   r   SaveUserCfgFiles  s    zIdleConf.SaveUserCfgFiles)r   r   r   r!   r   r@   rG   rV   r+   rZ   r^   r[   rb   rc   rj   re   ru   rr   r   r~   r   rv   r   r   r   r   r   rA   r   r   r   r   r   r7      s6   
	-@#
M 	r7   rB   c          	   C   s   t  d |  d  x |  D] } |  | j   } t  |  t  |  xf | D]^ } |  | j |  } t  |  t  |  x. | D]& } t  | d |  | j | |   q| WqH Wq Wd  S)N
=)rL   r%   r   r   )r?   r   r%   r   r   r   r   r   r   dumpCfg  s    



r   r8   r_   r`   )r!   r,   rF   Zconfigparserr   	Exceptionr   r	   r
   r   r   r"   r7   ZidleConfr   r   r=   r>   rL   r   r   r   r   r   <module>   s$   $G   	