Pavel "LEdoian" Turinsky
3 years ago
1 changed files with 64 additions and 64 deletions
@ -1,81 +1,81 @@ |
|||||
# -*- coding: utf-8 -*- |
# -*- coding: utf-8 -*- |
||||
|
|
||||
try: |
try: |
||||
from django.http import HttpResponse |
from django.http import HttpResponse |
||||
from django.utils.encoding import force_text |
from django.utils.encoding import force_text |
||||
except: |
except: |
||||
force_text = str |
force_text = str |
||||
|
|
||||
|
|
||||
class OvvpFile(object): |
class OvvpFile(object): |
||||
def __init__(self): |
def __init__(self): |
||||
# { header: value, ... } |
# { header: value, ... } |
||||
self.headers = {} |
self.headers = {} |
||||
# [ 'column-name', ... ] |
# [ 'column-name', ... ] |
||||
self.columns = [] |
self.columns = [] |
||||
# [ { column: value, ...}, ...] |
# [ { column: value, ...}, ...] |
||||
self.rows = [] |
self.rows = [] |
||||
|
|
||||
def to_lines(self): |
def to_lines(self): |
||||
# header |
# header |
||||
for hk in sorted(self.headers.keys()): |
for hk in sorted(self.headers.keys()): |
||||
yield '%s\t%s\n' % (hk, self.headers[hk]) |
yield '%s\t%s\n' % (hk, self.headers[hk]) |
||||
yield '\n' |
yield '\n' |
||||
# columns |
# columns |
||||
yield '\t'.join([c for c in self.columns]) + '\n' |
yield '\t'.join([c for c in self.columns]) + '\n' |
||||
# rows |
# rows |
||||
for r in self.rows: |
for r in self.rows: |
||||
yield '\t'.join([force_text(r[c]) for c in self.columns]) + '\n' |
yield '\t'.join([force_text(r[c]) for c in self.columns]) + '\n' |
||||
|
|
||||
def to_string(self): |
def to_string(self): |
||||
return ''.join(self.to_lines()) |
return ''.join(self.to_lines()) |
||||
|
|
||||
def to_HttpResponse(self): |
def to_HttpResponse(self): |
||||
return HttpResponse(self.to_string(), content_type='text/plain; charset=utf-8') |
return HttpResponse(self.to_string(), content_type='text/plain; charset=utf-8') |
||||
|
|
||||
def parse_from(self, source, with_headers=True): |
def parse_from(self, source, with_headers=True): |
||||
"Parse data from file, string or line iterator, overwriting self" |
"Parse data from file, string or line iterator, overwriting self" |
||||
if isinstance(source, str) or isinstance(source, unicode): |
if isinstance(source, str) or isinstance(source, unicode): |
||||
return self.parse_from(source.split('\n')) |
return self.parse_from(source.split('\n')) |
||||
|
|
||||
it = iter(source) |
it = iter(source) |
||||
|
|
||||
# header |
# header |
||||
self.headers = {} |
self.headers = {} |
||||
if with_headers: |
if with_headers: |
||||
for r in it: |
for r in it: |
||||
if isinstance(r, str): |
if isinstance(r, str): |
||||
r = r.decode('utf8') |
r = r.decode('utf8') |
||||
assert isinstance(r, unicode) |
assert isinstance(r, unicode) |
||||
r = r.rstrip('\n') |
r = r.rstrip('\n') |
||||
if r == u"": |
if r == u"": |
||||
break |
break |
||||
k, v = r.split(u'\t', 1) |
k, v = r.split(u'\t', 1) |
||||
self.headers[k] = v |
self.headers[k] = v |
||||
|
|
||||
# columns |
# columns |
||||
r = it.next() |
r = it.next() |
||||
if isinstance(r, str): |
if isinstance(r, str): |
||||
r = r.decode('utf8') |
r = r.decode('utf8') |
||||
self.columns = [cn.strip() for cn in r.split(u'\t') if cn.strip() != ""] |
self.columns = [cn.strip() for cn in r.split(u'\t') if cn.strip() != ""] |
||||
|
|
||||
# rows |
# rows |
||||
self.rows = [] |
self.rows = [] |
||||
for r in it: |
for r in it: |
||||
if isinstance(r, str): |
if isinstance(r, str): |
||||
r = r.decode('utf8') |
r = r.decode('utf8') |
||||
r = r.rstrip('\n') |
r = r.rstrip('\n') |
||||
if not r: |
if not r: |
||||
break |
break |
||||
rtup = r.split(u'\t') |
rtup = r.split(u'\t') |
||||
rdict = {} |
rdict = {} |
||||
for ci in range(len(self.columns)): |
for ci in range(len(self.columns)): |
||||
rdict[self.columns[ci]] = rtup[ci] |
rdict[self.columns[ci]] = rtup[ci] |
||||
self.rows.append(rdict) |
self.rows.append(rdict) |
||||
|
|
||||
|
|
||||
|
|
||||
def parse(source, with_headers=True): |
def parse(source, with_headers=True): |
||||
o = OvvpFile() |
o = OvvpFile() |
||||
o.parse_from(source, with_headers=with_headers) |
o.parse_from(source, with_headers=with_headers) |
||||
return o |
return o |
||||
|
Loading…
Reference in new issue