From 2d7bb3fa66b55c1af283b51853abe7f62a2a5f49 Mon Sep 17 00:00:00 2001
From: "Steven M. Gava" <elguavas@python.net>
Date: Tue, 29 Jan 2002 08:35:29 +0000
Subject: further work on config saving

---
 Lib/idlelib/configDialog.py  | 42 +++++++++++++++++++---------
 Lib/idlelib/configHandler.py | 65 +++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 90 insertions(+), 17 deletions(-)

diff --git a/Lib/idlelib/configDialog.py b/Lib/idlelib/configDialog.py
index 6664b9f..c325123 100644
--- a/Lib/idlelib/configDialog.py
+++ b/Lib/idlelib/configDialog.py
@@ -42,10 +42,10 @@ class ConfigDialog(Toplevel):
         #value a dictionary, whose key:value pairs are item=value pairs for
         #that config file section.
         self.changedItems={'main':{},'highlight':{},'keys':{},'extensions':{}}
-        #defaultItems. This dictionary is loaded with the values from the
-        #default config files. It is used for comparison with self.changedItems
-        #to decide which changed items actually need saving.
-        self.defaultItems=self.GetDefaultItems()
+#         #defaultItems. This dictionary is loaded with the values from the
+#         #default config files. It is used for comparison with self.changedItems
+#         #to decide which changed items actually need saving.
+#         self.defaultItems=self.GetDefaultItems()
         self.CreateWidgets()
         self.resizable(height=FALSE,width=FALSE)
         self.transient(parent)
@@ -472,6 +472,7 @@ class ConfigDialog(Toplevel):
         self.AddChangedItem('extensions',extension,'enabled',value)
 
     def AddChangedItem(self,type,section,item,value):
+        value=str(value) #make sure we use a string
         if not self.changedItems[type].has_key(section):
             self.changedItems[type][section]={}    
         self.changedItems[type][section][item]=value
@@ -522,6 +523,8 @@ class ConfigDialog(Toplevel):
                 message=('Your changes will be saved as a new Custom Key Set. '+
                         'Enter a name for your new Custom Key Set below.')
                 usedNames=idleConf.GetSectionList('user','keys')
+                for newName in self.changedItems['keys'].keys():
+                    if newName not in usedNames: usedNames.append(newName)
                 newKeySet=GetCfgSectionNameDialog(self,'New Custom Key Set',
                         message,usedNames)
                 if not newKeySet.result: #user cancelled custom key set creation
@@ -557,7 +560,8 @@ class ConfigDialog(Toplevel):
                     prevKeySet[event])
         #change gui over to the new key set
         customKeyList=idleConf.GetSectionList('user','keys')
-        customKeyList.append(newKeySetName)
+        for newName in self.changedItems['keys'].keys():
+            if newName not in customKeyList: customKeyList.append(newName)
         customKeyList.sort()
         print newKeySetName,customKeyList,self.changedItems['keys'][newKeySetName]
         self.optMenuKeysCustom.SetMenu(customKeyList,newKeySetName)
@@ -574,6 +578,8 @@ class ConfigDialog(Toplevel):
                 message=('Your changes will be saved as a new Custom Theme. '+
                         'Enter a name for your new Custom Theme below.')
                 usedNames=idleConf.GetSectionList('user','highlight')
+                for newName in self.changedItems['highlight'].keys():
+                    if newName not in usedNames: usedNames.append(newName)
                 newTheme=GetCfgSectionNameDialog(self,'New Custom Theme',
                         message,usedNames)
                 if not newTheme.result: #user cancelled custom theme creation
@@ -601,7 +607,8 @@ class ConfigDialog(Toplevel):
         self.changedItems['highlight'][newThemeName]=newTheme    
         #change gui over to the new theme
         customThemeList=idleConf.GetSectionList('user','highlight')
-        customThemeList.append(newThemeName)
+        for newName in self.changedItems['highlight'].keys():
+            if newName not in customThemeList: customThemeList.append(newName)
         customThemeList.sort()
         print newThemeName,customThemeList,newTheme
         self.optMenuThemeCustom.SetMenu(customThemeList,newThemeName)
@@ -805,21 +812,30 @@ class ConfigDialog(Toplevel):
         ### general page
         self.LoadGeneralCfg()
         
+    def SetUserValue(self,configType,section,item,value):
+        print idleConf.defaultCfg[configType].Get(section,item),value
+        if idleConf.defaultCfg[configType].has_option(section,item):
+            if idleConf.defaultCfg[configType].Get(section,item)==value:
+                #the setting equals a default setting, remove it from user cfg
+                return idleConf.userCfg[configType].RemoveOption(section,item)
+        #if we got here set the option
+        return idleConf.userCfg[configType].SetOption(section,item,value)
+            
     def SaveConfigs(self):
         """
         save configuration changes to user config files.
         """
-        #DEBUG
-        print self.defaultItems
-        print self.changedItems
         for configType in self.changedItems.keys():
+            cfgTypeHasChanges=0
             for section in self.changedItems[configType].keys():
                 for item in self.changedItems[configType][section].keys():
-                    #DEBUG
                     value=self.changedItems[configType][section][item]
-                    print configType, section, item, value 
-                    print self.changedItems
-                    
+                    print configType,section,item,value
+                    if self.SetUserValue(configType,section,item,value):
+                        cfgTypeHasChanges=1
+            if cfgTypeHasChanges: 
+                idleConf.userCfg[configType].Save()                
+    
     def Cancel(self):
         self.destroy()
 
diff --git a/Lib/idlelib/configHandler.py b/Lib/idlelib/configHandler.py
index d497eaa..ccb7f8a 100644
--- a/Lib/idlelib/configHandler.py
+++ b/Lib/idlelib/configHandler.py
@@ -57,14 +57,71 @@ class IdleConfParser(ConfigParser):
         
 class IdleUserConfParser(IdleConfParser):
     """
-    IdleConfigParser specialised for user configuration handling
+    IdleConfigParser specialised for user configuration handling.
     """
+
+    def AddSection(self,section):
+        """
+        if section doesn't exist, add it
+        """
+        if not self.has_section(section):
+            self.add_section(section)
+    
+    def RemoveEmptySections(self):
+        """
+        remove any sections that have no options
+        """
+        for section in self.sections():
+            if not self.GetOptionList(section):
+                self.remove_section(section) 
+    
+    def IsEmpty(self):
+        """
+        Remove empty sections and then return 1 if parser has no sections
+        left, else return 0.
+        """
+        self.RemoveEmptySections()
+        if self.sections():
+            return 0
+        else:
+            return 1
+    
+    def RemoveOption(self,section,option):
+        """
+        If section/option exists, remove it.
+        Returns 1 if option was removed, 0 otherwise.
+        """
+        if self.has_section(section):
+            return self.remove_option(section,option)
+    
+    def SetOption(self,section,option,value):
+        """
+        Sets option to value, adding section if required.
+        Returns 1 if option was added or changed, otherwise 0.
+        """
+        if self.has_option(section,option):
+            if self.get(section,option)==value:
+                return 0
+            else:
+                self.set(section,option,value)
+                return 1
+        else:
+            if not self.has_section(section):
+                self.add_section(section)
+            self.set(section,option,value)
+            return 1
+     
     def Save(self):
         """
-        write loaded user configuration file back to disk
+        If config isn't empty, write file to disk. If config is empty,
+        remove the file from disk if it exists.
         """
-        # this is a user config, it can be written to disk
-        self.write()
+        if not self.IsEmpty():
+            cfgFile=open(self.file,'w')
+            self.write(cfgFile)
+        else:
+            if os.path.exists(self.file):
+                os.remove(self.file)    
 
 class IdleConf:
     """
-- 
cgit v0.12

