Mercurial > fife-parpg
diff engine/python/fife/extensions/serializers/xmlobject.py @ 661:e3140f01749d
* Merged the light branch back into trunk.
* Modified the demos so they work with the new loaders and setting.
author | helios2000@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Fri, 05 Nov 2010 15:21:10 +0000 |
parents | 64738befdf3b |
children |
line wrap: on
line diff
--- a/engine/python/fife/extensions/serializers/xmlobject.py Wed Nov 03 13:44:12 2010 +0000 +++ b/engine/python/fife/extensions/serializers/xmlobject.py Fri Nov 05 15:21:10 2010 +0000 @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- - # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team +# Copyright (C) 2005-2010 by the FIFE team # http://www.fifengine.de # This file is part of FIFE. # @@ -21,17 +20,34 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # #################################################################### +""" submodule for xml map parsing """ + from fife import fife -from fife.extensions.serializers import * -from fife.extensions.fife_utils import * + +from fife.extensions.serializers import ET +from fife.extensions.serializers import SerializerError, InvalidFormat +from fife.extensions.serializers import NameClash, NotFound, WrongFileType class ObjectLocation(fife.ResourceLocation): + """ + + + """ def __init__(self, file, node=None): + """ + + """ fife.ResourceLocation.__init__(self, file) self.node = node class XMLObjectLoader(fife.ResourceLoader): + """ + + """ def __init__(self, image_pool, anim_pool, model, vfs=None): + """ + + """ self.image_pool = image_pool self.anim_pool = anim_pool self.model = model @@ -40,6 +56,9 @@ self.filename = '' def loadResource(self, location): + """ + + """ self.source = location self.filename = self.source.getFilename() self.node = None @@ -61,26 +80,36 @@ isobjectfile = False if not isobjectfile: + return raise WrongFileType('Tried to open non-object file %s with XMLObjectLoader.' % self.filename) + self.do_load_resource(f) def do_load_resource(self, file): + """ + + """ if file: tree = ET.parse(file) self.node = tree.getroot() self.parse_object(self.node) def parse_object(self, object): + """ + + """ if self.node.tag != 'object': raise InvalidFormat('Expected <object> tag, but found <%s>.' % self.node.tag) - id = object.get('id') - if not id: + _id = object.get('id') + if not _id: raise InvalidFormat('<object> declared without an id attribute.') + _id = str(_id) nspace = object.get('namespace') if not nspace: - raise InvalidFormat('<object> %s declared without a namespace attribute.' % str(id)) + raise InvalidFormat('<object> %s declared without a namespace attribute.' % str(_id)) + nspace = str(nspace) obj = None parent = object.get('parent', None) @@ -92,12 +121,12 @@ raise NameClash('%d objects found with identifier %s.' % (len(query), str(parent))) parent = query[0] - try: - obj = self.model.createObject(str(id), str(nspace), parent) - except RuntimeError, e: - if is_fife_exc(fife.NameClash, e): - raise NameClash('Tried to create already existing object, ignoring') - raise + # check if model already has this object + if not bool(self.model.getObject(_id, nspace)): + obj = self.model.createObject(_id, nspace, parent) + else: + print NameClash('Tried to create already existing object \n\t...ignoring: %s, %s' % (_id, nspace)) + return obj.setResourceLocation(self.source) fife.ObjectVisual.create(obj) @@ -111,6 +140,9 @@ self.parse_actions(object, obj) def parse_images(self, objelt, object): + """ + + """ for image in objelt.findall('image'): source = image.get('source') if not source: @@ -126,9 +158,11 @@ image_location .setYShift(int( image.get('y_offset', 0) )) id = self.image_pool.addResourceFromLocation(image_location) object.get2dGfxVisual().addStaticImage(int( image.get('direction', 0) ), id) - #img = self.image_pool.getImage(id) def parse_actions(self, objelt, object): + """ + + """ for action in objelt.findall('action'): id = action.get('id') if not id: @@ -139,6 +173,9 @@ self.parse_animations(action, act_obj) def parse_animations(self, actelt, action): + """ + + """ for anim in actelt.findall('animation'): source = anim.get('source') if not source: