Ignore:
Timestamp:
03/09/11 02:54:24 (8 years ago)
Author:
aspidites
Message:

Patch by Aspidites:

  • Modified settings api slightly to prepare for other changes. Settings.filenames is replaced by Settings.paths as it will be more useful, IMHO.
  • Added copyright notice to settings module
  • Renamed DataDirectory? to SystemDataDirectory? and added UserDataDirectory? to [fife] section of default config file.
  • Implemented proper command line interface for settings.py

+ If multiple system names are given, multiple settings files

are generated, with the system name prepended to the filename

+ Both system and user data directories may be specified

+ It is possible to change the filename
+ not speficying anything will assume the system to be the one that

the script was run on and a filename of system.cfg

  • saves directory is now configurable (located in user's data directory by default
  • removed hard coded paths to open and save file browsers
  • removed default argument for FileBrowser?'s gui xml since it didnt' exist
  • screenshot directory now resides in user data directory
  • updated help screen
  • screenshots directory is now created only when its needed
  • when saving of games is implemented, the same should be done for the saves directory
File:
1 edited

Legend:

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

    r806 r809  
    11#!/usr/bin/env python2 
     2 
     3#  Copyright (C) 2011  Edwin Marshall <emarshall85@gmail.com> 
    24 
    35#   This file is part of PARPG. 
     
    163165            user_path = system_path 
    164166 
    165         self.filenames = {'system': os.path.join(system_path,  
    166                                                  'system{0}'.format(self.suffix)), 
    167                           'user': os.path.join(user_path, 
    168                                                'user{0}'.format(self.suffix))} 
    169  
     167        self.paths = {'system': system_path, 'user': user_path} 
    170168        self.read() 
    171169 
     
    199197         
    200198        if filenames is None: 
    201             filenames = [self.filenames['system'], self.filenames['user']] 
     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))] 
     203        elif hasattr(filenames, 'split'): 
     204            filenames = [filenames] 
    202205 
    203206        for filename in filenames: 
     
    228231        """ 
    229232        if filename is None: 
    230             filename = self.filenames['user'] 
     233            filename = os.path.join(self.paths['user'],  
     234                                    'user{0}'.format(self.suffix)) 
    231235 
    232236        for section in self.sections: 
     
    258262                    if new_option: 
    259263                        while self.settings_file[start_of_section].startswith('#'): 
    260                             print self.settings_file[start_of_section] 
    261264                            start_of_section += 1 
    262265 
     
    272275        sections = self.__dict__.keys() 
    273276        sections.pop(sections.index('settings_file')) 
    274         sections.pop(sections.index('filenames')) 
     277        sections.pop(sections.index('paths')) 
    275278        sections.pop(sections.index('suffix')) 
    276279         
    277280        return sections 
    278281 
    279 #TODO: Change saves and screenshots directory to somewhere in the user's path, not the system's 
    280282DEFAULT_SETTINGS = """\ 
     283[fife] 
    281284# Options marked with ? are untested/unknown 
    282 [fife] 
    283  
    284 # directory where all other data directories reside 
     285 
    285286 
    286287# Game window's title (string) DO NOT EDIT! 
     
    314315IconsDirectory = icons 
    315316 
    316 # ? ([R, G, B]?) 
     317# ? ([R, G, B]) 
    317318ColorKey = [250, 0, 250] 
    318319 
     
    355356[parpg] 
    356357 
    357 # Subdirectory to load maps from (path) 
     358# System subdirectory to load maps from (path) 
    358359MapsDirectory = maps 
    359360 
     
    367368AllAgentsFile = all_agents.yaml 
    368369 
    369 # Subdirectory to load objects from (path) 
     370# System subdirectory to load objects from (path) 
    370371ObjectsDirectory = objects 
    371372 
     
    373374ObjectDatabaseFile = object_database.yaml 
    374375 
    375 # Subdirectory to load dialogues from (path) 
     376# System subdirectory to load dialogues from (path) 
    376377DialoguesDirectory = dialogue 
    377378 
    378 # Subdirectory to load quests from (path) 
     379# System subdirectory to load quests from (path) 
    379380QuestsDirectory = quests 
    380381 
    381 # Subdirectory to save screenshots to 
     382# User subdirectory to save screenshots to 
    382383ScreenshotsDirectory = screenshots 
    383384 
    384 # Subdirectory where gui files are loaded from (path) 
     385# User subdirectory to save games to 
     386SavesDirectory = saves 
     387 
     388# System subdirectory where gui files are loaded from (path) 
    385389GuiDirectory = gui 
    386390 
    387 # Subdirectory where cursors are loaded from (path) 
     391# System subdirectory where cursors are loaded from (path) 
    388392CursorDirectory = cursors 
    389393 
     
    407411""" 
    408412 
    409 def write_defaults(system=None, data_dir=""): 
     413def write_defaults(system=None, sys_dir="", user_dir="", filename='system.cfg'): 
    410414    #TODO: write defaults for Mac OS X 
    411415    if system is None: 
    412416        system = platform.system().lower() 
    413417 
    414     if len(data_dir) == 0: 
     418    # automatically set sys_dir since it wasn't defined 
     419    if len(sys_dir) == 0: 
    415420        if system == 'local': 
    416             data_dir = os.path.join('.') 
     421            sys_dir = os.path.join('.') 
    417422        elif system == 'linux': 
    418             data_dir = os.path.join(os.environ['XDG_DATA_DIRS'].split(':')[0], 
    419                                     'parpg') 
     423            sys_dir = os.path.join(os.environ['XDG_DATA_DIRS'].split(':')[0], 
     424                                   'parpg') 
    420425        elif system == 'windows': 
    421             data_dir = os.path.join(os.environ['PROGRAMFILES'], 
    422                                     'PARPG') 
    423  
     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 
    424438    default_settings = DEFAULT_SETTINGS.split('\n') 
    425     default_settings.insert(4, 'DataDirectory = {0}'.format(data_dir)) 
    426  
    427     with open('system.cfg', 'w') as f: 
     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: 
    428448        for line in default_settings: 
    429449            f.write(line + '\n') 
    430450 
    431451if __name__ == '__main__': 
    432     if len(sys.argv) > 1: 
    433         write_defaults(*sys.argv[1:]) 
    434     else: 
    435         write_defaults() 
     452    from optparse import OptionParser 
     453 
     454    usage = "usage: %prog [options] system[, system, ...]" 
     455    parser = OptionParser(usage=usage) 
     456 
     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.'))  
     463    parser.add_option('-f', '--filename', default='system.cfg', 
     464                      help='Filename of output configuration file') 
     465 
     466    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] 
Note: See TracChangeset for help on using the changeset viewer.