Beispiel #1
0
	def load(self, filename=None):
		"""
		Loads the XML file into memory and validates it.
		
		Raises a SerializerError exception if the file is not specified.
		
		@param filename: The file to load
		@type filename: C{str}
		
		@note: If the file does not exist it will automatically create a blank
		file for you.
		"""
		if filename:
			self._file = filename
		
		if not self._file:
			raise SerializerError("Cannot load file or create file.  No "
								  "filename specified!")
		
		if not os.path.exists(self._file):
			self._tree = ET.parse(StringIO(EMPTY_XML_FILE))
			self._tree.write(self._file, 'UTF-8')			
		else:
			self._tree = ET.parse(self._file)

		self._root_element = self._tree.getroot()
		self._validateTree()
Beispiel #2
0
    def save(self, filename=None):
        """
		Saves the XML file.
		
		@param filename: The file to save
		@type filename: C{str}
		
		@note: This Overwrites the file if it exists.
		"""
        if not self._initialized:
            self.load()
            self._initialized = True

        if filename:
            savefile = filename
        else:
            savefile = self._file

        if not savefile:
            raise SerializerError("Cannot save file.  No filename specified!")
        """ Writes the settings to file """
        self._indent(self._root_element)
        self._tree.write(savefile, 'UTF-8')
    def save(self, object):
        """ saves the data of a fife.Object to its xml file
		
		@type	object:	fife.Object
		@param	object:	the object which should be saved
		@rtype	bool
		@return	flag whether the saving was successful or not
		"""
        self.change = False
        result = False

        file = object.getFilename()
        if not file:
            raise SerializerError("Object cannot be saved, no file found %s" %
                                  object)
            return result

        if not self.vfs.exists(file):
            raise NotFound("File not within vfs: %s" % file)
            return result

        file_handle = self.vfs.open(file)
        file_handle.thisown = 1
        tree = ET.parse(file_handle)
        root = tree.getroot()

        object_id = object.getId()
        blocking = object.isBlocking()
        static = object.isStatic()

        # @todo:
        # compat layer - remove as soon as cell_pathfinding branch
        # is merged back to trunk/
        if hasattr(object, 'getCostId'):
            cost_id = object.getCostId()
        else:
            cost_id = ''
        if hasattr(object, 'getCost'):
            cost = object.getCost()
        else:
            cost = 0.0
        if hasattr(object, 'getCellStackPosition'):
            cellstack_pos = object.getCellStackPosition()
        else:
            cellstack_pos = 0

        if self.debug:
            print "XML tree dump: (pre-save)"
            ET.dump(root)
            print "Object data: "
            print "\tid", object_id
            print "\tblocking", blocking
            print "\tstatic", static
            print "\tcost id", cost_id
            print "\tcost", cost

        # check for compat mode
        if root.tag != 'assets':
            self.compat = True

        # in compat mode tree is <object>
        if self.compat:
            objects = [
                root,
            ]
        # new XML structure has tree root <assets> which groups multiple objects
        else:
            objects = root.findall("object")

        for obj in objects:
            _id = obj.get("id")
            if _id != object_id:
                if self.debug:
                    print "...ommitting object %s " % _id
                continue

            if 'blocking' not in obj.attrib or int(
                    obj.attrib['blocking']) != int(blocking):
                self.change = True
            if 'static' not in obj.attrib or int(
                    obj.attrib['static']) != int(static):
                self.change = True
            if 'cost_id' not in obj.attrib or str(
                    obj.attrib['cost_id']) != str(cost_id):
                self.change = True
            if 'cost' not in obj.attrib or float(
                    obj.attrib['cost']) != float(cost):
                self.change = True
            if 'cellstack_position' not in obj.attrib or int(
                    obj.attrib['cellstack_position']) != int(cellstack_pos):
                self.change = True

            obj.attrib['blocking'] = str(int(blocking))
            obj.attrib['static'] = str(int(static))
            if cost_id and cost:
                obj.attrib['cost_id'] = str(cost_id)
                obj.attrib['cost'] = str(cost)
            obj.attrib['cellstack_position'] = str(cellstack_pos)

            if self.debug and self.change:
                print "\tSet new data in xml tree: "
                print "\t\tblocking: ", obj.attrib['blocking']
                print "\t\tstatic: ", obj.attrib['static']

            images = obj.findall("image")
            actions = obj.findall("action")

            if self.debug:
                print "\tAttempting to save image data: "
                print "\t...found these image elements: "
                print "\t", images
                print "object dump: "
                print ET.dump(obj)

            self.save_images(images, object)
            self.save_actions(actions, object)

        if not self.change:
            return result

        xmlcontent = ET.tostring(root)

        if self.debug:
            print "XML tree dump: (post-manipulation)"
            ET.dump(root)

        # save xml data beneath the <?fife type="object"?> definition into the object file
        file = open(file, 'w')
        file.write(XMLObjectSaver.PROCESSING_INSTRUCTION + '\n')
        file.write(xmlcontent + "\n")
        file.close()
        result = True
        return result