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