| | 1753 | def load_settings(self, filename=None): |
| | 1754 | """Load settings from a configuration file.""" |
| | 1755 | if not filename: |
| | 1756 | filename = os.path.expanduser('~/.pyspacewarrc') |
| | 1757 | config = self.get_config_parser() |
| | 1758 | config.read([filename]) |
| | 1759 | for action in self.controls: |
| | 1760 | key = config.get('controls', action) |
| | 1761 | try: |
| | 1762 | key = int(key) |
| | 1763 | except ValueError: |
| | 1764 | key = None |
| | 1765 | self.set_control(action, key) |
| | 1766 | |
| | 1767 | def save_settings(self, filename=None): |
| | 1768 | """Save settings to a configuration file.""" |
| | 1769 | if not filename: |
| | 1770 | filename = os.path.expanduser('~/.pyspacewarrc') |
| | 1771 | config = self.get_config_parser() |
| | 1772 | config.write(file(filename, 'w')) |
| | 1773 | |
| | 1774 | def get_config_parser(self): |
| | 1775 | """Create a ConfigParser initialized with current settings.""" |
| | 1776 | config = ConfigParser.RawConfigParser() |
| | 1777 | config.add_section('controls') |
| | 1778 | for action, key in self.controls.items(): |
| | 1779 | config.set('controls', action, key) |
| | 1780 | return config |
| | 1781 | |