Skip to content

Commit d58479b

Browse files
committed
initial commit
0 parents  commit d58479b

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed

README.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
==============
2+
pyexcel-webio
3+
==============
4+
5+
.. image:: https://api.travis-ci.org/chfw/pyexcel-webio.png
6+
:target: http://travis-ci.org/chfw/pyexcel-webio
7+
8+
.. image:: https://coveralls.io/repos/chfw/pyexcel-webio/badge.png?branch=master
9+
:target: https://coveralls.io/r/chfw/pyexcel-webio?branch=master
10+
11+
12+
**pyexcel-webio** is a tiny interface library to unify the web extensions that uses `pyexcel <https://github.com/chfw/pyexcel>`__ . You may use it to write a web extension for your faviourite Python web framework.
13+
14+
15+
Installation
16+
============
17+
18+
You can install it via pip::
19+
20+
$ pip install pyexcel-webio
21+
22+
23+
or clone it and install it::
24+
25+
$ git clone http://github.com/chfw/pyexcel-webio.git
26+
$ cd pyexcel-webio
27+
$ python setup.py install
28+
29+
30+
Dependencies
31+
============
32+

pyexcel_webio/__init__.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import pyexcel as pe
2+
import sys
3+
PY2 = sys.version_info[0] == 2
4+
if PY2:
5+
from StringIO import StringIO as BytesIO
6+
else:
7+
from io import BytesIO
8+
9+
10+
FILE_TYPE_MIME_TABLE = {
11+
"csv": "text/csv",
12+
"tsv": "text/tab-separated-values",
13+
"csvz": "application/zip",
14+
"tsvz": "application/zip",
15+
"ods": "application/vnd.oasis.opendocument.spreadsheet",
16+
"xls": "application/vnd.ms-excel",
17+
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
18+
"xlsm": "application/vnd.ms-excel.sheet.macroenabled.12"
19+
}
20+
21+
22+
class ExcelInput(object):
23+
"""A generic interface for an excel file to be converted
24+
25+
The source could be from anywhere, memory or file system
26+
"""
27+
def load_single_sheet(self, file_name, sheet_name=None, **keywords):
28+
"""Abstract method
29+
30+
:param form_field_name: the file field name in the html form for file upload
31+
:param sheet_name: For an excel book, there could be multiple sheets. If it is left
32+
unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
33+
*sheet_name* should be None anyway.
34+
:param keywords: additional key words
35+
:returns: A sheet object
36+
"""
37+
raise NotImplementedError("Please implement this function")
38+
39+
def load_book(self, file_name, **keywords):
40+
"""Abstract method
41+
42+
:param form_field_name: the file field name in the html form for file upload
43+
:param keywords: additional key words
44+
:returns: A instance of :class:`Book`
45+
"""
46+
raise NotImplementedError("Please implement this function")
47+
48+
def get_sheet(self, file_name, sheet_name=None, **keywords):
49+
"""
50+
Get a :class:`Sheet` instance from the file
51+
52+
:param form_field_name: the file field name in the html form for file upload
53+
:param sheet_name: For an excel book, there could be multiple sheets. If it is left
54+
unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
55+
*sheet_name* should be None anyway.
56+
:param keywords: additional key words
57+
:returns: A sheet object
58+
"""
59+
return self.load_single_sheet(file_name, sheet_name, **keywords)
60+
61+
def get_array(self, file_name, sheet_name=None, **keywords):
62+
"""
63+
Get a list of lists from the file
64+
65+
:param form_field_name: the file field name in the html form for file upload
66+
:param sheet_name: For an excel book, there could be multiple sheets. If it is left
67+
unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
68+
*sheet_name* should be None anyway.
69+
:param keywords: additional key words
70+
:returns: A list of lists
71+
"""
72+
sheet = self.get_sheet(file_name, sheet_name, **keywords)
73+
return sheet.to_array()
74+
75+
def get_dict(self, file_name, sheet_name=None, name_columns_by_row=0, name_rows_by_column=-1, **keywords):
76+
"""Get a dictionary from the file
77+
78+
:param form_field_name: the file field name in the html form for file upload
79+
:param sheet_name: For an excel book, there could be multiple sheets. If it is left
80+
unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
81+
*sheet_name* should be None anyway.
82+
:param keywords: additional key words
83+
:returns: A dictionary
84+
"""
85+
sheet = self.load_single_sheet(file_name, sheet_name,
86+
name_columns_by_row=name_columns_by_row,
87+
name_rows_by_column=name_rows_by_column, **keywords)
88+
return sheet.to_dict()
89+
90+
def get_records(self, file_name, sheet_name=None, name_columns_by_row=0, name_rows_by_column=-1, **keywords):
91+
"""Get a list of records from the file
92+
93+
:param form_field_name: the file field name in the html form for file upload
94+
:param sheet_name: For an excel book, there could be multiple sheets. If it is left
95+
unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
96+
*sheet_name* should be None anyway.
97+
:param keywords: additional key words
98+
:returns: A list of records
99+
"""
100+
sheet = self.load_single_sheet(file_name, sheet_name,
101+
name_columns_by_row=name_columns_by_row,
102+
name_rows_by_column=name_rows_by_column, **keywords)
103+
return sheet.to_records()
104+
105+
def get_book(self, file_name, **keywords):
106+
"""Get a instance of :class:`Book` from the file
107+
:param form_field_name: the file field name in the html form for file upload
108+
:param keywords: additional key words
109+
:returns: A instance of :class:`Book`
110+
"""
111+
return self.load_book(file_name, **keywords)
112+
113+
def get_book_dict(self, file_name, **keywords):
114+
"""Get a dictionary of two dimensional array from the file
115+
116+
:param form_field_name: the file field name in the html form for file upload
117+
:param keywords: additional key words
118+
:returns: A dictionary of two dimensional arrays
119+
"""
120+
book = self.get_book(file_name, **keywords)
121+
return book.to_dict()
122+
123+
def dumpy_func(content, content_type=None, status=200):
124+
return None
125+
126+
ExcelResponse = dumpy_func
127+
128+
def make_response(pyexcel_instance, file_type, status=200):
129+
io = BytesIO()
130+
pyexcel_instance.save_to_memory(file_type, io)
131+
io.seek(0)
132+
return ExcelResponse(io.read(), content_type=FILE_TYPE_MIME_TABLE[file_type], status=status)
133+
134+
135+
def make_response_from_array(array, file_type, status=200):
136+
return make_response(pe.Sheet(array), file_type, status)
137+
138+
139+
def make_response_from_dict(adict, file_type, status=200):
140+
return make_response(pe.load_from_dict(adict), file_type, status)
141+
142+
143+
def make_response_from_records(records, file_type, status=200):
144+
return make_response(pe.load_from_records(records), file_type, status)
145+
146+
147+
def make_response_from_book_dict(adict, file_type, status=200):
148+
return make_response(pe.Book(adict), file_type, status)

pyexcel_webio/__init__.py~

Whitespace-only changes.

setup.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
try:
2+
from setuptools import setup, find_packages
3+
except ImportError:
4+
from ez_setup import use_setuptools
5+
use_setuptools()
6+
from setuptools import setup, find_packages
7+
8+
with open("README.rst", 'r') as readme:
9+
README_txt = readme.read()
10+
11+
dependencies = [
12+
'pyexcel>=0.1.2'
13+
]
14+
15+
setup(
16+
name='pyexcel-webio',
17+
author="C. W.",
18+
version='0.0.1',
19+
author_email="wangc_2011@hotmail.com",
20+
url="https://github.com/chfw/flask-pyexcel",
21+
description='A generic web extension for pyexcel.',
22+
install_requires=dependencies,
23+
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
24+
include_package_data=True,
25+
long_description=README_txt,
26+
zip_safe=False,
27+
tests_require=['nose'],
28+
keywords=['API', 'pyexcel', 'Excel', 'HTTP'],
29+
license='GNU GPLv3 or BSD',
30+
classifiers=[
31+
'Development Status :: 3 - Alpha',
32+
'Environment :: Web Environment',
33+
'Topic :: Internet :: WWW/HTTP',
34+
'Topic :: Software Development :: Libraries :: Python Modules',
35+
'Programming Language :: Python',
36+
'License :: OSI Approved :: BSD License',
37+
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
38+
'Intended Audience :: Developers',
39+
'Programming Language :: Python :: 2',
40+
'Programming Language :: Python :: 2.6',
41+
'Programming Language :: Python :: 2.7',
42+
'Programming Language :: Python :: 3',
43+
'Programming Language :: Python :: 3.3',
44+
'Programming Language :: Python :: 3.4',
45+
'Programming Language :: Python :: Implementation :: PyPy'
46+
]
47+
)

0 commit comments

Comments
 (0)