Ignore:
Timestamp:
12/12/10 03:58:45 (9 years ago)
Author:
technomage
Message:

Patch by Technomage

  • Replaced the test_dialogue.py testsuite with the test_dialogueprocessor.py testsuite and added/updated unittest TestCases? for all public methods of the DialogueProcessor? class.
  • DialogueProcessor? was refactored to be much more liberal about raising exceptions instead of silently handling errors; the code documentation has been updated to describe this new behavior.
  • Refactored exception-handling code in the dialogueparsers.py gamemodel.py modules related to the dialogue engine to deal with the changes to the DialogueProcessor? class.
  • Added a new method to the Dialogue class, getRootSection, which returns the root DialogueSection? for the a particular dialogue; also added some error checking code for inputs to the constructor.
  • Updated the run_tests.py script to import config.py and attempt to read the FIFE Python module path from it, just like run.py.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/game/scripts/dialogueprocessor.py

    r684 r685  
    153153        Prepare the L{DialogueProcessor} to process the L{Dialogue} by pushing the 
    154154        starting L{DialogueSection} onto the L{dialogue_section_stack}. 
     155         
     156        @raise TypeError: Unable to determine the root L{DialogueSection} 
     157            defined by the L{Dialogue}. 
    155158        """ 
    156159        dialogue = self.dialogue 
    157160        try: 
    158             start_section_id = dialogue.start_section_id 
    159             self.dialogue_section_stack.append( 
    160                 dialogue.sections[start_section_id] 
    161             ) 
    162         except (AttributeError, KeyError): 
     161            root_dialogue_section = dialogue.getRootSection() 
     162        except (RuntimeError,) as error: 
     163            self._logger.error(str(error)) 
    163164            raise TypeError(('unable to determine start DialogueSection for ' 
    164165                             '{0}').format(dialogue)) 
    165166        else: 
     167            self.dialogue_section_stack.append(root_dialogue_section) 
    166168            self.in_dialogue = True 
    167169            self._logger.info('initiated dialogue {0}'.format(dialogue)) 
     
    177179        @returns: valid responses. 
    178180        @rtype: list of L{DialogueResponses<DialogueResponse>} 
    179         """ 
     181         
     182        @raise RuntimeError: Any preconditions are not met. 
     183         
     184        @precondition: dialogue has been initiated via L{initiateDialogue}. 
     185        """ 
     186        if (not self.in_dialogue): 
     187            raise RuntimeError('dialogue has not be initiated via ' 
     188                               'initiateDialogue yet') 
    180189        current_dialogue_section = self.getCurrentDialogueSection() 
    181190        self.runDialogueActions(current_dialogue_section) 
     
    191200        @returns: section of dialogue currently being processed. 
    192201        @rtype: L{DialogueSection} 
    193         """ 
     202         
     203        @raise RuntimeError: Any preconditions are not met. 
     204         
     205        @precondition: dialogue has been initiated via L{initiateDialogue} and 
     206            L{dialogue_section_stack} contains at least one L{DialogueSection}. 
     207        """ 
     208        if (not self.in_dialogue): 
     209            raise RuntimeError('getCurrentDialogueSection called but the ' 
     210                               'dialogue has not been initiated yet') 
    194211        try: 
    195212            current_dialogue_section = self.dialogue_section_stack[-1] 
    196213        except IndexError: 
    197             self._logger.error('getCurrentDialogueSection called by no ' 
     214            raise RuntimeError('getCurrentDialogueSection called but no ' 
    198215                               'DialogueSections are in the stack') 
    199             current_dialogue_section = None 
    200216         
    201217        return current_dialogue_section 
     
    214230            try: 
    215231                command(self.game_state) 
    216             except (Exception) as error: 
     232            except (Exception,) as error: 
    217233                self._logger.error('failed to execute DialogueAction {0}: {1}' 
    218                                  .format(command.keyword, error)) 
     234                                   .format(command.keyword, error)) 
    219235                # TODO Technomage 2010-11-18: Undo previous actions when an 
    220236                #     action fails to execute. 
     
    222238            else: 
    223239                self._logger.debug('ran {0} with arguments {1}' 
    224                                  .format(getattr(type(command), '__name__'), 
    225                                                  command.arguments)) 
     240                                   .format(getattr(type(command), '__name__'), 
     241                                           command.arguments)) 
    226242     
    227243    def getValidResponses(self, dialogue_section): 
     
    237253        @rtype: list of L{DialogueResponses<DialogueResponse>} 
    238254        """ 
    239         if (dialogue_section is None): 
    240             # die nicely when the dialogue_section doesn't exist 
    241             return 
    242255        valid_responses = [] 
    243256        for dialogue_response in dialogue_section.responses: 
     
    246259                condition_met = condition is None or \ 
    247260                                eval(condition, self.game_state) 
    248             except (Exception) as error: 
     261            except (Exception,) as error: 
    249262                self._logger.error( 
    250263                    ('evaluation of condition "{0}" for {1} failed with ' 
     
    271284        @param dialogue_response: response to reply with. 
    272285        @type dialogue_response: L{DialogueReponse} 
    273         """ 
     286         
     287        @raise RuntimeError: Any precondition is not met. 
     288         
     289        @precondition: L{initiateDialogue} must be called before this method 
     290            is used. 
     291        """ 
     292        if (not self.in_dialogue): 
     293            raise RuntimeError('reply cannot be called until the dialogue has ' 
     294                               'been initiated via initiateDialogue') 
    274295        self._logger.info('replied with {0}'.format(dialogue_response)) 
     296        # FIXME: Technomage 2010-12-11: What happens if runDialogueActions 
     297        #     raises an error? 
    275298        self.runDialogueActions(dialogue_response) 
    276299        next_section_id = dialogue_response.next_section_id 
    277300        if (next_section_id == 'back'): 
    278301            if (len(self.dialogue_section_stack) == 1): 
    279                 self._logger.error('attempted to run goto: back action but ' 
    280                                  'stack does not contain a previous ' 
    281                                  'DialogueSection') 
     302                raise RuntimeError('attempted to run goto: back action but ' 
     303                                   'stack does not contain a previous ' 
     304                                   'DialogueSection') 
    282305            else: 
    283306                try: 
    284307                    self.dialogue_section_stack.pop() 
    285                 except IndexError: 
    286                     self._logger.error('attempted to run goto: back action ' 
     308                except (IndexError,): 
     309                    raise RuntimeError('attempted to run goto: back action ' 
    287310                                       'but the stack was empty') 
    288311                else: 
     
    294317            self._logger.debug('ran goto: end action, ended dialogue') 
    295318        else: 
    296             # get a n 
    297319            try: 
    298320                next_dialogue_section = \ 
    299321                    self.dialogue.sections[next_section_id] 
    300322            except KeyError: 
    301                 self._logger.error( 
     323                raise RuntimeError( 
    302324                    ('"{0}" is not a recognized goto: action or ' 
    303325                     'DialogueSection identifier').format(next_section_id) 
Note: See TracChangeset for help on using the changeset viewer.