1 import cherrypy
2 from cherrypy._cpcompat import ntob
3 from cherrypy.test import helper
4
5
7
9
10 class WSGIResponse(object):
11
12 def __init__(self, appresults):
13 self.appresults = appresults
14 self.iter = iter(appresults)
15
16 def __iter__(self):
17 return self
18
19 def next(self):
20 return self.iter.next()
21
22 def __next__(self):
23 return next(self.iter)
24
25 def close(self):
26 if hasattr(self.appresults, "close"):
27 self.appresults.close()
28
29 class ChangeCase(object):
30
31 def __init__(self, app, to=None):
32 self.app = app
33 self.to = to
34
35 def __call__(self, environ, start_response):
36 res = self.app(environ, start_response)
37
38 class CaseResults(WSGIResponse):
39
40 def next(this):
41 return getattr(this.iter.next(), self.to)()
42
43 def __next__(this):
44 return getattr(next(this.iter), self.to)()
45 return CaseResults(res)
46
47 class Replacer(object):
48
49 def __init__(self, app, map={}):
50 self.app = app
51 self.map = map
52
53 def __call__(self, environ, start_response):
54 res = self.app(environ, start_response)
55
56 class ReplaceResults(WSGIResponse):
57
58 def next(this):
59 line = this.iter.next()
60 for k, v in self.map.iteritems():
61 line = line.replace(k, v)
62 return line
63
64 def __next__(this):
65 line = next(this.iter)
66 for k, v in self.map.items():
67 line = line.replace(k, v)
68 return line
69 return ReplaceResults(res)
70
71 class Root(object):
72
73 def index(self):
74 return "HellO WoRlD!"
75 index.exposed = True
76
77 root_conf = {'wsgi.pipeline': [('replace', Replacer)],
78 'wsgi.replace.map': {ntob('L'): ntob('X'),
79 ntob('l'): ntob('r')},
80 }
81
82 app = cherrypy.Application(Root())
83 app.wsgiapp.pipeline.append(('changecase', ChangeCase))
84 app.wsgiapp.config['changecase'] = {'to': 'upper'}
85 cherrypy.tree.mount(app, config={'/': root_conf})
86 setup_server = staticmethod(setup_server)
87
95