Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 1 | """Provides access to stored IDLE configuration information. |
Steven M. Gava | c597640 | 2002-01-04 03:06:08 +0000 | [diff] [blame] | 2 | |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 3 | Refer to the comments at the beginning of config-main.def for a description of |
| 4 | the available configuration files and the design implemented to update user |
| 5 | configuration information. In particular, user configuration choices which |
| 6 | duplicate the defaults will be removed from the user's configuration files, |
Kurt B. Kaiser | e66675b | 2003-01-27 02:36:18 +0000 | [diff] [blame] | 7 | and if a file becomes empty, it will be deleted. |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 8 | |
| 9 | The contents of the user files may be altered using the Options/Configure IDLE |
| 10 | menu to access the configuration GUI (configDialog.py), or manually. |
| 11 | |
| 12 | Throughout this module there is an emphasis on returning useable defaults |
| 13 | when a problem occurs in returning a requested configuration value back to |
| 14 | idle. This is to allow IDLE to continue to function in spite of errors in |
| 15 | the retrieval of config information. When a default is returned instead of |
| 16 | a requested config value, a message is printed to stderr to aid in |
| 17 | configuration problem notification and resolution. |
| 18 | |
| 19 | """ |
| 20 | import os |
| 21 | import sys |
| 22 | import string |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 23 | from ConfigParser import ConfigParser, NoOptionError, NoSectionError |
| 24 | |
Neal Norwitz | 5b0b00f | 2002-11-30 19:10:19 +0000 | [diff] [blame] | 25 | class InvalidConfigType(Exception): pass |
| 26 | class InvalidConfigSet(Exception): pass |
| 27 | class InvalidFgBg(Exception): pass |
| 28 | class InvalidTheme(Exception): pass |
| 29 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 30 | class IdleConfParser(ConfigParser): |
| 31 | """ |
| 32 | A ConfigParser specialised for idle configuration file handling |
| 33 | """ |
| 34 | def __init__(self, cfgFile, cfgDefaults=None): |
| 35 | """ |
| 36 | cfgFile - string, fully specified configuration file name |
| 37 | """ |
| 38 | self.file=cfgFile |
| 39 | ConfigParser.__init__(self,defaults=cfgDefaults) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 40 | |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 41 | def Get(self, section, option, type=None, default=None): |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 42 | """ |
| 43 | Get an option value for given section/option or return default. |
| 44 | If type is specified, return as type. |
| 45 | """ |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 46 | if type=='bool': |
Steven M. Gava | 41a8532 | 2001-10-29 08:05:34 +0000 | [diff] [blame] | 47 | getVal=self.getboolean |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 48 | elif type=='int': |
Steven M. Gava | 41a8532 | 2001-10-29 08:05:34 +0000 | [diff] [blame] | 49 | getVal=self.getint |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 50 | else: |
Steven M. Gava | 41a8532 | 2001-10-29 08:05:34 +0000 | [diff] [blame] | 51 | getVal=self.get |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 52 | if self.has_option(section,option): |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 53 | #return getVal(section, option, raw, vars, default) |
Steven M. Gava | 429a86a | 2001-10-23 10:42:12 +0000 | [diff] [blame] | 54 | return getVal(section, option) |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 55 | else: |
| 56 | return default |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 57 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 58 | def GetOptionList(self,section): |
| 59 | """ |
| 60 | Get an option list for given section |
| 61 | """ |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 62 | if self.has_section(section): |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 63 | return self.options(section) |
| 64 | else: #return a default value |
| 65 | return [] |
| 66 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 67 | def Load(self): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 68 | """ |
| 69 | Load the configuration file from disk |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 70 | """ |
| 71 | self.read(self.file) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 72 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 73 | class IdleUserConfParser(IdleConfParser): |
| 74 | """ |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 75 | IdleConfigParser specialised for user configuration handling. |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 76 | """ |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 77 | |
| 78 | def AddSection(self,section): |
| 79 | """ |
| 80 | if section doesn't exist, add it |
| 81 | """ |
| 82 | if not self.has_section(section): |
| 83 | self.add_section(section) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 84 | |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 85 | def RemoveEmptySections(self): |
| 86 | """ |
| 87 | remove any sections that have no options |
| 88 | """ |
| 89 | for section in self.sections(): |
| 90 | if not self.GetOptionList(section): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 91 | self.remove_section(section) |
| 92 | |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 93 | def IsEmpty(self): |
| 94 | """ |
| 95 | Remove empty sections and then return 1 if parser has no sections |
| 96 | left, else return 0. |
| 97 | """ |
| 98 | self.RemoveEmptySections() |
| 99 | if self.sections(): |
| 100 | return 0 |
| 101 | else: |
| 102 | return 1 |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 103 | |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 104 | def RemoveOption(self,section,option): |
| 105 | """ |
| 106 | If section/option exists, remove it. |
| 107 | Returns 1 if option was removed, 0 otherwise. |
| 108 | """ |
| 109 | if self.has_section(section): |
| 110 | return self.remove_option(section,option) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 111 | |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 112 | def SetOption(self,section,option,value): |
| 113 | """ |
| 114 | Sets option to value, adding section if required. |
| 115 | Returns 1 if option was added or changed, otherwise 0. |
| 116 | """ |
| 117 | if self.has_option(section,option): |
| 118 | if self.get(section,option)==value: |
| 119 | return 0 |
| 120 | else: |
| 121 | self.set(section,option,value) |
| 122 | return 1 |
| 123 | else: |
| 124 | if not self.has_section(section): |
| 125 | self.add_section(section) |
| 126 | self.set(section,option,value) |
| 127 | return 1 |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 128 | |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 129 | def RemoveFile(self): |
| 130 | """ |
| 131 | Removes the user config file from disk if it exists. |
| 132 | """ |
| 133 | if os.path.exists(self.file): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 134 | os.remove(self.file) |
| 135 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 136 | def Save(self): |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 137 | """Update user configuration file. |
| 138 | |
| 139 | Remove empty sections. If resulting config isn't empty, write the file |
| 140 | to disk. If config is empty, remove the file from disk if it exists. |
| 141 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 142 | """ |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 143 | if not self.IsEmpty(): |
| 144 | cfgFile=open(self.file,'w') |
| 145 | self.write(cfgFile) |
| 146 | else: |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 147 | self.RemoveFile() |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 148 | |
| 149 | class IdleConf: |
| 150 | """ |
| 151 | holds config parsers for all idle config files: |
| 152 | default config files |
| 153 | (idle install dir)/config-main.def |
| 154 | (idle install dir)/config-extensions.def |
| 155 | (idle install dir)/config-highlight.def |
| 156 | (idle install dir)/config-keys.def |
| 157 | user config files |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 158 | (user home dir)/.idlerc/config-main.cfg |
| 159 | (user home dir)/.idlerc/config-extensions.cfg |
| 160 | (user home dir)/.idlerc/config-highlight.cfg |
| 161 | (user home dir)/.idlerc/config-keys.cfg |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 162 | """ |
| 163 | def __init__(self): |
| 164 | self.defaultCfg={} |
| 165 | self.userCfg={} |
| 166 | self.cfg={} |
| 167 | self.CreateConfigHandlers() |
| 168 | self.LoadCfgFiles() |
| 169 | #self.LoadCfg() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 170 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 171 | def CreateConfigHandlers(self): |
| 172 | """ |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 173 | set up a dictionary of config parsers for default and user |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 174 | configurations respectively |
| 175 | """ |
| 176 | #build idle install path |
| 177 | if __name__ != '__main__': # we were imported |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 178 | idleDir=os.path.dirname(__file__) |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 179 | else: # we were exec'ed (for testing only) |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 180 | idleDir=os.path.abspath(sys.path[0]) |
| 181 | userDir=self.GetUserCfgDir() |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 182 | configTypes=('main','extensions','highlight','keys') |
| 183 | defCfgFiles={} |
| 184 | usrCfgFiles={} |
| 185 | for cfgType in configTypes: #build config file names |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 186 | defCfgFiles[cfgType]=os.path.join(idleDir,'config-'+cfgType+'.def') |
| 187 | usrCfgFiles[cfgType]=os.path.join(userDir,'config-'+cfgType+'.cfg') |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 188 | for cfgType in configTypes: #create config parsers |
| 189 | self.defaultCfg[cfgType]=IdleConfParser(defCfgFiles[cfgType]) |
| 190 | self.userCfg[cfgType]=IdleUserConfParser(usrCfgFiles[cfgType]) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 191 | |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 192 | def GetUserCfgDir(self): |
| 193 | """ |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 194 | Creates (if required) and returns a filesystem directory for storing |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 195 | user config files. |
| 196 | """ |
| 197 | cfgDir='.idlerc' |
| 198 | userDir=os.path.expanduser('~') |
| 199 | if userDir != '~': #'HOME' exists as a key in os.environ |
| 200 | if not os.path.exists(userDir): |
| 201 | warn=('\n Warning: HOME environment variable points to\n '+ |
| 202 | userDir+'\n but the path does not exist.\n') |
| 203 | sys.stderr.write(warn) |
| 204 | userDir='~' |
| 205 | if userDir=='~': #we still don't have a home directory |
| 206 | #traditionally idle has defaulted to os.getcwd(), is this adeqate? |
| 207 | userDir = os.getcwd() #hack for no real homedir |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 208 | userDir=os.path.join(userDir,cfgDir) |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 209 | if not os.path.exists(userDir): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 210 | try: #make the config dir if it doesn't exist yet |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 211 | os.mkdir(userDir) |
| 212 | except IOError: |
| 213 | warn=('\n Warning: unable to create user config directory\n '+ |
| 214 | userDir+'\n') |
| 215 | sys.stderr.write(warn) |
| 216 | return userDir |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 217 | |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 218 | def GetOption(self, configType, section, option, default=None, type=None): |
Steven M. Gava | 429a86a | 2001-10-23 10:42:12 +0000 | [diff] [blame] | 219 | """ |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 220 | Get an option value for given config type and given general |
Steven M. Gava | 429a86a | 2001-10-23 10:42:12 +0000 | [diff] [blame] | 221 | configuration section/option or return a default. If type is specified, |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 222 | return as type. Firstly the user configuration is checked, with a |
| 223 | fallback to the default configuration, and a final 'catch all' |
| 224 | fallback to a useable passed-in default if the option isn't present in |
Steven M. Gava | 429a86a | 2001-10-23 10:42:12 +0000 | [diff] [blame] | 225 | either the user or the default configuration. |
| 226 | configType must be one of ('main','extensions','highlight','keys') |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 227 | If a default is returned a warning is printed to stderr. |
Steven M. Gava | 429a86a | 2001-10-23 10:42:12 +0000 | [diff] [blame] | 228 | """ |
| 229 | if self.userCfg[configType].has_option(section,option): |
| 230 | return self.userCfg[configType].Get(section, option, type=type) |
| 231 | elif self.defaultCfg[configType].has_option(section,option): |
| 232 | return self.defaultCfg[configType].Get(section, option, type=type) |
Steven M. Gava | 052937f | 2002-02-11 02:20:53 +0000 | [diff] [blame] | 233 | else: #returning default, print warning |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 234 | warning=('\n Warning: configHandler.py - IdleConf.GetOption -\n' |
| 235 | ' problem retrieving configration option %r\n' |
| 236 | ' from section %r.\n' |
| 237 | ' returning default value: %r\n' % |
| 238 | (option, section, default)) |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 239 | sys.stderr.write(warning) |
Steven M. Gava | 429a86a | 2001-10-23 10:42:12 +0000 | [diff] [blame] | 240 | return default |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 241 | |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 242 | def GetSectionList(self, configSet, configType): |
| 243 | """ |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 244 | Get a list of sections from either the user or default config for |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 245 | the given config type. |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 246 | configSet must be either 'user' or 'default' |
Steven M. Gava | 5f28e8f | 2002-01-21 06:38:21 +0000 | [diff] [blame] | 247 | configType must be one of ('main','extensions','highlight','keys') |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 248 | """ |
Steven M. Gava | 5f28e8f | 2002-01-21 06:38:21 +0000 | [diff] [blame] | 249 | if not (configType in ('main','extensions','highlight','keys')): |
Neal Norwitz | 5b0b00f | 2002-11-30 19:10:19 +0000 | [diff] [blame] | 250 | raise InvalidConfigType, 'Invalid configType specified' |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 251 | if configSet == 'user': |
| 252 | cfgParser=self.userCfg[configType] |
| 253 | elif configSet == 'default': |
| 254 | cfgParser=self.defaultCfg[configType] |
| 255 | else: |
Neal Norwitz | 5b0b00f | 2002-11-30 19:10:19 +0000 | [diff] [blame] | 256 | raise InvalidConfigSet, 'Invalid configSet specified' |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 257 | return cfgParser.sections() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 258 | |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 259 | def GetHighlight(self, theme, element, fgBg=None): |
| 260 | """ |
| 261 | return individual highlighting theme elements. |
| 262 | fgBg - string ('fg'or'bg') or None, if None return a dictionary |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 263 | containing fg and bg colours (appropriate for passing to Tkinter in, |
| 264 | e.g., a tag_config call), otherwise fg or bg colour only as specified. |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 265 | """ |
Steven M. Gava | 9f25e67 | 2002-02-11 02:51:18 +0000 | [diff] [blame] | 266 | if self.defaultCfg['highlight'].has_section(theme): |
| 267 | themeDict=self.GetThemeDict('default',theme) |
| 268 | else: |
| 269 | themeDict=self.GetThemeDict('user',theme) |
| 270 | fore=themeDict[element+'-foreground'] |
| 271 | if element=='cursor': #there is no config value for cursor bg |
| 272 | back=themeDict['normal-background'] |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 273 | else: |
Steven M. Gava | 9f25e67 | 2002-02-11 02:51:18 +0000 | [diff] [blame] | 274 | back=themeDict[element+'-background'] |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 275 | highlight={"foreground": fore,"background": back} |
| 276 | if not fgBg: #return dict of both colours |
| 277 | return highlight |
| 278 | else: #return specified colour only |
| 279 | if fgBg == 'fg': |
| 280 | return highlight["foreground"] |
| 281 | if fgBg == 'bg': |
| 282 | return highlight["background"] |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 283 | else: |
Neal Norwitz | 5b0b00f | 2002-11-30 19:10:19 +0000 | [diff] [blame] | 284 | raise InvalidFgBg, 'Invalid fgBg specified' |
Steven M. Gava | 9f25e67 | 2002-02-11 02:51:18 +0000 | [diff] [blame] | 285 | |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 286 | def GetThemeDict(self,type,themeName): |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 287 | """ |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 288 | type - string, 'default' or 'user' theme type |
| 289 | themeName - string, theme name |
| 290 | Returns a dictionary which holds {option:value} for each element |
| 291 | in the specified theme. Values are loaded over a set of ultimate last |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 292 | fallback defaults to guarantee that all theme elements are present in |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 293 | a newly created theme. |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 294 | """ |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 295 | if type == 'user': |
| 296 | cfgParser=self.userCfg['highlight'] |
| 297 | elif type == 'default': |
| 298 | cfgParser=self.defaultCfg['highlight'] |
| 299 | else: |
Neal Norwitz | 5b0b00f | 2002-11-30 19:10:19 +0000 | [diff] [blame] | 300 | raise InvalidTheme, 'Invalid theme type specified' |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 301 | #foreground and background values are provded for each theme element |
| 302 | #(apart from cursor) even though all these values are not yet used |
| 303 | #by idle, to allow for their use in the future. Default values are |
| 304 | #generally black and white. |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 305 | theme={ 'normal-foreground':'#000000', |
| 306 | 'normal-background':'#ffffff', |
| 307 | 'keyword-foreground':'#000000', |
| 308 | 'keyword-background':'#ffffff', |
Kurt B. Kaiser | 73360a3 | 2004-03-08 18:15:31 +0000 | [diff] [blame^] | 309 | 'builtin-foreground':'#000000', |
| 310 | 'builtin-background':'#ffffff', |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 311 | 'comment-foreground':'#000000', |
| 312 | 'comment-background':'#ffffff', |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 313 | 'string-foreground':'#000000', |
| 314 | 'string-background':'#ffffff', |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 315 | 'definition-foreground':'#000000', |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 316 | 'definition-background':'#ffffff', |
| 317 | 'hilite-foreground':'#000000', |
| 318 | 'hilite-background':'gray', |
| 319 | 'break-foreground':'#ffffff', |
| 320 | 'break-background':'#000000', |
| 321 | 'hit-foreground':'#ffffff', |
| 322 | 'hit-background':'#000000', |
| 323 | 'error-foreground':'#ffffff', |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 324 | 'error-background':'#000000', |
| 325 | #cursor (only foreground can be set) |
| 326 | 'cursor-foreground':'#000000', |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 327 | #shell window |
| 328 | 'stdout-foreground':'#000000', |
| 329 | 'stdout-background':'#ffffff', |
| 330 | 'stderr-foreground':'#000000', |
| 331 | 'stderr-background':'#ffffff', |
| 332 | 'console-foreground':'#000000', |
| 333 | 'console-background':'#ffffff' } |
| 334 | for element in theme.keys(): |
Steven M. Gava | 052937f | 2002-02-11 02:20:53 +0000 | [diff] [blame] | 335 | if not cfgParser.has_option(themeName,element): |
| 336 | #we are going to return a default, print warning |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 337 | warning=('\n Warning: configHandler.py - IdleConf.GetThemeDict' |
| 338 | ' -\n problem retrieving theme element %r' |
| 339 | '\n from theme %r.\n' |
| 340 | ' returning default value: %r\n' % |
| 341 | (element, themeName, theme[element])) |
Steven M. Gava | 052937f | 2002-02-11 02:20:53 +0000 | [diff] [blame] | 342 | sys.stderr.write(warning) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 343 | colour=cfgParser.Get(themeName,element,default=theme[element]) |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 344 | theme[element]=colour |
| 345 | return theme |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 346 | |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 347 | def CurrentTheme(self): |
| 348 | """ |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 349 | Returns the name of the currently active theme |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 350 | """ |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 351 | return self.GetOption('main','Theme','name',default='') |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 352 | |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 353 | def CurrentKeys(self): |
| 354 | """ |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 355 | Returns the name of the currently active key set |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 356 | """ |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 357 | return self.GetOption('main','Keys','name',default='') |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 358 | |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 359 | def GetExtensions(self, activeOnly=1): |
| 360 | """ |
| 361 | Gets a list of all idle extensions declared in the config files. |
| 362 | activeOnly - boolean, if true only return active (enabled) extensions |
| 363 | """ |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 364 | extns=self.RemoveKeyBindNames( |
| 365 | self.GetSectionList('default','extensions')) |
| 366 | userExtns=self.RemoveKeyBindNames( |
| 367 | self.GetSectionList('user','extensions')) |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 368 | for extn in userExtns: |
| 369 | if extn not in extns: #user has added own extension |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 370 | extns.append(extn) |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 371 | if activeOnly: |
| 372 | activeExtns=[] |
| 373 | for extn in extns: |
Steven M. Gava | 5f28e8f | 2002-01-21 06:38:21 +0000 | [diff] [blame] | 374 | if self.GetOption('extensions',extn,'enable',default=1, |
| 375 | type='bool'): |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 376 | #the extension is enabled |
| 377 | activeExtns.append(extn) |
| 378 | return activeExtns |
| 379 | else: |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 380 | return extns |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 381 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 382 | def RemoveKeyBindNames(self,extnNameList): |
| 383 | #get rid of keybinding section names |
| 384 | names=extnNameList |
| 385 | kbNameIndicies=[] |
| 386 | for name in names: |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 387 | if name.endswith('_bindings') or name.endswith('_cfgBindings'): |
| 388 | kbNameIndicies.append(names.index(name)) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 389 | kbNameIndicies.sort() |
| 390 | kbNameIndicies.reverse() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 391 | for index in kbNameIndicies: #delete each keybinding section name |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 392 | del(names[index]) |
| 393 | return names |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 394 | |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 395 | def GetExtnNameForEvent(self,virtualEvent): |
| 396 | """ |
| 397 | Returns the name of the extension that virtualEvent is bound in, or |
| 398 | None if not bound in any extension. |
| 399 | virtualEvent - string, name of the virtual event to test for, without |
| 400 | the enclosing '<< >>' |
| 401 | """ |
| 402 | extName=None |
| 403 | vEvent='<<'+virtualEvent+'>>' |
| 404 | for extn in self.GetExtensions(activeOnly=0): |
| 405 | for event in self.GetExtensionKeys(extn).keys(): |
| 406 | if event == vEvent: |
| 407 | extName=extn |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 408 | return extName |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 409 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 410 | def GetExtensionKeys(self,extensionName): |
| 411 | """ |
| 412 | returns a dictionary of the configurable keybindings for a particular |
| 413 | extension,as they exist in the dictionary returned by GetCurrentKeySet; |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 414 | that is, where previously used bindings are disabled. |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 415 | """ |
| 416 | keysName=extensionName+'_cfgBindings' |
| 417 | activeKeys=self.GetCurrentKeySet() |
| 418 | extKeys={} |
| 419 | if self.defaultCfg['extensions'].has_section(keysName): |
| 420 | eventNames=self.defaultCfg['extensions'].GetOptionList(keysName) |
| 421 | for eventName in eventNames: |
| 422 | event='<<'+eventName+'>>' |
| 423 | binding=activeKeys[event] |
| 424 | extKeys[event]=binding |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 425 | return extKeys |
| 426 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 427 | def __GetRawExtensionKeys(self,extensionName): |
| 428 | """ |
| 429 | returns a dictionary of the configurable keybindings for a particular |
| 430 | extension, as defined in the configuration files, or an empty dictionary |
| 431 | if no bindings are found |
| 432 | """ |
| 433 | keysName=extensionName+'_cfgBindings' |
| 434 | extKeys={} |
| 435 | if self.defaultCfg['extensions'].has_section(keysName): |
| 436 | eventNames=self.defaultCfg['extensions'].GetOptionList(keysName) |
| 437 | for eventName in eventNames: |
| 438 | binding=self.GetOption('extensions',keysName, |
| 439 | eventName,default='').split() |
| 440 | event='<<'+eventName+'>>' |
| 441 | extKeys[event]=binding |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 442 | return extKeys |
| 443 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 444 | def GetExtensionBindings(self,extensionName): |
| 445 | """ |
| 446 | Returns a dictionary of all the event bindings for a particular |
| 447 | extension. The configurable keybindings are returned as they exist in |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 448 | the dictionary returned by GetCurrentKeySet; that is, where re-used |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 449 | keybindings are disabled. |
| 450 | """ |
| 451 | bindsName=extensionName+'_bindings' |
| 452 | extBinds=self.GetExtensionKeys(extensionName) |
| 453 | #add the non-configurable bindings |
| 454 | if self.defaultCfg['extensions'].has_section(bindsName): |
| 455 | eventNames=self.defaultCfg['extensions'].GetOptionList(bindsName) |
| 456 | for eventName in eventNames: |
| 457 | binding=self.GetOption('extensions',bindsName, |
| 458 | eventName,default='').split() |
| 459 | event='<<'+eventName+'>>' |
| 460 | extBinds[event]=binding |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 461 | |
| 462 | return extBinds |
| 463 | |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 464 | def GetKeyBinding(self, keySetName, eventStr): |
| 465 | """ |
| 466 | returns the keybinding for a specific event. |
| 467 | keySetName - string, name of key binding set |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 468 | eventStr - string, the virtual event we want the binding for, |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 469 | represented as a string, eg. '<<event>>' |
| 470 | """ |
| 471 | eventName=eventStr[2:-2] #trim off the angle brackets |
| 472 | binding=self.GetOption('keys',keySetName,eventName,default='').split() |
| 473 | return binding |
| 474 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 475 | def GetCurrentKeySet(self): |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 476 | return self.GetKeySet(self.CurrentKeys()) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 477 | |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 478 | def GetKeySet(self,keySetName): |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 479 | """ |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 480 | Returns a dictionary of: all requested core keybindings, plus the |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 481 | keybindings for all currently active extensions. If a binding defined |
| 482 | in an extension is already in use, that binding is disabled. |
| 483 | """ |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 484 | keySet=self.GetCoreKeys(keySetName) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 485 | activeExtns=self.GetExtensions(activeOnly=1) |
| 486 | for extn in activeExtns: |
| 487 | extKeys=self.__GetRawExtensionKeys(extn) |
| 488 | if extKeys: #the extension defines keybindings |
| 489 | for event in extKeys.keys(): |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 490 | if extKeys[event] in keySet.values(): |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 491 | #the binding is already in use |
| 492 | extKeys[event]='' #disable this binding |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 493 | keySet[event]=extKeys[event] #add binding |
| 494 | return keySet |
| 495 | |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 496 | def IsCoreBinding(self,virtualEvent): |
| 497 | """ |
| 498 | returns true if the virtual event is bound in the core idle keybindings. |
| 499 | virtualEvent - string, name of the virtual event to test for, without |
| 500 | the enclosing '<< >>' |
| 501 | """ |
| 502 | return ('<<'+virtualEvent+'>>') in self.GetCoreKeys().keys() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 503 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 504 | def GetCoreKeys(self, keySetName=None): |
| 505 | """ |
| 506 | returns the requested set of core keybindings, with fallbacks if |
| 507 | required. |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 508 | Keybindings loaded from the config file(s) are loaded _over_ these |
| 509 | defaults, so if there is a problem getting any core binding there will |
| 510 | be an 'ultimate last resort fallback' to the CUA-ish bindings |
| 511 | defined here. |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 512 | """ |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 513 | keyBindings={ |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 514 | '<<copy>>': ['<Control-c>', '<Control-C>'], |
| 515 | '<<cut>>': ['<Control-x>', '<Control-X>'], |
| 516 | '<<paste>>': ['<Control-v>', '<Control-V>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 517 | '<<beginning-of-line>>': ['<Control-a>', '<Home>'], |
| 518 | '<<center-insert>>': ['<Control-l>'], |
| 519 | '<<close-all-windows>>': ['<Control-q>'], |
| 520 | '<<close-window>>': ['<Alt-F4>'], |
Kurt B. Kaiser | 84f4803 | 2002-09-26 22:13:22 +0000 | [diff] [blame] | 521 | '<<do-nothing>>': ['<Control-x>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 522 | '<<end-of-file>>': ['<Control-d>'], |
| 523 | '<<python-docs>>': ['<F1>'], |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 524 | '<<python-context-help>>': ['<Shift-F1>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 525 | '<<history-next>>': ['<Alt-n>'], |
| 526 | '<<history-previous>>': ['<Alt-p>'], |
| 527 | '<<interrupt-execution>>': ['<Control-c>'], |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 528 | '<<view-restart>>': ['<F6>'], |
Kurt B. Kaiser | 4cc5ef5 | 2003-01-22 00:23:23 +0000 | [diff] [blame] | 529 | '<<restart-shell>>': ['<Control-F6>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 530 | '<<open-class-browser>>': ['<Alt-c>'], |
| 531 | '<<open-module>>': ['<Alt-m>'], |
| 532 | '<<open-new-window>>': ['<Control-n>'], |
| 533 | '<<open-window-from-file>>': ['<Control-o>'], |
| 534 | '<<plain-newline-and-indent>>': ['<Control-j>'], |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 535 | '<<print-window>>': ['<Control-p>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 536 | '<<redo>>': ['<Control-y>'], |
| 537 | '<<remove-selection>>': ['<Escape>'], |
Kurt B. Kaiser | 2303b1c | 2003-11-24 05:26:16 +0000 | [diff] [blame] | 538 | '<<save-copy-of-window-as-file>>': ['<Alt-Shift-S>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 539 | '<<save-window-as-file>>': ['<Alt-s>'], |
| 540 | '<<save-window>>': ['<Control-s>'], |
| 541 | '<<select-all>>': ['<Alt-a>'], |
| 542 | '<<toggle-auto-coloring>>': ['<Control-slash>'], |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 543 | '<<undo>>': ['<Control-z>'], |
| 544 | '<<find-again>>': ['<Control-g>', '<F3>'], |
| 545 | '<<find-in-files>>': ['<Alt-F3>'], |
| 546 | '<<find-selection>>': ['<Control-F3>'], |
| 547 | '<<find>>': ['<Control-f>'], |
| 548 | '<<replace>>': ['<Control-h>'], |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 549 | '<<goto-line>>': ['<Alt-g>'], |
Kurt B. Kaiser | a9f8cbc | 2002-09-14 03:17:01 +0000 | [diff] [blame] | 550 | '<<smart-backspace>>': ['<Key-BackSpace>'], |
| 551 | '<<newline-and-indent>>': ['<Key-Return> <Key-KP_Enter>'], |
| 552 | '<<smart-indent>>': ['<Key-Tab>'], |
| 553 | '<<indent-region>>': ['<Control-Key-bracketright>'], |
| 554 | '<<dedent-region>>': ['<Control-Key-bracketleft>'], |
| 555 | '<<comment-region>>': ['<Alt-Key-3>'], |
| 556 | '<<uncomment-region>>': ['<Alt-Key-4>'], |
| 557 | '<<tabify-region>>': ['<Alt-Key-5>'], |
| 558 | '<<untabify-region>>': ['<Alt-Key-6>'], |
| 559 | '<<toggle-tabs>>': ['<Alt-Key-t>'], |
| 560 | '<<change-indentwidth>>': ['<Alt-Key-u>'] |
| 561 | } |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 562 | if keySetName: |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 563 | for event in keyBindings.keys(): |
| 564 | binding=self.GetKeyBinding(keySetName,event) |
Steven M. Gava | 4974575 | 2002-02-18 01:43:11 +0000 | [diff] [blame] | 565 | if binding: |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 566 | keyBindings[event]=binding |
Steven M. Gava | 4974575 | 2002-02-18 01:43:11 +0000 | [diff] [blame] | 567 | else: #we are going to return a default, print warning |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 568 | warning=('\n Warning: configHandler.py - IdleConf.GetCoreKeys' |
| 569 | ' -\n problem retrieving key binding for event %r' |
| 570 | '\n from key set %r.\n' |
| 571 | ' returning default value: %r\n' % |
| 572 | (event, keySetName, keyBindings[event])) |
Steven M. Gava | 4974575 | 2002-02-18 01:43:11 +0000 | [diff] [blame] | 573 | sys.stderr.write(warning) |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 574 | return keyBindings |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 575 | |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 576 | def GetExtraHelpSourceList(self,configSet): |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 577 | """Fetch list of extra help sources from a given configSet. |
Kurt B. Kaiser | e66675b | 2003-01-27 02:36:18 +0000 | [diff] [blame] | 578 | |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 579 | Valid configSets are 'user' or 'default'. Return a list of tuples of |
| 580 | the form (menu_item , path_to_help_file , option), or return the empty |
| 581 | list. 'option' is the sequence number of the help resource. 'option' |
| 582 | values determine the position of the menu items on the Help menu, |
| 583 | therefore the returned list must be sorted by 'option'. |
| 584 | |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 585 | """ |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 586 | helpSources=[] |
| 587 | if configSet=='user': |
| 588 | cfgParser=self.userCfg['main'] |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 589 | elif configSet=='default': |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 590 | cfgParser=self.defaultCfg['main'] |
| 591 | else: |
Neal Norwitz | 5b0b00f | 2002-11-30 19:10:19 +0000 | [diff] [blame] | 592 | raise InvalidConfigSet, 'Invalid configSet specified' |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 593 | options=cfgParser.GetOptionList('HelpFiles') |
| 594 | for option in options: |
| 595 | value=cfgParser.Get('HelpFiles',option,default=';') |
| 596 | if value.find(';')==-1: #malformed config entry with no ';' |
| 597 | menuItem='' #make these empty |
| 598 | helpPath='' #so value won't be added to list |
| 599 | else: #config entry contains ';' as expected |
| 600 | value=string.split(value,';') |
| 601 | menuItem=value[0].strip() |
| 602 | helpPath=value[1].strip() |
| 603 | if menuItem and helpPath: #neither are empty strings |
| 604 | helpSources.append( (menuItem,helpPath,option) ) |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 605 | helpSources.sort(self.__helpsort) |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 606 | return helpSources |
| 607 | |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 608 | def __helpsort(self, h1, h2): |
| 609 | if int(h1[2]) < int(h2[2]): |
| 610 | return -1 |
| 611 | elif int(h1[2]) > int(h2[2]): |
| 612 | return 1 |
| 613 | else: |
| 614 | return 0 |
| 615 | |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 616 | def GetAllExtraHelpSourcesList(self): |
| 617 | """ |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 618 | Returns a list of tuples containing the details of all additional help |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 619 | sources configured, or an empty list if there are none. Tuples are of |
| 620 | the format returned by GetExtraHelpSourceList. |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 621 | """ |
| 622 | allHelpSources=( self.GetExtraHelpSourceList('default')+ |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 623 | self.GetExtraHelpSourceList('user') ) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 624 | return allHelpSources |
| 625 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 626 | def LoadCfgFiles(self): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 627 | """ |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 628 | load all configuration files. |
| 629 | """ |
| 630 | for key in self.defaultCfg.keys(): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 631 | self.defaultCfg[key].Load() |
| 632 | self.userCfg[key].Load() #same keys |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 633 | |
| 634 | def SaveUserCfgFiles(self): |
| 635 | """ |
| 636 | write all loaded user configuration files back to disk |
| 637 | """ |
| 638 | for key in self.userCfg.keys(): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 639 | self.userCfg[key].Save() |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 640 | |
| 641 | idleConf=IdleConf() |
| 642 | |
| 643 | ### module test |
| 644 | if __name__ == '__main__': |
| 645 | def dumpCfg(cfg): |
| 646 | print '\n',cfg,'\n' |
| 647 | for key in cfg.keys(): |
| 648 | sections=cfg[key].sections() |
| 649 | print key |
| 650 | print sections |
| 651 | for section in sections: |
| 652 | options=cfg[key].options(section) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 653 | print section |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 654 | print options |
| 655 | for option in options: |
| 656 | print option, '=', cfg[key].Get(section,option) |
| 657 | dumpCfg(idleConf.defaultCfg) |
| 658 | dumpCfg(idleConf.userCfg) |
| 659 | print idleConf.userCfg['main'].Get('Theme','name') |
| 660 | #print idleConf.userCfg['highlight'].GetDefHighlight('Foo','normal') |