Skip to content
Snippets Groups Projects
Commit 2718f769 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

up

parent 5d7ace23
No related branches found
No related tags found
No related merge requests found
...@@ -23,6 +23,11 @@ ...@@ -23,6 +23,11 @@
# ** end header # ** end header
# #
"""
This script tries to read typical table data files (.csv etc.) with pandas and
creates a html (partial) representation of the table.
"""
import logging import logging
import os import os
import sys import sys
...@@ -72,17 +77,19 @@ def ending_is_valid(fipath): ...@@ -72,17 +77,19 @@ def ending_is_valid(fipath):
return get_ending(fipath) is not None return get_ending(fipath) is not None
def read_file(fipath): def read_file(fipath, ftype):
""" tries to read the provided file """ """ tries to read the provided file """
ending = get_ending(fipath)
try: try:
if ending in [".xls", ".xlsx"]: if ftype in [".xls", ".xlsx"]:
df = pd.read_excel(fipath) df = pd.read_excel(fipath)
elif ending == ".tsv": elif ftype == ".tsv":
df = pd.read_csv(fipath, sep="\t") df = pd.read_csv(fipath, sep="\t")
elif ending == ".csv": elif ftype == ".csv":
df = pd.read_csv(fipath) df = pd.read_csv(fipath)
else:
print("File type unknown: {}".format(ftype))
raise RuntimeError("")
except Exception: except Exception:
raise ValueError() raise ValueError()
...@@ -90,28 +97,33 @@ def read_file(fipath): ...@@ -90,28 +97,33 @@ def read_file(fipath):
def create_table_preview(fi): def create_table_preview(fi):
if not ending_is_valid: if not ending_is_valid(fi.path):
print("Cannot create preview for Entity with ID={}, because download" print("Cannot create preview for Entity with ID={}, because download"
"failed.".format(entity_id)) "failed.".format(entity_id))
sys.exit(1)
return
ending = get_ending(fi.path)
if not size_is_ok(fi): if not size_is_ok(fi):
print("Skipped creating a preview for Entity with ID={}, because the" print("Skipped creating a preview for Entity with ID={}, because the"
"file is large!".format(entity_id)) "file is large!".format(entity_id))
sys.exit(2)
return
try: try:
fipath = fi.download() tmpfile = fi.download()
except Exception: except Exception:
print("Cannot create preview for Entity with ID={}, because download" print("Cannot create preview for Entity with ID={}, because download"
"failed.".format(entity_id)) "failed.".format(entity_id))
sys.exit(3)
return
try: try:
df = read_file(fipath) df = read_file(tmpfile, ending)
except ValueError: except ValueError:
print("Cannot read File Entity with ID={}.".format(entity_id)) print("Cannot read File Entity with ID={}.".format(entity_id))
sys.exit(4)
return
print(df.to_html(max_cols=10, max_rows=10)) print(df.to_html(max_cols=10, max_rows=10))
......
...@@ -60,9 +60,11 @@ class PreviewTest(unittest.TestCase): ...@@ -60,9 +60,11 @@ class PreviewTest(unittest.TestCase):
files = ["test.csv", "test.tsv", "test.xls", "test.xlsx"] files = ["test.csv", "test.tsv", "test.xls", "test.xlsx"]
for fi in files: for fi in files:
assert fi.split(".")[1]+"file" in read_file(fi) assert fi.split(".")[1]+"file" in read_file(
fi, ftype="."+fi.split(".")[1])
badfiles = ["bad.csv", "bad.tsv", "bad.xls", "bad.xlsx"] badfiles = ["bad.csv", "bad.tsv", "bad.xls", "bad.xlsx"]
for bfi in badfiles: for bfi in badfiles:
self.assertRaises(ValueError, read_file, bfi) self.assertRaises(ValueError, read_file,
bfi, "."+bfi.split(".")[1])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment