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
Location:
branches/active/character_customization/game/parpg
Files:
8 edited

Legend:

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

    r795 r809  
    111111        """Initialise the instance. 
    112112           @return: None""" 
    113         self._setting = setting  
     113        self._setting = setting.fife 
    114114        self.engine = fife.Engine() 
    115         fonts_dir = os.path.join(self._setting.fife.DataDirectory, 
    116                                  self._setting.fife.FontsDirectory) 
     115        fonts_dir = os.path.join(self._setting.SystemDataDirectory, 
     116                                 self._setting.FontsDirectory) 
    117117        self.fonts_directory = fonts_dir 
    118118        self.loadSettings() 
     
    164164        """ 
    165165 
    166         data_dir = self._setting.fife.DataDirectory 
    167         fife_options = self._setting.fife 
    168  
    169166        engineSetting = self.engine.getSettings() 
    170         engineSetting.setDefaultFontGlyphs(self._setting.fife.FontGlyphs) 
     167        engineSetting.setDefaultFontGlyphs(self._setting.FontGlyphs) 
    171168        engineSetting.setDefaultFontPath(os.path.join(self.fonts_directory, 
    172                                                        fife_options.Font)) 
    173         engineSetting.setDefaultFontSize(self._setting.fife.DefaultFontSize) 
    174         engineSetting.setBitsPerPixel(self._setting.fife.BitsPerPixel) 
    175         engineSetting.setInitialVolume(self._setting.fife.InitialVolume) 
    176         engineSetting.setSDLRemoveFakeAlpha(self._setting.fife.SDLRemoveFakeAlpha) 
    177         engineSetting.setScreenWidth(self._setting.fife.ScreenWidth) 
    178         engineSetting.setScreenHeight(self._setting.fife.ScreenHeight) 
    179         engineSetting.setRenderBackend(self._setting.fife.RenderBackend) 
    180         engineSetting.setFullScreen(self._setting.fife.FullScreen) 
    181         engineSetting.setVideoDriver(self._setting.fife.VideoDriver) 
    182         engineSetting.setLightingModel(self._setting.fife.Lighting) 
    183         engineSetting.setColorKeyEnabled(self._setting.fife.ColorKeyEnabled) 
    184  
    185         key = [int(digit) for digit in self._setting.fife.ColorKey] 
    186         engineSetting.setColorKey(*key) 
    187  
    188         engineSetting.setWindowTitle(self._setting.fife.WindowTitle) 
    189         engineSetting.setWindowIcon(os.path.join(data_dir,  
    190                                                  fife_options.IconsDirectory, 
    191                                                  fife_options.WindowIcon)) 
     169                                                       self._setting.Font)) 
     170        engineSetting.setDefaultFontSize(self._setting.DefaultFontSize) 
     171        engineSetting.setBitsPerPixel(self._setting.BitsPerPixel) 
     172        engineSetting.setInitialVolume(self._setting.InitialVolume) 
     173        engineSetting.setSDLRemoveFakeAlpha(self._setting.SDLRemoveFakeAlpha) 
     174        engineSetting.setScreenWidth(self._setting.ScreenWidth) 
     175        engineSetting.setScreenHeight(self._setting.ScreenHeight) 
     176        engineSetting.setRenderBackend(self._setting.RenderBackend) 
     177        engineSetting.setFullScreen(self._setting.FullScreen) 
     178        engineSetting.setVideoDriver(self._setting.VideoDriver) 
     179        engineSetting.setLightingModel(self._setting.Lighting) 
     180        engineSetting.setColorKeyEnabled(self._setting.ColorKeyEnabled) 
     181 
     182        engineSetting.setColorKey(*[int(digit)  
     183                                    for digit in self._setting.ColorKey]) 
     184 
     185        engineSetting.setWindowTitle(self._setting.WindowTitle) 
     186        engineSetting.setWindowIcon(os.path.join(self._setting.SystemDataDirectory,  
     187                                                 self._setting.IconsDirectory, 
     188                                                 self._setting.WindowIcon)) 
    192189 
    193190    def createListener(self): 
  • branches/active/character_customization/game/parpg/controllerbase.py

    r794 r809  
    8484        """Reset cursor to default image. 
    8585           @return: None""" 
    86         image = os.path.join(self.model.settings.fife.DataDirectory, 
     86        image = os.path.join(self.model.settings.fife.SystemDataDirectory, 
    8787                             self.model.settings.parpg.GuiDirectory, 
    8888                             self.model.settings.parpg.CursorDirectory, 
  • branches/active/character_customization/game/parpg/gamemodel.py

    r797 r809  
    5858        @return: None""" 
    5959        self.settings = settings 
    60         data_dir = self.settings.fife.DataDirectory 
     60        data_dir = self.settings.fife.SystemDataDirectory 
    6161 
    6262        self.map_change = False 
  • branches/active/character_customization/game/parpg/gamescenecontroller.py

    r797 r809  
    133133            self.model.active_map.toggleRenderer('CoordinateRenderer') 
    134134        if(key_val == key.F7): 
    135             # F7 saves a screenshot to fife/clients/parpg/screenshots 
    136              
    137             screenshotdir = model.settings.parpg.ScreenshotsDirectory 
    138             screenshot_file = os.path.join(screenshotdir,  
     135            # F7 saves a screenshot to screenshots directory 
     136 
     137            fife_ = self.model.settings.fife 
     138            parpg_ = self.model.settings.parpg 
     139            screenshot_directory = os.path.join(fife_.UserDataDirectory, 
     140                                           parpg_.ScreenshotsDirectory) 
     141            # try to create the screenshots directory 
     142            try: 
     143                os.mkdir(screenshot_directory) 
     144            #TODO: distinguish between already existing permissions error 
     145            except OSError: 
     146                logger.warning("screenshot directory wasn't created.") 
     147 
     148            screenshot_file = os.path.join(screenshot_directory, 
    139149                                           'screen-{0}.png'.format( 
    140150                                           datetime.now().strftime( 
     
    248258                direction[0] += 1 
    249259                direction[1] -= 1 
    250                 image = os.path.join(settings.fife.DataDirectory, 
     260                image = os.path.join(settings.fife.SystemDataDirectory, 
    251261                                     settings.parpg.GuiDirectory, 
    252262                                     settings.parpg.CursorDirectory, 
     
    257267                direction[0] += 1 
    258268                direction[1] += 1 
    259                 image = os.path.join(settings.fife.DataDirectory, 
     269                image = os.path.join(settings.fife.SystemDataDirectory, 
    260270                                     settings.parpg.GuiDirectory, 
    261271                                     settings.parpg.CursorDirectory, 
     
    266276                direction[0] -= 1 
    267277                direction[1] += 1 
    268                 image = os.path.join(settings.fife.DataDirectory, 
     278                image = os.path.join(settings.fife.SystemDataDirectory, 
    269279                                     settings.parpg.GuiDirectory, 
    270280                                     settings.parpg.CursorDirectory, 
     
    275285                direction[0] -= 1 
    276286                direction[1] -= 1 
    277                 image = os.path.join(settings.fife.DataDirectory, 
     287                image = os.path.join(settings.fife.SystemDataDirectory, 
    278288                                     settings.parpg.GuiDirectory, 
    279289                                     settings.parpg.CursorDirectory, 
  • branches/active/character_customization/game/parpg/gui/filebrowser.py

    r797 r809  
    1616 
    1717import sys 
     18import os 
    1819import logging 
    1920 
     
    3233       doesn't exist yet. The select_dir option allows directories to be 
    3334       selected as well as files.""" 
    34     def __init__(self, engine, file_selected, close_callback = None, save_file=False, \ 
    35                  select_dir=False, extensions=('.dat',), \ 
    36                  gui_xml_path="gui/filebrowser.xml"): 
     35    def __init__(self, engine, settings, file_selected, gui_xml_path, 
     36                 close_callback=None, save_file=False, select_dir=False,  
     37                 extensions=('.dat',)): 
    3738        self.engine = engine 
     39        self.settings = settings 
     40        print self.settings.parpg.SavesDirectory 
    3841        self.file_selected = file_selected 
    3942 
     
    4245        self.select_dir = select_dir 
    4346        self.close_callback = close_callback 
     47        self.gui_xml_path = gui_xml_path  
    4448         
    45         self.gui_xml_path = gui_xml_path 
    46  
    4749        self.extensions = extensions 
    48         self.path = './saves/' 
     50        self.path = os.path.join(self.settings.fife.UserDataDirectory, 
     51                                 self.settings.parpg.SavesDirectory) 
    4952        self.dir_list = [] 
    5053        self.file_list = [] 
  • branches/active/character_customization/game/parpg/gui/hud.py

    r801 r809  
    1313#   You should have received a copy of the GNU General Public License 
    1414#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>. 
     15 
     16import os 
     17import logging 
    1518 
    1619from fife.extensions import pychan 
     
    2629from actionsbox import ActionsBox 
    2730 
    28 import logging 
    29  
    3031logger = logging.getLogger('hud') 
    31  
    3232class Hud(object): 
    3333    """Main Hud class""" 
     
    255255        k_text += "[br] A : Add a test action to the actions display" 
    256256        k_text += "[br] I : Toggle the inventory screen" 
    257         k_text += "[br] F5 : Take a screenshot" 
    258         k_text += "[br]      (saves to <parpg>/screenshots/)" 
     257        k_text += "[br] F7 : Take a screenshot" 
     258        k_text += "[br]      (Saves to screenshots directory)" 
    259259        k_text += "[br] F10 : Toggle console" 
    260260        k_text += "[br] PAUSE : (Un)Pause the game" 
     
    274274            @return: None""" 
    275275        self.stopActions() 
     276        xml_path = os.path.join(self.settings.fife.SystemDataDirectory, 
     277                                    self.settings.parpg.GuiDirectory, 
     278                                    'savebrowser.xml') 
    276279        save_browser = FileBrowser(self.engine, 
     280                                   self.settings, 
    277281                                   self.save_game_callback, 
     282                                   xml_path, 
    278283                                   self.loadsave_close, 
    279284                                   save_file=True, 
    280                                    gui_xml_path="gui/savebrowser.xml", 
    281                                    extensions = ('.dat')) 
     285                                   extensions=('.dat')) 
    282286        save_browser.showBrowser() 
    283287        self.controller.pause(True) 
     
    315319            @return: None""" 
    316320        self.stopActions() 
     321        xml_path = os.path.join(self.settings.fife.SystemDataDirectory, 
     322                                    self.settings.parpg.GuiDirectory, 
     323                                    'loadbrowser.xml') 
    317324        load_browser = FileBrowser(self.engine, 
     325                                   self.settings, 
    318326                                   self.load_game_callback, 
     327                                   xml_path, 
    319328                                   close_callback = self.loadsave_close, 
    320329                                   save_file=False, 
    321                                    gui_xml_path='gui/loadbrowser.xml', 
    322330                                   extensions=('.dat')) 
    323331        load_browser.showBrowser() 
     
    437445        self.main_menu.mapEvents(menu_events) 
    438446 
    439     def setOption(self, name, value): 
    440         """Set an option within the xml. 
    441            @type name: string 
    442            @param name: The name of the option within the xml 
    443            @type value: any 
    444            @param value: The value that the option 'name' should be set to 
    445            @return: None""" 
    446         element = self.settings.root_element.find(name) 
    447         if(element != None): 
    448             if(value != element.text): 
    449                 element.text = str(value) 
    450         else: 
    451             logger.warning("setting, {0} does not exist!".format(name)) 
    452      
    453447    def readyAction(self, ready_button): 
    454448        """ Called when the user selects a ready button from the HUD """ 
  • branches/active/character_customization/game/parpg/mainmenuview.py

    r801 r809  
    1313#   You should have received a copy of the GNU General Public License 
    1414#   along with PARPG.  If not, see <http://www.gnu.org/licenses/ 
     15 
     16import os 
    1517 
    1618from fife.extensions import pychan 
     
    8082        """ Called when the user wants to load a game. 
    8183            @return: None""" 
     84        xml_path = os.path.join(self.model.settings.fife.SystemDataDirectory, 
     85                                self.model.settings.parpg.GuiDirectory, 
     86                                'loadbrowser.xml') 
    8287        load_browser = FileBrowser(self.engine, 
     88                                   self.model.settings, 
    8389                                   self.load_game_callback, 
     90                                   gui_xml_path=xml_path, 
    8491                                   save_file=False, 
    85                                    gui_xml_path='gui/loadbrowser.xml', 
    8692                                   extensions=('.dat')) 
    8793        load_browser.showBrowser() 
  • 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.