Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add class ConfigChanges (changes_class_v4.py on issue).
  • Loading branch information
terryjreedy committed Jul 6, 2017
commit 32ae95966f5b7c466b2a34f5eb82673f58155ad5
74 changes: 74 additions & 0 deletions Lib/idlelib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,80 @@ def _warn(msg, *key):
_warned.add(key)


class ConfigChanges:
"""Manage a user's proposed configuration option changes.

Names used across multiple methods:
page -- one of the 4 top-level dicts representing a
.idlerc/config-x.cfg file.
config_type -- name of a page.
section -- a section within a page/file.
item -- name of a value within a section.
value -- value for the item.

Methods
add_item:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer to use add_item in above changes? Current code is using additem.

@terryjreedy terryjreedy Jul 7, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reviewing configparser parameters, changed to add_option. Will have to change all in test and configdialog and check its tests.

save_all: Save all the changes to the config file.

@mlouielu mlouielu Jul 7, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add set_value document (will dev use this method alone?)


reset: Clear all changes by clearing each page.
set_user_value: Set value *in idleConf* for page, section, item.
"""
def __init__(self):
"Create a page for each configuration file"
self.pages = {}
for config_type in ('main', 'highlight', 'keys', 'extensions'):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question on the tuple ('main', 'highlight', 'keys', 'extensions')

This is in the IdleConf.__init__:
self.config_types = ('main', 'extensions', 'highlight', 'keys')

Any benefit to making that a module constant?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it as idleConf attribute and used the attribute here to eliminate the duplication. Good catch.

self[config_type] = {}
self.pages.add(self[config_type])

def add_item(self, config_type, section, item, value):
"Add item/value pair for config_type and section."
page = self[config_type]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know if we needed to add a check for config_type to make sure it's valid before accessing it.

@terryjreedy terryjreedy Jul 7, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would add_option do if it did? It expects new sections. New config-types are a bug. A test of the user, config dialog, should fail. General principle: only check first or catch after if one has an alternative action in mind that is better than letting an exception bubble up.

value = str(value) # Make sure we use a string.
if section not in page:
page[section] = {}
page[section][item] = value

def save_all(self):
"Save configuration changes to the user config file."

idleConf.userCfg['main'].Save()
for config_type in changes:
cfg_type_changed = False
for section in changes[config_type]:
if section == 'HelpFiles':
# This section gets completely replaced.
idleConf.userCfg['main'].remove_section('HelpFiles')
cfg_type_changed = True
for item in changes[config_type][section]:
value = self.changed_items[config_type][section][item]
if self.set_user_value(config_type, section, item, value):
cfg_type_changed = True
if cfg_type_changed:
idleConf.userCfg[config_type].Save()
for config_type in ['keys', 'highlight']:
# Save these even if unchanged!
idleConf.userCfg[config_type].Save()
self.clear() # Clear the changed items dict.
self.save_all_changed_extensions() # Uses a different mechanism.

def delete_section(self, config_type, section):
"""Delete a section from this page.

Used to delete custom themes and keysets.
"""
if section in self[config_type]:
del self[config_type][section]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove one blank line

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I appreciate the review. Polishing a patch is really hard.

def clear(self):
"""Clear all 4 pages.

Called in save_all after saving to idleConf.
XXX Mark window *title* when there are changes; unmark here.
"""
for page in self.config_types:
page.clear()


# TODO Revise test output, write expanded unittest
def _dump(): # htest # (not really, but ignore in coverage)
from zlib import crc32
Expand Down