U .c@sdZddlmZddlZddlZddlmZddlZGdddeZ GdddeZ Gd d d eZ Gd d d eZ Gd dde Z GdddZeZeZddZGdddeZddZedkrddlmZedddddS)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 'extensions', 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__r r 3/opt/alt/python38/lib64/python3.8/idlelib/config.pyr#src@s eZdZdS)InvalidConfigSetNrr r r r r $sr c@s eZdZdS) InvalidThemeNrr r r r 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)defaultsstrictN)filer__init__)selfcfgFileZ cfgDefaultsr r r r+szIdleConfParser.__init__FcCsL|||s|S|dkr$|||S|dkr8|||S|j|||dSdS)z Get an option value for given section/option or return default. If type is specified, return as type. boolintrawN) has_optionZ getbooleanZgetintget)rsectionoptiontypedefaultrr r r Get2s   zIdleConfParser.GetcCs||r||SgSdS)z4Return a list of options for given section, else [].N) has_sectionoptionsrrr r r GetOptionListCs  zIdleConfParser.GetOptionListcCs|jr||jdS)z&Load the configuration file from disk.N)rreadrr r r LoadJszIdleConfParser.Load)N)NNF)rrr__doc__rrr"r%r r r r r 's   r c@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)IdleUserConfParserzG IdleConfigParser specialised for user configuration handling. cCs^|||r4||||kr dS||||dSn&||sH||||||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)rrrvaluer r r SetOptionTs   zIdleUserConfParser.SetOptioncCs||r|||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)rrrr r r RemoveOptiones  zIdleUserConfParser.RemoveOptioncCs||s||dS)z!If section doesn't exist, add it.N)rr)r!r r r AddSectionns zIdleUserConfParser.AddSectioncCs&|D]}||s||qdS)z)Remove any sections that have no options.N)sectionsr"remove_sectionr!r r r RemoveEmptySectionsss  z&IdleUserConfParser.RemoveEmptySectionscCs|| S)z9Return True if no sections after removing empty sections.)r0r.r$r r r IsEmptyyszIdleUserConfParser.IsEmptyc Cs|j}|r|ddkr|srzt|d}Wn(tk rTt|t|d}YnX|||W5QRXntj|jrt |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. r#wN) rr1openOSErrorosunlinkwritepathexistsremove)rZfnamerr r r Save~s zIdleUserConfParser.SaveN) rrrr&r+r,r-r0r1r<r r r r r'Os r'c @seZdZdZdFddZddZddZdGd d Zd dZddZ ddZ ddZ ddZ ddZ ddZeddZdHddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2d3d4d5d6d7d8d9h ZdId:d;Zdd?Zd@dAZdBdCZ dDdEZ!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,||dS)N)main highlightkeys extensions) config_types defaultCfguserCfgcfgCreateConfigHandlers LoadCfgFiles)rZ_utestr r r rszIdleConf.__init__cCsxtjt}tjrdn||_}|jD]H}t tj |d|d|j |<t tj |p\dd|d|j |<q*dS)z5Populate default and user config parser dictionaries.zconfig-z.defr2z.cfgN)r6r9dirname__file__idlelibtesting GetUserCfgDiruserdirrBr joinrCr'rD)rZidledirrNZcfg_typer r r rFs   zIdleConf.CreateConfigHandlersc Csd}tjd}|dkrbtj|sbtjs^d|d}zt|tjdWnt k r\YnXd}|dkrrt }tj ||}tj|szt |WnRt k rtjsd|d}zt|tjdWnt k rYnXt 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! )r6r9 expanduserr:rKrLprintsysstderrr5getcwdrOmkdir SystemExit)rZcfgDirZuserDirwarnr r r rMsD    zIdleConf.GetUserCfgDirNTc Csz0|j|||r.|j|j||||dWSWnDtk rtd||||j|j|||df}t||||YnXz0|j|||r|j|j||||dWSWntk 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: %rrz Warning: config.py - IdleConf.GetOption - problem retrieving configuration option %r from section %r. returning default value: %r)rDrr ValueError_warnrC) r configTyperrrrwarn_on_defaultrwarningr r r GetOptions:    zIdleConf.GetOptioncCs|j||||dS)z0Set section option to value in user config file.N)rDr+)rr\rrr*r r r r+szIdleConf.SetOptioncCsJ||jkrtd|dkr&|j|}n|dkr:|j|}ntd|S)zReturn sections for configSet configType configuration. configSet must be either 'user' or 'default' configType must be in self.config_types. zInvalid configType specifieduserrInvalid configSet specified)rBrrDrCr r.)r configSetr\ cfgParserr r r GetSectionLists   zIdleConf.GetSectionListcCsR|jd|rdnd}|||}||d}|dkr|jd|d d d }|sT|jd|d d d }|rz|rb|jn|j}|||rz|S|dkrd S|S)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. rlr?r@r>rrTrrrHZname2rjnamez IDLE Classic)r_rCrDr default_keys)rrZcfgnamerrrsourcer r r rnms z IdleConf.current_colors_and_keyscCs,tjdddkrdStjdkr$dSdSdS)NwinzIDLE Classic WindowsdarwinzIDLE Classic OSXzIDLE Modern Unix)rTplatformr r r r rss  zIdleConf.default_keysc Cs||dd}||dd}|D]}||kr(||q(dD]}||qD|rg}|D]Z}|jd|ddddr`|s|r|rd }nd }|jd||ddd d r||q`||q`|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. rrAr`)Z AutoCompleteZ CodeContextZFormatParagraphZ ParenMatchZenableTrrrZ enable_editorZ enable_shellF)rrr]N)RemoveKeyBindNamesrdappendr;r_) r active_onlyZ editor_onlyZ shell_onlyZextnsZ userExtnsextn activeExtnsrr r r GetExtensionss<        zIdleConf.GetExtensionscCsdd|DS)z:Return extnNameList with keybinding section names removed.cSsg|]}|ds|qS)) _bindings _cfgBindings)endswith).0nr r r s z/IdleConf.RemoveKeyBindNames..r )rZ extnNameListr r r rzszIdleConf.RemoveKeyBindNamescCsBd}d|d}|jddD] }||D]}||kr*|}q*q|S)zReturn the name of the extension binding virtualEvent, or None. virtualEvent - string, name of the virtual event to test for, without the enclosing '<< >>' N<<>>rr|)rGetExtensionKeys)r virtualEventZextNameZvEventr}eventr r r GetExtnNameForEvents zIdleConf.GetExtnNameForEventc Cs^|d}|}i}|jd|rZ|jd|}|D] }d|d}||}|||<q8|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. rrArr)GetCurrentKeySetrCrr") r extensionNamekeysNameZ activeKeysextKeys eventNames eventNamerbindingr r r rs  zIdleConf.GetExtensionKeyscCsd|d}i}|jd|r`|jd|}|D].}|jd||dd}d|d}|||<q0|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. rrArHrjrr)rCrr"r_split)rrrrrrrrr r r Z__GetRawExtensionKeyss   zIdleConf.__GetRawExtensionKeyscCsj|d}||}|jd|rf|jd|}|D].}|jd||dd}d|d}|||<q6|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. rrArHrjrr)rrCrr"r_r)rrZ bindsNameZextBindsrrrrr r r GetExtensionBindingss    zIdleConf.GetExtensionBindingscCs(|dd}|jd||ddd}|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@rHF)rr])r_r)r keySetNameZeventStrrrr r r GetKeyBindings    zIdleConf.GetKeyBindingcCsL||}tjdkrH|D]&\}}dd|D}||kr |||<q |S)z/Return CurrentKeys with 'darwin' modifications.rwcSsg|]}|ddqS)z.) GetKeySetrprTrxitems)rresultkvZv2r r r rs  zIdleConf.GetCurrentKeySetcCs`||}|jdd}|D]@}||}|r|D](}|||krLd||<||||<q0q|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 '' rrH) GetCoreKeysr_IdleConf__GetRawExtensionKeysvalues)rrZkeySetr~r}rrr r r r+s   zIdleConf.GetKeySetcCsd|d|kS)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 '<< >>' rr)r)rrr r r IsCoreBinding=szIdleConf.IsCoreBinding<><><><><><><><><>c;Csddgddgddgddgd gd gd gdgd gd gdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgdgd gd!d"gd#gd$gd%gd&gd'gd(gd)d*gd+gd,gd-gd.gd/gd0gd1gd2gd3gd4gd5gd6gd7gd8gd9gd:gd;gdgd?:}|r|jd@|s<|jd@|sz 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 zzz 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<>rrrrrrrrrr@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)rDrrCr[rformer_extension_events)rrZ keyBindingsr^rrr r r rOs =    zIdleConf.GetCoreKeysc Csg}|dkr|jd}n|dkr,|jd}ntd|d}|D]f}|jd|dd}|ddkrnd }d }n"|d}|d }|d }|rB|rB||||fqB|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>rra HelpFiles;rjrHrrcSs|dS)Nrr )rr r r z1IdleConf.GetExtraHelpSourceList..)key) rDrCr r"rfindrstripr{sort) rrbZ helpSourcesrcr rr*ZmenuItemZhelpPathr r r GetExtraHelpSourceLists&       zIdleConf.GetExtraHelpSourceListcCs|d|d}|S)zReturn a list of the details of all additional help sources. Tuples in the list are those of GetExtraHelpSourceList. rr`)r)rZallHelpSourcesr r r GetAllExtraHelpSourcesListsz#IdleConf.GetAllExtraHelpSourcesListc Cs|j||ddd}|j||dddd}|j||dd d d }|d krtd d |d}t|}|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 ZfontZcourierrjz font-sizerZ10rqz font-boldrrryZ TkFixedFontT)rrr:rootfamilysize Zweightboldre)r_rZactual) rrr\rrrrfZ actualFontr r r GetFonts     zIdleConf.GetFontcCs,|jD] }|j||j|qdS)zLoad all configuration files.N)rCr%rDrrr r r rGs zIdleConf.LoadCfgFilescCs|jD]}|j|qdS)z2Write all loaded user configuration files to disk.N)rDr<rr r r SaveUserCfgFiless zIdleConf.SaveUserCfgFiles)F)NNTF)TFF)N)"rrrr&rrFrMr_r+rdrirfrorprn staticmethodrsrrzrrrrrrrrrrrrrrGrr r r r r=s\  $ (D&  )   `  r=cGsH|f|}|tkrDzt|tjdWntk r8YnXt|dS)NrQ)_warnedrSrTrUr5add)msgrr r r r[s r[c@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. cCs.g|_tjD]}i||<|j||q dS)z)Create a page for each configuration fileN)pagesidleConfrBr{)r config_typer r r rs zConfigChanges.__init__cCs0||}t|}||kr i||<||||<dS)z0Add item/value pair for config_type and section.N)str)rrritemr*pager r r add_option%s zConfigChanges.add_optioncCsNtj|||r:tj||||kr:tj|||Stj||||S)zcReturn True if the configuration value was added or changed. Helper for save_all. )rrCrrrDr,r+)rrrr*r r r save_option-szConfigChanges.save_optionc Cstjdd}|D]v}d}||}|D]J}|dkrJtjddd}||D]\}}|||||rVd}qVq*|rtj|d}qdD]}tj|q||S)zSave configuration changes to the user config file. Clear self in preparation for additional changes. Return changed for testing. r>FrT)r@r?)rrDr<r/rrclear)rZchangedrZcfg_type_changedrrrr*r r r save_all:s&zConfigChanges.save_allcCs6|||kr|||=tj|}|||dS)zjDelete a section from self, userCfg, and file. Used to delete custom themes and keysets. N)rrDr/r<)rrrZ configpager r r delete_sectionXs     zConfigChanges.delete_sectioncCs|jD] }|qdS)zClear all 4 pages. Called in save_all after saving to idleConf. XXX Mark window *title* when there are changes; unmark here. N)rr)rrr r r rcs zConfigChanges.clearN) rrrr&rrrrrrrr r r r r s  rcsVddlmd\}}fddfdd}|tj|tjtd|d |d d dS) Nrcrc32)rrcs.t|}td7a|jddtat|dS)Nrzutf-8)encoding)rlineencodecrcrS)objZtxtrr r sprintrsz_dump..sprintc std|dt|D]n}||}|||D]H}|||}|||D] }|d||||qbq.dumpCfgz lines = z, crc = rH)sep)zlibrrrCrDrS)rrrr )rrr _dumpns    r__main__)r>zidlelib.idle_test.test_configrF) verbosityexit)r&Z configparserrr6rTZ tkinter.fontrrK Exceptionrr r r r'r=rr(rr[dictrrrZunittestr>r r r r s.  (Bq c