3  fޗ@sdZddlmZddlZddlZddlmZddlZGdddeZ GdddeZ Gd d d eZ Gd d d eZ Gd ddeZ Gddde ZGdddZeZeZddZGdddeZddZedkrddlmZedddddS)aEidlelib.config -- Manage IDLE configuration information. The comments at the beginning of config-main.def describe the 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 user file becomes empty, it will be deleted. The configuration database maps options to values. Conceptually, the database keys are tuples (config-type, section, item). As implemented, there are separate dicts for default and user values. Each has config-type keys 'main', 'extensions', 'highlight', and 'keys'. The value for each key is a ConfigParser instance that maps section and item to values. For 'main' and 'extenstons', user values override default values. For 'highlight' and 'keys', user sections augment the default sections (and must, therefore, have distinct names). 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. ) ConfigParserN)Fontc@s eZdZdS)InvalidConfigTypeN)__name__ __module__ __qualname__rr3/opt/alt/python36/lib64/python3.6/idlelib/config.pyr#src@s eZdZdS)InvalidConfigSetN)rrrrrrr r $sr c@s eZdZdS) InvalidFgBgN)rrrrrrr r %sr c@s eZdZdS) InvalidThemeN)rrrrrrr r &sr c@s4eZdZdZd ddZd ddZdd Zd d ZdS)IdleConfParserzI A ConfigParser specialised for idle configuration file handling NcCs||_tj||dddS)zK cfgFile - string, fully specified configuration file name F)ZdefaultsstrictN)filer__init__)selfcfgFileZ cfgDefaultsrrr r,szIdleConfParser.__init__FcCsL|j||s|S|dkr$|j||S|dkr8|j||S|j|||dSdS)z Get an option value for given section/option or return default. If type is specified, return as type. boolint)rawN) has_optionZ getbooleanZgetintget)rsectionoptiontypedefaultrrrr Get3s   zIdleConfParser.GetcCs|j|r|j|SgSdS)z4Return a list of options for given section, else [].N) has_sectionoptions)rrrrr GetOptionListDs  zIdleConfParser.GetOptionListcCs|jr|j|jdS)z&Load the configuration file from disk.N)rread)rrrr LoadKszIdleConfParser.Load)N)NNF)rrr__doc__rrrr!rrrr r (s   r c@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)IdleUserConfParserzG IdleConfigParser specialised for user configuration handling. cCs^|j||r4|j|||kr dS|j|||dSn&|j|sH|j||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)rrsetr add_section)rrrvaluerrr SetOptionUs   zIdleUserConfParser.SetOptioncCs|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 RemoveOptionfs  zIdleUserConfParser.RemoveOptioncCs|j|s|j|dS)z!If section doesn't exist, add it.N)rr%)rrrrr AddSectionos zIdleUserConfParser.AddSectioncCs*x$|jD]}|j|s |j|q WdS)z)Remove any sections that have no options.N)sectionsrremove_section)rrrrr RemoveEmptySectionsts z&IdleUserConfParser.RemoveEmptySectionscCs|j|j S)z9Return True if no sections after removing empty sections.)r,r*)rrrr IsEmptyzszIdleUserConfParser.IsEmptycCstjj|jrtj|jdS)z9Remove user config file self.file from disk if it exists.N)ospathexistsrremove)rrrr RemoveFileszIdleUserConfParser.RemoveFilecCsr|j}|rn|jsfyt|d}Wn(tk rHtj|t|d}YnX||j|WdQRXn|jdS)zUpdate user configuration file. If self not empty after removing empty sections, write the file to disk. Otherwise, remove the file from disk if it exists. wN)rr-openOSErrorr.unlinkwriter2)rZfnamerrrr Saves zIdleUserConfParser.SaveN) rrrr"r'r(r)r,r-r2r8rrrr r#Ps r#c@seZdZdZdEddZddZddZdFd d Zd dZddZ dGddZ ddZ ddZ ddZ ddZeddZdHddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2d3d4d5d6d7d8hZdId9d:Zd;d<Zd=d>Zd?d@ZdAdBZ dCdDZ!d S)JIdleConfalHold 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 FcCs0d|_i|_i|_i|_|s,|j|jdS)Nmain highlightkeys extensions)r:r;r<r=) config_types defaultCfguserCfgcfgCreateConfigHandlers LoadCfgFiles)rZ_utestrrr rszIdleConf.__init__cCstdkrtjjt}ntjjtjd}|j|_}i}i}xB|j D]8}tjj |d|d||<tjj |d|d||<qFWx2|j D](}t |||j |<t |||j|<qWdS)z5Populate default and user config parser dictionaries.__main__rzconfig-z.defz.cfgN)rr.r/dirname__file__abspathsys GetUserCfgDirZuserdirr>joinr r?r#r@)rZidleDiruserDirZ defCfgFilesZ usrCfgFilesZcfgTyperrr rBs  zIdleConf.CreateConfigHandlerscCsd}tjjd}|dkr\tjj|s\d|d}yt|tjdWntk rVYnXd}|dkrltj}tjj ||}tjj|sytj |Wn8tk rd|d}t j st|tjdt YnX|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/ expanduserr0printrHstderrr5getcwdrJmkdiridlelibZtesting SystemExit)rZcfgDirrKwarnrrr rIs,      zIdleConf.GetUserCfgDirNTc Csy.|j|j||r,|j|j||||dSWnDtk rrd||||j|j|||df}t||||YnXy.|j|j||r|j|j||||dSWntk rYnX|rd|||f}t|||||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. )rrzn Warning: config.py - IdleConf.GetOption - invalid %r value for configuration option %r from section %r: %r)rz Warning: config.py - IdleConf.GetOption - problem retrieving configuration option %r from section %r. returning default value: %r)r@rr ValueError_warnr?) r configTyperrrrwarn_on_defaultrwarningrrr GetOptions(   zIdleConf.GetOptioncCs|j|j|||dS)z0Set section option to value in user config file.N)r@r')rrWrrr&rrr r' szIdleConf.SetOptioncCsJ||jkrtd|dkr&|j|}n|dkr:|j|}ntd|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 configSetrW cfgParserrrr GetSectionLists   zIdleConf.GetSectionListcCs|jdj|r|jd|}n |jd|}||d}|dkrH|d}n ||d}||d}|sf|S|d krv|d S|d kr|d Std dS)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;rr[z -foregroundZcursorznormal-backgroundz -background) foreground backgroundZfgr_Zbgr`zInvalid fgBg specifiedN)r?r GetThemeDictr )rthemeelementZfgBgZ themeDictZforeZbackr;rrr GetHighlights      zIdleConf.GetHighlightcCs|dkr|jd}n|dkr(|jd}ntddddddddddddddddddddddddddddddd}xP|D]H}|j||sd ||||f}t|d|||j||||d ||<qvW|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. r[r;rzInvalid theme type specifiedz#000000z#ffffffZgray)znormal-foregroundznormal-backgroundzkeyword-foregroundzkeyword-backgroundzbuiltin-foregroundzbuiltin-backgroundzcomment-foregroundzcomment-backgroundzstring-foregroundzstring-backgroundzdefinition-foregroundzdefinition-backgroundzhilite-foregroundzhilite-backgroundzbreak-foregroundzbreak-backgroundzhit-foregroundzhit-backgroundzerror-foregroundzerror-backgroundzcursor-foregroundzstdout-foregroundzstdout-backgroundzstderr-foregroundzstderr-backgroundzconsole-foregroundzconsole-backgroundzcontext-foregroundzcontext-backgroundz{ Warning: config.IdleConf.GetThemeDict - problem retrieving theme element %r from theme %r. returning default color: %r)r)r@r?r rrVr)rrZ themeNamer]rbrcrYrrr ra;sT    zIdleConf.GetThemeDictcCs |jdS)z9Return the name of the currently active text color theme.Theme)current_colors_and_keys)rrrr CurrentTheme{szIdleConf.CurrentThemecCs |jdS)z0Return the name of the currently active key set.ZKeys)rf)rrrr CurrentKeysszIdleConf.CurrentKeyscCs|dkr dnd}|jd|dddd}d }|r>|jd|d d d }|sT|jd|d d d }|rz|rb|jn|j}||j|rz|S|dkrd S|jS)aReturn the currently active name for Theme or Keys section. idlelib.config-main.def ('default') includes these sections [Theme] default= 1 name= IDLE Classic name2= [Keys] default= 1 name= name2= Item 'name2', is used for built-in ('default') themes and keys added after 2015 Oct 1 and 2016 July 1. This kludge is needed because setting 'name' to a builtin not defined in older IDLEs to display multiple error messages or quit. See https://bugs.python.org/issue25313. When default = True, 'name2' takes precedence over 'name', while older IDLEs will just use name. When default = False, 'name2' may still be set, but it is ignored. rer;r<r:rrT)rrZname2)rnamez IDLE Classic)rZr?r@r default_keys)rrZcfgnamerrjsourcerrr rfs  z IdleConf.current_colors_and_keyscCs,tjdddkrdStjdkr$dSdSdS)NwinzIDLE Classic WindowsdarwinzIDLE Classic OSXzIDLE Modern Unix)rHplatformrrrr rks  zIdleConf.default_keysc Cs|j|jdd}|j|jdd}x|D]}||kr*|j|q*WxdD]}|j|qJW|rg}xb|D]Z}|jd|dd d d rj|s|r|rd }nd }|jd||d d ddr|j|qj|j|qjW|S|SdS)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. rr=r[ AutoComplete CodeContextFormatParagraph ParenMatchenableTr)rrZ enable_editorZ enable_shellF)rrrXN)rqrrrsrt)RemoveKeyBindNamesr^appendr1rZ) r active_onlyZ editor_onlyZ shell_onlyZextnsZ userExtnsextn activeExtnsrrrr GetExtensionss4     zIdleConf.GetExtensionscCsdd|DS)z:Return extnNameList with keybinding section names removed.cSsg|]}|jds|qS) _bindings _cfgBindings)r|r})endswith).0nrrr sz/IdleConf.RemoveKeyBindNames..r)rZ extnNameListrrr rvszIdleConf.RemoveKeyBindNamescCsJd}d|d}x4|jddD]$}x|j|D]}||kr.|}q.WqW|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<>r)rx)r{GetExtensionKeys)r virtualEventZextNameZvEventryeventrrr GetExtnNameForEvents  zIdleConf.GetExtnNameForEventc Csb|d}|j}i}|jdj|r^|jdj|}x(|D] }d|d}||}|||<q:W|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. r}r=z<>)GetCurrentKeySetr?rr) r extensionNamekeysNameZ activeKeysextKeys eventNames eventNamerbindingrrr rs   zIdleConf.GetExtensionKeyscCsh|d}i}|jdj|rd|jdj|}x6|D].}|jd||ddj}d|d}|||<q2W|S)aReturn 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. r}r=ri)rz<>)r?rrrZsplit)rrrrrrrrrrr Z__GetRawExtensionKeyss   zIdleConf.__GetRawExtensionKeyscCsn|d}|j|}|jdj|rj|jdj|}x6|D].}|jd||ddj}d|d}|||<q8W|S)aReturn 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. r|r=ri)rz<>)rr?rrrZr)rrZ bindsNameZextBindsrrrrrrr GetExtensionBindingss    zIdleConf.GetExtensionBindingscCs(|dd}|jd||dddj}|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<riF)rrX)rZr)r keySetNameZeventStrrrrrr GetKeyBinding%s   zIdleConf.GetKeyBindingcCsP|j|j}tjdkrLx2|jD]&\}}dd|D}||kr"|||<q"W|S)z/Return CurrentKeys with 'darwin' modifications.rocSsg|]}|jddqS)z.) GetKeySetrhrHrpitems)rresultkvZv2rrr r0s  zIdleConf.GetCurrentKeySetcCsh|j|}|jdd}xL|D]D}|j|}|rx0|D](}|||jkrPd||<||||<q4WqW|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 '' )rxri) GetCoreKeysr{_IdleConf__GetRawExtensionKeysvalues)rrZkeySetrzryrrrrr rAs     zIdleConf.GetKeySetcCsd|d|jkS)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<>)r)rrrrr IsCoreBindingSszIdleConf.IsCoreBindingz<>z<>z<>z<>z<>z<>z<>z<>c:Csddgddgddgddgd gd gd gdgd gd gdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgd gd!d"gd#gd$gd%gd&gd'gd(gd)d*gd+gd,gd-gd.gd/gd0gd1gd2gd3gd4gd5gd6gd7gd8gd9gd:gd;gd9}|r|jd?j|p|jd?j|s8d@|f}t|d?|nXxV|D]N}|j||}|r^|||<n,||jkr>dA||||f}t|d?||q>W|S)BaReturn 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 z z z z z z zz z zz zz zzzz zzz z z z z zz zz zzz z zzz z z zzz zz zzz z z z z z zzzzzzz zz z )9z<>z<>z <>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z <>z <>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>z<>r<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)r@rr?rVrformer_extension_events)rrZ keyBindingsrYrrrrr rcs      zIdleConf.GetCoreKeysc Csg}|dkr|jd}n|dkr,|jd}ntd|jd}xn|D]f}|jd|dd}|jddkrpd }d }n"|jd}|d j}|dj}|rD|rD|j|||fqDW|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'. r[r:rzInvalid configSet specified HelpFiles;)rrrircSs|dS)Nrr)rrrr sz1IdleConf.GetExtraHelpSourceList..)key) r@r?r rrfindrstriprwsort) rr\Z helpSourcesr]rrr&ZmenuItemZhelpPathrrr GetExtraHelpSourceLists&        zIdleConf.GetExtraHelpSourceListcCs|jd|jd}|S)zReturn a list of the details of all additional help sources. Tuples in the list are those of GetExtraHelpSourceList. rr[)r)rZallHelpSourcesrrr GetAllExtraHelpSourcesLists z#IdleConf.GetAllExtraHelpSourcesListc Cs|j||ddd}|j||dddd}|j||dd d d }|d krtd d |d}tj|}|d}|d}|d krvd}|ddk}|||rdndfS)aRetrieve a font from configuration (font, font-size, font-bold) Intercept the special value 'TkFixedFont' and substitute the actual font, factoring in some tweaks if needed for appearance sakes. The 'root' parameter can normally be any valid Tkinter widget. Return a tuple (family, size, weight) suitable for passing to tkinter.Font ZfontZcourier)rz font-sizerZ10)rrz font-boldrr)rrZ TkFixedFontT)rjr0rootfamilysize ZweightboldZnormal)rZrZactual) rrrWrrrrfZ actualFontrrr GetFonts     zIdleConf.GetFontcCs0x*|jD] }|j|j|j|jqWdS)zLoad all configuration files.N)r?r!r@)rrrrr rCs zIdleConf.LoadCfgFilescCs"x|jD]}|j|jqWdS)z2Write all loaded user configuration files to disk.N)r@r8)rrrrr SaveUserCfgFiles s zIdleConf.SaveUserCfgFiles)F)NNTF)N)TFF)N)"rrrr"rrBrIrZr'r^rdrargrhrf staticmethodrkr{rvrrrrrrrrrrrrrrCrrrrr r9sD    ' @&  (    _  r9c GsH|f|}|tkrDyt|tjdWntk r8YnXtj|dS)N)r)_warnedrNrHrOr5add)msgrrrr rVs rVc@sDeZdZdZddZddZeddZdd Zd d Z d d Z dS) ConfigChangesaManage a user's proposed configuration option changes. Names used across multiple methods: page -- one of the 4 top-level dicts representing a .idlerc/config-x.cfg file. config_type -- name of a page. section -- a section within a page/file. option -- name of an option within a section. value -- value for the option. Methods add_option: Add option and value to changes. save_option: Save option and value to config parser. save_all: Save all the changes to the config parser and file. delete_section: If section exists, delete from changes, userCfg, and file. clear: Clear all changes by clearing each page. cCs2g|_x&tjD]}i||<|jj||qWdS)z)Create a page for each configuration fileN)pagesidleConfr>rw)r config_typerrr r1s zConfigChanges.__init__cCs0||}t|}||kr i||<||||<dS)z0Add item/value pair for config_type and section.N)str)rrritemr&pagerrr add_option8s zConfigChanges.add_optioncCsNtj|j||r:tj|j|||kr:tj|j||Stj|j|||S)zcReturn True if the configuration value was added or changed. Helper for save_all. )rr?rrr@r(r')rrrr&rrr save_option@szConfigChanges.save_optioncCstjdjd}x|D]~}d}||}xV|D]N}|dkrNtjdjdd}x,||jD]\}}|j||||r\d}q\Wq.W|rtj|jd}qWxdD]}tj|jqW|j|S)zSave configuration changes to the user config file. Clear self in preparation for additional changes. Return changed for testing. r:FrTr<r;)r<r;)rr@r8r+rrclear)rZchangedrZcfg_type_changedrrrr&rrr save_allMs&    zConfigChanges.save_allcCs6|||kr|||=tj|}|j||jdS)zjDelete a section from self, userCfg, and file. Used to delete custom themes and keysets. N)rr@r+r8)rrrZ configpagerrr delete_sectionks     zConfigChanges.delete_sectioncCsx|jD] }|jqWdS)zClear all 4 pages. Called in save_all after saving to idleConf. XXX Mark window *title* when there are changes; unmark here. N)rr)rrrrr rvs zConfigChanges.clearN) rrrr"rrrrrrrrrrr rs  rcsVddlmd \}}fddfdd}|tj|tjtd|d|d d dS) Nr)crc32cs.t|}td7a|jddtat|dS)Nrzutf-8)encoding)rlineencodecrcrN)objZtxt)rrr sprintsz_dump..sprintcstd|dxt|jD]v}||j}||xT|D]L}||j|}||x(|D] }|d||j||qhWq@WqWdS)N z = )rNsortedr<r*rr)rArr*rrr)rrr dumpCfgs    z_dump..dumpCfgz lines = z, crc = ri)sep)rr)zlibrrr?r@rN)rrrr)rrr _dumps    rrD)r:zidlelib.idle_test.test_configrF) verbosityexit)r"Z configparserrr.rHZ tkinter.fontrrR Exceptionrr r r r r#r9rr$rrVdictrrrZunittestr:rrrr s0  (H} c