2015-05-14 17:55:08 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-06-29 00:27:02 +02:00
|
|
|
try:
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.utils.encoding import force_text
|
|
|
|
except:
|
|
|
|
force_text = str
|
|
|
|
|
|
|
|
|
2015-05-14 17:55:08 +02:00
|
|
|
class OvvpFile(object):
|
|
|
|
def __init__(self):
|
2015-06-29 00:27:02 +02:00
|
|
|
# { header: value, ... }
|
2015-05-14 17:55:08 +02:00
|
|
|
self.headers = {}
|
2015-06-29 00:27:02 +02:00
|
|
|
# [ 'column-name', ... ]
|
2015-05-14 17:55:08 +02:00
|
|
|
self.columns = []
|
2015-06-29 00:27:02 +02:00
|
|
|
# [ { column: value, ...}, ...]
|
2015-05-14 17:55:08 +02:00
|
|
|
self.rows = []
|
|
|
|
|
|
|
|
def to_lines(self):
|
|
|
|
# header
|
|
|
|
for hk in sorted(self.headers.keys()):
|
|
|
|
yield '%s\t%s\n' % (hk, self.headers[hk])
|
|
|
|
yield '\n'
|
|
|
|
# columns
|
|
|
|
yield '\t'.join([c for c in self.columns]) + '\n'
|
|
|
|
# rows
|
|
|
|
for r in self.rows:
|
2015-06-29 00:27:02 +02:00
|
|
|
yield '\t'.join([force_text(r[c]) for c in self.columns]) + '\n'
|
2015-05-14 17:55:08 +02:00
|
|
|
|
|
|
|
def to_string(self):
|
|
|
|
return ''.join(self.to_lines())
|
|
|
|
|
2015-06-29 00:27:02 +02:00
|
|
|
def to_HttpResponse(self):
|
|
|
|
return HttpResponse(self.to_string(), content_type='text/plain; charset=utf-8')
|
|
|
|
|
2015-05-15 00:19:40 +02:00
|
|
|
def parse_from(self, source, with_headers=True):
|
2015-05-14 17:55:08 +02:00
|
|
|
"Parse data from file, string or line iterator, overwriting self"
|
|
|
|
if isinstance(source, str) or isinstance(source, unicode):
|
|
|
|
return self.parse_from(source.split('\n'))
|
|
|
|
|
|
|
|
it = iter(source)
|
|
|
|
|
|
|
|
# header
|
|
|
|
self.headers = {}
|
2015-05-15 00:19:40 +02:00
|
|
|
if with_headers:
|
|
|
|
for r in it:
|
|
|
|
if isinstance(r, str):
|
|
|
|
r = r.decode('utf8')
|
|
|
|
assert isinstance(r, unicode)
|
|
|
|
r = r.rstrip('\n')
|
|
|
|
if r == u"":
|
|
|
|
break
|
|
|
|
k, v = r.split(u'\t', 1)
|
|
|
|
self.headers[k] = v
|
2015-05-14 17:55:08 +02:00
|
|
|
|
|
|
|
# columns
|
|
|
|
r = it.next()
|
2015-05-14 19:53:01 +02:00
|
|
|
if isinstance(r, str):
|
|
|
|
r = r.decode('utf8')
|
|
|
|
self.columns = [cn.strip() for cn in r.split(u'\t') if cn.strip() != ""]
|
2015-05-14 17:55:08 +02:00
|
|
|
|
|
|
|
# rows
|
|
|
|
self.rows = []
|
|
|
|
for r in it:
|
2015-05-14 19:53:01 +02:00
|
|
|
if isinstance(r, str):
|
|
|
|
r = r.decode('utf8')
|
|
|
|
r = r.rstrip('\n')
|
2015-05-14 17:55:08 +02:00
|
|
|
if not r:
|
|
|
|
break
|
|
|
|
rtup = r.split(u'\t')
|
|
|
|
rdict = {}
|
|
|
|
for ci in range(len(self.columns)):
|
|
|
|
rdict[self.columns[ci]] = rtup[ci]
|
|
|
|
self.rows.append(rdict)
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-15 00:19:40 +02:00
|
|
|
def parse(source, with_headers=True):
|
2015-05-14 17:55:08 +02:00
|
|
|
o = OvvpFile()
|
2015-05-15 00:19:40 +02:00
|
|
|
o.parse_from(source, with_headers=with_headers)
|
2015-05-14 17:55:08 +02:00
|
|
|
return o
|