diff options
author | Steven M. Gava <elguavas@python.net> | 2002-01-29 08:35:29 (GMT) |
---|---|---|
committer | Steven M. Gava <elguavas@python.net> | 2002-01-29 08:35:29 (GMT) |
commit | 2d7bb3fa66b55c1af283b51853abe7f62a2a5f49 (patch) | |
tree | 7ca276d3af0e0a52ce2b589eba9615764ed1a291 /Lib/idlelib/configHandler.py | |
parent | 150d09d3601821d85be03455ac8f8a4af566d08a (diff) | |
download | cpython-2d7bb3fa66b55c1af283b51853abe7f62a2a5f49.zip cpython-2d7bb3fa66b55c1af283b51853abe7f62a2a5f49.tar.gz cpython-2d7bb3fa66b55c1af283b51853abe7f62a2a5f49.tar.bz2 |
further work on config saving
Diffstat (limited to 'Lib/idlelib/configHandler.py')
-rw-r--r-- | Lib/idlelib/configHandler.py | 65 |
1 files changed, 61 insertions, 4 deletions
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: """ |