source: cows/trunk/cows/pylons/genshi_util.py @ 3481

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/pylons/genshi_util.py@4078
Revision 3481, 1.4 KB checked in by spascoe, 12 years ago (diff)

WMS 1.1.1 capabilities improved

Line 
1"""
2Utilities for use with genshi
3
4@author: Stephen Pascoe
5
6"""
7
8from genshi import *
9
10class RenameElementFilter(object):
11    """
12    Rename the root element in the stream.
13
14    Filters of this class will replicate the stream until the first START event
15    then change the event element's QName.  It will then count opening and closing
16    tags until it finds the matching close tag and replace the QName in that.  It
17    then continues to replicate the stream.
18
19    """
20
21    def __init__(self, newQName):
22        self.newQName = newQName
23        # status 0: awaiting START, 1: awaiting END, 2: noop
24        self.status = 0
25        self.tcount = 0
26
27    def __call__(self, stream):
28        for kind, data, pos in stream:
29            if self.status == 0:
30                if kind == Stream.START:
31                    self.status = 1
32                    self.tcount = 1
33                    yield kind, (self.newQName, data[1]), pos
34                else:
35                    yield kind, data, pos
36            elif self.status == 1:
37                if kind == Stream.START:
38                    self.tcount += 1
39                elif kind == Stream.END:
40                    self.tcount -= 1
41
42                if self.tcount == 0:
43                    self.status == 2
44                    yield kind, self.newQName, pos
45                else:
46                    yield kind, data, pos
47            elif self.status == 2:
48                yield kind, data, pos
Note: See TracBrowser for help on using the repository browser.