Ignore:
Timestamp:
11/05/09 19:27:51 (10 years ago)
Author:
eliedebrauwer
Message:

Ticket #147. Patch by eliedebrauwer: Added more verbosity in error reporting, updated unittest to be more verbose as well, added standalone validator utility. fixes[s:trac, t:147]

File:
1 edited

Legend:

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

    r369 r370  
    2121 
    2222class DialogueFormatException(Exception): 
    23     """ Exception thrown when the DialogueValidator has encountered an error. """ 
     23    """ Exception thrown when the DialogueValidator has encountered an  
     24    error. """ 
    2425    pass 
    2526 
    2627class DialogueValidator(object): 
    2728 
    28     def validateDialogue(self, file_name, topdir="."): 
    29         """ Checks whether a yaml file is containing a valid dialogue  
    30         @type file_name: string  
    31         @param file_name: File name of the yaml file to validate 
     29    def validateDialogue(self, tree, topdir): 
     30        """ Checks whether a given tree is containing a valid dialogue. 
     31        @type tree 
     32        @param tree 
    3233        @type topdir: string 
    3334        @param topdir: Top directory where to start searching for files. 
    34         @raises: DialogueFormatException on error 
    35         @return: True on success""" 
    36  
    37         self.tree = yaml.load(file(file_name)) 
     35        @raises: dialogueFormatException on error 
     36        @return: true on success""" 
     37     
     38        self.tree = tree  
    3839        self.topdir = topdir 
    3940         
     
    5152        if not "SECTIONS" in self.tree or not isinstance(self.tree["SECTIONS"],\ 
    5253                                                         types.DictionaryType): 
    53             raise DialogueFormatException("Node: SECTIONS not found or invalid type") 
     54            raise DialogueFormatException("Node: SECTIONS not found or invalid") 
    5455 
    5556        # Start node should be valid  
     
    6364             
    6465        return True  
     66     
     67    def validateDialogueFromFile(self, file_name, topdir="."): 
     68        """ Checks whether a yaml file is containing a valid dialogue  
     69        @type file_name: string  
     70        @param file_name: File name of the yaml file to validate 
     71        @type topdir: string 
     72        @param topdir: Top directory where to start searching for files. 
     73        @raises: DialogueFormatException on error 
     74        @return: True on success""" 
     75 
     76        tree = yaml.load(file(file_name)) 
     77        return self.validateDialogue(tree, topdir) 
    6578 
    6679    def __validateAvatar(self): 
    6780        """ Check that the avatar is an existing file.  
    6881        @raises: DialogueFormatException on error. """ 
    69          
    7082        fname = os.path.join(self.topdir,self.tree["AVATAR"]) 
    7183        if not os.path.isfile(fname): 
    72             raise DialogueFormatException("File containing avatar could not " +\ 
    73                                           "be found") 
     84            raise DialogueFormatException("Avatar file could not " +\ 
     85                                          "be found: " + fname) 
    7486 
    7587         
     
    96108                            "Section: " + section_name + " has an invalid " +\ 
    97109                            action + " node") 
    98                     self.__validateResponses(entry["responses"]) 
     110                    self.__validateResponses(entry["responses"], section_name) 
    99111                elif action == "meet": 
    100112                    if not isinstance(entry[action], types.StringType): 
     
    103115                            action + " node") 
    104116                    #TODO: verify person  
    105                 elif action in ("complete_quest", "start_quest", "delete_quest"): 
     117                elif action in ("complete_quest", "start_quest", \ 
     118                                "delete_quest"): 
    106119                    if not isinstance(entry[action], types.StringType): 
    107120                        raise DialogueFormatException(\ 
     
    115128                            action + " node") 
    116129                    #TODO: verify object 
    117                 elif action in ("increase_value", "decrease_value", "set_value"): 
     130                elif action in ("increase_value", "decrease_value", \ 
     131                                "set_value"): 
    118132                    if not isinstance(entry[action], types.DictionaryType): 
    119133                        raise DialogueFormatException(\ 
     
    133147             
    134148 
    135     def __validateResponses(self, responses): 
     149    def __validateResponses(self, responses, section_name): 
    136150        """ Checks if the list of responses is a valid list. 
    137151        @type responses: List 
    138152        @param respones: A list of response 
     153        @type section_name: String 
     154        @param section_name: The section name these responses belong to 
    139155        @raise DialogueFormatException on error  
    140156        @return True When the lists is a valid list""" 
     
    145161             
    146162            if not self.__isValidSectionName(option[1]): 
    147                 raise DialogueFormatException("Invalid target section given " +\ 
    148                                               option[1]) 
     163                raise DialogueFormatException("Section: " + section_name + \ 
     164                                              " contains an invalid target: " +\ 
     165                                              option[1] + " in response") 
    149166             
    150167            #TODO: option[2] might be a conditional  
Note: See TracChangeset for help on using the changeset viewer.