a
    ze                     @   s   d dl mZ d dlZd dlZd dlmZ d dlZG dd deZG dd deZ	G dd	 d	eZ
G d
d deZG dd deZG dd dZe Ze Zdd ZG dd deZdd Zedkrd dlmZ edddd dS )    )ConfigParserN)Fontc                   @   s   e Zd ZdS )InvalidConfigTypeN__name__
__module____qualname__ r	   r	   $/usr/lib/python3.9/idlelib/config.pyr   #       r   c                   @   s   e Zd ZdS )InvalidConfigSetNr   r	   r	   r	   r
   r   $   r   r   c                   @   s   e Zd ZdS )InvalidThemeNr   r	   r	   r	   r
   r   %   r   r   c                   @   s0   e Zd ZdddZdddZdd Zd	d
 ZdS )IdleConfParserNc                 C   s   || _ tj| |dd d S )NF)defaultsstrict)filer   __init__)selfcfgFileZcfgDefaultsr	   r	   r
   r   +   s    zIdleConfParser.__init__Fc                 C   sL   |  ||s|S |dkr$| ||S |dkr8| ||S | j|||dS d S )Nboolintraw)
has_optionZ
getbooleanZgetintget)r   sectionoptiontypedefaultr   r	   r	   r
   Get2   s    zIdleConfParser.Getc                 C   s   |  |r| |S g S d S N)has_sectionoptionsr   r   r	   r	   r
   GetOptionListC   s    

zIdleConfParser.GetOptionListc                 C   s   | j r| | j  d S r    )r   readr   r	   r	   r
   LoadJ   s    zIdleConfParser.Load)N)NNF)r   r   r   r   r   r$   r'   r	   r	   r	   r
   r   '   s   

r   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )IdleUserConfParserc                 C   s^   |  ||r4| |||kr dS | ||| dS n&| |sH| | | ||| dS d S )NFT)r   r   setr!   add_section)r   r   r   valuer	   r	   r
   	SetOptionT   s    

zIdleUserConfParser.SetOptionc                 C   s   |  |r| ||S dS )NF)r!   Zremove_option)r   r   r   r	   r	   r
   RemoveOptione   s    
zIdleUserConfParser.RemoveOptionc                 C   s   |  |s| | d S r    )r!   r*   r#   r	   r	   r
   
AddSectionn   s    
zIdleUserConfParser.AddSectionc                 C   s&   |   D ]}| |s| | qd S r    )sectionsr$   remove_sectionr#   r	   r	   r
   RemoveEmptySectionss   s    
z&IdleUserConfParser.RemoveEmptySectionsc                 C   s   |    |   S r    )r1   r/   r&   r	   r	   r
   IsEmptyy   s    zIdleUserConfParser.IsEmptyc                 C   s   | j }|r|d dkr|  szt|d}W n& tyR   t| t|d}Y n0 | | | W d    q1 sx0    Y  ntj| j rt	| j  d S )Nr   #w)
r   r2   openOSErrorosunlinkwritepathexistsremove)r   Zfnamer   r	   r	   r
   Save~   s    
*zIdleUserConfParser.SaveN)	r   r   r   r,   r-   r.   r1   r2   r=   r	   r	   r	   r
   r(   O   s   	r(   c                   @   s   e Zd Z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 Z
dd Zdd Zdd Zedd Z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h d0Zd@d1d2Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z dS )AIdleConfFc                 C   s0   d| _ i | _i | _i | _|s,|   |   d S )N)main	highlightkeys
extensions)config_types
defaultCfguserCfgcfgCreateConfigHandlersLoadCfgFiles)r   Z_utestr	   r	   r
   r      s    zIdleConf.__init__c                 C   sx   t jt}tjrdn|   | _}| jD ]H}t	t j
|d| d| j|< tt j
|p\dd| d| j|< q*d S )N zconfig-z.defr3   z.cfg)r7   r:   dirname__file__idlelibtestingGetUserCfgDiruserdirrC   r   joinrD   r(   rE   )r   ZidledirrO   Zcfg_typer	   r	   r
   rG      s    

zIdleConf.CreateConfigHandlersc                 C   s   d}t jd}|dkr`t j|s`tjs\d| d }zt|tjd W n t	yZ   Y n0 d}|dkrpt 
 }t j||}t j|szt | W nN t	y   tjsd| d }zt|tjd W n t	y   Y n0 tY n0 |S )Nz.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!
)r7   r:   
expanduserr;   rL   rM   printsysstderrr6   getcwdrP   mkdir
SystemExit)r   ZcfgDirZuserDirwarnr	   r	   r
   rN      sD    
zIdleConf.GetUserCfgDirNTc           	      C   s   z0| j | ||r.| j | j||||dW S W nB tyr   d|||| j | j|||df }t|||| Y n0 z0| j| ||r| j| j||||dW S W n ty   Y n0 |rd|||f }t|||| |S )N)r   r   zn
 Warning: config.py - IdleConf.GetOption -
 invalid %r value for configuration option %r
 from section %r: %rr   z
 Warning: config.py - IdleConf.GetOption -
 problem retrieving configuration option %r
 from section %r.
 returning default value: %r)rE   r   r   
ValueError_warnrD   )	r   
configTyper   r   r   r   warn_on_defaultr   warningr	   r	   r
   	GetOption   s2    
zIdleConf.GetOptionc                 C   s   | j | ||| d S r    )rE   r,   )r   r]   r   r   r+   r	   r	   r
   r,      s    zIdleConf.SetOptionc                 C   sJ   || j vrtd|dkr&| j| }n|dkr:| j| }ntd| S )NzInvalid configType specifieduserr   Invalid configSet specified)rC   r   rE   rD   r   r/   )r   	configSetr]   	cfgParserr	   r	   r
   GetSectionList  s    
zIdleConf.GetSectionListc                 C   sR   | j d |rdnd}| ||}||d  }|dkr<d}||d  }||dS )	Nr@   r   ra   z-foregroundZcursornormalz-background)Z
foregroundZ
background)rD   r!   GetThemeDict)r   themeelementrF   Z
theme_dictZforeZbackr	   r	   r
   GetHighlight  s    zIdleConf.GetHighlightc                  C   s   |dkr| j d }n|dkr(| jd }ntddddddddddddddddddddddddddddddddd}|D ]R}|||s|d	sd
|||| f }t|d|| |j|||| d||< qx|S )Nra   r@   r   zInvalid theme type specifiedz#000000z#ffffffZgray)znormal-foregroundznormal-backgroundzkeyword-foregroundzkeyword-backgroundzbuiltin-foregroundzbuiltin-backgroundzcomment-foregroundzcomment-backgroundzstring-foregroundzstring-backgroundzdefinition-foregroundzdefinition-backgroundzhilite-foregroundzhilite-backgroundzbreak-foregroundzbreak-backgroundzhit-foregroundzhit-backgroundzerror-foregroundzerror-backgroundzcontext-foregroundzcontext-backgroundzlinenumber-foregroundzlinenumber-backgroundzcursor-foregroundzstdout-foregroundzstdout-backgroundzstderr-foregroundzstderr-backgroundzconsole-foregroundzconsole-background)zcontext-zlinenumber-z{
 Warning: config.IdleConf.GetThemeDict -
 problem retrieving theme element %r
 from theme %r.
 returning default color: %rr   )rE   rD   r   r   
startswithr\   r   )r   r   Z	themeNamerd   rh   ri   r_   r	   r	   r
   rg   !  sb    "
zIdleConf.GetThemeDictc                 C   s
   |  dS )NThemecurrent_colors_and_keysr&   r	   r	   r
   CurrentThemee  s    zIdleConf.CurrentThemec                 C   s
   |  dS )NZKeysrn   r&   r	   r	   r
   CurrentKeysi  s    zIdleConf.CurrentKeysc                 C   s   |dkrdnd}| j d|dddd}d	}|r>| j d|d
d	d}|sT| j d|dd	d}|rz|rb| jn| j}|| |rz|S |dkrdS |  S )Nrm   r@   rA   r?   r   r   Tr   r   rI   Zname2rk   namezIDLE Classic)r`   rD   rE   r!   default_keys)r   r   Zcfgnamer   rs   sourcer	   r	   r
   ro   m  s    
z IdleConf.current_colors_and_keysc                   C   s,   t jd d dkrdS t jdkr$dS dS d S )N   winzIDLE Classic WindowsdarwinzIDLE Classic OSXzIDLE Modern Unix)rU   platformr	   r	   r	   r
   rt     s
    
zIdleConf.default_keysc           	   	   C   s   |  | dd}|  | dd}|D ]}||vr(|| q(dD ]}|| qD|rg }|D ]Z}| jd|ddddr`|s|r|rd	}nd
}| jd||ddddr|| q`|| q`|S |S d S )Nr   rB   ra   )ZAutoCompleteZCodeContextZFormatParagraphZ
ParenMatchenableTr   r   r   Zenable_editorZenable_shellF)r   r   r^   )RemoveKeyBindNamesre   appendr<   r`   )	r   active_onlyZeditor_onlyZ
shell_onlyZextnsZ	userExtnsextnactiveExtnsr   r	   r	   r
   GetExtensions  s:    


zIdleConf.GetExtensionsc                 C   s   dd |D S )Nc                 S   s   g | ]}| d s|qS ))	_bindings_cfgBindings)endswith).0nr	   r	   r
   
<listcomp>  r   z/IdleConf.RemoveKeyBindNames.<locals>.<listcomp>r	   )r   ZextnNameListr	   r	   r
   r|     s    zIdleConf.RemoveKeyBindNamesc                 C   sB   d }d| d }| j ddD ] }| |D ]}||kr*|}q*q|S )N<<>>r   r~   )r   GetExtensionKeys)r   virtualEventZextNameZvEventr   eventr	   r	   r
   GetExtnNameForEvent  s    zIdleConf.GetExtnNameForEventc           	      C   s^   |d }|   }i }| jd |rZ| jd |}|D ] }d| d }|| }|||< q8|S )Nr   rB   r   r   )GetCurrentKeySetrD   r!   r$   )	r   extensionNamekeysNameZ
activeKeysextKeys
eventNames	eventNamer   bindingr	   r	   r
   r     s    
zIdleConf.GetExtensionKeysc                 C   sd   |d }i }| j d |r`| j d |}|D ].}| jd||dd }d| d }|||< q0|S )Nr   rB   rI   rk   r   r   )rD   r!   r$   r`   split)r   r   r   r   r   r   r   r   r	   r	   r
   Z__GetRawExtensionKeys  s    

zIdleConf.__GetRawExtensionKeysc                 C   sj   |d }|  |}| jd |rf| jd |}|D ].}| jd||dd }d| d }|||< q6|S )Nr   rB   rI   rk   r   r   )r   rD   r!   r$   r`   r   )r   r   Z	bindsNameZextBindsr   r   r   r   r	   r	   r
   GetExtensionBindings  s    


zIdleConf.GetExtensionBindingsc                 C   s(   |dd }| j d||ddd }|S )N   rA   rI   F)r   r^   )r`   r   )r   
keySetNameZeventStrr   r   r	   r	   r
   GetKeyBinding  s
    
zIdleConf.GetKeyBindingc                 C   sL   |  |  }tjdkrH| D ]&\}}dd |D }||kr |||< q |S )Nrx   c                 S   s   g | ]}| d dqS )z<Alt-z<Option-)replace)r   xr	   r	   r
   r   %  r   z-IdleConf.GetCurrentKeySet.<locals>.<listcomp>)	GetKeySetrq   rU   ry   items)r   resultkvZv2r	   r	   r
   r     s    

zIdleConf.GetCurrentKeySetc                 C   s`   |  |}| jdd}|D ]@}| |}|r|D ](}|| | v rLd||< || ||< q0q|S )N   r   rI   )GetCoreKeysr   _IdleConf__GetRawExtensionKeysvalues)r   r   ZkeySetr   r   r   r   r	   r	   r
   r   +  s    

zIdleConf.GetKeySetc                 C   s   d| d |   v S )Nr   r   )r   )r   r   r	   r	   r
   IsCoreBinding=  s    zIdleConf.IsCoreBinding>	   <<flash-paren>><<expand-word>><<check-module>><<run-module>><<zoom-height>><<force-open-completions>><<force-open-calltip>><<format-paragraph>><<run-custom>>c              ;   C   s  ddgddgddgddgd	gd
gdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgd gd!d"gd#gd$gd%gd&gd'gd(gd)d*gd+gd,gd-gd.gd/gd0gd1gd2gd3gd4gd5gd6gd7gd8gd9gd:gd;gd<gd=gd>gd?:}|r| j d@ |s<| jd@ |s<dA|f }t|d@| nT|D ]N}| ||}|r`|||< n,|| jvr@dB|||| f }t|d@|| q@|S )CNz<Control-c>z<Control-C>z<Control-x>z<Control-X>z<Control-v>z<Control-V>z<Control-a>z<Home>z<Control-l>z<Control-q>z<Alt-F4>z<Control-d>z<F1>z
<Shift-F1>z<Alt-n>z<Alt-p>z<F6>z<Control-F6>z<Alt-c>z<Alt-m>z<Control-n>z<Control-o>z<Control-j>z<Control-p>z<Control-y>z<Escape>z<Alt-Shift-S>z<Alt-s>z<Control-s>z<Alt-a>z<Control-slash>z<Control-z>z<Control-g>z<F3>z<Alt-F3>z<Control-F3>z<Control-f>z<Control-h>z<Alt-g>z<Key-BackSpace>z<Key-Return>z<Key-KP_Enter>z	<Key-Tab>z<Control-Key-bracketright>z<Control-Key-bracketleft>z<Alt-Key-3>z<Alt-Key-4>z<Alt-Key-5>z<Alt-Key-6>z<Alt-Key-t>z<Alt-Key-u>z<Control-Key-BackSpace>z<Control-Key-Delete>z<Control-Key-space>z<Alt-Key-slash>z<Control-Key-backslash>z<Control-Key-0>z<Alt-Key-q>z<Key-F5>z<Shift-Key-F5>z<Alt-Key-x>z<Alt-Key-2>):z<<copy>>z<<cut>>z	<<paste>>z<<beginning-of-line>>z<<center-insert>>z<<close-all-windows>>z<<close-window>>z<<do-nothing>>z<<end-of-file>>z<<python-docs>>z<<python-context-help>>z<<history-next>>z<<history-previous>>z<<interrupt-execution>>z<<view-restart>>z<<restart-shell>>z<<open-class-browser>>z<<open-module>>z<<open-new-window>>z<<open-window-from-file>>z<<plain-newline-and-indent>>z<<print-window>>z<<redo>>z<<remove-selection>>z<<save-copy-of-window-as-file>>z<<save-window-as-file>>z<<save-window>>z<<select-all>>z<<toggle-auto-coloring>>z<<undo>>z<<find-again>>z<<find-in-files>>z<<find-selection>>z<<find>>z<<replace>>z<<goto-line>>z<<smart-backspace>>z<<newline-and-indent>>z<<smart-indent>>z<<indent-region>>z<<dedent-region>>z<<comment-region>>z<<uncomment-region>>z<<tabify-region>>z<<untabify-region>>z<<toggle-tabs>>z<<change-indentwidth>>z<<del-word-left>>z<<del-word-right>>r   r   r   r   r   r   r   r   r   rA   za
 Warning: config.py - IdleConf.GetCoreKeys -
 key set %r is not defined, using default bindings.z
 Warning: config.py - IdleConf.GetCoreKeys -
 problem retrieving key binding for event %r
 from key set %r.
 returning default value: %r)rE   r!   rD   r\   r   former_extension_events)r   r   ZkeyBindingsr_   r   r   r	   r	   r
   r   O  s    
=
zIdleConf.GetCoreKeysc           	      C   s   g }|dkr| j d }n|dkr,| jd }ntd|d}|D ]f}|jd|dd}|ddkrnd	}d	}n"|d}|d
  }|d  }|rB|rB||||f qB|j	dd d |S )Nra   r?   r   rb   	HelpFiles;rk   rI   r   r   c                 S   s   | d S )Nr   r	   )r   r	   r	   r
   <lambda>  r   z1IdleConf.GetExtraHelpSourceList.<locals>.<lambda>)key)
rE   rD   r   r$   r   findr   stripr}   sort)	r   rc   ZhelpSourcesrd   r"   r   r+   ZmenuItemZhelpPathr	   r	   r
   GetExtraHelpSourceList  s&    


zIdleConf.GetExtraHelpSourceListc                 C   s   |  d|  d }|S )Nr   ra   )r   )r   ZallHelpSourcesr	   r	   r
   GetAllExtraHelpSourcesList  s    z#IdleConf.GetAllExtraHelpSourcesListc           	      C   s   | j ||ddd}| j ||dddd}| j ||dd	d
d}|dkrtdd|d}t|}|d }|d }|d	krvd}|d dk}|||rdndfS )NZfontZcourierrk   z	font-sizer   Z10rr   z	font-boldr   r   r{   ZTkFixedFontT)rs   r;   rootfamilysize
   Zweightboldrf   )r`   r   actual)	r   r   r]   r   r   r   r   fZ
actualFontr	   r	   r
   GetFont  s     
zIdleConf.GetFontc                 C   s,   | j D ] }| j |   | j|   qd S r    )rD   r'   rE   r   r   r	   r	   r
   rH     s    
zIdleConf.LoadCfgFilesc                 C   s   | j D ]}| j |   qd S r    )rE   r=   r   r	   r	   r
   SaveUserCfgFiles  s    
zIdleConf.SaveUserCfgFiles)F)NNTF)TFF)N)!r   r   r   r   rG   rN   r`   r,   re   rj   rg   rp   rq   ro   staticmethodrt   r   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   rH   r   r	   r	   r	   r
   r>      sD   

$  
(D&
  
)
` 	r>   c                 G   sF   | f| }|t vrBzt| tjd W n ty6   Y n0 t | d S )NrR   )_warnedrT   rU   rV   r6   add)msgr   r	   r	   r
   r\     s    
r\   c                   @   s@   e Zd Zdd Zdd Zedd Zdd Zd	d
 Zdd Z	dS )ConfigChangesc                 C   s.   g | _ tjD ]}i | |< | j | |  qd S r    )pagesidleConfrC   r}   )r   config_typer	   r	   r
   r     s    
zConfigChanges.__init__c                 C   s0   | | }t |}||vr i ||< ||| |< d S r    )str)r   r   r   itemr+   pager	   r	   r
   
add_option%  s
    zConfigChanges.add_optionc                 C   sN   t j|  ||r:t j|  |||kr:t j|  ||S t j|  |||S r    )r   rD   r   r   rE   r-   r,   )r   r   r   r+   r	   r	   r
   save_option-  s    zConfigChanges.save_optionc              	   C   s   t jd   d}| D ]v}d}| | }|D ]J}|dkrJt jd d d}||  D ]\}}| ||||rVd}qVq*|rt j|   d}qdD ]}t j|   q|   |S )Nr?   Fr   T)rA   r@   )r   rE   r=   r0   r   r   clear)r   changedr   Zcfg_type_changedr   r   r   r+   r	   r	   r
   save_all:  s&    zConfigChanges.save_allc                 C   s6   || | v r| | |= t j| }|| |  d S r    )r   rE   r0   r=   )r   r   r   Z
configpager	   r	   r
   delete_sectionX  s
    


zConfigChanges.delete_sectionc                 C   s   | j D ]}|  qd S r    )r   r   )r   r   r	   r	   r
   r   c  s    
zConfigChanges.clearN)
r   r   r   r   r   r   r   r   r   r   r	   r	   r	   r
   r     s   
r   c                     sV   ddl m  d\} } fddfdd}|tj |tj td| d	|d
d d S )Nr   crc32)r   r   c                    s.   t | }td7 a |jddtat| d S )Nr   zutf-8)encoding)r   lineencodecrcrT   )objZtxtr   r	   r
   sprintr  s    z_dump.<locals>.sprintc              	      s   t d| d t|  D ]n}| |  } |  | |D ]H}| | |} |  | |D ] } |d | | ||  qbq<qd S )N
z = )rT   sortedrA   r/   r"   r   )rF   r   r/   r   r"   r   )r   r	   r
   dumpCfgz  s    z_dump.<locals>.dumpCfgz	
lines = z, crc = rI   )sep)zlibr   r   rD   rE   rT   )r   r   r   r	   )r   r   r
   _dumpn  s    

r   __main__)r?   zidlelib.idle_test.test_configr   F)	verbosityexit)configparserr   r7   rU   Ztkinter.fontr   rL   	Exceptionr   r   r   r   r(   r>   r   r)   r   r\   dictr   r   r   Zunittestr?   r	   r	   r	   r
   <module>   s,   (B    q
c