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()
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()
def cb_open(self, args): """Callback when open was clicked in the file menu""" import tkinter.filedialog import tkinter.messagebox # Based on code from unknown-horizons try: selected_file = tkinter.filedialog.askopenfilename( filetypes=[(_("fife map file"), ".xml",)], title=_("Open file")) except ImportError: # tkinter may be missing5555 selected_file = "" if selected_file: tree = ET.parse(selected_file) if tree.getroot().tag == "map": filename = os.path.relpath(selected_file, os.getcwd()) fife_map = self.app.editor.load_map(filename) for cam in fife_map.getCameras(): if cam.getLocationRef().getMap().getId() == fife_map.getId(): game_map = GameMap(fife_map, fife_map.getId(), cam.getId(), dict(), self.app) self.app.add_map(fife_map.getId(), game_map) self.app.switch_map(game_map.name) self.reset_maps_menu() return
def do_load_resource(self, file): """ """ if file: tree = ET.parse(file) self.node = tree.getroot() self.parse_object(self.node)
def loadResource(self, location): """ overwrite of B{fife.ResourceLoader} @type location: object @param location: path to a map file as a fife.ResourceLocation @return FIFE map object @rtype object """ start_time = time.time() self.source = location f = self.vfs.open(self.source) f.thisown = 1 tree = ET.parse(f) root = tree.getroot() map = self.parse_map(root) self.time_to_load = time.time() - start_time return map
def loadXMLAnimation(engine, filename): f = engine.getVFS().open(filename) f.thisown = 1 imgMgr = engine.getImageManager() tree = ET.parse(f) node = tree.getroot() animation = fife.Animation.createAnimation() common_frame_delay = int(node.get('delay', 0)) x_offset = int(node.get('x_offset', 0)) y_offset = int(node.get('y_offset', 0)) animation.setActionFrame(int(node.get('action', 0))) frames = node.findall('frame') if not frames: raise InvalidFormat('animation without <frame>s') for frame in frames: source = frame.get('source') if not source: raise InvalidFormat('animation without <frame>s') frame_x_offset = int(frame.get('x_offset', x_offset)) frame_y_offset = int(frame.get('y_offset', y_offset)) frame_delay = int(frame.get('delay', common_frame_delay)) # xml paths are relative to the directory of the file they're used in. path = filename.split('/') path.pop() path.append(str(source)) image_file = '/'.join(path) img = imgMgr.create(image_file) img.setXShift(frame_x_offset) img.setYShift(frame_y_offset) animation.addFrame(img, frame_delay) # animation.thisown = 0 return animation
def do_load_resource(self): f = self.vfs.open(self.filename) f.thisown = 1 tree = ET.parse(f) self.node = tree.getroot() animation = fife.Animation() common_frame_delay = int(self.node.get('delay', 0)) x_offset = int(self.node.get('x_offset', 0)) y_offset = int(self.node.get('y_offset', 0)) animation.setActionFrame(int(self.node.get('action', 0))) frames = self.node.findall('frame') if not frames: raise InvalidFormat('animation without <frame>s') for frame in frames: source = frame.get('source') if not source: raise InvalidFormat('animation without <frame>s') frame_x_offset = int(frame.get('x_offset', x_offset)) frame_y_offset = int(frame.get('y_offset', y_offset)) frame_delay = int(frame.get('delay', common_frame_delay)) # xml paths are relative to the directory of the file they're used in. path = self.filename.split('/') path.pop() path.append(str(source)) image_location = fife.ImageLocation('/'.join(path)) image_location.setXShift(frame_x_offset) image_location.setYShift(frame_y_offset) image_index = self.imagepool.addResourceFromLocation(image_location) animation.addFrame(fife.ResourcePtr(self.imagepool,image_index), frame_delay) animation.thisown = 0 return animation
def loadXMLAnimation(engine, filename): f = engine.getVFS().open(filename) f.thisown = 1 imgMgr = engine.getImageManager() tree = ET.parse(f) node = tree.getroot() animation = fife.Animation.createAnimation() common_width = int(node.get('width', 0)) common_height = int(node.get('height', 0)) common_y_offset = int(node.get('y_offset', 0)) common_frame_delay = int(node.get('delay', 0)) common_x_offset = int(node.get('x_offset', 0)) common_y_offset = int(node.get('y_offset', 0)) animation.setActionFrame(int(node.get('action', 0))) frames = node.findall('frame') if not frames: raise InvalidFormat('animation without <frame>s') atlas = node.get('atlas') if atlas: path = filename.split('/') path.pop() path.append(str(atlas)) atlas_file = '/'.join(path) if imgMgr.exists(atlas_file): atlas_img = imgMgr.getPtr(str(atlas_file)) else: atlas_img = imgMgr.create(str(atlas_file)) # parse atlas animation format 2 (e.g. cursor) for frame in frames: source = frame.get('source') if not source: raise InvalidFormat('animation without <frame>s') frame_x_pos = int(frame.get('xpos', 0)) frame_y_pos = int(frame.get('ypos', 0)) frame_x_offset = int(frame.get('x_offset', common_x_offset)) frame_y_offset = int(frame.get('y_offset', common_y_offset)) frame_delay = int(frame.get('delay', common_frame_delay)) frame_width = int(frame.get('width', common_width)) frame_height = int(frame.get('height', common_height)) # xml paths are relative to the directory of the file they're used in. path = filename.split('/') path.pop() path.append(str(source)) frame_file = '/'.join(path) frame_file = atlas,":",frame_file if imgMgr.exists(str(frame_file)): frame_img = imgMgr.getPtr(str(frame_file)) else: frame_img = imgMgr.create(str(frame_file)) region = fife.Rect(frame_x_pos, frame_y_pos, frame_width, frame_height) frame_img.useSharedImage(atlas_img, region) frame_img.setXShift(frame_x_offset) frame_img.setYShift(frame_y_offset) animation.addFrame(frame_img, frame_delay) else: # parse single images for frame in frames: source = frame.get('source') if not source: raise InvalidFormat('animation without <frame>s') frame_x_offset = int(frame.get('x_offset', common_x_offset)) frame_y_offset = int(frame.get('y_offset', common_y_offset)) frame_delay = int(frame.get('delay', common_frame_delay)) # xml paths are relative to the directory of the file they're used in. path = filename.split('/') path.pop() path.append(str(source)) image_file = '/'.join(path) img = imgMgr.create(image_file) img.setXShift(frame_x_offset) img.setYShift(frame_y_offset) animation.addFrame(img, frame_delay) # animation.thisown = 0 return animation
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
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() cost_id = object.getCostId() cost = object.getCost() cellstack_pos = object.getCellStackPosition() 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 int(obj.attrib['blocking']) != int(blocking): self.change = True if int(obj.attrib['static']) != int(static): self.change = True if str(obj.attrib['cost_id']) != str(cost_id): self.change = True if float(obj.attrib['cost']) != float(cost): self.change = True if int(obj.attrib['cellstack_position']) != int(cellstack_pos): self.change = True obj.attrib['blocking'] = str(int(blocking)) obj.attrib['static'] = str(int(static)) 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
def loadXMLAnimation(engine, filename): f = engine.getVFS().open(filename) f.thisown = 1 imgMgr = engine.getImageManager() tree = ET.parse(f) node = tree.getroot() animation = fife.Animation.createAnimation() common_width = int(node.get('width', 0)) common_height = int(node.get('height', 0)) common_y_offset = int(node.get('y_offset', 0)) common_frame_delay = int(node.get('delay', 0)) common_x_offset = int(node.get('x_offset', 0)) common_y_offset = int(node.get('y_offset', 0)) animation.setActionFrame(int(node.get('action', 0))) frames = node.findall('frame') if not frames: raise InvalidFormat('animation without <frame>s') atlas = node.get('atlas') if atlas: path = filename.split('/') path.pop() path.append(str(atlas)) atlas_file = '/'.join(path) if imgMgr.exists(atlas_file): atlas_img = imgMgr.getPtr(str(atlas_file)) else: atlas_img = imgMgr.create(str(atlas_file)) # parse atlas animation format 2 (e.g. cursor) for frame in frames: source = frame.get('source') if not source: raise InvalidFormat('animation without <frame>s') frame_x_pos = int(frame.get('xpos', 0)) frame_y_pos = int(frame.get('ypos', 0)) frame_x_offset = int(frame.get('x_offset', common_x_offset)) frame_y_offset = int(frame.get('y_offset', common_y_offset)) frame_delay = int(frame.get('delay', common_frame_delay)) frame_width = int(frame.get('width', common_width)) frame_height = int(frame.get('height', common_height)) # xml paths are relative to the directory of the file they're used in. path = filename.split('/') path.pop() path.append(str(source)) frame_file = '/'.join(path) frame_file = atlas, ":", frame_file if imgMgr.exists(str(frame_file)): frame_img = imgMgr.getPtr(str(frame_file)) else: frame_img = imgMgr.create(str(frame_file)) region = fife.Rect(frame_x_pos, frame_y_pos, frame_width, frame_height) frame_img.useSharedImage(atlas_img, region) frame_img.setXShift(frame_x_offset) frame_img.setYShift(frame_y_offset) animation.addFrame(frame_img, frame_delay) else: # parse single images for frame in frames: source = frame.get('source') if not source: raise InvalidFormat('animation without <frame>s') frame_x_offset = int(frame.get('x_offset', common_x_offset)) frame_y_offset = int(frame.get('y_offset', common_y_offset)) frame_delay = int(frame.get('delay', common_frame_delay)) # xml paths are relative to the directory of the file they're used in. path = filename.split('/') path.pop() path.append(str(source)) image_file = '/'.join(path) img = imgMgr.create(image_file) img.setXShift(frame_x_offset) img.setYShift(frame_y_offset) animation.addFrame(img, frame_delay) # animation.thisown = 0 return animation