comparison contenttransformer/transformers.py @ 9:051d4d39b4b9

* separate out get_response to its own function * keep track of raw content_type for later usage (muahaha)
author k0s <k0scist@gmail.com>
date Sun, 07 Feb 2010 19:03:15 -0500
parents 68643e72c749
children a9ddcfc7c4e8
comparison
equal deleted inserted replaced
8:805cadc2b825 9:051d4d39b4b9
2 import subprocess 2 import subprocess
3 from webob import Request, Response 3 from webob import Request, Response
4 4
5 class Transformer(object): 5 class Transformer(object):
6 """abstract base class for transformer objects""" 6 """abstract base class for transformer objects"""
7 def __init__(self, content): 7 def __init__(self, content, content_type):
8 self.content = content 8 self.content = content
9 self.content_type = content_type
9 10
10 def transform(self, request): 11 def transform(self, request):
11 """returns a tuple of (body, content-type)""" 12 """returns a tuple of (body, content-type)"""
12 raise NotImplementedError 13 raise NotImplementedError
13 14
14 def __call__(self, environ, start_response): 15 def __call__(self, environ, start_response):
15 request = Request(environ) 16 request = Request(environ)
17 response = self.get_response(request)
18 return response(environ, start_response)
19
20 def get_response(self, request):
21 if request.GET.get('format') == 'raw':
22 return Response(content_type=self.content_type, body=self.content)
16 content_type, body = self.transform(request) 23 content_type, body = self.transform(request)
17 return Response(content_type=content_type, body=body)(environ, start_response) 24 return Response(content_type=content_type, body=body)
25
18 26
19 class Graphviz(Transformer): 27 class Graphviz(Transformer):
20 def transform(self, request): 28 def transform(self, request):
21 """create a Graphviz object""" 29 """create a Graphviz object"""
22 process = subprocess.Popen(['dot', '-Tpng'], 30 process = subprocess.Popen(['dot', '-Tpng'],