Terry Jan Reedy | f46b782 | 2016-11-07 17:15:01 -0500 | [diff] [blame] | 1 | """idlelib.config -- Manage IDLE configuration information. |
Steven M. Gava | c597640 | 2002-01-04 03:06:08 +0000 | [diff] [blame] | 2 | |
Terry Jan Reedy | f46b782 | 2016-11-07 17:15:01 -0500 | [diff] [blame] | 3 | The comments at the beginning of config-main.def describe the |
| 4 | configuration files and the design implemented to update user |
| 5 | configuration information. In particular, user configuration choices |
| 6 | which duplicate the defaults will be removed from the user's |
| 7 | configuration files, and if a user file becomes empty, it will be |
| 8 | deleted. |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 9 | |
KunYuChen | f3e8209 | 2017-06-21 12:30:45 +0800 | [diff] [blame] | 10 | The configuration database maps options to values. Conceptually, the |
Terry Jan Reedy | f46b782 | 2016-11-07 17:15:01 -0500 | [diff] [blame] | 11 | database keys are tuples (config-type, section, item). As implemented, |
| 12 | there are separate dicts for default and user values. Each has |
| 13 | config-type keys 'main', 'extensions', 'highlight', and 'keys'. The |
| 14 | value for each key is a ConfigParser instance that maps section and item |
| 15 | to values. For 'main' and 'extenstons', user values override |
| 16 | default values. For 'highlight' and 'keys', user sections augment the |
| 17 | default sections (and must, therefore, have distinct names). |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 18 | |
| 19 | Throughout this module there is an emphasis on returning useable defaults |
| 20 | when a problem occurs in returning a requested configuration value back to |
| 21 | idle. This is to allow IDLE to continue to function in spite of errors in |
| 22 | the retrieval of config information. When a default is returned instead of |
| 23 | a requested config value, a message is printed to stderr to aid in |
| 24 | configuration problem notification and resolution. |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 25 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 26 | # TODOs added Oct 2014, tjr |
| 27 | |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 28 | from configparser import ConfigParser |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 29 | import os |
| 30 | import sys |
Guido van Rossum | 36e0a92 | 2007-07-20 04:05:57 +0000 | [diff] [blame] | 31 | |
Victor Stinner | d6debb2 | 2017-03-27 16:05:26 +0200 | [diff] [blame] | 32 | from tkinter.font import Font |
Louie Lu | f776eb0 | 2017-07-19 05:17:56 +0800 | [diff] [blame] | 33 | import idlelib |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 34 | |
Neal Norwitz | 5b0b00f | 2002-11-30 19:10:19 +0000 | [diff] [blame] | 35 | class InvalidConfigType(Exception): pass |
| 36 | class InvalidConfigSet(Exception): pass |
| 37 | class InvalidFgBg(Exception): pass |
| 38 | class InvalidTheme(Exception): pass |
| 39 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 40 | class IdleConfParser(ConfigParser): |
| 41 | """ |
| 42 | A ConfigParser specialised for idle configuration file handling |
| 43 | """ |
| 44 | def __init__(self, cfgFile, cfgDefaults=None): |
| 45 | """ |
| 46 | cfgFile - string, fully specified configuration file name |
| 47 | """ |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 48 | self.file = cfgFile # This is currently '' when testing. |
Serhiy Storchaka | 8995300 | 2013-02-07 15:24:36 +0200 | [diff] [blame] | 49 | ConfigParser.__init__(self, defaults=cfgDefaults, strict=False) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 50 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 51 | def Get(self, section, option, type=None, default=None, raw=False): |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 52 | """ |
| 53 | Get an option value for given section/option or return default. |
| 54 | If type is specified, return as type. |
| 55 | """ |
Terry Jan Reedy | a9421fb | 2014-10-22 20:15:18 -0400 | [diff] [blame] | 56 | # TODO Use default as fallback, at least if not None |
| 57 | # Should also print Warning(file, section, option). |
| 58 | # Currently may raise ValueError |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 59 | if not self.has_option(section, option): |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 60 | return default |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 61 | if type == 'bool': |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 62 | return self.getboolean(section, option) |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 63 | elif type == 'int': |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 64 | return self.getint(section, option) |
| 65 | else: |
| 66 | return self.get(section, option, raw=raw) |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 67 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 68 | def GetOptionList(self, section): |
| 69 | "Return a list of options for given section, else []." |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 70 | if self.has_section(section): |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 71 | return self.options(section) |
| 72 | else: #return a default value |
| 73 | return [] |
| 74 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 75 | def Load(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 76 | "Load the configuration file from disk." |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 77 | if self.file: |
| 78 | self.read(self.file) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 79 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 80 | class IdleUserConfParser(IdleConfParser): |
| 81 | """ |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 82 | IdleConfigParser specialised for user configuration handling. |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 83 | """ |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 84 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 85 | def SetOption(self, section, option, value): |
| 86 | """Return True if option is added or changed to value, else False. |
| 87 | |
| 88 | Add section if required. False means option already had value. |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 89 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 90 | if self.has_option(section, option): |
| 91 | if self.get(section, option) == value: |
| 92 | return False |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 93 | else: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 94 | self.set(section, option, value) |
| 95 | return True |
Steven M. Gava | 2d7bb3f | 2002-01-29 08:35:29 +0000 | [diff] [blame] | 96 | else: |
| 97 | if not self.has_section(section): |
| 98 | self.add_section(section) |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 99 | self.set(section, option, value) |
| 100 | return True |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 101 | |
Louie Lu | 50c9435 | 2017-07-13 02:05:32 +0800 | [diff] [blame] | 102 | def RemoveOption(self, section, option): |
| 103 | """Return True if option is removed from section, else False. |
| 104 | |
| 105 | False if either section does not exist or did not have option. |
| 106 | """ |
| 107 | if self.has_section(section): |
| 108 | return self.remove_option(section, option) |
| 109 | return False |
| 110 | |
| 111 | def AddSection(self, section): |
| 112 | "If section doesn't exist, add it." |
| 113 | if not self.has_section(section): |
| 114 | self.add_section(section) |
| 115 | |
| 116 | def RemoveEmptySections(self): |
| 117 | "Remove any sections that have no options." |
| 118 | for section in self.sections(): |
| 119 | if not self.GetOptionList(section): |
| 120 | self.remove_section(section) |
| 121 | |
| 122 | def IsEmpty(self): |
| 123 | "Return True if no sections after removing empty sections." |
| 124 | self.RemoveEmptySections() |
| 125 | return not self.sections() |
| 126 | |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 127 | def RemoveFile(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 128 | "Remove user config file self.file from disk if it exists." |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 129 | if os.path.exists(self.file): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 130 | os.remove(self.file) |
| 131 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 132 | def Save(self): |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 133 | """Update user configuration file. |
| 134 | |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 135 | If self not empty after removing empty sections, write the file |
| 136 | to disk. Otherwise, remove the file from disk if it exists. |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 137 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 138 | """ |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 139 | fname = self.file |
| 140 | if fname: |
| 141 | if not self.IsEmpty(): |
| 142 | try: |
| 143 | cfgFile = open(fname, 'w') |
| 144 | except OSError: |
| 145 | os.unlink(fname) |
| 146 | cfgFile = open(fname, 'w') |
| 147 | with cfgFile: |
| 148 | self.write(cfgFile) |
| 149 | else: |
| 150 | self.RemoveFile() |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 151 | |
| 152 | class IdleConf: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 153 | """Hold config parsers for all idle config files in singleton instance. |
| 154 | |
| 155 | Default config files, self.defaultCfg -- |
| 156 | for config_type in self.config_types: |
| 157 | (idle install dir)/config-{config-type}.def |
| 158 | |
| 159 | User config files, self.userCfg -- |
| 160 | for config_type in self.config_types: |
| 161 | (user home dir)/.idlerc/config-{config-type}.cfg |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 162 | """ |
Louie Lu | f776eb0 | 2017-07-19 05:17:56 +0800 | [diff] [blame] | 163 | def __init__(self, _utest=False): |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 164 | self.config_types = ('main', 'highlight', 'keys', 'extensions') |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 165 | self.defaultCfg = {} |
| 166 | self.userCfg = {} |
| 167 | self.cfg = {} # TODO use to select userCfg vs defaultCfg |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 168 | |
Louie Lu | f776eb0 | 2017-07-19 05:17:56 +0800 | [diff] [blame] | 169 | if not _utest: |
| 170 | self.CreateConfigHandlers() |
| 171 | self.LoadCfgFiles() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 172 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 173 | def CreateConfigHandlers(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 174 | "Populate default and user config parser dictionaries." |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 175 | #build idle install path |
| 176 | if __name__ != '__main__': # we were imported |
terryjreedy | 223c7e7 | 2017-07-07 22:28:06 -0400 | [diff] [blame] | 177 | idleDir = os.path.dirname(__file__) |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 178 | else: # we were exec'ed (for testing only) |
terryjreedy | 223c7e7 | 2017-07-07 22:28:06 -0400 | [diff] [blame] | 179 | idleDir = os.path.abspath(sys.path[0]) |
| 180 | self.userdir = userDir = self.GetUserCfgDir() |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 181 | |
| 182 | defCfgFiles = {} |
| 183 | usrCfgFiles = {} |
| 184 | # TODO eliminate these temporaries by combining loops |
| 185 | for cfgType in self.config_types: #build config file names |
| 186 | defCfgFiles[cfgType] = os.path.join( |
| 187 | idleDir, 'config-' + cfgType + '.def') |
| 188 | usrCfgFiles[cfgType] = os.path.join( |
| 189 | userDir, 'config-' + cfgType + '.cfg') |
| 190 | for cfgType in self.config_types: #create config parsers |
| 191 | self.defaultCfg[cfgType] = IdleConfParser(defCfgFiles[cfgType]) |
| 192 | self.userCfg[cfgType] = IdleUserConfParser(usrCfgFiles[cfgType]) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 193 | |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 194 | def GetUserCfgDir(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 195 | """Return a filesystem directory for storing user config files. |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 196 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 197 | Creates it if required. |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 198 | """ |
Kurt B. Kaiser | 1b6f398 | 2005-01-11 19:29:39 +0000 | [diff] [blame] | 199 | cfgDir = '.idlerc' |
| 200 | userDir = os.path.expanduser('~') |
| 201 | if userDir != '~': # expanduser() found user home dir |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 202 | if not os.path.exists(userDir): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 203 | warn = ('\n Warning: os.path.expanduser("~") points to\n ' + |
| 204 | userDir + ',\n but the path does not exist.') |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 205 | try: |
Terry Jan Reedy | 81b062f | 2014-09-19 22:38:41 -0400 | [diff] [blame] | 206 | print(warn, file=sys.stderr) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 207 | except OSError: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 208 | pass |
Kurt B. Kaiser | 1b6f398 | 2005-01-11 19:29:39 +0000 | [diff] [blame] | 209 | userDir = '~' |
| 210 | if userDir == "~": # still no path to home! |
| 211 | # traditionally IDLE has defaulted to os.getcwd(), is this adequate? |
| 212 | userDir = os.getcwd() |
| 213 | userDir = os.path.join(userDir, cfgDir) |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 214 | if not os.path.exists(userDir): |
Kurt B. Kaiser | 1b6f398 | 2005-01-11 19:29:39 +0000 | [diff] [blame] | 215 | try: |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 216 | os.mkdir(userDir) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 217 | except OSError: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 218 | warn = ('\n Warning: unable to create user config directory\n' + |
| 219 | userDir + '\n Check path and permissions.\n Exiting!\n') |
Louie Lu | f776eb0 | 2017-07-19 05:17:56 +0800 | [diff] [blame] | 220 | if not idlelib.testing: |
| 221 | print(warn, file=sys.stderr) |
Kurt B. Kaiser | 1b6f398 | 2005-01-11 19:29:39 +0000 | [diff] [blame] | 222 | raise SystemExit |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 223 | # TODO continue without userDIr instead of exit |
Steven M. Gava | 7cff66d | 2002-02-01 03:02:37 +0000 | [diff] [blame] | 224 | return userDir |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 225 | |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 226 | def GetOption(self, configType, section, option, default=None, type=None, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 227 | warn_on_default=True, raw=False): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 228 | """Return a value for configType section option, or default. |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 229 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 230 | If type is not None, return a value of that type. Also pass raw |
| 231 | to the config parser. First try to return a valid value |
| 232 | (including type) from a user configuration. If that fails, try |
| 233 | the default configuration. If that fails, return default, with a |
| 234 | default of None. |
| 235 | |
| 236 | Warn if either user or default configurations have an invalid value. |
| 237 | Warn if default is returned and warn_on_default is True. |
Steven M. Gava | 429a86af | 2001-10-23 10:42:12 +0000 | [diff] [blame] | 238 | """ |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 239 | try: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 240 | if self.userCfg[configType].has_option(section, option): |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 241 | return self.userCfg[configType].Get(section, option, |
| 242 | type=type, raw=raw) |
| 243 | except ValueError: |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 244 | warning = ('\n Warning: config.py - IdleConf.GetOption -\n' |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 245 | ' invalid %r value for configuration option %r\n' |
Terry Jan Reedy | 81b062f | 2014-09-19 22:38:41 -0400 | [diff] [blame] | 246 | ' from section %r: %r' % |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 247 | (type, option, section, |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 248 | self.userCfg[configType].Get(section, option, raw=raw))) |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 249 | _warn(warning, configType, section, option) |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 250 | try: |
| 251 | if self.defaultCfg[configType].has_option(section,option): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 252 | return self.defaultCfg[configType].Get( |
| 253 | section, option, type=type, raw=raw) |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 254 | except ValueError: |
| 255 | pass |
| 256 | #returning default, print warning |
| 257 | if warn_on_default: |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 258 | warning = ('\n Warning: config.py - IdleConf.GetOption -\n' |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 259 | ' problem retrieving configuration option %r\n' |
| 260 | ' from section %r.\n' |
Terry Jan Reedy | 81b062f | 2014-09-19 22:38:41 -0400 | [diff] [blame] | 261 | ' returning default value: %r' % |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 262 | (option, section, default)) |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 263 | _warn(warning, configType, section, option) |
Andrew Svetlov | 8a495a4 | 2012-12-24 13:15:43 +0200 | [diff] [blame] | 264 | return default |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 265 | |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 266 | def SetOption(self, configType, section, option, value): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 267 | """Set section option to value in user config file.""" |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 268 | self.userCfg[configType].SetOption(section, option, value) |
| 269 | |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 270 | def GetSectionList(self, configSet, configType): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 271 | """Return sections for configSet configType configuration. |
| 272 | |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 273 | configSet must be either 'user' or 'default' |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 274 | configType must be in self.config_types. |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 275 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 276 | if not (configType in self.config_types): |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 277 | raise InvalidConfigType('Invalid configType specified') |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 278 | if configSet == 'user': |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 279 | cfgParser = self.userCfg[configType] |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 280 | elif configSet == 'default': |
| 281 | cfgParser=self.defaultCfg[configType] |
| 282 | else: |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 283 | raise InvalidConfigSet('Invalid configSet specified') |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 284 | return cfgParser.sections() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 285 | |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 286 | def GetHighlight(self, theme, element, fgBg=None): |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 287 | """Return individual theme element highlight color(s). |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 288 | |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 289 | fgBg - string ('fg' or 'bg') or None. |
| 290 | If None, return a dictionary containing fg and bg colors with |
| 291 | keys 'foreground' and 'background'. Otherwise, only return |
| 292 | fg or bg color, as specified. Colors are intended to be |
| 293 | 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] | 294 | """ |
Steven M. Gava | 9f25e67 | 2002-02-11 02:51:18 +0000 | [diff] [blame] | 295 | if self.defaultCfg['highlight'].has_section(theme): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 296 | themeDict = self.GetThemeDict('default', theme) |
Steven M. Gava | 9f25e67 | 2002-02-11 02:51:18 +0000 | [diff] [blame] | 297 | else: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 298 | themeDict = self.GetThemeDict('user', theme) |
| 299 | fore = themeDict[element + '-foreground'] |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 300 | if element == 'cursor': # There is no config value for cursor bg |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 301 | back = themeDict['normal-background'] |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 302 | else: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 303 | back = themeDict[element + '-background'] |
| 304 | highlight = {"foreground": fore, "background": back} |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 305 | if not fgBg: # Return dict of both colors |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 306 | return highlight |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 307 | else: # Return specified color only |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 308 | if fgBg == 'fg': |
| 309 | return highlight["foreground"] |
| 310 | if fgBg == 'bg': |
| 311 | return highlight["background"] |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 312 | else: |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 313 | raise InvalidFgBg('Invalid fgBg specified') |
Steven M. Gava | 9f25e67 | 2002-02-11 02:51:18 +0000 | [diff] [blame] | 314 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 315 | def GetThemeDict(self, type, themeName): |
| 316 | """Return {option:value} dict for elements in themeName. |
| 317 | |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 318 | type - string, 'default' or 'user' theme type |
| 319 | themeName - string, theme name |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 320 | Values are loaded over ultimate fallback defaults to guarantee |
| 321 | that all theme elements are present in a newly created theme. |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 322 | """ |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 323 | if type == 'user': |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 324 | cfgParser = self.userCfg['highlight'] |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 325 | elif type == 'default': |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 326 | cfgParser = self.defaultCfg['highlight'] |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 327 | else: |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 328 | raise InvalidTheme('Invalid theme type specified') |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 329 | # Provide foreground and background colors for each theme |
| 330 | # element (other than cursor) even though some values are not |
| 331 | # yet used by idle, to allow for their use in the future. |
| 332 | # Default values are generally black and white. |
| 333 | # TODO copy theme from a class attribute. |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 334 | theme ={'normal-foreground':'#000000', |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 335 | 'normal-background':'#ffffff', |
| 336 | 'keyword-foreground':'#000000', |
| 337 | 'keyword-background':'#ffffff', |
Kurt B. Kaiser | 73360a3 | 2004-03-08 18:15:31 +0000 | [diff] [blame] | 338 | 'builtin-foreground':'#000000', |
| 339 | 'builtin-background':'#ffffff', |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 340 | 'comment-foreground':'#000000', |
| 341 | 'comment-background':'#ffffff', |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 342 | 'string-foreground':'#000000', |
| 343 | 'string-background':'#ffffff', |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 344 | 'definition-foreground':'#000000', |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 345 | 'definition-background':'#ffffff', |
| 346 | 'hilite-foreground':'#000000', |
| 347 | 'hilite-background':'gray', |
| 348 | 'break-foreground':'#ffffff', |
| 349 | 'break-background':'#000000', |
| 350 | 'hit-foreground':'#ffffff', |
| 351 | 'hit-background':'#000000', |
| 352 | 'error-foreground':'#ffffff', |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 353 | 'error-background':'#000000', |
| 354 | #cursor (only foreground can be set) |
| 355 | 'cursor-foreground':'#000000', |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 356 | #shell window |
| 357 | 'stdout-foreground':'#000000', |
| 358 | 'stdout-background':'#ffffff', |
| 359 | 'stderr-foreground':'#000000', |
| 360 | 'stderr-background':'#ffffff', |
| 361 | 'console-foreground':'#000000', |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 362 | 'console-background':'#ffffff', |
Cheryl Sabella | de65162 | 2018-06-01 21:45:54 -0400 | [diff] [blame] | 363 | 'context-foreground':'#000000', |
| 364 | 'context-background':'#ffffff', |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 365 | } |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 366 | for element in theme: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 367 | if not cfgParser.has_option(themeName, element): |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 368 | # Print warning that will return a default color |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 369 | warning = ('\n Warning: config.IdleConf.GetThemeDict' |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 370 | ' -\n problem retrieving theme element %r' |
| 371 | '\n from theme %r.\n' |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 372 | ' returning default color: %r' % |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 373 | (element, themeName, theme[element])) |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 374 | _warn(warning, 'highlight', themeName, element) |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 375 | theme[element] = cfgParser.Get( |
| 376 | themeName, element, default=theme[element]) |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 377 | return theme |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 378 | |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 379 | def CurrentTheme(self): |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 380 | "Return the name of the currently active text color theme." |
| 381 | return self.current_colors_and_keys('Theme') |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 382 | |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 383 | def CurrentKeys(self): |
| 384 | """Return the name of the currently active key set.""" |
| 385 | return self.current_colors_and_keys('Keys') |
| 386 | |
| 387 | def current_colors_and_keys(self, section): |
| 388 | """Return the currently active name for Theme or Keys section. |
| 389 | |
| 390 | idlelib.config-main.def ('default') includes these sections |
| 391 | |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 392 | [Theme] |
| 393 | default= 1 |
| 394 | name= IDLE Classic |
| 395 | name2= |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 396 | |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 397 | [Keys] |
| 398 | default= 1 |
| 399 | name= |
| 400 | name2= |
| 401 | |
| 402 | Item 'name2', is used for built-in ('default') themes and keys |
| 403 | added after 2015 Oct 1 and 2016 July 1. This kludge is needed |
| 404 | because setting 'name' to a builtin not defined in older IDLEs |
| 405 | to display multiple error messages or quit. |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 406 | See https://bugs.python.org/issue25313. |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 407 | When default = True, 'name2' takes precedence over 'name', |
| 408 | while older IDLEs will just use name. When default = False, |
| 409 | 'name2' may still be set, but it is ignored. |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 410 | """ |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 411 | cfgname = 'highlight' if section == 'Theme' else 'keys' |
Terry Jan Reedy | 5acf4e5 | 2016-08-24 22:08:01 -0400 | [diff] [blame] | 412 | default = self.GetOption('main', section, 'default', |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 413 | type='bool', default=True) |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 414 | name = '' |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 415 | if default: |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 416 | name = self.GetOption('main', section, 'name2', default='') |
| 417 | if not name: |
| 418 | name = self.GetOption('main', section, 'name', default='') |
| 419 | if name: |
| 420 | source = self.defaultCfg if default else self.userCfg |
| 421 | if source[cfgname].has_section(name): |
| 422 | return name |
| 423 | return "IDLE Classic" if section == 'Theme' else self.default_keys() |
| 424 | |
| 425 | @staticmethod |
| 426 | def default_keys(): |
| 427 | if sys.platform[:3] == 'win': |
| 428 | return 'IDLE Classic Windows' |
| 429 | elif sys.platform == 'darwin': |
| 430 | return 'IDLE Classic OSX' |
Terry Jan Reedy | c15a7c6 | 2015-11-12 15:06:07 -0500 | [diff] [blame] | 431 | else: |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 432 | return 'IDLE Modern Unix' |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 433 | |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 434 | def GetExtensions(self, active_only=True, |
| 435 | editor_only=False, shell_only=False): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 436 | """Return extensions in default and user config-extensions files. |
| 437 | |
| 438 | If active_only True, only return active (enabled) extensions |
| 439 | and optionally only editor or shell extensions. |
| 440 | If active_only False, return all extensions. |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 441 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 442 | extns = self.RemoveKeyBindNames( |
| 443 | self.GetSectionList('default', 'extensions')) |
| 444 | userExtns = self.RemoveKeyBindNames( |
| 445 | self.GetSectionList('user', 'extensions')) |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 446 | for extn in userExtns: |
| 447 | if extn not in extns: #user has added own extension |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 448 | extns.append(extn) |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 449 | for extn in ('AutoComplete','CodeContext', |
| 450 | 'FormatParagraph','ParenMatch'): |
| 451 | extns.remove(extn) |
| 452 | # specific exclusions because we are storing config for mainlined old |
| 453 | # extensions in config-extensions.def for backward compatibility |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 454 | if active_only: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 455 | activeExtns = [] |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 456 | for extn in extns: |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 457 | if self.GetOption('extensions', extn, 'enable', default=True, |
| 458 | type='bool'): |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 459 | #the extension is enabled |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 460 | if editor_only or shell_only: # TODO both True contradict |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 461 | if editor_only: |
| 462 | option = "enable_editor" |
| 463 | else: |
| 464 | option = "enable_shell" |
| 465 | if self.GetOption('extensions', extn,option, |
| 466 | default=True, type='bool', |
| 467 | warn_on_default=False): |
| 468 | activeExtns.append(extn) |
| 469 | else: |
| 470 | activeExtns.append(extn) |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 471 | return activeExtns |
| 472 | else: |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 473 | return extns |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 474 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 475 | def RemoveKeyBindNames(self, extnNameList): |
| 476 | "Return extnNameList with keybinding section names removed." |
Louie Lu | f776eb0 | 2017-07-19 05:17:56 +0800 | [diff] [blame] | 477 | return [n for n in extnNameList if not n.endswith(('_bindings', '_cfgBindings'))] |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 478 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 479 | def GetExtnNameForEvent(self, virtualEvent): |
| 480 | """Return the name of the extension binding virtualEvent, or None. |
| 481 | |
| 482 | virtualEvent - string, name of the virtual event to test for, |
| 483 | without the enclosing '<< >>' |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 484 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 485 | extName = None |
| 486 | vEvent = '<<' + virtualEvent + '>>' |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 487 | for extn in self.GetExtensions(active_only=0): |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 488 | for event in self.GetExtensionKeys(extn): |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 489 | if event == vEvent: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 490 | extName = extn # TODO return here? |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 491 | return extName |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 492 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 493 | def GetExtensionKeys(self, extensionName): |
| 494 | """Return dict: {configurable extensionName event : active keybinding}. |
| 495 | |
| 496 | Events come from default config extension_cfgBindings section. |
| 497 | Keybindings come from GetCurrentKeySet() active key dict, |
| 498 | where previously used bindings are disabled. |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 499 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 500 | keysName = extensionName + '_cfgBindings' |
| 501 | activeKeys = self.GetCurrentKeySet() |
| 502 | extKeys = {} |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 503 | if self.defaultCfg['extensions'].has_section(keysName): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 504 | eventNames = self.defaultCfg['extensions'].GetOptionList(keysName) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 505 | for eventName in eventNames: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 506 | event = '<<' + eventName + '>>' |
| 507 | binding = activeKeys[event] |
| 508 | extKeys[event] = binding |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 509 | return extKeys |
| 510 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 511 | def __GetRawExtensionKeys(self,extensionName): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 512 | """Return dict {configurable extensionName event : keybinding list}. |
| 513 | |
| 514 | Events come from default config extension_cfgBindings section. |
| 515 | Keybindings list come from the splitting of GetOption, which |
| 516 | tries user config before default config. |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 517 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 518 | keysName = extensionName+'_cfgBindings' |
| 519 | extKeys = {} |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 520 | if self.defaultCfg['extensions'].has_section(keysName): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 521 | eventNames = self.defaultCfg['extensions'].GetOptionList(keysName) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 522 | for eventName in eventNames: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 523 | binding = self.GetOption( |
| 524 | 'extensions', keysName, eventName, default='').split() |
| 525 | event = '<<' + eventName + '>>' |
| 526 | extKeys[event] = binding |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 527 | return extKeys |
| 528 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 529 | def GetExtensionBindings(self, extensionName): |
| 530 | """Return dict {extensionName event : active or defined keybinding}. |
| 531 | |
| 532 | Augment self.GetExtensionKeys(extensionName) with mapping of non- |
| 533 | configurable events (from default config) to GetOption splits, |
| 534 | as in self.__GetRawExtensionKeys. |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 535 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 536 | bindsName = extensionName + '_bindings' |
| 537 | extBinds = self.GetExtensionKeys(extensionName) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 538 | #add the non-configurable bindings |
| 539 | if self.defaultCfg['extensions'].has_section(bindsName): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 540 | eventNames = self.defaultCfg['extensions'].GetOptionList(bindsName) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 541 | for eventName in eventNames: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 542 | binding = self.GetOption( |
| 543 | 'extensions', bindsName, eventName, default='').split() |
| 544 | event = '<<' + eventName + '>>' |
| 545 | extBinds[event] = binding |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 546 | |
| 547 | return extBinds |
| 548 | |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 549 | def GetKeyBinding(self, keySetName, eventStr): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 550 | """Return the keybinding list for keySetName eventStr. |
| 551 | |
| 552 | keySetName - name of key binding set (config-keys section). |
| 553 | eventStr - virtual event, including brackets, as in '<<event>>'. |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 554 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 555 | eventName = eventStr[2:-2] #trim off the angle brackets |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 556 | binding = self.GetOption('keys', keySetName, eventName, default='', |
| 557 | warn_on_default=False).split() |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 558 | return binding |
| 559 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 560 | def GetCurrentKeySet(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 561 | "Return CurrentKeys with 'darwin' modifications." |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 562 | result = self.GetKeySet(self.CurrentKeys()) |
| 563 | |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 564 | if sys.platform == "darwin": |
| 565 | # OS X Tk variants do not support the "Alt" keyboard modifier. |
| 566 | # So replace all keybingings that use "Alt" with ones that |
| 567 | # use the "Option" keyboard modifier. |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 568 | # TODO (Ned?): the "Option" modifier does not work properly for |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 569 | # Cocoa Tk and XQuartz Tk so we should not use it |
| 570 | # in default OS X KeySets. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 571 | for k, v in result.items(): |
| 572 | v2 = [ x.replace('<Alt-', '<Option-') for x in v ] |
| 573 | if v != v2: |
| 574 | result[k] = v2 |
| 575 | |
| 576 | return result |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 577 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 578 | def GetKeySet(self, keySetName): |
| 579 | """Return event-key dict for keySetName core plus active extensions. |
| 580 | |
| 581 | If a binding defined in an extension is already in use, the |
| 582 | extension binding is disabled by being set to '' |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 583 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 584 | keySet = self.GetCoreKeys(keySetName) |
| 585 | activeExtns = self.GetExtensions(active_only=1) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 586 | for extn in activeExtns: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 587 | extKeys = self.__GetRawExtensionKeys(extn) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 588 | if extKeys: #the extension defines keybindings |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 589 | for event in extKeys: |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 590 | if extKeys[event] in keySet.values(): |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 591 | #the binding is already in use |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 592 | extKeys[event] = '' #disable this binding |
| 593 | keySet[event] = extKeys[event] #add binding |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 594 | return keySet |
| 595 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 596 | def IsCoreBinding(self, virtualEvent): |
| 597 | """Return True if the virtual event is one of the core idle key events. |
| 598 | |
| 599 | virtualEvent - string, name of the virtual event to test for, |
| 600 | without the enclosing '<< >>' |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 601 | """ |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 602 | return ('<<'+virtualEvent+'>>') in self.GetCoreKeys() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 603 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 604 | # TODO make keyBindins a file or class attribute used for test above |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 605 | # and copied in function below. |
| 606 | |
| 607 | former_extension_events = { # Those with user-configurable keys. |
| 608 | '<<force-open-completions>>', '<<expand-word>>', |
| 609 | '<<force-open-calltip>>', '<<flash-paren>>', '<<format-paragraph>>', |
| 610 | '<<run-module>>', '<<check-module>>', '<<zoom-height>>'} |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 611 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 612 | def GetCoreKeys(self, keySetName=None): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 613 | """Return dict of core virtual-key keybindings for keySetName. |
| 614 | |
| 615 | The default keySetName None corresponds to the keyBindings base |
| 616 | dict. If keySetName is not None, bindings from the config |
| 617 | file(s) are loaded _over_ these defaults, so if there is a |
| 618 | problem getting any core binding there will be an 'ultimate last |
| 619 | resort fallback' to the CUA-ish bindings defined here. |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 620 | """ |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 621 | keyBindings={ |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 622 | '<<copy>>': ['<Control-c>', '<Control-C>'], |
| 623 | '<<cut>>': ['<Control-x>', '<Control-X>'], |
| 624 | '<<paste>>': ['<Control-v>', '<Control-V>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 625 | '<<beginning-of-line>>': ['<Control-a>', '<Home>'], |
| 626 | '<<center-insert>>': ['<Control-l>'], |
| 627 | '<<close-all-windows>>': ['<Control-q>'], |
| 628 | '<<close-window>>': ['<Alt-F4>'], |
Kurt B. Kaiser | 84f4803 | 2002-09-26 22:13:22 +0000 | [diff] [blame] | 629 | '<<do-nothing>>': ['<Control-x>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 630 | '<<end-of-file>>': ['<Control-d>'], |
| 631 | '<<python-docs>>': ['<F1>'], |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 632 | '<<python-context-help>>': ['<Shift-F1>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 633 | '<<history-next>>': ['<Alt-n>'], |
| 634 | '<<history-previous>>': ['<Alt-p>'], |
| 635 | '<<interrupt-execution>>': ['<Control-c>'], |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 636 | '<<view-restart>>': ['<F6>'], |
Kurt B. Kaiser | 4cc5ef5 | 2003-01-22 00:23:23 +0000 | [diff] [blame] | 637 | '<<restart-shell>>': ['<Control-F6>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 638 | '<<open-class-browser>>': ['<Alt-c>'], |
| 639 | '<<open-module>>': ['<Alt-m>'], |
| 640 | '<<open-new-window>>': ['<Control-n>'], |
| 641 | '<<open-window-from-file>>': ['<Control-o>'], |
| 642 | '<<plain-newline-and-indent>>': ['<Control-j>'], |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 643 | '<<print-window>>': ['<Control-p>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 644 | '<<redo>>': ['<Control-y>'], |
| 645 | '<<remove-selection>>': ['<Escape>'], |
Kurt B. Kaiser | 2303b1c | 2003-11-24 05:26:16 +0000 | [diff] [blame] | 646 | '<<save-copy-of-window-as-file>>': ['<Alt-Shift-S>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 647 | '<<save-window-as-file>>': ['<Alt-s>'], |
| 648 | '<<save-window>>': ['<Control-s>'], |
| 649 | '<<select-all>>': ['<Alt-a>'], |
| 650 | '<<toggle-auto-coloring>>': ['<Control-slash>'], |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 651 | '<<undo>>': ['<Control-z>'], |
| 652 | '<<find-again>>': ['<Control-g>', '<F3>'], |
| 653 | '<<find-in-files>>': ['<Alt-F3>'], |
| 654 | '<<find-selection>>': ['<Control-F3>'], |
| 655 | '<<find>>': ['<Control-f>'], |
| 656 | '<<replace>>': ['<Control-h>'], |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 657 | '<<goto-line>>': ['<Alt-g>'], |
Kurt B. Kaiser | a9f8cbc | 2002-09-14 03:17:01 +0000 | [diff] [blame] | 658 | '<<smart-backspace>>': ['<Key-BackSpace>'], |
Andrew Svetlov | 67ac079 | 2012-03-29 19:01:28 +0300 | [diff] [blame] | 659 | '<<newline-and-indent>>': ['<Key-Return>', '<Key-KP_Enter>'], |
Kurt B. Kaiser | a9f8cbc | 2002-09-14 03:17:01 +0000 | [diff] [blame] | 660 | '<<smart-indent>>': ['<Key-Tab>'], |
| 661 | '<<indent-region>>': ['<Control-Key-bracketright>'], |
| 662 | '<<dedent-region>>': ['<Control-Key-bracketleft>'], |
| 663 | '<<comment-region>>': ['<Alt-Key-3>'], |
| 664 | '<<uncomment-region>>': ['<Alt-Key-4>'], |
| 665 | '<<tabify-region>>': ['<Alt-Key-5>'], |
| 666 | '<<untabify-region>>': ['<Alt-Key-6>'], |
| 667 | '<<toggle-tabs>>': ['<Alt-Key-t>'], |
Kurt B. Kaiser | 3069dbb | 2005-01-28 00:16:16 +0000 | [diff] [blame] | 668 | '<<change-indentwidth>>': ['<Alt-Key-u>'], |
| 669 | '<<del-word-left>>': ['<Control-Key-BackSpace>'], |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 670 | '<<del-word-right>>': ['<Control-Key-Delete>'], |
| 671 | '<<force-open-completions>>': ['<Control-Key-space>'], |
| 672 | '<<expand-word>>': ['<Alt-Key-slash>'], |
| 673 | '<<force-open-calltip>>': ['<Control-Key-backslash>'], |
| 674 | '<<flash-paren>>': ['<Control-Key-0>'], |
| 675 | '<<format-paragraph>>': ['<Alt-Key-q>'], |
| 676 | '<<run-module>>': ['<Key-F5>'], |
| 677 | '<<check-module>>': ['<Alt-Key-x>'], |
| 678 | '<<zoom-height>>': ['<Alt-Key-2>'], |
Kurt B. Kaiser | a9f8cbc | 2002-09-14 03:17:01 +0000 | [diff] [blame] | 679 | } |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 680 | |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 681 | if keySetName: |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 682 | if not (self.userCfg['keys'].has_section(keySetName) or |
| 683 | self.defaultCfg['keys'].has_section(keySetName)): |
| 684 | warning = ( |
| 685 | '\n Warning: config.py - IdleConf.GetCoreKeys -\n' |
| 686 | ' key set %r is not defined, using default bindings.' % |
| 687 | (keySetName,) |
| 688 | ) |
| 689 | _warn(warning, 'keys', keySetName) |
| 690 | else: |
| 691 | for event in keyBindings: |
| 692 | binding = self.GetKeyBinding(keySetName, event) |
| 693 | if binding: |
| 694 | keyBindings[event] = binding |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 695 | # Otherwise return default in keyBindings. |
| 696 | elif event not in self.former_extension_events: |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 697 | warning = ( |
| 698 | '\n Warning: config.py - IdleConf.GetCoreKeys -\n' |
| 699 | ' problem retrieving key binding for event %r\n' |
| 700 | ' from key set %r.\n' |
| 701 | ' returning default value: %r' % |
| 702 | (event, keySetName, keyBindings[event]) |
| 703 | ) |
| 704 | _warn(warning, 'keys', keySetName, event) |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 705 | return keyBindings |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 706 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 707 | def GetExtraHelpSourceList(self, configSet): |
| 708 | """Return list of extra help sources from a given configSet. |
Kurt B. Kaiser | e66675b | 2003-01-27 02:36:18 +0000 | [diff] [blame] | 709 | |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 710 | Valid configSets are 'user' or 'default'. Return a list of tuples of |
| 711 | the form (menu_item , path_to_help_file , option), or return the empty |
| 712 | list. 'option' is the sequence number of the help resource. 'option' |
| 713 | values determine the position of the menu items on the Help menu, |
| 714 | therefore the returned list must be sorted by 'option'. |
| 715 | |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 716 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 717 | helpSources = [] |
| 718 | if configSet == 'user': |
| 719 | cfgParser = self.userCfg['main'] |
| 720 | elif configSet == 'default': |
| 721 | cfgParser = self.defaultCfg['main'] |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 722 | else: |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 723 | raise InvalidConfigSet('Invalid configSet specified') |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 724 | options=cfgParser.GetOptionList('HelpFiles') |
| 725 | for option in options: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 726 | value=cfgParser.Get('HelpFiles', option, default=';') |
| 727 | if value.find(';') == -1: #malformed config entry with no ';' |
| 728 | menuItem = '' #make these empty |
| 729 | helpPath = '' #so value won't be added to list |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 730 | else: #config entry contains ';' as expected |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 731 | value=value.split(';') |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 732 | menuItem=value[0].strip() |
| 733 | helpPath=value[1].strip() |
| 734 | if menuItem and helpPath: #neither are empty strings |
| 735 | helpSources.append( (menuItem,helpPath,option) ) |
Kurt B. Kaiser | 4718bf8 | 2008-02-12 21:34:12 +0000 | [diff] [blame] | 736 | helpSources.sort(key=lambda x: x[2]) |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 737 | return helpSources |
| 738 | |
| 739 | def GetAllExtraHelpSourcesList(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 740 | """Return a list of the details of all additional help sources. |
| 741 | |
| 742 | Tuples in the list are those of GetExtraHelpSourceList. |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 743 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 744 | allHelpSources = (self.GetExtraHelpSourceList('default') + |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 745 | self.GetExtraHelpSourceList('user') ) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 746 | return allHelpSources |
| 747 | |
Terry Jan Reedy | d87d168 | 2015-08-01 18:57:33 -0400 | [diff] [blame] | 748 | def GetFont(self, root, configType, section): |
| 749 | """Retrieve a font from configuration (font, font-size, font-bold) |
| 750 | Intercept the special value 'TkFixedFont' and substitute |
| 751 | the actual font, factoring in some tweaks if needed for |
| 752 | appearance sakes. |
| 753 | |
| 754 | The 'root' parameter can normally be any valid Tkinter widget. |
| 755 | |
| 756 | Return a tuple (family, size, weight) suitable for passing |
| 757 | to tkinter.Font |
| 758 | """ |
| 759 | family = self.GetOption(configType, section, 'font', default='courier') |
| 760 | size = self.GetOption(configType, section, 'font-size', type='int', |
| 761 | default='10') |
| 762 | bold = self.GetOption(configType, section, 'font-bold', default=0, |
| 763 | type='bool') |
| 764 | if (family == 'TkFixedFont'): |
Terry Jan Reedy | 1080d13 | 2016-06-09 21:09:15 -0400 | [diff] [blame] | 765 | f = Font(name='TkFixedFont', exists=True, root=root) |
| 766 | actualFont = Font.actual(f) |
| 767 | family = actualFont['family'] |
| 768 | size = actualFont['size'] |
| 769 | if size <= 0: |
| 770 | size = 10 # if font in pixels, ignore actual size |
| 771 | bold = actualFont['weight'] == 'bold' |
Terry Jan Reedy | d87d168 | 2015-08-01 18:57:33 -0400 | [diff] [blame] | 772 | return (family, size, 'bold' if bold else 'normal') |
| 773 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 774 | def LoadCfgFiles(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 775 | "Load all configuration files." |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 776 | for key in self.defaultCfg: |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 777 | self.defaultCfg[key].Load() |
| 778 | self.userCfg[key].Load() #same keys |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 779 | |
| 780 | def SaveUserCfgFiles(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 781 | "Write all loaded user configuration files to disk." |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 782 | for key in self.userCfg: |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 783 | self.userCfg[key].Save() |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 784 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 785 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 786 | idleConf = IdleConf() |
| 787 | |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 788 | _warned = set() |
| 789 | def _warn(msg, *key): |
| 790 | key = (msg,) + key |
| 791 | if key not in _warned: |
| 792 | try: |
| 793 | print(msg, file=sys.stderr) |
| 794 | except OSError: |
| 795 | pass |
| 796 | _warned.add(key) |
| 797 | |
| 798 | |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 799 | class ConfigChanges(dict): |
| 800 | """Manage a user's proposed configuration option changes. |
| 801 | |
| 802 | Names used across multiple methods: |
| 803 | page -- one of the 4 top-level dicts representing a |
| 804 | .idlerc/config-x.cfg file. |
| 805 | config_type -- name of a page. |
| 806 | section -- a section within a page/file. |
| 807 | option -- name of an option within a section. |
| 808 | value -- value for the option. |
| 809 | |
| 810 | Methods |
| 811 | add_option: Add option and value to changes. |
| 812 | save_option: Save option and value to config parser. |
| 813 | save_all: Save all the changes to the config parser and file. |
csabella | 6d13b22 | 2017-07-11 19:09:44 -0400 | [diff] [blame] | 814 | delete_section: If section exists, |
| 815 | delete from changes, userCfg, and file. |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 816 | clear: Clear all changes by clearing each page. |
| 817 | """ |
| 818 | def __init__(self): |
| 819 | "Create a page for each configuration file" |
| 820 | self.pages = [] # List of unhashable dicts. |
| 821 | for config_type in idleConf.config_types: |
| 822 | self[config_type] = {} |
| 823 | self.pages.append(self[config_type]) |
| 824 | |
| 825 | def add_option(self, config_type, section, item, value): |
| 826 | "Add item/value pair for config_type and section." |
| 827 | page = self[config_type] |
| 828 | value = str(value) # Make sure we use a string. |
| 829 | if section not in page: |
| 830 | page[section] = {} |
| 831 | page[section][item] = value |
| 832 | |
| 833 | @staticmethod |
| 834 | def save_option(config_type, section, item, value): |
| 835 | """Return True if the configuration value was added or changed. |
| 836 | |
| 837 | Helper for save_all. |
| 838 | """ |
| 839 | if idleConf.defaultCfg[config_type].has_option(section, item): |
| 840 | if idleConf.defaultCfg[config_type].Get(section, item) == value: |
| 841 | # The setting equals a default setting, remove it from user cfg. |
| 842 | return idleConf.userCfg[config_type].RemoveOption(section, item) |
| 843 | # If we got here, set the option. |
| 844 | return idleConf.userCfg[config_type].SetOption(section, item, value) |
| 845 | |
| 846 | def save_all(self): |
| 847 | """Save configuration changes to the user config file. |
| 848 | |
Louie Lu | 50c9435 | 2017-07-13 02:05:32 +0800 | [diff] [blame] | 849 | Clear self in preparation for additional changes. |
| 850 | Return changed for testing. |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 851 | """ |
| 852 | idleConf.userCfg['main'].Save() |
Louie Lu | 50c9435 | 2017-07-13 02:05:32 +0800 | [diff] [blame] | 853 | |
| 854 | changed = False |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 855 | for config_type in self: |
| 856 | cfg_type_changed = False |
| 857 | page = self[config_type] |
| 858 | for section in page: |
| 859 | if section == 'HelpFiles': # Remove it for replacement. |
| 860 | idleConf.userCfg['main'].remove_section('HelpFiles') |
| 861 | cfg_type_changed = True |
| 862 | for item, value in page[section].items(): |
| 863 | if self.save_option(config_type, section, item, value): |
| 864 | cfg_type_changed = True |
| 865 | if cfg_type_changed: |
| 866 | idleConf.userCfg[config_type].Save() |
Louie Lu | 50c9435 | 2017-07-13 02:05:32 +0800 | [diff] [blame] | 867 | changed = True |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 868 | for config_type in ['keys', 'highlight']: |
| 869 | # Save these even if unchanged! |
| 870 | idleConf.userCfg[config_type].Save() |
| 871 | self.clear() |
| 872 | # ConfigDialog caller must add the following call |
| 873 | # self.save_all_changed_extensions() # Uses a different mechanism. |
Louie Lu | 50c9435 | 2017-07-13 02:05:32 +0800 | [diff] [blame] | 874 | return changed |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 875 | |
| 876 | def delete_section(self, config_type, section): |
| 877 | """Delete a section from self, userCfg, and file. |
| 878 | |
| 879 | Used to delete custom themes and keysets. |
| 880 | """ |
| 881 | if section in self[config_type]: |
| 882 | del self[config_type][section] |
| 883 | configpage = idleConf.userCfg[config_type] |
| 884 | configpage.remove_section(section) |
| 885 | configpage.Save() |
| 886 | |
| 887 | def clear(self): |
| 888 | """Clear all 4 pages. |
| 889 | |
| 890 | Called in save_all after saving to idleConf. |
| 891 | XXX Mark window *title* when there are changes; unmark here. |
| 892 | """ |
| 893 | for page in self.pages: |
| 894 | page.clear() |
| 895 | |
| 896 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 897 | # TODO Revise test output, write expanded unittest |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 898 | def _dump(): # htest # (not really, but ignore in coverage) |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 899 | from zlib import crc32 |
| 900 | line, crc = 0, 0 |
| 901 | |
| 902 | def sprint(obj): |
| 903 | global line, crc |
| 904 | txt = str(obj) |
| 905 | line += 1 |
| 906 | crc = crc32(txt.encode(encoding='utf-8'), crc) |
| 907 | print(txt) |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 908 | #print('***', line, crc, '***') # Uncomment for diagnosis. |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 909 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 910 | def dumpCfg(cfg): |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 911 | print('\n', cfg, '\n') # Cfg has variable '0xnnnnnnnn' address. |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 912 | for key in sorted(cfg.keys()): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 913 | sections = cfg[key].sections() |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 914 | sprint(key) |
| 915 | sprint(sections) |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 916 | for section in sections: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 917 | options = cfg[key].options(section) |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 918 | sprint(section) |
| 919 | sprint(options) |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 920 | for option in options: |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 921 | sprint(option + ' = ' + cfg[key].Get(section, option)) |
| 922 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 923 | dumpCfg(idleConf.defaultCfg) |
| 924 | dumpCfg(idleConf.userCfg) |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 925 | print('\nlines = ', line, ', crc = ', crc, sep='') |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 926 | |
| 927 | if __name__ == '__main__': |
Terry Jan Reedy | 4d92158 | 2018-06-19 19:12:52 -0400 | [diff] [blame] | 928 | from unittest import main |
| 929 | main('idlelib.idle_test.test_config', verbosity=2, exit=False) |
| 930 | |
| 931 | # Run revised _dump() as htest? |