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', |
| 362 | 'console-background':'#ffffff' } |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 363 | for element in theme: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 364 | if not cfgParser.has_option(themeName, element): |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 365 | # Print warning that will return a default color |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 366 | warning = ('\n Warning: config.IdleConf.GetThemeDict' |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 367 | ' -\n problem retrieving theme element %r' |
| 368 | '\n from theme %r.\n' |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 369 | ' returning default color: %r' % |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 370 | (element, themeName, theme[element])) |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 371 | _warn(warning, 'highlight', themeName, element) |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 372 | theme[element] = cfgParser.Get( |
| 373 | themeName, element, default=theme[element]) |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 374 | return theme |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 375 | |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 376 | def CurrentTheme(self): |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 377 | "Return the name of the currently active text color theme." |
| 378 | return self.current_colors_and_keys('Theme') |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 379 | |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 380 | def CurrentKeys(self): |
| 381 | """Return the name of the currently active key set.""" |
| 382 | return self.current_colors_and_keys('Keys') |
| 383 | |
| 384 | def current_colors_and_keys(self, section): |
| 385 | """Return the currently active name for Theme or Keys section. |
| 386 | |
| 387 | idlelib.config-main.def ('default') includes these sections |
| 388 | |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 389 | [Theme] |
| 390 | default= 1 |
| 391 | name= IDLE Classic |
| 392 | name2= |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 393 | |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 394 | [Keys] |
| 395 | default= 1 |
| 396 | name= |
| 397 | name2= |
| 398 | |
| 399 | Item 'name2', is used for built-in ('default') themes and keys |
| 400 | added after 2015 Oct 1 and 2016 July 1. This kludge is needed |
| 401 | because setting 'name' to a builtin not defined in older IDLEs |
| 402 | to display multiple error messages or quit. |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 403 | See https://bugs.python.org/issue25313. |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 404 | When default = True, 'name2' takes precedence over 'name', |
| 405 | while older IDLEs will just use name. When default = False, |
| 406 | 'name2' may still be set, but it is ignored. |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 407 | """ |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 408 | cfgname = 'highlight' if section == 'Theme' else 'keys' |
Terry Jan Reedy | 5acf4e5 | 2016-08-24 22:08:01 -0400 | [diff] [blame] | 409 | default = self.GetOption('main', section, 'default', |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 410 | type='bool', default=True) |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 411 | name = '' |
Terry Jan Reedy | d0c0f00 | 2015-11-12 15:02:57 -0500 | [diff] [blame] | 412 | if default: |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 413 | name = self.GetOption('main', section, 'name2', default='') |
| 414 | if not name: |
| 415 | name = self.GetOption('main', section, 'name', default='') |
| 416 | if name: |
| 417 | source = self.defaultCfg if default else self.userCfg |
| 418 | if source[cfgname].has_section(name): |
| 419 | return name |
| 420 | return "IDLE Classic" if section == 'Theme' else self.default_keys() |
| 421 | |
| 422 | @staticmethod |
| 423 | def default_keys(): |
| 424 | if sys.platform[:3] == 'win': |
| 425 | return 'IDLE Classic Windows' |
| 426 | elif sys.platform == 'darwin': |
| 427 | return 'IDLE Classic OSX' |
Terry Jan Reedy | c15a7c6 | 2015-11-12 15:06:07 -0500 | [diff] [blame] | 428 | else: |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 429 | return 'IDLE Modern Unix' |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 430 | |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 431 | def GetExtensions(self, active_only=True, |
| 432 | editor_only=False, shell_only=False): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 433 | """Return extensions in default and user config-extensions files. |
| 434 | |
| 435 | If active_only True, only return active (enabled) extensions |
| 436 | and optionally only editor or shell extensions. |
| 437 | If active_only False, return all extensions. |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 438 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 439 | extns = self.RemoveKeyBindNames( |
| 440 | self.GetSectionList('default', 'extensions')) |
| 441 | userExtns = self.RemoveKeyBindNames( |
| 442 | self.GetSectionList('user', 'extensions')) |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 443 | for extn in userExtns: |
| 444 | if extn not in extns: #user has added own extension |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 445 | extns.append(extn) |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 446 | if active_only: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 447 | activeExtns = [] |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 448 | for extn in extns: |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 449 | if self.GetOption('extensions', extn, 'enable', default=True, |
| 450 | type='bool'): |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 451 | #the extension is enabled |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 452 | if editor_only or shell_only: # TODO both True contradict |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 453 | if editor_only: |
| 454 | option = "enable_editor" |
| 455 | else: |
| 456 | option = "enable_shell" |
| 457 | if self.GetOption('extensions', extn,option, |
| 458 | default=True, type='bool', |
| 459 | warn_on_default=False): |
| 460 | activeExtns.append(extn) |
| 461 | else: |
| 462 | activeExtns.append(extn) |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 463 | return activeExtns |
| 464 | else: |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 465 | return extns |
Steven M. Gava | ad4f532 | 2002-01-03 12:05:17 +0000 | [diff] [blame] | 466 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 467 | def RemoveKeyBindNames(self, extnNameList): |
| 468 | "Return extnNameList with keybinding section names removed." |
Louie Lu | f776eb0 | 2017-07-19 05:17:56 +0800 | [diff] [blame] | 469 | 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] | 470 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 471 | def GetExtnNameForEvent(self, virtualEvent): |
| 472 | """Return the name of the extension binding virtualEvent, or None. |
| 473 | |
| 474 | virtualEvent - string, name of the virtual event to test for, |
| 475 | without the enclosing '<< >>' |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 476 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 477 | extName = None |
| 478 | vEvent = '<<' + virtualEvent + '>>' |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 479 | for extn in self.GetExtensions(active_only=0): |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 480 | for event in self.GetExtensionKeys(extn): |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 481 | if event == vEvent: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 482 | extName = extn # TODO return here? |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 483 | return extName |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 484 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 485 | def GetExtensionKeys(self, extensionName): |
| 486 | """Return dict: {configurable extensionName event : active keybinding}. |
| 487 | |
| 488 | Events come from default config extension_cfgBindings section. |
| 489 | Keybindings come from GetCurrentKeySet() active key dict, |
| 490 | where previously used bindings are disabled. |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 491 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 492 | keysName = extensionName + '_cfgBindings' |
| 493 | activeKeys = self.GetCurrentKeySet() |
| 494 | extKeys = {} |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 495 | if self.defaultCfg['extensions'].has_section(keysName): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 496 | eventNames = self.defaultCfg['extensions'].GetOptionList(keysName) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 497 | for eventName in eventNames: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 498 | event = '<<' + eventName + '>>' |
| 499 | binding = activeKeys[event] |
| 500 | extKeys[event] = binding |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 501 | return extKeys |
| 502 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 503 | def __GetRawExtensionKeys(self,extensionName): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 504 | """Return dict {configurable extensionName event : keybinding list}. |
| 505 | |
| 506 | Events come from default config extension_cfgBindings section. |
| 507 | Keybindings list come from the splitting of GetOption, which |
| 508 | tries user config before default config. |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 509 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 510 | keysName = extensionName+'_cfgBindings' |
| 511 | extKeys = {} |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 512 | if self.defaultCfg['extensions'].has_section(keysName): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 513 | eventNames = self.defaultCfg['extensions'].GetOptionList(keysName) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 514 | for eventName in eventNames: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 515 | binding = self.GetOption( |
| 516 | 'extensions', keysName, eventName, default='').split() |
| 517 | event = '<<' + eventName + '>>' |
| 518 | extKeys[event] = binding |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 519 | return extKeys |
| 520 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 521 | def GetExtensionBindings(self, extensionName): |
| 522 | """Return dict {extensionName event : active or defined keybinding}. |
| 523 | |
| 524 | Augment self.GetExtensionKeys(extensionName) with mapping of non- |
| 525 | configurable events (from default config) to GetOption splits, |
| 526 | as in self.__GetRawExtensionKeys. |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 527 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 528 | bindsName = extensionName + '_bindings' |
| 529 | extBinds = self.GetExtensionKeys(extensionName) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 530 | #add the non-configurable bindings |
| 531 | if self.defaultCfg['extensions'].has_section(bindsName): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 532 | eventNames = self.defaultCfg['extensions'].GetOptionList(bindsName) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 533 | for eventName in eventNames: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 534 | binding = self.GetOption( |
| 535 | 'extensions', bindsName, eventName, default='').split() |
| 536 | event = '<<' + eventName + '>>' |
| 537 | extBinds[event] = binding |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 538 | |
| 539 | return extBinds |
| 540 | |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 541 | def GetKeyBinding(self, keySetName, eventStr): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 542 | """Return the keybinding list for keySetName eventStr. |
| 543 | |
| 544 | keySetName - name of key binding set (config-keys section). |
| 545 | eventStr - virtual event, including brackets, as in '<<event>>'. |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 546 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 547 | eventName = eventStr[2:-2] #trim off the angle brackets |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 548 | binding = self.GetOption('keys', keySetName, eventName, default='', |
| 549 | warn_on_default=False).split() |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 550 | return binding |
| 551 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 552 | def GetCurrentKeySet(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 553 | "Return CurrentKeys with 'darwin' modifications." |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 554 | result = self.GetKeySet(self.CurrentKeys()) |
| 555 | |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 556 | if sys.platform == "darwin": |
| 557 | # OS X Tk variants do not support the "Alt" keyboard modifier. |
| 558 | # So replace all keybingings that use "Alt" with ones that |
| 559 | # use the "Option" keyboard modifier. |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 560 | # TODO (Ned?): the "Option" modifier does not work properly for |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 561 | # Cocoa Tk and XQuartz Tk so we should not use it |
| 562 | # in default OS X KeySets. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 563 | for k, v in result.items(): |
| 564 | v2 = [ x.replace('<Alt-', '<Option-') for x in v ] |
| 565 | if v != v2: |
| 566 | result[k] = v2 |
| 567 | |
| 568 | return result |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 569 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 570 | def GetKeySet(self, keySetName): |
| 571 | """Return event-key dict for keySetName core plus active extensions. |
| 572 | |
| 573 | If a binding defined in an extension is already in use, the |
| 574 | extension binding is disabled by being set to '' |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 575 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 576 | keySet = self.GetCoreKeys(keySetName) |
| 577 | activeExtns = self.GetExtensions(active_only=1) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 578 | for extn in activeExtns: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 579 | extKeys = self.__GetRawExtensionKeys(extn) |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 580 | if extKeys: #the extension defines keybindings |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 581 | for event in extKeys: |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 582 | if extKeys[event] in keySet.values(): |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 583 | #the binding is already in use |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 584 | extKeys[event] = '' #disable this binding |
| 585 | keySet[event] = extKeys[event] #add binding |
Steven M. Gava | f9bb90e | 2002-01-24 06:02:50 +0000 | [diff] [blame] | 586 | return keySet |
| 587 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 588 | def IsCoreBinding(self, virtualEvent): |
| 589 | """Return True if the virtual event is one of the core idle key events. |
| 590 | |
| 591 | virtualEvent - string, name of the virtual event to test for, |
| 592 | without the enclosing '<< >>' |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 593 | """ |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 594 | return ('<<'+virtualEvent+'>>') in self.GetCoreKeys() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 595 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 596 | # TODO make keyBindins a file or class attribute used for test above |
| 597 | # and copied in function below |
| 598 | |
Steven M. Gava | c628a06 | 2002-01-19 10:33:21 +0000 | [diff] [blame] | 599 | def GetCoreKeys(self, keySetName=None): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 600 | """Return dict of core virtual-key keybindings for keySetName. |
| 601 | |
| 602 | The default keySetName None corresponds to the keyBindings base |
| 603 | dict. If keySetName is not None, bindings from the config |
| 604 | file(s) are loaded _over_ these defaults, so if there is a |
| 605 | problem getting any core binding there will be an 'ultimate last |
| 606 | resort fallback' to the CUA-ish bindings defined here. |
Steven M. Gava | 2a63a07 | 2001-10-26 06:50:54 +0000 | [diff] [blame] | 607 | """ |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 608 | keyBindings={ |
Steven M. Gava | a498af2 | 2002-02-01 01:33:36 +0000 | [diff] [blame] | 609 | '<<copy>>': ['<Control-c>', '<Control-C>'], |
| 610 | '<<cut>>': ['<Control-x>', '<Control-X>'], |
| 611 | '<<paste>>': ['<Control-v>', '<Control-V>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 612 | '<<beginning-of-line>>': ['<Control-a>', '<Home>'], |
| 613 | '<<center-insert>>': ['<Control-l>'], |
| 614 | '<<close-all-windows>>': ['<Control-q>'], |
| 615 | '<<close-window>>': ['<Alt-F4>'], |
Kurt B. Kaiser | 84f4803 | 2002-09-26 22:13:22 +0000 | [diff] [blame] | 616 | '<<do-nothing>>': ['<Control-x>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 617 | '<<end-of-file>>': ['<Control-d>'], |
| 618 | '<<python-docs>>': ['<F1>'], |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 619 | '<<python-context-help>>': ['<Shift-F1>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 620 | '<<history-next>>': ['<Alt-n>'], |
| 621 | '<<history-previous>>': ['<Alt-p>'], |
| 622 | '<<interrupt-execution>>': ['<Control-c>'], |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 623 | '<<view-restart>>': ['<F6>'], |
Kurt B. Kaiser | 4cc5ef5 | 2003-01-22 00:23:23 +0000 | [diff] [blame] | 624 | '<<restart-shell>>': ['<Control-F6>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 625 | '<<open-class-browser>>': ['<Alt-c>'], |
| 626 | '<<open-module>>': ['<Alt-m>'], |
| 627 | '<<open-new-window>>': ['<Control-n>'], |
| 628 | '<<open-window-from-file>>': ['<Control-o>'], |
| 629 | '<<plain-newline-and-indent>>': ['<Control-j>'], |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 630 | '<<print-window>>': ['<Control-p>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 631 | '<<redo>>': ['<Control-y>'], |
| 632 | '<<remove-selection>>': ['<Escape>'], |
Kurt B. Kaiser | 2303b1c | 2003-11-24 05:26:16 +0000 | [diff] [blame] | 633 | '<<save-copy-of-window-as-file>>': ['<Alt-Shift-S>'], |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 634 | '<<save-window-as-file>>': ['<Alt-s>'], |
| 635 | '<<save-window>>': ['<Control-s>'], |
| 636 | '<<select-all>>': ['<Alt-a>'], |
| 637 | '<<toggle-auto-coloring>>': ['<Control-slash>'], |
Steven M. Gava | 0cae01c | 2002-01-04 07:53:06 +0000 | [diff] [blame] | 638 | '<<undo>>': ['<Control-z>'], |
| 639 | '<<find-again>>': ['<Control-g>', '<F3>'], |
| 640 | '<<find-in-files>>': ['<Alt-F3>'], |
| 641 | '<<find-selection>>': ['<Control-F3>'], |
| 642 | '<<find>>': ['<Control-f>'], |
| 643 | '<<replace>>': ['<Control-h>'], |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 644 | '<<goto-line>>': ['<Alt-g>'], |
Kurt B. Kaiser | a9f8cbc | 2002-09-14 03:17:01 +0000 | [diff] [blame] | 645 | '<<smart-backspace>>': ['<Key-BackSpace>'], |
Andrew Svetlov | 67ac079 | 2012-03-29 19:01:28 +0300 | [diff] [blame] | 646 | '<<newline-and-indent>>': ['<Key-Return>', '<Key-KP_Enter>'], |
Kurt B. Kaiser | a9f8cbc | 2002-09-14 03:17:01 +0000 | [diff] [blame] | 647 | '<<smart-indent>>': ['<Key-Tab>'], |
| 648 | '<<indent-region>>': ['<Control-Key-bracketright>'], |
| 649 | '<<dedent-region>>': ['<Control-Key-bracketleft>'], |
| 650 | '<<comment-region>>': ['<Alt-Key-3>'], |
| 651 | '<<uncomment-region>>': ['<Alt-Key-4>'], |
| 652 | '<<tabify-region>>': ['<Alt-Key-5>'], |
| 653 | '<<untabify-region>>': ['<Alt-Key-6>'], |
| 654 | '<<toggle-tabs>>': ['<Alt-Key-t>'], |
Kurt B. Kaiser | 3069dbb | 2005-01-28 00:16:16 +0000 | [diff] [blame] | 655 | '<<change-indentwidth>>': ['<Alt-Key-u>'], |
| 656 | '<<del-word-left>>': ['<Control-Key-BackSpace>'], |
| 657 | '<<del-word-right>>': ['<Control-Key-Delete>'] |
Kurt B. Kaiser | a9f8cbc | 2002-09-14 03:17:01 +0000 | [diff] [blame] | 658 | } |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 659 | if keySetName: |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 660 | if not (self.userCfg['keys'].has_section(keySetName) or |
| 661 | self.defaultCfg['keys'].has_section(keySetName)): |
| 662 | warning = ( |
| 663 | '\n Warning: config.py - IdleConf.GetCoreKeys -\n' |
| 664 | ' key set %r is not defined, using default bindings.' % |
| 665 | (keySetName,) |
| 666 | ) |
| 667 | _warn(warning, 'keys', keySetName) |
| 668 | else: |
| 669 | for event in keyBindings: |
| 670 | binding = self.GetKeyBinding(keySetName, event) |
| 671 | if binding: |
| 672 | keyBindings[event] = binding |
| 673 | else: #we are going to return a default, print warning |
| 674 | warning = ( |
| 675 | '\n Warning: config.py - IdleConf.GetCoreKeys -\n' |
| 676 | ' problem retrieving key binding for event %r\n' |
| 677 | ' from key set %r.\n' |
| 678 | ' returning default value: %r' % |
| 679 | (event, keySetName, keyBindings[event]) |
| 680 | ) |
| 681 | _warn(warning, 'keys', keySetName, event) |
Steven M. Gava | 17d0154 | 2001-12-03 00:37:28 +0000 | [diff] [blame] | 682 | return keyBindings |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 683 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 684 | def GetExtraHelpSourceList(self, configSet): |
| 685 | """Return list of extra help sources from a given configSet. |
Kurt B. Kaiser | e66675b | 2003-01-27 02:36:18 +0000 | [diff] [blame] | 686 | |
Kurt B. Kaiser | 8e92bf7 | 2003-01-14 22:03:31 +0000 | [diff] [blame] | 687 | Valid configSets are 'user' or 'default'. Return a list of tuples of |
| 688 | the form (menu_item , path_to_help_file , option), or return the empty |
| 689 | list. 'option' is the sequence number of the help resource. 'option' |
| 690 | values determine the position of the menu items on the Help menu, |
| 691 | therefore the returned list must be sorted by 'option'. |
| 692 | |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 693 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 694 | helpSources = [] |
| 695 | if configSet == 'user': |
| 696 | cfgParser = self.userCfg['main'] |
| 697 | elif configSet == 'default': |
| 698 | cfgParser = self.defaultCfg['main'] |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 699 | else: |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 700 | raise InvalidConfigSet('Invalid configSet specified') |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 701 | options=cfgParser.GetOptionList('HelpFiles') |
| 702 | for option in options: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 703 | value=cfgParser.Get('HelpFiles', option, default=';') |
| 704 | if value.find(';') == -1: #malformed config entry with no ';' |
| 705 | menuItem = '' #make these empty |
| 706 | helpPath = '' #so value won't be added to list |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 707 | else: #config entry contains ';' as expected |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 708 | value=value.split(';') |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 709 | menuItem=value[0].strip() |
| 710 | helpPath=value[1].strip() |
| 711 | if menuItem and helpPath: #neither are empty strings |
| 712 | helpSources.append( (menuItem,helpPath,option) ) |
Kurt B. Kaiser | 4718bf8 | 2008-02-12 21:34:12 +0000 | [diff] [blame] | 713 | helpSources.sort(key=lambda x: x[2]) |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 714 | return helpSources |
| 715 | |
| 716 | def GetAllExtraHelpSourcesList(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 717 | """Return a list of the details of all additional help sources. |
| 718 | |
| 719 | Tuples in the list are those of GetExtraHelpSourceList. |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 720 | """ |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 721 | allHelpSources = (self.GetExtraHelpSourceList('default') + |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 722 | self.GetExtraHelpSourceList('user') ) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 723 | return allHelpSources |
| 724 | |
Terry Jan Reedy | d87d168 | 2015-08-01 18:57:33 -0400 | [diff] [blame] | 725 | def GetFont(self, root, configType, section): |
| 726 | """Retrieve a font from configuration (font, font-size, font-bold) |
| 727 | Intercept the special value 'TkFixedFont' and substitute |
| 728 | the actual font, factoring in some tweaks if needed for |
| 729 | appearance sakes. |
| 730 | |
| 731 | The 'root' parameter can normally be any valid Tkinter widget. |
| 732 | |
| 733 | Return a tuple (family, size, weight) suitable for passing |
| 734 | to tkinter.Font |
| 735 | """ |
| 736 | family = self.GetOption(configType, section, 'font', default='courier') |
| 737 | size = self.GetOption(configType, section, 'font-size', type='int', |
| 738 | default='10') |
| 739 | bold = self.GetOption(configType, section, 'font-bold', default=0, |
| 740 | type='bool') |
| 741 | if (family == 'TkFixedFont'): |
Terry Jan Reedy | 1080d13 | 2016-06-09 21:09:15 -0400 | [diff] [blame] | 742 | f = Font(name='TkFixedFont', exists=True, root=root) |
| 743 | actualFont = Font.actual(f) |
| 744 | family = actualFont['family'] |
| 745 | size = actualFont['size'] |
| 746 | if size <= 0: |
| 747 | size = 10 # if font in pixels, ignore actual size |
| 748 | bold = actualFont['weight'] == 'bold' |
Terry Jan Reedy | d87d168 | 2015-08-01 18:57:33 -0400 | [diff] [blame] | 749 | return (family, size, 'bold' if bold else 'normal') |
| 750 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 751 | def LoadCfgFiles(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 752 | "Load all configuration files." |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 753 | for key in self.defaultCfg: |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 754 | self.defaultCfg[key].Load() |
| 755 | self.userCfg[key].Load() #same keys |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 756 | |
| 757 | def SaveUserCfgFiles(self): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 758 | "Write all loaded user configuration files to disk." |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 759 | for key in self.userCfg: |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 760 | self.userCfg[key].Save() |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 761 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 762 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 763 | idleConf = IdleConf() |
| 764 | |
Terry Jan Reedy | 9bdb1ed | 2016-07-10 13:46:34 -0400 | [diff] [blame] | 765 | _warned = set() |
| 766 | def _warn(msg, *key): |
| 767 | key = (msg,) + key |
| 768 | if key not in _warned: |
| 769 | try: |
| 770 | print(msg, file=sys.stderr) |
| 771 | except OSError: |
| 772 | pass |
| 773 | _warned.add(key) |
| 774 | |
| 775 | |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 776 | class ConfigChanges(dict): |
| 777 | """Manage a user's proposed configuration option changes. |
| 778 | |
| 779 | Names used across multiple methods: |
| 780 | page -- one of the 4 top-level dicts representing a |
| 781 | .idlerc/config-x.cfg file. |
| 782 | config_type -- name of a page. |
| 783 | section -- a section within a page/file. |
| 784 | option -- name of an option within a section. |
| 785 | value -- value for the option. |
| 786 | |
| 787 | Methods |
| 788 | add_option: Add option and value to changes. |
| 789 | save_option: Save option and value to config parser. |
| 790 | save_all: Save all the changes to the config parser and file. |
csabella | 6d13b22 | 2017-07-11 19:09:44 -0400 | [diff] [blame] | 791 | delete_section: If section exists, |
| 792 | delete from changes, userCfg, and file. |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 793 | clear: Clear all changes by clearing each page. |
| 794 | """ |
| 795 | def __init__(self): |
| 796 | "Create a page for each configuration file" |
| 797 | self.pages = [] # List of unhashable dicts. |
| 798 | for config_type in idleConf.config_types: |
| 799 | self[config_type] = {} |
| 800 | self.pages.append(self[config_type]) |
| 801 | |
| 802 | def add_option(self, config_type, section, item, value): |
| 803 | "Add item/value pair for config_type and section." |
| 804 | page = self[config_type] |
| 805 | value = str(value) # Make sure we use a string. |
| 806 | if section not in page: |
| 807 | page[section] = {} |
| 808 | page[section][item] = value |
| 809 | |
| 810 | @staticmethod |
| 811 | def save_option(config_type, section, item, value): |
| 812 | """Return True if the configuration value was added or changed. |
| 813 | |
| 814 | Helper for save_all. |
| 815 | """ |
| 816 | if idleConf.defaultCfg[config_type].has_option(section, item): |
| 817 | if idleConf.defaultCfg[config_type].Get(section, item) == value: |
| 818 | # The setting equals a default setting, remove it from user cfg. |
| 819 | return idleConf.userCfg[config_type].RemoveOption(section, item) |
| 820 | # If we got here, set the option. |
| 821 | return idleConf.userCfg[config_type].SetOption(section, item, value) |
| 822 | |
| 823 | def save_all(self): |
| 824 | """Save configuration changes to the user config file. |
| 825 | |
Louie Lu | 50c9435 | 2017-07-13 02:05:32 +0800 | [diff] [blame] | 826 | Clear self in preparation for additional changes. |
| 827 | Return changed for testing. |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 828 | """ |
| 829 | idleConf.userCfg['main'].Save() |
Louie Lu | 50c9435 | 2017-07-13 02:05:32 +0800 | [diff] [blame] | 830 | |
| 831 | changed = False |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 832 | for config_type in self: |
| 833 | cfg_type_changed = False |
| 834 | page = self[config_type] |
| 835 | for section in page: |
| 836 | if section == 'HelpFiles': # Remove it for replacement. |
| 837 | idleConf.userCfg['main'].remove_section('HelpFiles') |
| 838 | cfg_type_changed = True |
| 839 | for item, value in page[section].items(): |
| 840 | if self.save_option(config_type, section, item, value): |
| 841 | cfg_type_changed = True |
| 842 | if cfg_type_changed: |
| 843 | idleConf.userCfg[config_type].Save() |
Louie Lu | 50c9435 | 2017-07-13 02:05:32 +0800 | [diff] [blame] | 844 | changed = True |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 845 | for config_type in ['keys', 'highlight']: |
| 846 | # Save these even if unchanged! |
| 847 | idleConf.userCfg[config_type].Save() |
| 848 | self.clear() |
| 849 | # ConfigDialog caller must add the following call |
| 850 | # self.save_all_changed_extensions() # Uses a different mechanism. |
Louie Lu | 50c9435 | 2017-07-13 02:05:32 +0800 | [diff] [blame] | 851 | return changed |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 852 | |
| 853 | def delete_section(self, config_type, section): |
| 854 | """Delete a section from self, userCfg, and file. |
| 855 | |
| 856 | Used to delete custom themes and keysets. |
| 857 | """ |
| 858 | if section in self[config_type]: |
| 859 | del self[config_type][section] |
| 860 | configpage = idleConf.userCfg[config_type] |
| 861 | configpage.remove_section(section) |
| 862 | configpage.Save() |
| 863 | |
| 864 | def clear(self): |
| 865 | """Clear all 4 pages. |
| 866 | |
| 867 | Called in save_all after saving to idleConf. |
| 868 | XXX Mark window *title* when there are changes; unmark here. |
| 869 | """ |
| 870 | for page in self.pages: |
| 871 | page.clear() |
| 872 | |
| 873 | |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 874 | # TODO Revise test output, write expanded unittest |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 875 | def _dump(): # htest # (not really, but ignore in coverage) |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 876 | from zlib import crc32 |
| 877 | line, crc = 0, 0 |
| 878 | |
| 879 | def sprint(obj): |
| 880 | global line, crc |
| 881 | txt = str(obj) |
| 882 | line += 1 |
| 883 | crc = crc32(txt.encode(encoding='utf-8'), crc) |
| 884 | print(txt) |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 885 | #print('***', line, crc, '***') # Uncomment for diagnosis. |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 886 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 887 | def dumpCfg(cfg): |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 888 | print('\n', cfg, '\n') # Cfg has variable '0xnnnnnnnn' address. |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 889 | for key in sorted(cfg.keys()): |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 890 | sections = cfg[key].sections() |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 891 | sprint(key) |
| 892 | sprint(sections) |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 893 | for section in sections: |
Terry Jan Reedy | deb7bf1 | 2014-10-06 23:26:26 -0400 | [diff] [blame] | 894 | options = cfg[key].options(section) |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 895 | sprint(section) |
| 896 | sprint(options) |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 897 | for option in options: |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 898 | sprint(option + ' = ' + cfg[key].Get(section, option)) |
| 899 | |
Steven M. Gava | c11ccf3 | 2001-09-24 09:43:17 +0000 | [diff] [blame] | 900 | dumpCfg(idleConf.defaultCfg) |
| 901 | dumpCfg(idleConf.userCfg) |
Terry Jan Reedy | 2279aeb | 2016-07-05 20:09:53 -0400 | [diff] [blame] | 902 | print('\nlines = ', line, ', crc = ', crc, sep='') |
terryjreedy | 349abd9 | 2017-07-07 16:00:57 -0400 | [diff] [blame] | 903 | |
| 904 | if __name__ == '__main__': |
| 905 | import unittest |
| 906 | unittest.main('idlelib.idle_test.test_config', |
| 907 | verbosity=2, exit=False) |
| 908 | #_dump() |