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