further work on config saving
authorSteven M. Gava <elguavas@python.net>
Tue, 29 Jan 2002 08:35:29 +0000 (08:35 +0000)
committerSteven M. Gava <elguavas@python.net>
Tue, 29 Jan 2002 08:35:29 +0000 (08:35 +0000)
Lib/idlelib/configDialog.py
Lib/idlelib/configHandler.py

index 6664b9f0ae6a6387a88b400d46a0e7711b20cbaf..c32512337ca52acba04d8b5e3e418a20d166fb9c 100644 (file)
@@ -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()
 
index d497eaa1cf3c59c4836f282cad641e15ea3d54f7..ccb7f8aeb938ea31669d4faea25f5bccef04d43b 100644 (file)
@@ -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:
     """