def start_element_handler(self, name, attributes): # All we want to do is construct our own Element instead of # minidom.Element, unfortunately the only way to do this is to # copy this whole function from expatbuilder.py if ' ' in name: uri, localname, prefix, qname = expatbuilder._parse_ns_name( self, name) else: uri = expatbuilder.EMPTY_NAMESPACE qname = name localname = None prefix = expatbuilder.EMPTY_PREFIX node = Element(qname, uri, prefix, localname) node.ownerDocument = self.document expatbuilder._append_child(self.curNode, node) self.curNode = node if self._ns_ordered_prefixes: for prefix, uri in self._ns_ordered_prefixes: if prefix: a = minidom.Attr( expatbuilder._intern(self, 'xmlns:' + prefix), expatbuilder.XMLNS_NAMESPACE, prefix, "xmlns") else: a = minidom.Attr("xmlns", expatbuilder.XMLNS_NAMESPACE, "xmlns", expatbuilder.EMPTY_PREFIX) d = a.childNodes[0].__dict__ d['data'] = d['nodeValue'] = uri d = a.__dict__ d['value'] = d['nodeValue'] = uri d['ownerDocument'] = self.document expatbuilder._set_attribute_node(node, a) del self._ns_ordered_prefixes[:] if attributes: _attrs = node._attrs _attrsNS = node._attrsNS for i in range(0, len(attributes), 2): aname = attributes[i] value = attributes[i + 1] if ' ' in aname: uri, localname, prefix, qname = expatbuilder._parse_ns_name( self, aname) a = minidom.Attr(qname, uri, localname, prefix) _attrs[qname] = a _attrsNS[(uri, localname)] = a else: a = minidom.Attr(aname, expatbuilder.EMPTY_NAMESPACE, aname, expatbuilder.EMPTY_PREFIX) _attrs[aname] = a _attrsNS[(expatbuilder.EMPTY_NAMESPACE, aname)] = a d = a.childNodes[0].__dict__ d['data'] = d['nodeValue'] = value d = a.__dict__ d['ownerDocument'] = self.document d['value'] = d['nodeValue'] = value d['ownerElement'] = node
def start_element_handler(self, name, attributes): if ' ' in name: uri, localname, prefix, qname = _parse_ns_name(self, name) else: uri = EMPTY_NAMESPACE qname = name localname = None prefix = EMPTY_PREFIX node = minidom.Element(qname, uri, prefix, localname) node.ownerDocument = self.document _append_child(self.curNode, node) self.curNode = node if self._ns_ordered_prefixes: for prefix, uri in self._ns_ordered_prefixes: if prefix: a = minidom.Attr(_intern(self, 'xmlns:' + prefix), XMLNS_NAMESPACE, prefix, "xmlns") else: a = minidom.Attr("xmlns", XMLNS_NAMESPACE, "xmlns", EMPTY_PREFIX) # we're only interested in the URI as text at this point uri = uri or "" d = a.childNodes[0].__dict__ d['data'] = d['nodeValue'] = uri d = a.__dict__ d['value'] = d['nodeValue'] = uri d['ownerDocument'] = self.document _set_attribute_node(node, a) del self._ns_ordered_prefixes[:] if attributes: _attrs = node._attrs _attrsNS = node._attrsNS for i in range(0, len(attributes), 2): aname = attributes[i] value = attributes[i + 1] if ' ' in aname: uri, localname, prefix, qname = _parse_ns_name(self, aname) a = minidom.Attr(qname, uri, localname, prefix) _attrs[qname] = a _attrsNS[(uri, localname)] = a else: a = minidom.Attr(aname, EMPTY_NAMESPACE, aname, EMPTY_PREFIX) _attrs[aname] = a _attrsNS[(EMPTY_NAMESPACE, aname)] = a d = a.childNodes[0].__dict__ d['data'] = d['nodeValue'] = value d = a.__dict__ d['ownerDocument'] = self.document d['value'] = d['nodeValue'] = value d['ownerElement'] = node
def start_element_handler(self, name, attributes): if ' ' in name: uri, localname, prefix, qname = _parse_ns_name(self, name) else: uri = EMPTY_NAMESPACE qname = name localname = None prefix = EMPTY_PREFIX node = minidom.Element(qname, uri, prefix, localname) node.ownerDocument = self.document _append_child(self.curNode, node) self.curNode = node if self._ns_ordered_prefixes: for prefix, uri in self._ns_ordered_prefixes: if prefix: a = minidom.Attr(_intern(self, 'xmlns:' + prefix), XMLNS_NAMESPACE, prefix, "xmlns") else: a = minidom.Attr("xmlns", XMLNS_NAMESPACE, "xmlns", EMPTY_PREFIX) a.value = uri a.ownerDocument = self.document _set_attribute_node(node, a) del self._ns_ordered_prefixes[:] if attributes: node._ensure_attributes() _attrs = node._attrs _attrsNS = node._attrsNS for i in range(0, len(attributes), 2): aname = attributes[i] value = attributes[i + 1] if ' ' in aname: uri, localname, prefix, qname = _parse_ns_name(self, aname) a = minidom.Attr(qname, uri, localname, prefix) _attrs[qname] = a _attrsNS[(uri, localname)] = a else: a = minidom.Attr(aname, EMPTY_NAMESPACE, aname, EMPTY_PREFIX) _attrs[aname] = a _attrsNS[(EMPTY_NAMESPACE, aname)] = a a.ownerDocument = self.document a.value = value a.ownerElement = node
def start_element_handler(self, name, attributes): node = self.document.createElement(name) _append_child(self.curNode, node) self.curNode = node if attributes: for i in range(0, len(attributes), 2): a = minidom.Attr(attributes[i], EMPTY_NAMESPACE, None, EMPTY_PREFIX) value = attributes[i + 1] a.value = value a.ownerDocument = self.document _set_attribute_node(node, a) if node is not self.document.documentElement: self._finish_start_element(node)
def start_element_handler(self, name, attributes): node = self.document.createElement(name) _append_child(self.curNode, node) self.curNode = node if attributes: for i in range(0, len(attributes), 2): a = minidom.Attr(attributes[i], EMPTY_NAMESPACE, None, EMPTY_PREFIX) value = attributes[i + 1] d = a.childNodes[0].__dict__ d['data'] = d['nodeValue'] = value d = a.__dict__ d['value'] = d['nodeValue'] = value d['ownerDocument'] = self.document _set_attribute_node(node, a) if node is not self.document.documentElement: self._finish_start_element(node) return
def main(): """Here is where the magic happens.""" for mapfolder in os.listdir(MAP_SRC_DIR): src = os.path.join(MAP_SRC_DIR, mapfolder) dest = os.path.join(MAP_DEST_DIR, mapfolder + '.xml') if os.path.exists(dest): os.remove(dest) shutil.copy(os.path.join(src, 'map.xml'), MAP_DEST_DIR) os.rename(os.path.join(MAP_DEST_DIR, 'map.xml'), os.path.join(MAP_DEST_DIR, os.path.basename(src) + '.xml')) mapsurf = pygame.image.load(os.path.join(src, 'map.png')) mapdata = pygame.PixelArray(mapsurf) width, height = mapsurf.get_size() tiles = [] spawnpoints = [] for row in range(height): for col in range(width): if mapdata[col][row] == mapsurf.map_rgb(WHITE): tiles.append((col, row)) elif mapdata[col][row] == mapsurf.map_rgb(BLUE): spawnpoints.append((col, row)) doc = dom.parse(dest) maptag = doc.firstChild # Add 'size' attr size_attr = dom.Attr('size') size_attr.value = '{0}x{1}'.format(width, height) maptag.setAttributeNode(size_attr) maptag.appendChild(create_vec_lst_tag('Spawnpoints', spawnpoints)) maptag.appendChild(create_vec_lst_tag('Tiles', tiles)) with open(dest, 'w') as xml_file: doc.writexml(xml_file)
def start_element_handler(self, name, attributes): # All we want to do is construct our own Element instead of # minidom.Element, unfortunately the only way to do this is to # copy this whole function from expatbuilder.py if " " in name: uri, localname, prefix, qname = expatbuilder._parse_ns_name(self, name) else: uri = expatbuilder.EMPTY_NAMESPACE qname = name localname = None prefix = expatbuilder.EMPTY_PREFIX node = Element(qname, uri, prefix, localname) node.ownerDocument = self.document expatbuilder._append_child(self.curNode, node) self.curNode = node if self._ns_ordered_prefixes: for prefix, uri in self._ns_ordered_prefixes: if prefix: a = minidom.Attr( expatbuilder._intern(self, "xmlns:" + prefix), expatbuilder.XMLNS_NAMESPACE, prefix, "xmlns", ) else: a = minidom.Attr( "xmlns", expatbuilder.XMLNS_NAMESPACE, "xmlns", expatbuilder.EMPTY_PREFIX, ) a.value = uri a.ownerDocument = self.document expatbuilder._set_attribute_node(node, a) del self._ns_ordered_prefixes[:] if attributes: if hasattr(node, "_ensure_attributes"): node._ensure_attributes() # Python 3 only _attrs = node._attrs _attrsNS = node._attrsNS for i in range(0, len(attributes), 2): aname = attributes[i] value = attributes[i + 1] if " " in aname: uri, localname, prefix, qname = expatbuilder._parse_ns_name( self, aname ) a = minidom.Attr(qname, uri, localname, prefix) _attrs[qname] = a _attrsNS[(uri, localname)] = a else: a = minidom.Attr( aname, expatbuilder.EMPTY_NAMESPACE, aname, expatbuilder.EMPTY_PREFIX, ) _attrs[aname] = a _attrsNS[(expatbuilder.EMPTY_NAMESPACE, aname)] = a a.ownerDocument = self.document a.value = value a.ownerElement = node