Ignore:
Timestamp:
03/09/11 05:23:21 (8 years ago)
Author:
aspidites
Message:

Patch by Aspidites

  • Removed SystemDataDirectory? and UserDataDirectory?
    • it is the the settings module's job to keep track of this sort of thing, not fife or PARPG's
    • consequently, setting.py's command line interface became simple
    • this will also prevent those "I generated a settings file but PARPG doesnt run" complaints
    • I think I'm going to have some helper methods to generate platform-specific paths at run-time
  • User data directory is now properly created
  • added platform_paths(system) which returns the platform-specific paths for the given system
  • if no system is given, it gives the paths for the system that the script was run on
  • changed logic of settings.py so that it is not required to pass a path.
  • not passing a path invokes paltform_paths
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/active/character_customization/game/parpg/settings.py

    r809 r810  
    8080 
    8181            @param option: name of the option to assign a value to. 
    82             @type option: string 
    83             @param value: value to be assigned to the option. 
     82            @type option: string @param value: value to be assigned to the option. 
    8483            @type value: int, float, string, boolean, or list 
    8584        """ 
     
    130129        and the options defined within those sections. 
    131130    """ 
    132     def __init__(self, system_path, user_path=None, suffix='.cfg'): 
    133         """ initializes a new settings object. 
     131    def __init__(self, system_path=None, user_path=None, suffix='.cfg'): 
     132        """ initializes a new settings object. If no paths are given, they are 
     133            guessed based on whatever platform the script was run on. 
    134134 
    135135            Examples: 
     
    142142 
    143143                settings = Settings('.') 
     144 
     145                settigns = Settings() 
    144146 
    145147            @param system_path: Path to the system settings file. 
     
    155157            @type suffix: string 
    156158        """ 
    157  
    158159        if not suffix.startswith('.'): 
    159160            suffix = '.' + suffix 
     
    162163        self.settings_file = '' 
    163164 
    164         if user_path is None: 
    165             user_path = system_path 
    166  
    167         self.paths = {'system': system_path, 'user': user_path} 
     165        if system_path is None and user_path is None: 
     166            # use platform-specific values as paths 
     167            self.paths = {} 
     168            self.paths['system'], self.paths['user'] = self.platform_paths() 
     169        else: 
     170            self.paths = {'system': system_path, 'user': user_path} 
     171 
    168172        self.read() 
     173 
    169174 
    170175    def __getattr__(self, name): 
     
    186191        return getattr(self, name) 
    187192 
     193    def platform_paths(self, system=None): 
     194        if system is None: 
     195            system = platform.system() 
     196         
     197        if system.lower() == 'linux': 
     198            return (os.path.join(os.environ['XDG_DATA_DIRS'].split(':')[0],  
     199                                 'parpg'), 
     200                    os.path.join(os.environ['XDG_CONFIG_HOME'], 'parpg')) 
     201        elif system.lower() == 'windows': 
     202            return (os.path.join(os.environ['PROGRAMFILES'], 'PARPG'), 
     203                    os.path.join(os.environ['USERDATA'], 'PARPG')) 
     204        else: 
     205            return None 
     206 
     207 
    188208    def read(self, filenames=None): 
    189209        """ Reads a settings file and populates the settings object  
     
    197217         
    198218        if filenames is None: 
    199             filenames = [os.path.join(self.paths['system'],  
    200                          'system{0}'.format(self.suffix)), 
    201                          os.path.join(self.paths['user'], 
    202                          'user{0}'.format(self.suffix))] 
     219            filenames = [os.path.join(self.paths[key],  
     220                                      '{0}{1}'.format(key, self.suffix)) 
     221                         for key in self.paths.keys()  
     222                         if self.paths[key] is not None] 
    203223        elif hasattr(filenames, 'split'): 
    204224            filenames = [filenames] 
     
    280300        return sections 
    281301 
     302    @property 
     303    def sys_path(self): 
     304        return self.paths['system'] 
     305 
     306    @property 
     307    def user_path(self): 
     308        return self.paths['user'] 
     309 
    282310DEFAULT_SETTINGS = """\ 
    283311[fife] 
    284312# Options marked with ? are untested/unknown 
    285313 
    286  
    287314# Game window's title (string) DO NOT EDIT! 
    288315WindowTitle = PARPG Techdemo 2 
     
    411438""" 
    412439 
    413 def write_defaults(system=None, sys_dir="", user_dir="", filename='system.cfg'): 
    414     #TODO: write defaults for Mac OS X 
    415     if system is None: 
    416         system = platform.system().lower() 
    417  
    418     # automatically set sys_dir since it wasn't defined 
    419     if len(sys_dir) == 0: 
    420         if system == 'local': 
    421             sys_dir = os.path.join('.') 
    422         elif system == 'linux': 
    423             sys_dir = os.path.join(os.environ['XDG_DATA_DIRS'].split(':')[0], 
    424                                    'parpg') 
    425         elif system == 'windows': 
    426             sys_dir = os.path.join(os.environ['PROGRAMFILES'], 'PARPG') 
    427  
    428     # automatically set user_dir since it wasn't defined 
    429     if len(user_dir) == 0: 
    430         if system == 'local': 
    431             user_dir = os.path.join('.') 
    432         elif system == 'linux': 
    433             user_dir = os.path.join(os.environ['XDG_CONFIG_HOME'], 'parpg') 
    434         elif system == 'windows': 
    435             user_dir = os.path.join(os.environ['APPDATA'], 'PARPG') 
    436  
    437     # insert the defined system and user data directories into the template 
    438     default_settings = DEFAULT_SETTINGS.split('\n') 
    439     default_settings.insert(3, ('# Directory where files necessary to run ' 
    440                                 'PARPG are located\n' 
    441                                 'SystemDataDirectory = {0}\n\n' 
    442                                 '# Directory where saved games and ' 
    443                                 'screenshots are stored\n' 
    444                                 'UserDataDirectory = {1}').format(sys_dir, 
    445                                                                   user_dir)) 
    446  
    447     with open(filename, 'w') as f: 
    448         for line in default_settings: 
    449             f.write(line + '\n') 
    450  
    451440if __name__ == '__main__': 
    452441    from optparse import OptionParser 
     
    455444    parser = OptionParser(usage=usage) 
    456445 
    457     parser.add_option('-s', '--sys-dir', default='', 
    458                       help=('System Data Directory where data files ' 
    459                             'necessary to make the game run are stored')) 
    460     parser.add_option('-u', '--user-dir', default='', 
    461                       help=('User Data Directory where saved games and ' 
    462                             'screenshots are stored.'))  
    463446    parser.add_option('-f', '--filename', default='system.cfg', 
    464447                      help='Filename of output configuration file') 
    465448 
    466449    opts, args = parser.parse_args() 
    467  
    468     if len(args) == 0: 
    469         write_defaults(None, opts.sys_dir, opts.user_dir) 
    470     else:  
    471         valid_systems = ['local', 'linux', 'windows'] 
    472         # remove invalid systems from args 
    473         args = [arg for arg in args if arg in valid_systems] 
    474  
    475         if len(args) == 0: 
    476             parser.show_help() 
    477         elif len(args) == 1: 
    478             write_defaults(arg, opts.sys_dir, opts.user_dir, opts.filename) 
    479         else: 
    480             [write_defaults(arg, opts.sys_dir, opts.user_dir, 
    481                             '{0}_{1}'.format(arg, opts.filename))  
    482              for arg in args] 
     450     
     451    with open(opts.filename, 'w') as f: 
     452        for line in DEFAULT_SETTINGS: 
     453            f.write(line) 
Note: See TracChangeset for help on using the changeset viewer.