source: trunk/game/scripts/objectLoader.py @ 300

Revision 262, 2.3 KB checked in by barra_parpg, 10 years ago (diff)

Patch by mvbarracuda:

  • Applied svn:eol-style >> native to all Python files
  • Resolved inconsistent line endings for gamestate.py, composed.py & base.py
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18from xml.sax import make_parser
19from xml.sax.handler import ContentHandler
20import sys
21
22class ObjectXMLParser(ContentHandler):
23    """ObjectXMLParser call constructors to make GameObjects using information
24       provided in _objects.xml files."""
25    def __init__(self):
26        """Initializes the ObjectXMLParser."""
27        # local_info is a list of dictionaries. When startElement is called
28        # (through code in the scripts.Engine), this list is populated.
29        self.local_info = []
30        # parser is created when getObjects is called.
31        self.parser = None
32   
33    def getObjects(self, file_desc):
34        """Reads the object data into dictionaries out of which
35        the game objects can be constructed.
36           @type file_desc: File
37           @param file_desc: an open file from which we read
38           @return: None"""
39        parser = make_parser()
40        parser.setContentHandler(self)
41        parser.parse(file_desc)
42        self.agent_layer = None
43
44    def startElement(self, name, attrs):
45        """Called every time we meet a new element in the XML file. This
46           function is specified in ContentHandler, and is called by the parser.
47           @type name: string
48           @param name: XML element?
49           @type attrs: ???
50           @param attrs: XML attributes
51           @return: None"""
52        # For now, only looking for game_obj things
53        if str(name) == "object":
54            obj_info = {}
55            # we need to convert all the unicode strings to ascii strings
56            for key, val in attrs.items():
57                obj_info[str(key)] = str(val)
58            self.local_info.append(obj_info)
Note: See TracBrowser for help on using the repository browser.