From: Terry Jan Reedy Date: Mon, 6 Oct 2014 05:32:21 +0000 (-0400) Subject: Update 2.7 idlelib.configHandler and reduce differences from 3.4 code. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f8b7e78aa2651199f4a0d6f624ebcc8ed3c44973;p=python Update 2.7 idlelib.configHandler and reduce differences from 3.4 code. --- diff --git a/Lib/idlelib/configHandler.py b/Lib/idlelib/configHandler.py index 909125803b..9b726e1e22 100644 --- a/Lib/idlelib/configHandler.py +++ b/Lib/idlelib/configHandler.py @@ -15,11 +15,12 @@ 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. + """ from __future__ import print_function import os import sys -import string + from ConfigParser import ConfigParser class InvalidConfigType(Exception): pass @@ -36,7 +37,7 @@ class IdleConfParser(ConfigParser): cfgFile - string, fully specified configuration file name """ self.file=cfgFile - ConfigParser.__init__(self,defaults=cfgDefaults) + ConfigParser.__init__(self, defaults=cfgDefaults) def Get(self, section, option, type=None, default=None, raw=False): """ @@ -144,7 +145,8 @@ class IdleUserConfParser(IdleConfParser): except IOError: os.unlink(fname) cfgFile = open(fname, 'w') - self.write(cfgFile) + with cfgFile: + self.write(cfgFile) else: self.RemoveFile() @@ -284,13 +286,13 @@ class IdleConf: configType must be one of ('main','extensions','highlight','keys') """ if not (configType in ('main','extensions','highlight','keys')): - raise InvalidConfigType, 'Invalid configType specified' + raise InvalidConfigType('Invalid configType specified') if configSet == 'user': cfgParser=self.userCfg[configType] elif configSet == 'default': cfgParser=self.defaultCfg[configType] else: - raise InvalidConfigSet, 'Invalid configSet specified' + raise InvalidConfigSet('Invalid configSet specified') return cfgParser.sections() def GetHighlight(self, theme, element, fgBg=None): @@ -318,7 +320,7 @@ class IdleConf: if fgBg == 'bg': return highlight["background"] else: - raise InvalidFgBg, 'Invalid fgBg specified' + raise InvalidFgBg('Invalid fgBg specified') def GetThemeDict(self,type,themeName): """ @@ -334,7 +336,7 @@ class IdleConf: elif type == 'default': cfgParser=self.defaultCfg['highlight'] else: - raise InvalidTheme, 'Invalid theme type specified' + raise InvalidTheme('Invalid theme type specified') #foreground and background values are provded for each theme element #(apart from cursor) even though all these values are not yet used #by idle, to allow for their use in the future. Default values are @@ -368,7 +370,7 @@ class IdleConf: 'stderr-background':'#ffffff', 'console-foreground':'#000000', 'console-background':'#ffffff' } - for element in theme.keys(): + for element in theme: if not cfgParser.has_option(themeName,element): #we are going to return a default, print warning warning=('\n Warning: configHandler.py - IdleConf.GetThemeDict' @@ -452,7 +454,7 @@ class IdleConf: extName=None vEvent='<<'+virtualEvent+'>>' for extn in self.GetExtensions(active_only=0): - for event in self.GetExtensionKeys(extn).keys(): + for event in self.GetExtensionKeys(extn): if event == vEvent: extName=extn return extName @@ -550,7 +552,7 @@ class IdleConf: for extn in activeExtns: extKeys=self.__GetRawExtensionKeys(extn) if extKeys: #the extension defines keybindings - for event in extKeys.keys(): + for event in extKeys: if extKeys[event] in keySet.values(): #the binding is already in use extKeys[event]='' #disable this binding @@ -563,7 +565,7 @@ class IdleConf: virtualEvent - string, name of the virtual event to test for, without the enclosing '<< >>' """ - return ('<<'+virtualEvent+'>>') in self.GetCoreKeys().keys() + return ('<<'+virtualEvent+'>>') in self.GetCoreKeys() def GetCoreKeys(self, keySetName=None): """ @@ -626,7 +628,7 @@ class IdleConf: '<>': [''] } if keySetName: - for event in keyBindings.keys(): + for event in keyBindings: binding=self.GetKeyBinding(keySetName,event) if binding: keyBindings[event]=binding @@ -658,7 +660,7 @@ class IdleConf: elif configSet=='default': cfgParser=self.defaultCfg['main'] else: - raise InvalidConfigSet, 'Invalid configSet specified' + raise InvalidConfigSet('Invalid configSet specified') options=cfgParser.GetOptionList('HelpFiles') for option in options: value=cfgParser.Get('HelpFiles',option,default=';') @@ -666,7 +668,7 @@ class IdleConf: menuItem='' #make these empty helpPath='' #so value won't be added to list else: #config entry contains ';' as expected - value=string.split(value,';') + value=value.split(';') menuItem=value[0].strip() helpPath=value[1].strip() if menuItem and helpPath: #neither are empty strings @@ -688,7 +690,7 @@ class IdleConf: """ load all configuration files. """ - for key in self.defaultCfg.keys(): + for key in self.defaultCfg: self.defaultCfg[key].Load() self.userCfg[key].Load() #same keys @@ -696,7 +698,7 @@ class IdleConf: """ write all loaded user configuration files back to disk """ - for key in self.userCfg.keys(): + for key in self.userCfg: self.userCfg[key].Save() idleConf=IdleConf() @@ -705,7 +707,7 @@ idleConf=IdleConf() if __name__ == '__main__': def dumpCfg(cfg): print('\n', cfg, '\n') - for key in cfg.keys(): + for key in cfg: sections=cfg[key].sections() print(key) print(sections)