[t@s$dZddlZddlZddlmZGdddeZGdddeZGdd d eZGd d d eZ Gd d d eZ Gddde Z GdddZ e Z edkr ddZee jee jee jdjddndS)aProvides 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@seZdZdS)InvalidConfigTypeN)__name__ __module__ __qualname__rr+/usr/lib/python3.4/idlelib/configHandler.pyrs rc@seZdZdS)InvalidConfigSetN)rrrrrrrr s r c@seZdZdS) InvalidFgBgN)rrrrrrrr s r c@seZdZdS) InvalidThemeN)rrrrrrrr s r c@sReZdZdZdddZdddddZdd Zd d ZdS) IdleConfParserzI A ConfigParser specialised for idle configuration file handling NcCs&||_tj|d|dddS)zK cfgFile - string, fully specified configuration file name ZdefaultsstrictFN)filer__init__)selfcfgFileZ cfgDefaultsrrrr#s zIdleConfParser.__init__FcCsh|j||s|S|dkr2|j||S|dkrN|j||S|j||d|SdS)z Get an option value for given section/option or return default. If type is specified, return as type. boolintrawN) has_optionZ getbooleanZgetintget)rsectionoptiontypedefaultrrrrGet*s  zIdleConfParser.GetcCs$|j|r|j|SgSdS)z4Return a list of options for given section, else [].N) has_sectionoptions)rrrrr GetOptionList;s zIdleConfParser.GetOptionListcCs|j|jdS)z&Load the configuration file from disk.N)readr)rrrrLoadBszIdleConfParser.Load)rrr__doc__rrrr rrrrr s  r c@sjeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)IdleUserConfParserzG IdleConfigParser specialised for user configuration handling. cCs#|j|s|j|ndS)z!If section doesn't exist, add it.N)r add_section)rrrrr AddSectionKszIdleUserConfParser.AddSectioncCs:x3|jD]%}|j|s |j|q q WdS)z)Remove any sections that have no options.N)sectionsrZremove_section)rrrrrRemoveEmptySectionsPsz&IdleUserConfParser.RemoveEmptySectionscCs|j|j S)z9Return True if no sections after removing empty sections.)r&r%)rrrrIsEmptyVs zIdleUserConfParser.IsEmptycCs#|j|r|j||SdS)zReturn True if option is removed from section, else False. False if either section does not exist or did not have option. F)rZ remove_option)rrrrrr RemoveOption[szIdleUserConfParser.RemoveOptioncCs|j||rH|j|||kr.dS|j|||dSn6|j|sg|j|n|j|||dSdS)zReturn True if option is added or changed to value, else False. Add section if required. False means option already had value. FTN)rrsetrr#)rrrvaluerrr SetOptiondszIdleUserConfParser.SetOptioncCs,tjj|jr(tj|jndS)z9Remove user config file self.file from disk if it exists.N)ospathexistsrremove)rrrr RemoveFileuszIdleUserConfParser.RemoveFilecCs|jsv|j}yt|d}Wn.tk rXtj|t|d}YnX||j|WdQXn |jdS)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'ropenOSErrorr,unlinkwriter0)rZfnamerrrrSavezs    zIdleUserConfParser.SaveN) rrrr!r$r&r'r(r+r0r6rrrrr"Fs      r"c@s]eZdZdZddZddZddZddd d d d Zd dZddZ dddZ ddZ ddZ ddZ d d d ddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zdd-d.Zd/d0Zd1d2Zd3d4Zd5d6ZdS)7IdleConfalHold 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 cCs<d|_i|_i|_i|_|j|jdS)Nmain extensions highlightkeys)zmainz extensionsz highlightzkeys) config_types defaultCfguserCfgcfgCreateConfigHandlers LoadCfgFiles)rrrrrs      zIdleConf.__init__cCstdkr!tjjt}ntjjtjd}|j}i}i}xS|jD]H}tjj |d|d||)rZidleDiruserDirZ defCfgFilesZ usrCfgFilesZcfgTyperrrr@s    zIdleConf.CreateConfigHandlerscCsd}tjjd}|dkr|tjj|s|d|d}yt|dtjWntk roYnXd}q|n|dkrtj}ntjj ||}tjj|sytj |Wqtk r d|d}t|dtjt YqXn|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.rz2 Warning: unable to create user config directory z( Check path and permissions. Exiting! ) r,r- expanduserr.printrFstderrr3getcwdrHmkdir SystemExit)rZcfgDirrIwarnrrrrGs*     zIdleConf.GetUserCfgDirNTFc !CsQy@|j|j||r?|j|j||d|d|SWnmtk rd||||j|j||d|f}yt|dtjWntk rYnXYnXy@|j|j||r|j|j||d|d|SWntk rYnX|rMd|||f}yt|dtjWqMtk rIYqMXn|S)aReturn 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. rrzu Warning: configHandler.py - IdleConf.GetOption - invalid %r value for configuration option %r from section %r: %rrz Warning: configHandler.py - IdleConf.GetOption - problem retrieving configuration option %r from section %r. returning default value: %r) r>rr ValueErrorrLrFrMr3r=) r configTyperrrrwarn_on_defaultrwarningrrr GetOptions4   #     zIdleConf.GetOptioncCs|j|j|||dS)z0Set section option to value in user config file.N)r>r+)rrSrrr*rrrr+szIdleConf.SetOptioncCsl||jkrtdn|dkr:|j|}n(|dkrV|j|}n td|jS)zReturn sections for configSet configType configuration. configSet must be either 'user' or 'default' configType must be in self.config_types. zInvalid configType specifieduserrzInvalid configSet specified)r<rr>r=r r%)r configSetrS cfgParserrrrGetSectionLists   zIdleConf.GetSectionListcCs|jdj|r+|jd|}n|jd|}||d}|dkrd|d}n||d}i|d6|d 6}|s|S|d kr|dS|d kr|d Std d S)aReturn individual theme element highlight color(s). fgBg - string ('fg' or 'bg') or None. If None, return a dictionary containing fg and bg colors with keys 'foreground' and 'background'. Otherwise, only return fg or bg color, as specified. Colors are intended to be appropriate for passing to Tkinter in, e.g., a tag_config call). r:rrWz -foregroundZcursorznormal-backgroundz -backgroundZ foregroundZ backgroundZfgZbgzInvalid fgBg specifiedN)r=r GetThemeDictr )rthemeelementZfgBgZ themeDictZforeZbackr:rrr GetHighlights     zIdleConf.GetHighlightcCs|dkr|jd}n(|dkr8|jd}n tdidd6dd6dd 6dd 6dd 6dd 6dd 6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd 6dd!6dd"6}x|D]~}|j||sld#||||f}yt|d$tjWqltk rhYqlXn|j||d||||r=r rrLrFrMr3r)rrZ themeNamerYr\r]rUrrrr[4sV       zIdleConf.GetThemeDictcCs|jdddddS)z.Return the name of the currently active theme.r8Themenamer)rV)rrrr CurrentThemetszIdleConf.CurrentThemecCs|jdddddS)z0Return the name of the currently active key set.r8ZKeysr`rra)rV)rrrr CurrentKeysxszIdleConf.CurrentKeysc Cs|j|jdd}|j|jdd}x*|D]"}||kr=|j|q=q=W|rg}x|D]}|jd|dddddrv|s|r|rd}nd }|jd||ddddd d r|j|qq|j|qvqvW|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. rr9rWenableTrrZ enable_editorZ enable_shellrTFN)RemoveKeyBindNamesrZappendrV) r active_onlyZ editor_onlyZ shell_onlyZextnsZ userExtnsextn activeExtnsrrrr GetExtensions|s.        zIdleConf.GetExtensionscCsq|}g}x6|D].}|jdr|j|j|qqW|jddx|D] }||=q\W|S)z:Return extnNameList with keybinding section names removed. _bindings _cfgBindingsreverseT)rkrl)endswithrfindexsort)rZ extnNameListnamesZkbNameIndiciesr`rorrrres   zIdleConf.RemoveKeyBindNamescCsdd}d|d}xI|jddD]5}x,|j|D]}||kr=|}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<>rgr)rjGetExtensionKeys)r virtualEventZextNameZvEventrheventrrrGetExtnNameForEvents zIdleConf.GetExtnNameForEventc Cs|d}|j}i}|jdj|r~|jdj|}x3|D](}d|d}||}|||>)GetCurrentKeySetr=rr) r extensionNamekeysNameZ activeKeysextKeys eventNames eventNamertbindingrrrrrs    zIdleConf.GetExtensionKeyscCs|d}i}|jdj|r|jdj|}xJ|D]?}|jd||ddj}d|d}|||>)r=rrrVsplit)rrwrxryrzr{r|rtrrrZ__GetRawExtensionKeyss  zIdleConf.__GetRawExtensionKeyscCs|d}|j|}|jdj|r|jdj|}xJ|D]?}|jd||ddj}d|d}|||>)rrr=rrrVr})rrwZ bindsNameZextBindsrzr{r|rtrrrGetExtensionBindingss  zIdleConf.GetExtensionBindingscCs5|dd}|jd||ddj}|S)zReturn the keybinding list for keySetName eventStr. keySetName - name of key binding set (config-keys section). eventStr - virtual event, including brackets, as in '<>'. r;rra)rVr})r keySetNameZeventStrr{r|rrr GetKeyBindings!zIdleConf.GetKeyBindingcCst|j|j}tjdkrpxI|jD]8\}}dd|D}||kr1|||s z-IdleConf.GetCurrentKeySet..) GetKeySetrcrFplatformitems)rresultkvZv2rrrrvs zIdleConf.GetCurrentKeySetcCs|j|}|jdd}xh|D]`}|j|}|r(xB|D]7}|||jkrsd||>' z<>)r)rrsrrr IsCoreBinding szIdleConf.IsCoreBindingc Csi1ddgd6ddgd6ddgd 6d d gd 6d gd6dgd6dgd6dgd6dgd6dgd6dgd6dgd6dgd6dgd6dgd 6d!gd"6d#gd$6d%gd&6d'gd(6d)gd*6d+gd,6d-gd.6d/gd06d1gd26d3gd46d5gd66d7gd86d9gd:6d;gd<6d=gd>6d?d@gdA6dBgdC6dDgdE6dFgdG6dHgdI6dJgdK6dLgdM6dNdOgdP6dQgdR6dSgdT6dUgdV6dWgdX6dYgdZ6d[gd\6d]gd^6d_gd`6dagdb6dcgdd6degdf6}|rxy|D]n}|j||}|r:|||z z<>z z z<>z z z <>z zz<>z z<>z z<>zz<>z<>z z<>zz<>z z<>zz<>zz<>z<>zz<>z z<>zz<>zz<>z z<>z z<>z z<>z z<>z z<>zz<>z z<>zz<>z z<>zz<>zz<>z z<>z zz<>zz<>z z<>z z<>z z <>zz <>zz<>z zz<>z z<>zz<>zz<>z z<>z z<>z z<>z z<>z z<>z z<>zz<>zz<>z Warning: configHandler.py - IdleConf.GetCoreKeys - problem retrieving key binding for event %r from key set %r. returning default value: %rr)rrLrFrMr3)rrZ keyBindingsrtr|rUrrrr+s|                                                      zIdleConf.GetCoreKeysc Csg}|dkr"|jd}n(|dkr>|jd}n td|jd}x|D]}|jd|dd}|jdd krd}d}n/|jd}|d j}|dj}|r`|r`|j|||fq`q`W|j d d d |S)aReturn 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'. rWr8rzInvalid configSet specifiedZ HelpFiles;rrarkeycSs|dS)Nrr)rrrrsz1IdleConf.GetExtraHelpSourceList..) r>r=r rrfindr}striprfrp) rrXZ helpSourcesrYrrr*ZmenuItemZhelpPathrrrGetExtraHelpSourceListxs&       zIdleConf.GetExtraHelpSourceListcCs |jd|jd}|S)zReturn a list of the details of all additional help sources. Tuples in the list are those of GetExtraHelpSourceList. rrW)r)rZallHelpSourcesrrrGetAllExtraHelpSourcesLists z#IdleConf.GetAllExtraHelpSourcesListcCs:x3|jD](}|j|j|j|jq WdS)zLoad all configuration files.N)r=r r>)rrrrrrAszIdleConf.LoadCfgFilescCs)x"|jD]}|j|jq WdS)z2Write all loaded user configuration files to disk.N)r>r6)rrrrrSaveUserCfgFilesszIdleConf.SaveUserCfgFiles)rrrr!rr@rGrVr+rZr^r[rbrcrjrerurrrr~rrvrrrrrrArrrrrr7s6   -   @  #       M r7rBc Cstd|dx|D]}||j}t|t|xf|D]^}||j|}t|t|x.|D]&}t|d||j||q|WqHWqWdS)N =)rLr%rr)r?rr%rrrrrrdumpCfgs       rr8r_r`)r!r,rFZ configparserr Exceptionrr r r r r"r7ZidleConfrrr=r>rLrrrrrs$  'G"