CSVRenderer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 2 1
A __call__() 0 19 3
1
# http://docs.pylonsproject.org/projects/pyramid-cookbook/en/latest/templates/customrenderers.html
2
3
import csv
4
import io
5
6
class CSVRenderer(object):
7
   def __init__(self, info):
8
      pass
9
10
   def __call__(self, value, system):
11
      """ Returns a plain CSV-encoded string with content-type
12
      ``text/csv``. The content-type may be overridden by
13
      setting ``request.response.content_type``."""
14
15
      request = system.get('request')
16
      if request is not None:
17
         response = request.response
18
         ct = response.content_type
19
         if ct == response.default_content_type:
20
            response.content_type = 'text/csv'
21
22
      fout = io.StringIO()
23
      writer = csv.writer(fout, delimiter=',', quotechar=',', quoting=csv.QUOTE_MINIMAL)
24
25
      writer.writerow(value.get('header', []))
26
      writer.writerows(value.get('rows', []))
27
28
      return fout.getvalue()
29
30