o =?h@sdZddlmZddlZddlZddlmZddlZGdddeZ GdddeZ Gd d d eZ Gd d d eZ Gd dde Z GdddZeZeZddZGdddeZddZedkrrddlmZedddddSdS)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@ eZdZdS)InvalidConfigTypeN__name__ __module__ __qualname__r r 5/opt/alt/python310/lib64/python3.10/idlelib/config.pyr# rc@r)InvalidConfigSetNrr r r r r $r r c@r) InvalidThemeNrr r r r r%r rc@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__FcCsH|||s|S|dkr|||S|dkr|||S|j|||dS)z Get an option value for given section/option or return default. If type is specified, return as type. boolintraw) has_optionZ getbooleanZgetintget)rsectionoptiontypedefaultrr r r Get2s   zIdleConfParser.GetcCs||r ||SgS)z4Return a list of options for given section, else [].) has_sectionoptionsrrr r r GetOptionListCs  zIdleConfParser.GetOptionListcCs|jr ||jdSdS)z&Load the configuration file from disk.N)rreadrr r r LoadJszIdleConfParser.LoadN)NNF)rrr __doc__rr r$r'r r r r r's    rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)IdleUserConfParserzG IdleConfigParser specialised for user configuration handling. cCsX|||r||||krdS||||dS||s#||||||dS)zReturn True if option is added or changed to value, else False. Add section if required. False means option already had value. FT)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)r!Z remove_option)rrrr r r RemoveOptiones  zIdleUserConfParser.RemoveOptioncCs||s ||dSdS)z!If section doesn't exist, add it.N)r!r,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.)r3r1r&r r r IsEmptyys zIdleUserConfParser.IsEmptycCs|j}|rS|ddkrU|sDzt|d}Wnty)t|t|d}Ynw|||WddS1s=wYdStj|jrWt |jdSdSdSdS)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) rr4openOSErrorosunlinkwritepathexistsremove)rZfnamerr r r Save~s    " zIdleUserConfParser.SaveN) rrr r)r.r/r0r3r4r?r r r r r*Os  r*c@s eZdZdZd>ddZddZddZ d?d d Zd dZddZ ddZ ddZ ddZ ddZ ddZeddZ d@ddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zhd1ZdAd2d3Zd4d5Zd6d7Zd8d9Zd:d;Z drb) 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 sz/IdleConf.RemoveKeyBindNames..r )rZ extnNameListr r r rszIdleConf.RemoveKeyBindNamescCsBd}d|d}|jddD]}||D]}||kr|}qq|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 virtualEventZextNameZvEventreventr r r GetExtnNameForEvents zIdleConf.GetExtnNameForEventc Cs^|d}|}i}|jd|r-|jd|}|D]}d|d}||}|||<q|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. rrDrr)GetCurrentKeySetrFr!r$) r extensionNamekeysNameZ activeKeysextKeys eventNames eventNamerbindingr r r rs  zIdleConf.GetExtensionKeyscCsd|d}i}|jd|r0|jd|}|D]}|jd||dd}d|d}|||<q|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. rrDrKrmrr)rFr!r$rbsplit)rrrrrrrrr r r Z__GetRawExtensionKeyss  zIdleConf.__GetRawExtensionKeyscCsj|d}||}|jd|r3|jd|}|D]}|jd||dd}d|d}|||<q|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. rrDrKrmrr)rrFr!r$rbr)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 '<>'. rCrKF)rr`)rbr)r keySetNameZeventStrrrr r r GetKeyBindings  zIdleConf.GetKeyBindingcCsL||}tjdkr$|D]\}}dd|D}||kr#|||<q|S)z/Return CurrentKeys with 'darwin' modifications.r|cSsg|]}|ddqS)z.) GetKeySetrurWr}items)rresultkvZv2r r r rs zIdleConf.GetCurrentKeySetcCs`||}|jdd}|D] }||}|r-|D]}|||vr&d||<||||<qq |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 '' rrK) GetCoreKeysr_IdleConf__GetRawExtensionKeysvalues)rrZkeySetrrrrr r r r+s   zIdleConf.GetKeySetcCsd|d|vS)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 Cspidddgdddgddd gd d d gd dgddgddgddgddgddgddgddgddgddgdd gd!d"gd#d$gid%d&gd'd(gd)d*gd+d,gd-d.gd/d0gd1d2gd3d4gd5d6gd7d8gd9d:gd;dgd?d@dAgdBdCgdDdEgdFdGgidHdIgdJdKgdLdMgdNdOdPgdQdRgdSdTgdUdVgdWdXgdYdZgd[d\gd]d^gd_d`gdadbgdcddgdedfgdgdhgdidjgdkgdlgdmgdngdogdpgdqgdr}|r6|jds|s |jds|s dt|f}t|ds||S|D](}|||}|r|||<q ||jvr4du||||f}t|ds||q |S)vaReturn 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 <>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<>zrzrzzzz zzz z )rrrrrrrrCza 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)rGr!rFr^rformer_extension_events)rrZ keyBindingsrarrr r r rOs        !"#$%&'()*+,-./01234=     zIdleConf.GetCoreKeysc Csg}|dkr |jd}n|dkr|jd}ntd|d}|D]3}|jd|dd}|ddkr7d }d }n|d}|d }|d }|rT|rT||||fq!|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'. rcrArrd HelpFiles;rmrKrrcSs|dS)Nrr )rr r r sz1IdleConf.GetExtraHelpSourceList..)key) rGrFr r$r findrstriprsort) rreZ helpSourcesrfr"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. rrc)r)rZallHelpSourcesr r r GetAllExtraHelpSourcesListsz#IdleConf.GetAllExtraHelpSourcesListc Cs|j||ddd}|j||dddd}|j||dd d d }|d krAtd d |d}t|}|d}|d}|d kr;d}|ddk}|||rHdfSdfS)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 ZfontZcourierrmz font-sizerZ10rvz font-boldrrr~Z TkFixedFontT)rwr=rootfamilysize Zweightboldrh)rbrZactual) rrr_rrrrfZ actualFontr r r GetFonts     zIdleConf.GetFontcCs,|jD]}|j||j|qdS)zLoad all configuration files.N)rFr'rGrrr r r rJs zIdleConf.LoadCfgFilescCs|jD] }|j|qdS)z2Write all loaded user configuration files to disk.N)rGr?rr r r SaveUserCfgFiless zIdleConf.SaveUserCfgFiles)F)NNTF)TFFr()"rrr r)rrIrPrbr.rgrlrirsrurr staticmethodrxrrrrrrrrrrrrrrrrJrr r r r r@sH   $ (D&  )  `   r@cGsJ|f|}|tvr#z t|tjdWn tyYnwt|dSdS)NrT)_warnedrVrWrXr8add)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||qdS)z)Create a page for each configuration fileN)pagesidleConfrEr)r config_typer r r rs  zConfigChanges.__init__cCs0||}t|}||vri||<||||<dS)z0Add item/value pair for config_type and section.N)str)rrritemr-pager r r add_option%s zConfigChanges.add_optioncCsNtj|||rtj||||krtj|||Stj||||S)zcReturn True if the configuration value was added or changed. Helper for save_all. )rrFrr rGr/r.)rrrr-r r r save_option-szConfigChanges.save_optionc Cstjdd}|D];}d}||}|D]%}|dkr%tjddd}||D]\}}|||||r9d}q+q|rFtj|d}q dD] }tj|qI||S)zSave configuration changes to the user config file. Clear self in preparation for additional changes. Return changed for testing. rAFrT)rCrB)rrGr?r2rrclear)rZchangedrZcfg_type_changedrrrr-r r r save_all:s,zConfigChanges.save_allcCs6|||vr |||=tj|}|||dS)zjDelete a section from self, userCfg, and file. Used to delete custom themes and keysets. N)rrGr2r?)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) rrr r)rrrrrrrr r r r r s   rcsVddlmd\}}fddfdd}|tj|tjtd|d |d d dS) Nrcrc32)rrcs.t|}td7a|jddtat|dS)Nrzutf-8)encoding)rlineencodecrcrV)objZtxtrr r sprintrs z_dump..sprintc std|dt|D]7}||}|||D]$}|||}|||D]}|d||||q1qq dS)N z = )rVsortedrCr1r"r )rHrr1rr"r)rr r dumpCfgzs  z_dump..dumpCfgz lines = z, crc = rK)sep)zlibrrrFrGrV)rrrr )rrr _dumpns    r__main__)rAzidlelib.idle_test.test_configrF) verbosityexit)r)Z configparserrr9rWZ tkinter.fontrrN Exceptionrr rrr*r@rr+rr^dictrrrZunittestrAr r r r s2  (Bq c