mamweb/aesop/ovvpfile.py

31 lines
778 B
Python
Raw Normal View History

from django.http import HttpResponse
from django.utils.encoding import force_str
2015-05-14 17:55:08 +02:00
class OvvpFile:
2021-11-07 00:55:42 +01:00
def __init__(self):
# { header: value, ... }
self.headers = {}
# [ 'column-name', ... ]
self.columns = []
# [ { column: value, ...}, ...]
self.rows = []
2015-05-14 17:55:08 +02:00
2021-11-07 00:55:42 +01:00
def to_lines(self):
# header
for hk in sorted(self.headers.keys()):
yield f'{hk}\t{self.headers[hk]}\n'
2021-11-07 00:55:42 +01:00
yield '\n'
# columns
yield '\t'.join(self.columns) + '\n'
2021-11-07 00:55:42 +01:00
# rows
for r in self.rows:
yield '\t'.join([force_str(r[c]) for c in self.columns]) + '\n'
2015-05-14 17:55:08 +02:00
2021-11-07 00:55:42 +01:00
def to_string(self):
return ''.join(self.to_lines())
2015-05-14 17:55:08 +02:00
# Pozn: tohle je ta jediná funkce, která se reálně používá…
2021-11-07 00:55:42 +01:00
def to_HttpResponse(self):
return HttpResponse(self.to_string(), content_type='text/plain; charset=utf-8')