Section = dict "Map item names to item values." # If we decide to add section methods, # class Section(dict): pass class Page(dict): """Map section names to sections. New methods: additem: apply: delete_section?: These are properly Page methods, not Section methods, because the section parameters are not Sections, just possible section names. """ # Configparser somewhat misleadingly calls this a 'ConfigParser' # but a page may or may not use its parser method to load sections. def __init__(self, config_type): self.config_type = config_type def additem(self, section, item, value): """Add item/value pair to a section of a page. Each ConfigDialog tab uses this to set values for its page. """ if section not in self: self[section] = Section() self[section][item] = str(value) def apply(self, section, new_items): # inline code for keys and highlight """Apply changes in section to dict new_items. One config entry for keysets and themes is the name of the keyset/theme and it contains all the settings for that entry. This applies the changes to a loaded keyset/theme to create a new list of item/value pairs. The output is used as input to save_new_config. """ try: new_items.update(self[section]) except KeyError: pass def changed(self): for section in self: if section == 'HelpFiles': #this section gets completely replaced idleConf.userCfg[self.name].remove_section('HelpFiles') return True for item in self[section]: value = self[section][item] if self.set_user_value(section, item, value): return True return False def save(self): idleConf.userCfg[self.name].Save() def set_user_value(self, section, item, value): # old """Test if new value is same as the default. If value is in default, return true if it is removed from the custom config file. If value is not in default, return true if it is added or saved to user config. """ if idleConf.defaultCfg[self.name].has_option(section, item): if idleConf.defaultCfg[self.name].Get(section, item) == value: #the setting equals a default setting, remove it from user cfg return idleConf.userCfg[self.name].RemoveOption(section, item) #if we got here set the option return idleConf.userCfg[self.name].SetOption(section, item, value) def delete_section(self, section): """Delete a section from this page. Used to delete custom themes and keysets. """ if section in self: del self[section] # So trivial we could leave inline, where 'self' will be page reference. class Changes: """Manage a user's proposed configuration option changes. One instance: pages manage changes to the corresponding user files. All changes are either cancelled or applied in a single transaction. Arguments used across multiple methods: These share the definition used in config.py. page (type, config_type) section item value Methods used by ConfigDialog: save_all: Save all the changes to the config file. save_new_config: Apply all the changes for key Methods used only for testing: Methods used internally: reset: Clear all changes by clearing each page. set_user_value: Set value *in idleConf* for page, section, item. TODO: - Evaluate extensions usage of this class. """ def __init__(self): "Create a page for each configuration file" self.main = Page('main') self.themes = Page('highlights') self.keys = Page('keys') self.extensions = Page('extensions') self.pages = (self.main, self.themes, self.keys, self.extensions) def clear(self): # was reset_changed_items """Clear all 4 pages. Called in save_all(_changed_configs) after saving to idleConf. XXX Expose to user as [Clear changes] XXX Mark window *title* when there are changes; unmark here. """ for page in self.pages: page.clear() def save_all(self): # renamed from save_all_changed_configs """Save configuration changes to the user config file. XXX - what to do with save_all_changed_extensions """ self.main.save() for page in self.pages: if page.changed() or page in [self.keys, self.themes]: page.save() self.clear() # self.save_all_changed_extensions() # uses a different mechanism def save_new_config(self, config_type, section, items_): # renamed from save_new_key_set and save_new_theme """Update IdleConf.userCfg values for all sections within a config_type. This is used for keysets and themes since those have many items that represent what the keyset or theme is. """ pass