| 1 | n/a | """Provides access to stored IDLE configuration information. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Refer to the comments at the beginning of config-main.def for a description of |
|---|
| 4 | n/a | the available configuration files and the design implemented to update user |
|---|
| 5 | n/a | configuration information. In particular, user configuration choices which |
|---|
| 6 | n/a | duplicate the defaults will be removed from the user's configuration files, |
|---|
| 7 | n/a | and if a file becomes empty, it will be deleted. |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | The contents of the user files may be altered using the Options/Configure IDLE |
|---|
| 10 | n/a | menu to access the configuration GUI (configDialog.py), or manually. |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | Throughout this module there is an emphasis on returning useable defaults |
|---|
| 13 | n/a | when a problem occurs in returning a requested configuration value back to |
|---|
| 14 | n/a | idle. This is to allow IDLE to continue to function in spite of errors in |
|---|
| 15 | n/a | the retrieval of config information. When a default is returned instead of |
|---|
| 16 | n/a | a requested config value, a message is printed to stderr to aid in |
|---|
| 17 | n/a | configuration problem notification and resolution. |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | """ |
|---|
| 20 | n/a | import os |
|---|
| 21 | n/a | import sys |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | from idlelib import macosxSupport |
|---|
| 24 | n/a | from configparser import ConfigParser, NoOptionError, NoSectionError |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | class InvalidConfigType(Exception): pass |
|---|
| 27 | n/a | class InvalidConfigSet(Exception): pass |
|---|
| 28 | n/a | class InvalidFgBg(Exception): pass |
|---|
| 29 | n/a | class InvalidTheme(Exception): pass |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | class IdleConfParser(ConfigParser): |
|---|
| 32 | n/a | """ |
|---|
| 33 | n/a | A ConfigParser specialised for idle configuration file handling |
|---|
| 34 | n/a | """ |
|---|
| 35 | n/a | def __init__(self, cfgFile, cfgDefaults=None): |
|---|
| 36 | n/a | """ |
|---|
| 37 | n/a | cfgFile - string, fully specified configuration file name |
|---|
| 38 | n/a | """ |
|---|
| 39 | n/a | self.file=cfgFile |
|---|
| 40 | n/a | ConfigParser.__init__(self, defaults=cfgDefaults, strict=False) |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | def Get(self, section, option, type=None, default=None, raw=False): |
|---|
| 43 | n/a | """ |
|---|
| 44 | n/a | Get an option value for given section/option or return default. |
|---|
| 45 | n/a | If type is specified, return as type. |
|---|
| 46 | n/a | """ |
|---|
| 47 | n/a | if not self.has_option(section, option): |
|---|
| 48 | n/a | return default |
|---|
| 49 | n/a | if type=='bool': |
|---|
| 50 | n/a | return self.getboolean(section, option) |
|---|
| 51 | n/a | elif type=='int': |
|---|
| 52 | n/a | return self.getint(section, option) |
|---|
| 53 | n/a | else: |
|---|
| 54 | n/a | return self.get(section, option, raw=raw) |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | def GetOptionList(self,section): |
|---|
| 57 | n/a | """ |
|---|
| 58 | n/a | Get an option list for given section |
|---|
| 59 | n/a | """ |
|---|
| 60 | n/a | if self.has_section(section): |
|---|
| 61 | n/a | return self.options(section) |
|---|
| 62 | n/a | else: #return a default value |
|---|
| 63 | n/a | return [] |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | def Load(self): |
|---|
| 66 | n/a | """ |
|---|
| 67 | n/a | Load the configuration file from disk |
|---|
| 68 | n/a | """ |
|---|
| 69 | n/a | self.read(self.file) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | class IdleUserConfParser(IdleConfParser): |
|---|
| 72 | n/a | """ |
|---|
| 73 | n/a | IdleConfigParser specialised for user configuration handling. |
|---|
| 74 | n/a | """ |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | def AddSection(self,section): |
|---|
| 77 | n/a | """ |
|---|
| 78 | n/a | if section doesn't exist, add it |
|---|
| 79 | n/a | """ |
|---|
| 80 | n/a | if not self.has_section(section): |
|---|
| 81 | n/a | self.add_section(section) |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | def RemoveEmptySections(self): |
|---|
| 84 | n/a | """ |
|---|
| 85 | n/a | remove any sections that have no options |
|---|
| 86 | n/a | """ |
|---|
| 87 | n/a | for section in self.sections(): |
|---|
| 88 | n/a | if not self.GetOptionList(section): |
|---|
| 89 | n/a | self.remove_section(section) |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | def IsEmpty(self): |
|---|
| 92 | n/a | """ |
|---|
| 93 | n/a | Remove empty sections and then return 1 if parser has no sections |
|---|
| 94 | n/a | left, else return 0. |
|---|
| 95 | n/a | """ |
|---|
| 96 | n/a | self.RemoveEmptySections() |
|---|
| 97 | n/a | if self.sections(): |
|---|
| 98 | n/a | return 0 |
|---|
| 99 | n/a | else: |
|---|
| 100 | n/a | return 1 |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | def RemoveOption(self,section,option): |
|---|
| 103 | n/a | """ |
|---|
| 104 | n/a | If section/option exists, remove it. |
|---|
| 105 | n/a | Returns 1 if option was removed, 0 otherwise. |
|---|
| 106 | n/a | """ |
|---|
| 107 | n/a | if self.has_section(section): |
|---|
| 108 | n/a | return self.remove_option(section,option) |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | def SetOption(self,section,option,value): |
|---|
| 111 | n/a | """ |
|---|
| 112 | n/a | Sets option to value, adding section if required. |
|---|
| 113 | n/a | Returns 1 if option was added or changed, otherwise 0. |
|---|
| 114 | n/a | """ |
|---|
| 115 | n/a | if self.has_option(section,option): |
|---|
| 116 | n/a | if self.get(section,option)==value: |
|---|
| 117 | n/a | return 0 |
|---|
| 118 | n/a | else: |
|---|
| 119 | n/a | self.set(section,option,value) |
|---|
| 120 | n/a | return 1 |
|---|
| 121 | n/a | else: |
|---|
| 122 | n/a | if not self.has_section(section): |
|---|
| 123 | n/a | self.add_section(section) |
|---|
| 124 | n/a | self.set(section,option,value) |
|---|
| 125 | n/a | return 1 |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | def RemoveFile(self): |
|---|
| 128 | n/a | """ |
|---|
| 129 | n/a | Removes the user config file from disk if it exists. |
|---|
| 130 | n/a | """ |
|---|
| 131 | n/a | if os.path.exists(self.file): |
|---|
| 132 | n/a | os.remove(self.file) |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | def Save(self): |
|---|
| 135 | n/a | """Update user configuration file. |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | Remove empty sections. If resulting config isn't empty, write the file |
|---|
| 138 | n/a | to disk. If config is empty, remove the file from disk if it exists. |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | """ |
|---|
| 141 | n/a | if not self.IsEmpty(): |
|---|
| 142 | n/a | fname = self.file |
|---|
| 143 | n/a | try: |
|---|
| 144 | n/a | cfgFile = open(fname, 'w') |
|---|
| 145 | n/a | except OSError: |
|---|
| 146 | n/a | os.unlink(fname) |
|---|
| 147 | n/a | cfgFile = open(fname, 'w') |
|---|
| 148 | n/a | with cfgFile: |
|---|
| 149 | n/a | self.write(cfgFile) |
|---|
| 150 | n/a | else: |
|---|
| 151 | n/a | self.RemoveFile() |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | class IdleConf: |
|---|
| 154 | n/a | """ |
|---|
| 155 | n/a | holds config parsers for all idle config files: |
|---|
| 156 | n/a | default config files |
|---|
| 157 | n/a | (idle install dir)/config-main.def |
|---|
| 158 | n/a | (idle install dir)/config-extensions.def |
|---|
| 159 | n/a | (idle install dir)/config-highlight.def |
|---|
| 160 | n/a | (idle install dir)/config-keys.def |
|---|
| 161 | n/a | user config files |
|---|
| 162 | n/a | (user home dir)/.idlerc/config-main.cfg |
|---|
| 163 | n/a | (user home dir)/.idlerc/config-extensions.cfg |
|---|
| 164 | n/a | (user home dir)/.idlerc/config-highlight.cfg |
|---|
| 165 | n/a | (user home dir)/.idlerc/config-keys.cfg |
|---|
| 166 | n/a | """ |
|---|
| 167 | n/a | def __init__(self): |
|---|
| 168 | n/a | self.defaultCfg={} |
|---|
| 169 | n/a | self.userCfg={} |
|---|
| 170 | n/a | self.cfg={} |
|---|
| 171 | n/a | self.CreateConfigHandlers() |
|---|
| 172 | n/a | self.LoadCfgFiles() |
|---|
| 173 | n/a | #self.LoadCfg() |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | def CreateConfigHandlers(self): |
|---|
| 176 | n/a | """ |
|---|
| 177 | n/a | set up a dictionary of config parsers for default and user |
|---|
| 178 | n/a | configurations respectively |
|---|
| 179 | n/a | """ |
|---|
| 180 | n/a | #build idle install path |
|---|
| 181 | n/a | if __name__ != '__main__': # we were imported |
|---|
| 182 | n/a | idleDir=os.path.dirname(__file__) |
|---|
| 183 | n/a | else: # we were exec'ed (for testing only) |
|---|
| 184 | n/a | idleDir=os.path.abspath(sys.path[0]) |
|---|
| 185 | n/a | userDir=self.GetUserCfgDir() |
|---|
| 186 | n/a | configTypes=('main','extensions','highlight','keys') |
|---|
| 187 | n/a | defCfgFiles={} |
|---|
| 188 | n/a | usrCfgFiles={} |
|---|
| 189 | n/a | for cfgType in configTypes: #build config file names |
|---|
| 190 | n/a | defCfgFiles[cfgType]=os.path.join(idleDir,'config-'+cfgType+'.def') |
|---|
| 191 | n/a | usrCfgFiles[cfgType]=os.path.join(userDir,'config-'+cfgType+'.cfg') |
|---|
| 192 | n/a | for cfgType in configTypes: #create config parsers |
|---|
| 193 | n/a | self.defaultCfg[cfgType]=IdleConfParser(defCfgFiles[cfgType]) |
|---|
| 194 | n/a | self.userCfg[cfgType]=IdleUserConfParser(usrCfgFiles[cfgType]) |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | def GetUserCfgDir(self): |
|---|
| 197 | n/a | """ |
|---|
| 198 | n/a | Creates (if required) and returns a filesystem directory for storing |
|---|
| 199 | n/a | user config files. |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | """ |
|---|
| 202 | n/a | cfgDir = '.idlerc' |
|---|
| 203 | n/a | userDir = os.path.expanduser('~') |
|---|
| 204 | n/a | if userDir != '~': # expanduser() found user home dir |
|---|
| 205 | n/a | if not os.path.exists(userDir): |
|---|
| 206 | n/a | warn = ('\n Warning: os.path.expanduser("~") points to\n '+ |
|---|
| 207 | n/a | userDir+',\n but the path does not exist.\n') |
|---|
| 208 | n/a | try: |
|---|
| 209 | n/a | sys.stderr.write(warn) |
|---|
| 210 | n/a | except OSError: |
|---|
| 211 | n/a | pass |
|---|
| 212 | n/a | userDir = '~' |
|---|
| 213 | n/a | if userDir == "~": # still no path to home! |
|---|
| 214 | n/a | # traditionally IDLE has defaulted to os.getcwd(), is this adequate? |
|---|
| 215 | n/a | userDir = os.getcwd() |
|---|
| 216 | n/a | userDir = os.path.join(userDir, cfgDir) |
|---|
| 217 | n/a | if not os.path.exists(userDir): |
|---|
| 218 | n/a | try: |
|---|
| 219 | n/a | os.mkdir(userDir) |
|---|
| 220 | n/a | except OSError: |
|---|
| 221 | n/a | warn = ('\n Warning: unable to create user config directory\n'+ |
|---|
| 222 | n/a | userDir+'\n Check path and permissions.\n Exiting!\n\n') |
|---|
| 223 | n/a | sys.stderr.write(warn) |
|---|
| 224 | n/a | raise SystemExit |
|---|
| 225 | n/a | return userDir |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | def GetOption(self, configType, section, option, default=None, type=None, |
|---|
| 228 | n/a | warn_on_default=True, raw=False): |
|---|
| 229 | n/a | """ |
|---|
| 230 | n/a | Get an option value for given config type and given general |
|---|
| 231 | n/a | configuration section/option or return a default. If type is specified, |
|---|
| 232 | n/a | return as type. Firstly the user configuration is checked, with a |
|---|
| 233 | n/a | fallback to the default configuration, and a final 'catch all' |
|---|
| 234 | n/a | fallback to a useable passed-in default if the option isn't present in |
|---|
| 235 | n/a | either the user or the default configuration. |
|---|
| 236 | n/a | configType must be one of ('main','extensions','highlight','keys') |
|---|
| 237 | n/a | If a default is returned, and warn_on_default is True, a warning is |
|---|
| 238 | n/a | printed to stderr. |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | """ |
|---|
| 241 | n/a | try: |
|---|
| 242 | n/a | if self.userCfg[configType].has_option(section,option): |
|---|
| 243 | n/a | return self.userCfg[configType].Get(section, option, |
|---|
| 244 | n/a | type=type, raw=raw) |
|---|
| 245 | n/a | except ValueError: |
|---|
| 246 | n/a | warning = ('\n Warning: configHandler.py - IdleConf.GetOption -\n' |
|---|
| 247 | n/a | ' invalid %r value for configuration option %r\n' |
|---|
| 248 | n/a | ' from section %r: %r\n' % |
|---|
| 249 | n/a | (type, option, section, |
|---|
| 250 | n/a | self.userCfg[configType].Get(section, option, |
|---|
| 251 | n/a | raw=raw))) |
|---|
| 252 | n/a | try: |
|---|
| 253 | n/a | sys.stderr.write(warning) |
|---|
| 254 | n/a | except OSError: |
|---|
| 255 | n/a | pass |
|---|
| 256 | n/a | try: |
|---|
| 257 | n/a | if self.defaultCfg[configType].has_option(section,option): |
|---|
| 258 | n/a | return self.defaultCfg[configType].Get(section, option, |
|---|
| 259 | n/a | type=type, raw=raw) |
|---|
| 260 | n/a | except ValueError: |
|---|
| 261 | n/a | pass |
|---|
| 262 | n/a | #returning default, print warning |
|---|
| 263 | n/a | if warn_on_default: |
|---|
| 264 | n/a | warning = ('\n Warning: configHandler.py - IdleConf.GetOption -\n' |
|---|
| 265 | n/a | ' problem retrieving configuration option %r\n' |
|---|
| 266 | n/a | ' from section %r.\n' |
|---|
| 267 | n/a | ' returning default value: %r\n' % |
|---|
| 268 | n/a | (option, section, default)) |
|---|
| 269 | n/a | try: |
|---|
| 270 | n/a | sys.stderr.write(warning) |
|---|
| 271 | n/a | except OSError: |
|---|
| 272 | n/a | pass |
|---|
| 273 | n/a | return default |
|---|
| 274 | n/a | |
|---|
| 275 | n/a | def SetOption(self, configType, section, option, value): |
|---|
| 276 | n/a | """In user's config file, set section's option to value. |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | """ |
|---|
| 279 | n/a | self.userCfg[configType].SetOption(section, option, value) |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | def GetSectionList(self, configSet, configType): |
|---|
| 282 | n/a | """ |
|---|
| 283 | n/a | Get a list of sections from either the user or default config for |
|---|
| 284 | n/a | the given config type. |
|---|
| 285 | n/a | configSet must be either 'user' or 'default' |
|---|
| 286 | n/a | configType must be one of ('main','extensions','highlight','keys') |
|---|
| 287 | n/a | """ |
|---|
| 288 | n/a | if not (configType in ('main','extensions','highlight','keys')): |
|---|
| 289 | n/a | raise InvalidConfigType('Invalid configType specified') |
|---|
| 290 | n/a | if configSet == 'user': |
|---|
| 291 | n/a | cfgParser=self.userCfg[configType] |
|---|
| 292 | n/a | elif configSet == 'default': |
|---|
| 293 | n/a | cfgParser=self.defaultCfg[configType] |
|---|
| 294 | n/a | else: |
|---|
| 295 | n/a | raise InvalidConfigSet('Invalid configSet specified') |
|---|
| 296 | n/a | return cfgParser.sections() |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | def GetHighlight(self, theme, element, fgBg=None): |
|---|
| 299 | n/a | """ |
|---|
| 300 | n/a | return individual highlighting theme elements. |
|---|
| 301 | n/a | fgBg - string ('fg'or'bg') or None, if None return a dictionary |
|---|
| 302 | n/a | containing fg and bg colours (appropriate for passing to Tkinter in, |
|---|
| 303 | n/a | e.g., a tag_config call), otherwise fg or bg colour only as specified. |
|---|
| 304 | n/a | """ |
|---|
| 305 | n/a | if self.defaultCfg['highlight'].has_section(theme): |
|---|
| 306 | n/a | themeDict=self.GetThemeDict('default',theme) |
|---|
| 307 | n/a | else: |
|---|
| 308 | n/a | themeDict=self.GetThemeDict('user',theme) |
|---|
| 309 | n/a | fore=themeDict[element+'-foreground'] |
|---|
| 310 | n/a | if element=='cursor': #there is no config value for cursor bg |
|---|
| 311 | n/a | back=themeDict['normal-background'] |
|---|
| 312 | n/a | else: |
|---|
| 313 | n/a | back=themeDict[element+'-background'] |
|---|
| 314 | n/a | highlight={"foreground": fore,"background": back} |
|---|
| 315 | n/a | if not fgBg: #return dict of both colours |
|---|
| 316 | n/a | return highlight |
|---|
| 317 | n/a | else: #return specified colour only |
|---|
| 318 | n/a | if fgBg == 'fg': |
|---|
| 319 | n/a | return highlight["foreground"] |
|---|
| 320 | n/a | if fgBg == 'bg': |
|---|
| 321 | n/a | return highlight["background"] |
|---|
| 322 | n/a | else: |
|---|
| 323 | n/a | raise InvalidFgBg('Invalid fgBg specified') |
|---|
| 324 | n/a | |
|---|
| 325 | n/a | def GetThemeDict(self,type,themeName): |
|---|
| 326 | n/a | """ |
|---|
| 327 | n/a | type - string, 'default' or 'user' theme type |
|---|
| 328 | n/a | themeName - string, theme name |
|---|
| 329 | n/a | Returns a dictionary which holds {option:value} for each element |
|---|
| 330 | n/a | in the specified theme. Values are loaded over a set of ultimate last |
|---|
| 331 | n/a | fallback defaults to guarantee that all theme elements are present in |
|---|
| 332 | n/a | a newly created theme. |
|---|
| 333 | n/a | """ |
|---|
| 334 | n/a | if type == 'user': |
|---|
| 335 | n/a | cfgParser=self.userCfg['highlight'] |
|---|
| 336 | n/a | elif type == 'default': |
|---|
| 337 | n/a | cfgParser=self.defaultCfg['highlight'] |
|---|
| 338 | n/a | else: |
|---|
| 339 | n/a | raise InvalidTheme('Invalid theme type specified') |
|---|
| 340 | n/a | #foreground and background values are provded for each theme element |
|---|
| 341 | n/a | #(apart from cursor) even though all these values are not yet used |
|---|
| 342 | n/a | #by idle, to allow for their use in the future. Default values are |
|---|
| 343 | n/a | #generally black and white. |
|---|
| 344 | n/a | theme={ 'normal-foreground':'#000000', |
|---|
| 345 | n/a | 'normal-background':'#ffffff', |
|---|
| 346 | n/a | 'keyword-foreground':'#000000', |
|---|
| 347 | n/a | 'keyword-background':'#ffffff', |
|---|
| 348 | n/a | 'builtin-foreground':'#000000', |
|---|
| 349 | n/a | 'builtin-background':'#ffffff', |
|---|
| 350 | n/a | 'comment-foreground':'#000000', |
|---|
| 351 | n/a | 'comment-background':'#ffffff', |
|---|
| 352 | n/a | 'string-foreground':'#000000', |
|---|
| 353 | n/a | 'string-background':'#ffffff', |
|---|
| 354 | n/a | 'definition-foreground':'#000000', |
|---|
| 355 | n/a | 'definition-background':'#ffffff', |
|---|
| 356 | n/a | 'hilite-foreground':'#000000', |
|---|
| 357 | n/a | 'hilite-background':'gray', |
|---|
| 358 | n/a | 'break-foreground':'#ffffff', |
|---|
| 359 | n/a | 'break-background':'#000000', |
|---|
| 360 | n/a | 'hit-foreground':'#ffffff', |
|---|
| 361 | n/a | 'hit-background':'#000000', |
|---|
| 362 | n/a | 'error-foreground':'#ffffff', |
|---|
| 363 | n/a | 'error-background':'#000000', |
|---|
| 364 | n/a | #cursor (only foreground can be set) |
|---|
| 365 | n/a | 'cursor-foreground':'#000000', |
|---|
| 366 | n/a | #shell window |
|---|
| 367 | n/a | 'stdout-foreground':'#000000', |
|---|
| 368 | n/a | 'stdout-background':'#ffffff', |
|---|
| 369 | n/a | 'stderr-foreground':'#000000', |
|---|
| 370 | n/a | 'stderr-background':'#ffffff', |
|---|
| 371 | n/a | 'console-foreground':'#000000', |
|---|
| 372 | n/a | 'console-background':'#ffffff' } |
|---|
| 373 | n/a | for element in theme: |
|---|
| 374 | n/a | if not cfgParser.has_option(themeName,element): |
|---|
| 375 | n/a | #we are going to return a default, print warning |
|---|
| 376 | n/a | warning=('\n Warning: configHandler.py - IdleConf.GetThemeDict' |
|---|
| 377 | n/a | ' -\n problem retrieving theme element %r' |
|---|
| 378 | n/a | '\n from theme %r.\n' |
|---|
| 379 | n/a | ' returning default value: %r\n' % |
|---|
| 380 | n/a | (element, themeName, theme[element])) |
|---|
| 381 | n/a | try: |
|---|
| 382 | n/a | sys.stderr.write(warning) |
|---|
| 383 | n/a | except OSError: |
|---|
| 384 | n/a | pass |
|---|
| 385 | n/a | colour=cfgParser.Get(themeName,element,default=theme[element]) |
|---|
| 386 | n/a | theme[element]=colour |
|---|
| 387 | n/a | return theme |
|---|
| 388 | n/a | |
|---|
| 389 | n/a | def CurrentTheme(self): |
|---|
| 390 | n/a | """ |
|---|
| 391 | n/a | Returns the name of the currently active theme |
|---|
| 392 | n/a | """ |
|---|
| 393 | n/a | return self.GetOption('main','Theme','name',default='') |
|---|
| 394 | n/a | |
|---|
| 395 | n/a | def CurrentKeys(self): |
|---|
| 396 | n/a | """ |
|---|
| 397 | n/a | Returns the name of the currently active key set |
|---|
| 398 | n/a | """ |
|---|
| 399 | n/a | return self.GetOption('main','Keys','name',default='') |
|---|
| 400 | n/a | |
|---|
| 401 | n/a | def GetExtensions(self, active_only=True, editor_only=False, shell_only=False): |
|---|
| 402 | n/a | """ |
|---|
| 403 | n/a | Gets a list of all idle extensions declared in the config files. |
|---|
| 404 | n/a | active_only - boolean, if true only return active (enabled) extensions |
|---|
| 405 | n/a | """ |
|---|
| 406 | n/a | extns=self.RemoveKeyBindNames( |
|---|
| 407 | n/a | self.GetSectionList('default','extensions')) |
|---|
| 408 | n/a | userExtns=self.RemoveKeyBindNames( |
|---|
| 409 | n/a | self.GetSectionList('user','extensions')) |
|---|
| 410 | n/a | for extn in userExtns: |
|---|
| 411 | n/a | if extn not in extns: #user has added own extension |
|---|
| 412 | n/a | extns.append(extn) |
|---|
| 413 | n/a | if active_only: |
|---|
| 414 | n/a | activeExtns=[] |
|---|
| 415 | n/a | for extn in extns: |
|---|
| 416 | n/a | if self.GetOption('extensions', extn, 'enable', default=True, |
|---|
| 417 | n/a | type='bool'): |
|---|
| 418 | n/a | #the extension is enabled |
|---|
| 419 | n/a | if editor_only or shell_only: |
|---|
| 420 | n/a | if editor_only: |
|---|
| 421 | n/a | option = "enable_editor" |
|---|
| 422 | n/a | else: |
|---|
| 423 | n/a | option = "enable_shell" |
|---|
| 424 | n/a | if self.GetOption('extensions', extn,option, |
|---|
| 425 | n/a | default=True, type='bool', |
|---|
| 426 | n/a | warn_on_default=False): |
|---|
| 427 | n/a | activeExtns.append(extn) |
|---|
| 428 | n/a | else: |
|---|
| 429 | n/a | activeExtns.append(extn) |
|---|
| 430 | n/a | return activeExtns |
|---|
| 431 | n/a | else: |
|---|
| 432 | n/a | return extns |
|---|
| 433 | n/a | |
|---|
| 434 | n/a | def RemoveKeyBindNames(self,extnNameList): |
|---|
| 435 | n/a | #get rid of keybinding section names |
|---|
| 436 | n/a | names=extnNameList |
|---|
| 437 | n/a | kbNameIndicies=[] |
|---|
| 438 | n/a | for name in names: |
|---|
| 439 | n/a | if name.endswith(('_bindings', '_cfgBindings')): |
|---|
| 440 | n/a | kbNameIndicies.append(names.index(name)) |
|---|
| 441 | n/a | kbNameIndicies.sort() |
|---|
| 442 | n/a | kbNameIndicies.reverse() |
|---|
| 443 | n/a | for index in kbNameIndicies: #delete each keybinding section name |
|---|
| 444 | n/a | del(names[index]) |
|---|
| 445 | n/a | return names |
|---|
| 446 | n/a | |
|---|
| 447 | n/a | def GetExtnNameForEvent(self,virtualEvent): |
|---|
| 448 | n/a | """ |
|---|
| 449 | n/a | Returns the name of the extension that virtualEvent is bound in, or |
|---|
| 450 | n/a | None if not bound in any extension. |
|---|
| 451 | n/a | virtualEvent - string, name of the virtual event to test for, without |
|---|
| 452 | n/a | the enclosing '<< >>' |
|---|
| 453 | n/a | """ |
|---|
| 454 | n/a | extName=None |
|---|
| 455 | n/a | vEvent='<<'+virtualEvent+'>>' |
|---|
| 456 | n/a | for extn in self.GetExtensions(active_only=0): |
|---|
| 457 | n/a | for event in self.GetExtensionKeys(extn): |
|---|
| 458 | n/a | if event == vEvent: |
|---|
| 459 | n/a | extName=extn |
|---|
| 460 | n/a | return extName |
|---|
| 461 | n/a | |
|---|
| 462 | n/a | def GetExtensionKeys(self,extensionName): |
|---|
| 463 | n/a | """ |
|---|
| 464 | n/a | returns a dictionary of the configurable keybindings for a particular |
|---|
| 465 | n/a | extension,as they exist in the dictionary returned by GetCurrentKeySet; |
|---|
| 466 | n/a | that is, where previously used bindings are disabled. |
|---|
| 467 | n/a | """ |
|---|
| 468 | n/a | keysName=extensionName+'_cfgBindings' |
|---|
| 469 | n/a | activeKeys=self.GetCurrentKeySet() |
|---|
| 470 | n/a | extKeys={} |
|---|
| 471 | n/a | if self.defaultCfg['extensions'].has_section(keysName): |
|---|
| 472 | n/a | eventNames=self.defaultCfg['extensions'].GetOptionList(keysName) |
|---|
| 473 | n/a | for eventName in eventNames: |
|---|
| 474 | n/a | event='<<'+eventName+'>>' |
|---|
| 475 | n/a | binding=activeKeys[event] |
|---|
| 476 | n/a | extKeys[event]=binding |
|---|
| 477 | n/a | return extKeys |
|---|
| 478 | n/a | |
|---|
| 479 | n/a | def __GetRawExtensionKeys(self,extensionName): |
|---|
| 480 | n/a | """ |
|---|
| 481 | n/a | returns a dictionary of the configurable keybindings for a particular |
|---|
| 482 | n/a | extension, as defined in the configuration files, or an empty dictionary |
|---|
| 483 | n/a | if no bindings are found |
|---|
| 484 | n/a | """ |
|---|
| 485 | n/a | keysName=extensionName+'_cfgBindings' |
|---|
| 486 | n/a | extKeys={} |
|---|
| 487 | n/a | if self.defaultCfg['extensions'].has_section(keysName): |
|---|
| 488 | n/a | eventNames=self.defaultCfg['extensions'].GetOptionList(keysName) |
|---|
| 489 | n/a | for eventName in eventNames: |
|---|
| 490 | n/a | binding=self.GetOption('extensions',keysName, |
|---|
| 491 | n/a | eventName,default='').split() |
|---|
| 492 | n/a | event='<<'+eventName+'>>' |
|---|
| 493 | n/a | extKeys[event]=binding |
|---|
| 494 | n/a | return extKeys |
|---|
| 495 | n/a | |
|---|
| 496 | n/a | def GetExtensionBindings(self,extensionName): |
|---|
| 497 | n/a | """ |
|---|
| 498 | n/a | Returns a dictionary of all the event bindings for a particular |
|---|
| 499 | n/a | extension. The configurable keybindings are returned as they exist in |
|---|
| 500 | n/a | the dictionary returned by GetCurrentKeySet; that is, where re-used |
|---|
| 501 | n/a | keybindings are disabled. |
|---|
| 502 | n/a | """ |
|---|
| 503 | n/a | bindsName=extensionName+'_bindings' |
|---|
| 504 | n/a | extBinds=self.GetExtensionKeys(extensionName) |
|---|
| 505 | n/a | #add the non-configurable bindings |
|---|
| 506 | n/a | if self.defaultCfg['extensions'].has_section(bindsName): |
|---|
| 507 | n/a | eventNames=self.defaultCfg['extensions'].GetOptionList(bindsName) |
|---|
| 508 | n/a | for eventName in eventNames: |
|---|
| 509 | n/a | binding=self.GetOption('extensions',bindsName, |
|---|
| 510 | n/a | eventName,default='').split() |
|---|
| 511 | n/a | event='<<'+eventName+'>>' |
|---|
| 512 | n/a | extBinds[event]=binding |
|---|
| 513 | n/a | |
|---|
| 514 | n/a | return extBinds |
|---|
| 515 | n/a | |
|---|
| 516 | n/a | def GetKeyBinding(self, keySetName, eventStr): |
|---|
| 517 | n/a | """ |
|---|
| 518 | n/a | returns the keybinding for a specific event. |
|---|
| 519 | n/a | keySetName - string, name of key binding set |
|---|
| 520 | n/a | eventStr - string, the virtual event we want the binding for, |
|---|
| 521 | n/a | represented as a string, eg. '<<event>>' |
|---|
| 522 | n/a | """ |
|---|
| 523 | n/a | eventName=eventStr[2:-2] #trim off the angle brackets |
|---|
| 524 | n/a | binding=self.GetOption('keys',keySetName,eventName,default='').split() |
|---|
| 525 | n/a | return binding |
|---|
| 526 | n/a | |
|---|
| 527 | n/a | def GetCurrentKeySet(self): |
|---|
| 528 | n/a | result = self.GetKeySet(self.CurrentKeys()) |
|---|
| 529 | n/a | |
|---|
| 530 | n/a | if macosxSupport.runningAsOSXApp(): |
|---|
| 531 | n/a | # We're using AquaTk, replace all keybingings that use the |
|---|
| 532 | n/a | # Alt key by ones that use the Option key because the former |
|---|
| 533 | n/a | # don't work reliably. |
|---|
| 534 | n/a | for k, v in result.items(): |
|---|
| 535 | n/a | v2 = [ x.replace('<Alt-', '<Option-') for x in v ] |
|---|
| 536 | n/a | if v != v2: |
|---|
| 537 | n/a | result[k] = v2 |
|---|
| 538 | n/a | |
|---|
| 539 | n/a | return result |
|---|
| 540 | n/a | |
|---|
| 541 | n/a | def GetKeySet(self,keySetName): |
|---|
| 542 | n/a | """ |
|---|
| 543 | n/a | Returns a dictionary of: all requested core keybindings, plus the |
|---|
| 544 | n/a | keybindings for all currently active extensions. If a binding defined |
|---|
| 545 | n/a | in an extension is already in use, that binding is disabled. |
|---|
| 546 | n/a | """ |
|---|
| 547 | n/a | keySet=self.GetCoreKeys(keySetName) |
|---|
| 548 | n/a | activeExtns=self.GetExtensions(active_only=1) |
|---|
| 549 | n/a | for extn in activeExtns: |
|---|
| 550 | n/a | extKeys=self.__GetRawExtensionKeys(extn) |
|---|
| 551 | n/a | if extKeys: #the extension defines keybindings |
|---|
| 552 | n/a | for event in extKeys: |
|---|
| 553 | n/a | if extKeys[event] in keySet.values(): |
|---|
| 554 | n/a | #the binding is already in use |
|---|
| 555 | n/a | extKeys[event]='' #disable this binding |
|---|
| 556 | n/a | keySet[event]=extKeys[event] #add binding |
|---|
| 557 | n/a | return keySet |
|---|
| 558 | n/a | |
|---|
| 559 | n/a | def IsCoreBinding(self,virtualEvent): |
|---|
| 560 | n/a | """ |
|---|
| 561 | n/a | returns true if the virtual event is bound in the core idle keybindings. |
|---|
| 562 | n/a | virtualEvent - string, name of the virtual event to test for, without |
|---|
| 563 | n/a | the enclosing '<< >>' |
|---|
| 564 | n/a | """ |
|---|
| 565 | n/a | return ('<<'+virtualEvent+'>>') in self.GetCoreKeys() |
|---|
| 566 | n/a | |
|---|
| 567 | n/a | def GetCoreKeys(self, keySetName=None): |
|---|
| 568 | n/a | """ |
|---|
| 569 | n/a | returns the requested set of core keybindings, with fallbacks if |
|---|
| 570 | n/a | required. |
|---|
| 571 | n/a | Keybindings loaded from the config file(s) are loaded _over_ these |
|---|
| 572 | n/a | defaults, so if there is a problem getting any core binding there will |
|---|
| 573 | n/a | be an 'ultimate last resort fallback' to the CUA-ish bindings |
|---|
| 574 | n/a | defined here. |
|---|
| 575 | n/a | """ |
|---|
| 576 | n/a | keyBindings={ |
|---|
| 577 | n/a | '<<copy>>': ['<Control-c>', '<Control-C>'], |
|---|
| 578 | n/a | '<<cut>>': ['<Control-x>', '<Control-X>'], |
|---|
| 579 | n/a | '<<paste>>': ['<Control-v>', '<Control-V>'], |
|---|
| 580 | n/a | '<<beginning-of-line>>': ['<Control-a>', '<Home>'], |
|---|
| 581 | n/a | '<<center-insert>>': ['<Control-l>'], |
|---|
| 582 | n/a | '<<close-all-windows>>': ['<Control-q>'], |
|---|
| 583 | n/a | '<<close-window>>': ['<Alt-F4>'], |
|---|
| 584 | n/a | '<<do-nothing>>': ['<Control-x>'], |
|---|
| 585 | n/a | '<<end-of-file>>': ['<Control-d>'], |
|---|
| 586 | n/a | '<<python-docs>>': ['<F1>'], |
|---|
| 587 | n/a | '<<python-context-help>>': ['<Shift-F1>'], |
|---|
| 588 | n/a | '<<history-next>>': ['<Alt-n>'], |
|---|
| 589 | n/a | '<<history-previous>>': ['<Alt-p>'], |
|---|
| 590 | n/a | '<<interrupt-execution>>': ['<Control-c>'], |
|---|
| 591 | n/a | '<<view-restart>>': ['<F6>'], |
|---|
| 592 | n/a | '<<restart-shell>>': ['<Control-F6>'], |
|---|
| 593 | n/a | '<<open-class-browser>>': ['<Alt-c>'], |
|---|
| 594 | n/a | '<<open-module>>': ['<Alt-m>'], |
|---|
| 595 | n/a | '<<open-new-window>>': ['<Control-n>'], |
|---|
| 596 | n/a | '<<open-window-from-file>>': ['<Control-o>'], |
|---|
| 597 | n/a | '<<plain-newline-and-indent>>': ['<Control-j>'], |
|---|
| 598 | n/a | '<<print-window>>': ['<Control-p>'], |
|---|
| 599 | n/a | '<<redo>>': ['<Control-y>'], |
|---|
| 600 | n/a | '<<remove-selection>>': ['<Escape>'], |
|---|
| 601 | n/a | '<<save-copy-of-window-as-file>>': ['<Alt-Shift-S>'], |
|---|
| 602 | n/a | '<<save-window-as-file>>': ['<Alt-s>'], |
|---|
| 603 | n/a | '<<save-window>>': ['<Control-s>'], |
|---|
| 604 | n/a | '<<select-all>>': ['<Alt-a>'], |
|---|
| 605 | n/a | '<<toggle-auto-coloring>>': ['<Control-slash>'], |
|---|
| 606 | n/a | '<<undo>>': ['<Control-z>'], |
|---|
| 607 | n/a | '<<find-again>>': ['<Control-g>', '<F3>'], |
|---|
| 608 | n/a | '<<find-in-files>>': ['<Alt-F3>'], |
|---|
| 609 | n/a | '<<find-selection>>': ['<Control-F3>'], |
|---|
| 610 | n/a | '<<find>>': ['<Control-f>'], |
|---|
| 611 | n/a | '<<replace>>': ['<Control-h>'], |
|---|
| 612 | n/a | '<<goto-line>>': ['<Alt-g>'], |
|---|
| 613 | n/a | '<<smart-backspace>>': ['<Key-BackSpace>'], |
|---|
| 614 | n/a | '<<newline-and-indent>>': ['<Key-Return>', '<Key-KP_Enter>'], |
|---|
| 615 | n/a | '<<smart-indent>>': ['<Key-Tab>'], |
|---|
| 616 | n/a | '<<indent-region>>': ['<Control-Key-bracketright>'], |
|---|
| 617 | n/a | '<<dedent-region>>': ['<Control-Key-bracketleft>'], |
|---|
| 618 | n/a | '<<comment-region>>': ['<Alt-Key-3>'], |
|---|
| 619 | n/a | '<<uncomment-region>>': ['<Alt-Key-4>'], |
|---|
| 620 | n/a | '<<tabify-region>>': ['<Alt-Key-5>'], |
|---|
| 621 | n/a | '<<untabify-region>>': ['<Alt-Key-6>'], |
|---|
| 622 | n/a | '<<toggle-tabs>>': ['<Alt-Key-t>'], |
|---|
| 623 | n/a | '<<change-indentwidth>>': ['<Alt-Key-u>'], |
|---|
| 624 | n/a | '<<del-word-left>>': ['<Control-Key-BackSpace>'], |
|---|
| 625 | n/a | '<<del-word-right>>': ['<Control-Key-Delete>'] |
|---|
| 626 | n/a | } |
|---|
| 627 | n/a | if keySetName: |
|---|
| 628 | n/a | for event in keyBindings: |
|---|
| 629 | n/a | binding=self.GetKeyBinding(keySetName,event) |
|---|
| 630 | n/a | if binding: |
|---|
| 631 | n/a | keyBindings[event]=binding |
|---|
| 632 | n/a | else: #we are going to return a default, print warning |
|---|
| 633 | n/a | warning=('\n Warning: configHandler.py - IdleConf.GetCoreKeys' |
|---|
| 634 | n/a | ' -\n problem retrieving key binding for event %r' |
|---|
| 635 | n/a | '\n from key set %r.\n' |
|---|
| 636 | n/a | ' returning default value: %r\n' % |
|---|
| 637 | n/a | (event, keySetName, keyBindings[event])) |
|---|
| 638 | n/a | try: |
|---|
| 639 | n/a | sys.stderr.write(warning) |
|---|
| 640 | n/a | except OSError: |
|---|
| 641 | n/a | pass |
|---|
| 642 | n/a | return keyBindings |
|---|
| 643 | n/a | |
|---|
| 644 | n/a | def GetExtraHelpSourceList(self,configSet): |
|---|
| 645 | n/a | """Fetch list of extra help sources from a given configSet. |
|---|
| 646 | n/a | |
|---|
| 647 | n/a | Valid configSets are 'user' or 'default'. Return a list of tuples of |
|---|
| 648 | n/a | the form (menu_item , path_to_help_file , option), or return the empty |
|---|
| 649 | n/a | list. 'option' is the sequence number of the help resource. 'option' |
|---|
| 650 | n/a | values determine the position of the menu items on the Help menu, |
|---|
| 651 | n/a | therefore the returned list must be sorted by 'option'. |
|---|
| 652 | n/a | |
|---|
| 653 | n/a | """ |
|---|
| 654 | n/a | helpSources=[] |
|---|
| 655 | n/a | if configSet=='user': |
|---|
| 656 | n/a | cfgParser=self.userCfg['main'] |
|---|
| 657 | n/a | elif configSet=='default': |
|---|
| 658 | n/a | cfgParser=self.defaultCfg['main'] |
|---|
| 659 | n/a | else: |
|---|
| 660 | n/a | raise InvalidConfigSet('Invalid configSet specified') |
|---|
| 661 | n/a | options=cfgParser.GetOptionList('HelpFiles') |
|---|
| 662 | n/a | for option in options: |
|---|
| 663 | n/a | value=cfgParser.Get('HelpFiles',option,default=';') |
|---|
| 664 | n/a | if value.find(';')==-1: #malformed config entry with no ';' |
|---|
| 665 | n/a | menuItem='' #make these empty |
|---|
| 666 | n/a | helpPath='' #so value won't be added to list |
|---|
| 667 | n/a | else: #config entry contains ';' as expected |
|---|
| 668 | n/a | value=value.split(';') |
|---|
| 669 | n/a | menuItem=value[0].strip() |
|---|
| 670 | n/a | helpPath=value[1].strip() |
|---|
| 671 | n/a | if menuItem and helpPath: #neither are empty strings |
|---|
| 672 | n/a | helpSources.append( (menuItem,helpPath,option) ) |
|---|
| 673 | n/a | helpSources.sort(key=lambda x: x[2]) |
|---|
| 674 | n/a | return helpSources |
|---|
| 675 | n/a | |
|---|
| 676 | n/a | def GetAllExtraHelpSourcesList(self): |
|---|
| 677 | n/a | """ |
|---|
| 678 | n/a | Returns a list of tuples containing the details of all additional help |
|---|
| 679 | n/a | sources configured, or an empty list if there are none. Tuples are of |
|---|
| 680 | n/a | the format returned by GetExtraHelpSourceList. |
|---|
| 681 | n/a | """ |
|---|
| 682 | n/a | allHelpSources=( self.GetExtraHelpSourceList('default')+ |
|---|
| 683 | n/a | self.GetExtraHelpSourceList('user') ) |
|---|
| 684 | n/a | return allHelpSources |
|---|
| 685 | n/a | |
|---|
| 686 | n/a | def LoadCfgFiles(self): |
|---|
| 687 | n/a | """ |
|---|
| 688 | n/a | load all configuration files. |
|---|
| 689 | n/a | """ |
|---|
| 690 | n/a | for key in self.defaultCfg: |
|---|
| 691 | n/a | self.defaultCfg[key].Load() |
|---|
| 692 | n/a | self.userCfg[key].Load() #same keys |
|---|
| 693 | n/a | |
|---|
| 694 | n/a | def SaveUserCfgFiles(self): |
|---|
| 695 | n/a | """ |
|---|
| 696 | n/a | write all loaded user configuration files back to disk |
|---|
| 697 | n/a | """ |
|---|
| 698 | n/a | for key in self.userCfg: |
|---|
| 699 | n/a | self.userCfg[key].Save() |
|---|
| 700 | n/a | |
|---|
| 701 | n/a | idleConf=IdleConf() |
|---|
| 702 | n/a | |
|---|
| 703 | n/a | ### module test |
|---|
| 704 | n/a | if __name__ == '__main__': |
|---|
| 705 | n/a | def dumpCfg(cfg): |
|---|
| 706 | n/a | print('\n',cfg,'\n') |
|---|
| 707 | n/a | for key in cfg: |
|---|
| 708 | n/a | sections=cfg[key].sections() |
|---|
| 709 | n/a | print(key) |
|---|
| 710 | n/a | print(sections) |
|---|
| 711 | n/a | for section in sections: |
|---|
| 712 | n/a | options=cfg[key].options(section) |
|---|
| 713 | n/a | print(section) |
|---|
| 714 | n/a | print(options) |
|---|
| 715 | n/a | for option in options: |
|---|
| 716 | n/a | print(option, '=', cfg[key].Get(section,option)) |
|---|
| 717 | n/a | dumpCfg(idleConf.defaultCfg) |
|---|
| 718 | n/a | dumpCfg(idleConf.userCfg) |
|---|
| 719 | n/a | print(idleConf.userCfg['main'].Get('Theme','name')) |
|---|
| 720 | n/a | #print idleConf.userCfg['highlight'].GetDefHighlight('Foo','normal') |
|---|