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