Ovvpfile: protřídění mrtvého kódu
Jestli jsem to rozbil, tak to revertněte. Ale myslím, že jsem fakt smazal jen kód, který nemohl jít spustit…
This commit is contained in:
parent
5e07412d92
commit
0f381a7529
1 changed files with 4 additions and 55 deletions
|
@ -1,13 +1,8 @@
|
||||||
# -*- coding: utf-8 -*-
|
from django.http import HttpResponse
|
||||||
|
from django.utils.encoding import force_text
|
||||||
try:
|
|
||||||
from django.http import HttpResponse
|
|
||||||
from django.utils.encoding import force_text
|
|
||||||
except:
|
|
||||||
force_text = str
|
|
||||||
|
|
||||||
|
|
||||||
class OvvpFile(object):
|
class OvvpFile:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# { header: value, ... }
|
# { header: value, ... }
|
||||||
self.headers = {}
|
self.headers = {}
|
||||||
|
@ -30,52 +25,6 @@ class OvvpFile(object):
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return ''.join(self.to_lines())
|
return ''.join(self.to_lines())
|
||||||
|
|
||||||
|
# Pozn: tohle je ta jediná funkce, která se reálně používá…
|
||||||
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):
|
|
||||||
"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 = {}
|
|
||||||
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
|
|
||||||
|
|
||||||
# columns
|
|
||||||
r = it.next()
|
|
||||||
if isinstance(r, str):
|
|
||||||
r = r.decode('utf8')
|
|
||||||
self.columns = [cn.strip() for cn in r.split(u'\t') if cn.strip() != ""]
|
|
||||||
|
|
||||||
# rows
|
|
||||||
self.rows = []
|
|
||||||
for r in it:
|
|
||||||
if isinstance(r, str):
|
|
||||||
r = r.decode('utf8')
|
|
||||||
r = r.rstrip('\n')
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def parse(source, with_headers=True):
|
|
||||||
o = OvvpFile()
|
|
||||||
o.parse_from(source, with_headers=with_headers)
|
|
||||||
return o
|
|
||||||
|
|
Loading…
Reference in a new issue