Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f9bb90e

Browse files
author
Steven M. Gava
committed
further work on saving configs
1 parent 813b56e commit f9bb90e

3 files changed

Lines changed: 165 additions & 47 deletions

File tree

Lib/idlelib/configDialog.py

Lines changed: 97 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from dynOptionMenuWidget import DynOptionMenu
1010
from tabpage import TabPageSet
1111
from keybindingDialog import GetKeysDialog
12-
12+
from configSectionNameDialog import GetCfgSectionNameDialog
1313
class ConfigDialog(Toplevel):
1414
"""
1515
configuration dialog for idle
@@ -222,7 +222,7 @@ def CreatePageHighlight(self):
222222
value=0,text='Background',command=self.SetColourSampleBinding)
223223
self.fgHilite.set(1)
224224
buttonSaveCustomTheme=Button(frameCustom,
225-
text='Save as a Custom Theme')
225+
text='Save as New Custom Theme')
226226
#frameTheme
227227
labelThemeTitle=Label(frameTheme,text='Select a Highlighting Theme')
228228
labelTypeTitle=Label(frameTheme,text='Select : ')
@@ -287,7 +287,7 @@ def CreatePageKeys(self):
287287
self.listBindings.config(xscrollcommand=scrollTargetX.set)
288288
self.buttonNewKeys=Button(frameCustom,text='Get New Keys for Selection',
289289
command=self.GetNewKeys,state=DISABLED)
290-
buttonSaveCustomKeys=Button(frameCustom,text='Save as a Custom Key Set')
290+
buttonSaveCustomKeys=Button(frameCustom,text='Save as New Custom Key Set')
291291
#frameKeySets
292292
labelKeysTitle=Label(frameKeySets,text='Select a Key Set')
293293
labelTypeTitle=Label(frameKeySets,text='Select : ')
@@ -496,6 +496,7 @@ def SetThemeType(self):
496496
self.buttonDeleteCustomTheme.config(state=DISABLED)
497497
else:
498498
self.optMenuThemeBuiltin.config(state=DISABLED)
499+
self.radioThemeCustom.config(state=NORMAL)
499500
self.optMenuThemeCustom.config(state=NORMAL)
500501
self.buttonDeleteCustomTheme.config(state=NORMAL)
501502

@@ -506,22 +507,107 @@ def SetKeysType(self):
506507
self.buttonDeleteCustomKeys.config(state=DISABLED)
507508
else:
508509
self.optMenuKeysBuiltin.config(state=DISABLED)
510+
self.radioKeysCustom.config(state=NORMAL)
509511
self.optMenuKeysCustom.config(state=NORMAL)
510512
self.buttonDeleteCustomKeys.config(state=NORMAL)
511513

514+
def GetNewKeys(self):
515+
listIndex=self.listBindings.index(ANCHOR)
516+
binding=self.listBindings.get(listIndex)
517+
bindName=binding.split()[0] #first part, up to first space
518+
currentKeySequences=idleConf.GetCurrentKeySet().values()
519+
newKeys=GetKeysDialog(self,'Get New Keys',bindName,currentKeySequences)
520+
if newKeys.result: #new keys were specified
521+
if self.keysAreDefault.get(): #current key set is a built-in
522+
message=('Your changes will be saved as a new Custom Key Set. '+
523+
'Enter a name for your new Custom Key Set below.')
524+
usedNames=idleConf.GetSectionList('user','keys')
525+
newKeySet=GetCfgSectionNameDialog(self,'New Custom Key Set',
526+
message,usedNames)
527+
if not newKeySet.result: #user cancelled custom key set creation
528+
self.listBindings.select_set(listIndex)
529+
self.listBindings.select_anchor(listIndex)
530+
return
531+
else: #create new custom key set based on previously active key set
532+
self.CreateNewKeySet(newKeySet.result)
533+
self.listBindings.delete(listIndex)
534+
self.listBindings.insert(listIndex,bindName+' - '+newKeys.result)
535+
self.listBindings.select_set(listIndex)
536+
self.listBindings.select_anchor(listIndex)
537+
self.keyBinding.set(newKeys.result)
538+
else:
539+
self.listBindings.select_set(listIndex)
540+
self.listBindings.select_anchor(listIndex)
541+
542+
def KeyBindingSelected(self,event):
543+
self.buttonNewKeys.config(state=NORMAL)
544+
545+
def CreateNewKeySet(self,newKeySetName):
546+
#creates new custom key set based on the previously active key set,
547+
#and makes the new key set active
548+
if self.keysAreDefault.get():
549+
keySetName=self.builtinKeys.get()
550+
else:
551+
keySetName=self.customKeys.get()
552+
prevKeySet=idleConf.GetKeySet(keySetName)
553+
#add the new key set to changedItems
554+
for event in prevKeySet.keys():
555+
eventName=event[2:-2] #trim off the angle brackets
556+
self.AddChangedItem('keys',newKeySetName,eventName,
557+
prevKeySet[event])
558+
#change gui over to the new key set
559+
customKeyList=idleConf.GetSectionList('user','keys')
560+
customKeyList.append(newKeySetName)
561+
customKeyList.sort()
562+
print newKeySetName,customKeyList,self.changedItems['keys'][newKeySetName]
563+
self.optMenuKeysCustom.SetMenu(customKeyList,newKeySetName)
564+
self.keysAreDefault.set(0)
565+
self.SetKeysType()
566+
512567
def GetColour(self):
513568
target=self.highlightTarget.get()
514569
rgbTuplet, colourString = tkColorChooser.askcolor(parent=self,
515570
title='Pick new colour for : '+target,
516571
initialcolor=self.frameColourSet.cget('bg'))
517572
if colourString: #user didn't cancel
573+
if self.themeIsBuiltin.get(): #current theme is a built-in
574+
message=('Your changes will be saved as a new Custom Theme. '+
575+
'Enter a name for your new Custom Theme below.')
576+
usedNames=idleConf.GetSectionList('user','highlight')
577+
newTheme=GetCfgSectionNameDialog(self,'New Custom Theme',
578+
message,usedNames)
579+
if not newTheme.result: #user cancelled custom theme creation
580+
return
581+
else: #create new custom theme based on previously active theme
582+
self.CreateNewTheme(newTheme.result)
518583
self.colour.set(colourString)
519584
self.frameColourSet.config(bg=colourString)#set sample
520585
if self.fgHilite.get(): plane='foreground'
521586
else: plane='background'
522587
apply(self.textHighlightSample.tag_config,
523588
(self.themeElements[target][0],),{plane:colourString})
524589

590+
def CreateNewTheme(self,newThemeName):
591+
#creates new custom theme based on the previously active theme,
592+
#and makes the new theme active
593+
if self.themeIsBuiltin.get():
594+
themeType='default'
595+
themeName=self.builtinTheme.get()
596+
else:
597+
themeType='user'
598+
themeName=self.customTheme.get()
599+
newTheme=idleConf.GetThemeDict(themeType,themeName)
600+
#add the new theme to changedItems
601+
self.changedItems['highlight'][newThemeName]=newTheme
602+
#change gui over to the new theme
603+
customThemeList=idleConf.GetSectionList('user','highlight')
604+
customThemeList.append(newThemeName)
605+
customThemeList.sort()
606+
print newThemeName,customThemeList,newTheme
607+
self.optMenuThemeCustom.SetMenu(customThemeList,newThemeName)
608+
self.themeIsBuiltin.set(0)
609+
self.SetThemeType()
610+
525611
def OnListFontButtonRelease(self,event):
526612
self.fontName.set(self.listFontName.get(ANCHOR))
527613
self.SetFontSample()
@@ -620,17 +706,21 @@ def LoadThemeCfg(self):
620706
##load available theme option menus
621707
if self.themeIsBuiltin.get(): #default theme selected
622708
itemList=idleConf.GetSectionList('default','highlight')
709+
itemList.sort()
623710
self.optMenuThemeBuiltin.SetMenu(itemList,currentOption)
624711
itemList=idleConf.GetSectionList('user','highlight')
712+
itemList.sort()
625713
if not itemList:
626714
self.radioThemeCustom.config(state=DISABLED)
627715
self.customTheme.set('- no custom themes -')
628716
else:
629717
self.optMenuThemeCustom.SetMenu(itemList,itemList[0])
630718
else: #user theme selected
631719
itemList=idleConf.GetSectionList('user','highlight')
720+
itemList.sort()
632721
self.optMenuThemeCustom.SetMenu(itemList,currentOption)
633722
itemList=idleConf.GetSectionList('default','highlight')
723+
itemList.sort()
634724
self.optMenuThemeBuiltin.SetMenu(itemList,itemList[0])
635725
self.SetThemeType()
636726
##load theme element option menu
@@ -654,17 +744,21 @@ def LoadKeyCfg(self):
654744
##load available keyset option menus
655745
if self.keysAreDefault.get(): #default theme selected
656746
itemList=idleConf.GetSectionList('default','keys')
747+
itemList.sort()
657748
self.optMenuKeysBuiltin.SetMenu(itemList,currentOption)
658749
itemList=idleConf.GetSectionList('user','keys')
750+
itemList.sort()
659751
if not itemList:
660752
self.radioKeysCustom.config(state=DISABLED)
661753
self.customKeys.set('- no custom keys -')
662754
else:
663755
self.optMenuKeysCustom.SetMenu(itemList,itemList[0])
664756
else: #user theme selected
665757
itemList=idleConf.GetSectionList('user','keys')
758+
itemList.sort()
666759
self.optMenuKeysCustom.SetMenu(itemList,currentOption)
667760
itemList=idleConf.GetSectionList('default','keys')
761+
itemList.sort()
668762
self.optMenuKeysBuiltin.SetMenu(itemList,itemList[0])
669763
self.SetKeysType()
670764
##load keyset element list
@@ -676,25 +770,6 @@ def LoadKeyCfg(self):
676770
bindName=bindName[2:-2] #trim off the angle brackets
677771
self.listBindings.insert(END, bindName+' - '+key)
678772

679-
def GetNewKeys(self):
680-
listIndex=self.listBindings.index(ANCHOR)
681-
binding=self.listBindings.get(listIndex)
682-
bindName=binding.split()[0] #first part, up to first space
683-
currentKeySequences=idleConf.GetCurrentKeySet().values()
684-
newKeys=GetKeysDialog(self,'Get New Keys',bindName,currentKeySequences)
685-
if newKeys.result: #new keys were specified
686-
self.listBindings.delete(listIndex)
687-
self.listBindings.insert(listIndex,bindName+' - '+newKeys.result)
688-
self.listBindings.select_set(listIndex)
689-
self.listBindings.select_anchor(listIndex)
690-
self.keyBinding.set(newKeys.result)
691-
else:
692-
self.listBindings.select_set(listIndex)
693-
self.listBindings.select_anchor(listIndex)
694-
695-
def KeyBindingSelected(self,event):
696-
self.buttonNewKeys.config(state=NORMAL)
697-
698773
def LoadGeneralCfg(self):
699774
#startup state
700775
self.startupEdit.set(idleConf.GetOption('main','General',

Lib/idlelib/configHandler.py

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, cfgFile, cfgDefaults=None):
2323
self.file=cfgFile
2424
ConfigParser.__init__(self,defaults=cfgDefaults)
2525

26-
def Get(self, section, option, type=None):
26+
def Get(self, section, option, type=None, default=None):
2727
"""
2828
Get an option value for given section/option or return default.
2929
If type is specified, return as type.
@@ -35,8 +35,10 @@ def Get(self, section, option, type=None):
3535
else:
3636
getVal=self.get
3737
if self.has_option(section,option):
38-
#return getVal(section, option, raw, vars)
38+
#return getVal(section, option, raw, vars, default)
3939
return getVal(section, option)
40+
else:
41+
return default
4042

4143
def GetOptionList(self,section):
4244
"""
@@ -188,21 +190,63 @@ def GetHighlight(self, theme, element, fgBg=None):
188190
else:
189191
raise 'Invalid fgBg specified'
190192

191-
192-
def GetTheme(self, name=None):
193-
"""
194-
Gets the requested theme or returns a final fallback theme in case
195-
one can't be obtained from either the user or default config files.
196-
"""
197-
pass
198-
193+
def GetThemeDict(self,type,themeName):
194+
"""
195+
type - string, 'default' or 'user' theme type
196+
themeName - string, theme name
197+
Returns a dictionary which holds {option:value} for each element
198+
in the specified theme. Values are loaded over a set of ultimate last
199+
fallback defaults to guarantee that all theme elements are present in
200+
a newly created theme.
201+
"""
202+
if type == 'user':
203+
cfgParser=self.userCfg['highlight']
204+
elif type == 'default':
205+
cfgParser=self.defaultCfg['highlight']
206+
else:
207+
raise 'Invalid theme type specified'
208+
#foreground and background values are provded for each theme element
209+
#(apart from cursor) even though all these values are not yet used
210+
#by idle, to allow for their use in the future. Default values are
211+
#generally black and white.
212+
theme={ 'normal-foreground':'#000000',
213+
'normal-background':'#ffffff',
214+
'keyword-foreground':'#000000',
215+
'keyword-background':'#ffffff',
216+
'comment-foreground':'#000000',
217+
'comment-background':'#ffffff',
218+
'string-foreground':'#000000',
219+
'string-background':'#ffffff',
220+
'definition-foreground':'#000000',
221+
'definition-background':'#ffffff',
222+
'hilite-foreground':'#000000',
223+
'hilite-background':'gray',
224+
'break-foreground':'#ffffff',
225+
'break-background':'#000000',
226+
'hit-foreground':'#ffffff',
227+
'hit-background':'#000000',
228+
'error-foreground':'#ffffff',
229+
'error-background':'#000000',
230+
#cursor (only foreground can be set)
231+
'cursor-foreground':'#000000',
232+
#shell window
233+
'stdout-foreground':'#000000',
234+
'stdout-background':'#ffffff',
235+
'stderr-foreground':'#000000',
236+
'stderr-background':'#ffffff',
237+
'console-foreground':'#000000',
238+
'console-background':'#ffffff' }
239+
for element in theme.keys():
240+
colour=cfgParser.Get(type,themeName,element,default=theme[element])
241+
theme[element]=colour
242+
return theme
243+
199244
def CurrentTheme(self):
200245
"""
201246
Returns the name of the currently active theme
202247
"""
203248
return self.GetOption('main','Theme','name',default='')
204249

205-
206250
def CurrentKeys(self):
207251
"""
208252
Returns the name of the currently active theme
@@ -299,8 +343,6 @@ def GetExtensionBindings(self,extensionName):
299343

300344
return extBinds
301345

302-
303-
304346
def GetKeyBinding(self, keySetName, eventStr):
305347
"""
306348
returns the keybinding for a specific event.
@@ -313,32 +355,35 @@ def GetKeyBinding(self, keySetName, eventStr):
313355
return binding
314356

315357
def GetCurrentKeySet(self):
358+
return self.GetKeySet(self.CurrentKeys())
359+
360+
def GetKeySet(self,keySetName):
316361
"""
317-
Returns a dictionary of: all current core keybindings, plus the
362+
Returns a dictionary of: all requested core keybindings, plus the
318363
keybindings for all currently active extensions. If a binding defined
319364
in an extension is already in use, that binding is disabled.
320365
"""
321-
currentKeySet=self.GetCoreKeys(keySetName=self.CurrentKeys())
366+
keySet=self.GetCoreKeys(keySetName)
322367
activeExtns=self.GetExtensions(activeOnly=1)
323368
for extn in activeExtns:
324369
extKeys=self.__GetRawExtensionKeys(extn)
325370
if extKeys: #the extension defines keybindings
326371
for event in extKeys.keys():
327-
if extKeys[event] in currentKeySet.values():
372+
if extKeys[event] in keySet.values():
328373
#the binding is already in use
329374
extKeys[event]='' #disable this binding
330-
currentKeySet[event]=extKeys[event] #add binding
331-
return currentKeySet
332-
375+
keySet[event]=extKeys[event] #add binding
376+
return keySet
377+
333378
def GetCoreKeys(self, keySetName=None):
334379
"""
335380
returns the requested set of core keybindings, with fallbacks if
336381
required.
382+
Keybindings loaded from the config file(s) are loaded _over_ these
383+
defaults, so if there is a problem getting any core binding there will
384+
be an 'ultimate last resort fallback' to the CUA-ish bindings
385+
defined here.
337386
"""
338-
#keybindings loaded from the config file(s) are loaded _over_ these
339-
#defaults, so if there is a problem getting any core binding there will
340-
#be an 'ultimate last resort fallback' to the CUA-ish bindings
341-
#defined here.
342387
keyBindings={
343388
'<<Copy>>': ['<Control-c>', '<Control-C>'],
344389
'<<Cut>>': ['<Control-x>', '<Control-X>'],

Lib/idlelib/keybindingDialog.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,6 @@ def KeysOk(self):
264264
#test the dialog
265265
root=Tk()
266266
def run():
267-
#import aboutDialog
268-
#aboutDialog.AboutDialog(root,'About')
269267
keySeq=''
270268
dlg=GetKeysDialog(root,'Get Keys','find-again',[])
271269
print dlg.result

0 commit comments

Comments
 (0)