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