File contents
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>mapfiler.py</title>
<meta name="generator" content="emacs 22.0.50.1; htmlfontify 0.20">
<style type="text/css"><!--
body { background: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: none; }
span.default { background: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: none; }
span.default a { background: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: underline; }
span.function-name { color: rgb(0, 0, 255); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: none; }
span.function-name a { color: rgb(0, 0, 255); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: underline; }
span.type { color: rgb(34, 139, 34); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: none; }
span.type a { color: rgb(34, 139, 34); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: underline; }
span.keyword { color: rgb(160, 32, 240); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: none; }
span.keyword a { color: rgb(160, 32, 240); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: underline; }
span.string { color: rgb(0, 100, 0); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: none; }
span.string a { color: rgb(0, 100, 0); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: underline; }
span.comment { color: rgb(178, 34, 34); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: none; }
span.comment a { color: rgb(178, 34, 34); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: underline; }
span.comment-delimiter { color: rgb(178, 34, 34); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: none; }
span.comment-delimiter a { color: rgb(178, 34, 34); background: rgb(255, 255, 255); font-style: normal; font-weight: 500; font-stretch: normal; font-family: adobe-courier; font-size: 11pt; text-decoration: underline; }
--></style>
</head>
<body>
<pre>
<span class="comment-delimiter">#</span><span class="comment">! /usr/bin/env python
</span>
<span class="string">"""
Extends ElementTree to allow easy construction of Mapserver Mapfiles
See mapfiler.py for copyright information.
"""</span>
<span class="comment-delimiter"># </span><span class="comment">--------------------------------------------------------------------
</span><span class="comment-delimiter">#</span><span class="comment">
</span><span class="comment-delimiter"># </span><span class="comment">Adapted from ElementTree Toolkit
</span><span class="comment-delimiter">#</span><span class="comment">
</span><span class="comment-delimiter"># </span><span class="comment">--------------------------------------------------------------------
</span><span class="comment-delimiter"># </span><span class="comment">The ElementTree toolkit is
</span><span class="comment-delimiter">#</span><span class="comment">
</span><span class="comment-delimiter"># </span><span class="comment">Copyright (c) 1999-2004 by Fredrik Lundh
</span><span class="comment-delimiter"># </span><span class="comment">--------------------------------------------------------------------
</span>
<span class="comment-delimiter"># </span><span class="comment">------------------------------------------------------------------------
</span><span class="comment-delimiter"># </span><span class="comment">
</span><span class="comment-delimiter"># </span><span class="comment">Copyright (c) 2006 Allan Doyle
</span><span class="comment-delimiter"># </span><span class="comment">Copyright (c) 2006 EOGEO
</span><span class="comment-delimiter">#</span><span class="comment">
</span><span class="comment-delimiter"># </span><span class="comment">Permission is hereby granted, free of charge, to any person
</span><span class="comment-delimiter"># </span><span class="comment">obtaining a copy of this software and associated documentation
</span><span class="comment-delimiter"># </span><span class="comment">files (the "Software"), to deal in the Software without
</span><span class="comment-delimiter"># </span><span class="comment">restriction, including without limitation the rights to use, copy,
</span><span class="comment-delimiter"># </span><span class="comment">modify, merge, publish, distribute, sublicense, and/or sell copies
</span><span class="comment-delimiter"># </span><span class="comment">of the Software, and to permit persons to whom the Software is
</span><span class="comment-delimiter"># </span><span class="comment">furnished to do so, subject to the following conditions:
</span><span class="comment-delimiter"># </span><span class="comment">
</span><span class="comment-delimiter"># </span><span class="comment">The above copyright notice and this permission notice shall be
</span><span class="comment-delimiter"># </span><span class="comment">included in all copies or substantial portions of the Software.
</span><span class="comment-delimiter"># </span><span class="comment">
</span><span class="comment-delimiter"># </span><span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
</span><span class="comment-delimiter"># </span><span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
</span><span class="comment-delimiter"># </span><span class="comment">MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
</span><span class="comment-delimiter"># </span><span class="comment">NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
</span><span class="comment-delimiter"># </span><span class="comment">HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
</span><span class="comment-delimiter"># </span><span class="comment">WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
</span><span class="comment-delimiter"># </span><span class="comment">OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
</span><span class="comment-delimiter"># </span><span class="comment">DEALINGS IN THE SOFTWARE.
</span><span class="comment-delimiter"># </span><span class="comment">
</span><span class="comment-delimiter"># </span><span class="comment">------------------------------------------------------------------------
</span>
<span class="comment-delimiter"># </span><span class="comment">python includes
</span><span class="keyword">import</span> re
<span class="comment-delimiter"># </span><span class="comment">package includes
</span><span class="keyword">from</span> elementtree.ElementTree <span class="keyword">import</span> *
<span class="keyword">class</span> <span class="type">MapElementTree</span>(ElementTree):
_pad = <span class="string">' '</span>
_level = 0
<span class="keyword">def</span> <span class="function-name">__init__</span>(self, element=None, file=None):
ElementTree.__init__(self, element, file)
<span class="keyword">def</span> <span class="function-name">_write</span>(self, file, node, encoding, namespaces):
self._pad = self._pad + <span class="string">' '</span>
<span class="comment-delimiter"># </span><span class="comment">write XML to file
</span> tag = node.tag
<span class="keyword">if</span> tag <span class="keyword">is</span> Comment:
file.write(<span class="string">"%s# %s\n"</span> % (self._pad[0:self._level], node.text))
<span class="keyword">else:</span>
items = node.items()
<span class="keyword">if</span> self._level > 0:
file.write(<span class="string">"\n"</span>)
file.write(self._pad[0:self._level] + tag)
file.write(<span class="string">"\n"</span>)
self._level = self._level + 1
<span class="keyword">if</span> items:
items.sort() <span class="comment-delimiter"># </span><span class="comment">lexical order
</span> <span class="keyword">for</span> k, v <span class="keyword">in</span> items:
file.write(<span class="string">'%s%s %s\n'</span> % (self._pad[0:self._level], k,v))
<span class="keyword">if</span> node.text <span class="keyword">or</span> node:
<span class="keyword">if</span> node.text:
file.write(re.sub(<span class="string">"\t"</span>, self._pad[0:self._level], node.text))
file.write(<span class="string">"\n"</span>)
<span class="keyword">for</span> n <span class="keyword">in</span> node:
self._write(file, n, encoding, namespaces)
self._level = self._level - 1
file.write(<span class="string">"%sEND\n"</span> % self._pad[0:self._level-1])
<span class="keyword">if</span> node.tail:
file.write(node.tail)
</pre>
</body>
</html>