Skip to content

Commit 78ed849

Browse files
committed
updated code, test and readme
1 parent daba016 commit 78ed849

File tree

3 files changed

+122
-5
lines changed

3 files changed

+122
-5
lines changed

README.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,61 @@ or clone it and install it::
2727
$ python setup.py install
2828

2929

30+
Usage
31+
=========
32+
33+
This small section outlines the steps to adapt **pyexcel-webio** for your favourite web framework. For illustration purpose, I took **Flask** micro-framework as an example.
34+
35+
1. Inherit **ExcelInput** class and implement **load_single_sheet** and **load_book** methods depending on the parameters you will have. For example, **Flask.Request** put the incoming file in **Flask.Request.files** and the key is the field name in the html form::
36+
37+
from flask import Flask, Request
38+
import pyexcel as pe
39+
from pyexcel.ext import webio
40+
41+
class ExcelRequest(webio.ExcelInput, Request):
42+
def _get_file_tuple(self, field_name):
43+
filehandle = self.files[field_name]
44+
filename = filehandle.filename
45+
extension = filename.split(".")[1]
46+
return extension, filehandle
47+
48+
def load_single_sheet(self, field_name=None, sheet_name=None, **keywords):
49+
file_type, file_handle = self._get_file_tuple(field_name)
50+
return pe.load_from_memory(file_type, file_handle.read(), sheet_name, **keywords)
51+
52+
def load_book(self, field_name=None, **keywords):
53+
file_type, file_handle = self._get_file_tuple(field_name)
54+
return pe.load_book_from_memory(file_type, file_handle.read(), **keywords)
55+
56+
2. Plugin in a response method that has the following signature::
57+
58+
def your_func(content, content_type=None, status=200):
59+
....
60+
61+
or a response class has the same signature::
62+
63+
class YourClass:
64+
def __init__(self, content, content_type=None, status=200):
65+
....
66+
67+
For example, with **Flask**, it is just a few lines::
68+
69+
from flask import Response
70+
71+
72+
webio.ExcelResponse = Response
73+
74+
75+
3. Then make the proxy for **make_response** series by simply copying the following lines to your extension::
76+
77+
from pyexcel.ext.webio import (
78+
make_response,
79+
make_response_from_array,
80+
make_response_from_dict,
81+
make_response_from_records,
82+
make_response_from_book_dict
83+
)
84+
3085
Dependencies
3186
============
3287

pyexcel_webio/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def get_records(self, sheet_name=None, name_columns_by_row=0, **keywords):
109109

110110
def save_to_database(self, session=None, table=None, sheet_name=None, name_columns_by_row=0, **keywords):
111111
sheet = self.load_single_sheet(sheet_name=sheet_name, name_columns_by_row=name_columns_by_row, **keywords)
112-
sheet.save_to_database(session, table)
112+
if sheet:
113+
sheet.save_to_database(session, table)
113114

114115
def get_book(self, **keywords):
115116
"""Get a instance of :class:`Book` from the file
@@ -135,7 +136,8 @@ def get_book_dict(self, **keywords):
135136

136137
def save_book_to_database(self, session, tables, **keywords):
137138
book = self.load_book(**keywords)
138-
book.save_to_database(session, tables)
139+
if book:
140+
book.save_to_database(session, tables)
139141

140142

141143
def dumpy_func(content, content_type=None, status=200):

tests/test_webio.py

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,80 @@
44
from pyexcel.ext import xls
55
from db import Session, Base, Signature, Signature2, engine
66
import sys
7-
87
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
98
from ordereddict import OrderedDict
109
else:
1110
from collections import OrderedDict
11+
from nose.tools import raises
12+
1213

1314
OUTPUT = "response_test.xls"
1415

1516

1617
class TestInput(webio.ExcelInput):
18+
"""This is sample implementation that read excel source from file"""
1719
def load_single_sheet(self, filename=None, sheet_name=None, **keywords):
20+
"""Load a single sheet"""
1821
return pe.get_sheet(file_name=filename, **keywords)
1922

2023
def load_book(self, filename=None, **keywords):
24+
"""Load a book"""
2125
return pe.get_book(file_name=filename, **keywords)
2226

27+
2328
def dumpy_response(content, content_type=None, status=200):
29+
"""A dummy response"""
2430
f = open(OUTPUT, 'wb')
2531
f.write(content)
2632
f.close()
2733

2834

2935
webio.ExcelResponse = dumpy_response
30-
31-
36+
37+
38+
class TestExceptions:
39+
@raises(NotImplementedError)
40+
def test_load_single_sheet(self):
41+
testinput = webio.ExcelInput()
42+
testinput.get_sheet(filename="test") # booom
43+
44+
@raises(NotImplementedError)
45+
def test_load_book(self):
46+
testinput = webio.ExcelInput()
47+
testinput.get_book(filename="test") # booom
48+
49+
def test_get_sheet(self):
50+
myinput = TestInput()
51+
sheet = myinput.get_sheet(unrelated="foo bar")
52+
assert sheet == None
53+
54+
def test_get_array(self):
55+
myinput = TestInput()
56+
array = myinput.get_array(unrelated="foo bar")
57+
assert array == None
58+
59+
def test_get_dict(self):
60+
myinput = TestInput()
61+
result = myinput.get_dict(unrelated="foo bar")
62+
assert result == None
63+
64+
def test_get_records(self):
65+
myinput = TestInput()
66+
result = myinput.get_records(unrelated="foo bar")
67+
assert result == None
68+
69+
def test_get_book(self):
70+
myinput = TestInput()
71+
result = myinput.get_book(unrelated="foo bar")
72+
assert result == None
73+
74+
def test_get_book_dict(self):
75+
myinput = TestInput()
76+
result = myinput.get_book_dict(unrelated="foo bar")
77+
assert result == None
78+
79+
# excel inputs
80+
3281
class TestExcelInput:
3382
def setUp(self):
3483
self.data = [
@@ -103,10 +152,21 @@ def test_get_book_dict(self):
103152
assert result["sheet1"] == self.data
104153
assert result["sheet2"] == self.data1
105154

155+
def test_save_to_database(self):
156+
Base.metadata.drop_all(engine)
157+
Base.metadata.create_all(engine)
158+
self.session = Session()
159+
myinput = TestInput()
160+
myinput.save_book_to_database(filename=self.testfile, session=self.session, tables=[Signature, Signature2])
161+
array = pe.get_array(session=self.session, table=Signature)
162+
assert array == self.data
163+
array = pe.get_array(session=self.session, table=Signature2)
164+
assert array == self.data1
106165

107166
def tearDown(self):
108167
os.unlink(self.testfile)
109168

169+
## responses
110170

111171
class TestResponse:
112172
def setUp(self):

0 commit comments

Comments
 (0)