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