Show
Ignore:
Timestamp:
12/25/06 19:16:08 (5 years ago)
Author:
mg
Message:

Write keyboard controls to a config file.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/pyspacewar/ui.py

    r226 r227  
    1313import random 
    1414import itertools 
     15import ConfigParser 
    1516 
    1617import pygame 
     
    17501751        assert len(self.controls) == len(self.rev_controls) 
    17511752 
     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 
    17521782    def init(self): 
    17531783        """Initialize the user interface."""