mapfiler.py
Click here to get the file
Size
3.1 kB
-
File type
text/python-source
File contents
#! /usr/bin/env python
"""
Extends ElementTree to allow easy construction of Mapserver Mapfiles
See mapfiler.py for copyright information.
"""
# --------------------------------------------------------------------
#
# Adapted from ElementTree Toolkit
#
# --------------------------------------------------------------------
# The ElementTree toolkit is
#
# Copyright (c) 1999-2004 by Fredrik Lundh
# --------------------------------------------------------------------
# ------------------------------------------------------------------------
#
# Copyright (c) 2006 Allan Doyle
# Copyright (c) 2006 EOGEO
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
# ------------------------------------------------------------------------
# python includes
import re
# package includes
from elementtree.ElementTree import *
class MapElementTree(ElementTree):
_pad = ' '
_level = 0
def __init__(self, element=None, file=None):
ElementTree.__init__(self, element, file)
def _write(self, file, node, encoding, namespaces):
self._pad = self._pad + ' '
# write XML to file
tag = node.tag
if tag is Comment:
file.write("%s# %s\n" % (self._pad[0:self._level], node.text))
else:
items = node.items()
if self._level > 0:
file.write("\n")
file.write(self._pad[0:self._level] + tag)
file.write("\n")
self._level = self._level + 1
if items:
items.sort() # lexical order
for k, v in items:
file.write('%s%s %s\n' % (self._pad[0:self._level], k,v))
if node.text or node:
if node.text:
file.write(re.sub("\t", self._pad[0:self._level], node.text))
file.write("\n")
for n in node:
self._write(file, n, encoding, namespaces)
self._level = self._level - 1
file.write("%sEND\n" % self._pad[0:self._level-1])
if node.tail:
file.write(node.tail)