@@ -40,19 +40,20 @@ class ExcelInput(object):
40
40
def load_single_sheet (self , sheet_name = None , ** keywords ):
41
41
"""Abstract method
42
42
43
- :param form_field_name: the file field name in the html form for file upload
44
- :param sheet_name: For an excel book, there could be multiple sheets. If it is left
45
- unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
46
- *sheet_name* should be None anyway.
43
+ :param sheet_name: For an excel book, there could be multiple
44
+ sheets. If it is left unspecified, the
45
+ sheet at index 0 is loaded. For 'csv', 'tsv'
46
+ file, *sheet_name* should be None anyway.
47
47
:param keywords: additional key words
48
48
:returns: A sheet object
49
49
"""
50
50
raise NotImplementedError ("Please implement this function" )
51
51
52
52
def load_book (self , ** keywords ):
53
53
"""Abstract method
54
-
55
- :param form_field_name: the file field name in the html form for file upload
54
+
55
+ :param form_field_name: the file field name in the html
56
+ form for file upload
56
57
:param keywords: additional key words
57
58
:returns: A instance of :class:`Book`
58
59
"""
@@ -61,24 +62,24 @@ def load_book(self, **keywords):
61
62
def get_sheet (self , sheet_name = None , ** keywords ):
62
63
"""
63
64
Get a :class:`Sheet` instance 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.
65
+
66
+ :param sheet_name: For an excel book, there could be multiple
67
+ sheets. If it is left unspecified, the
68
+ sheet at index 0 is loaded. For 'csv',
69
+ 'tsv' file, *sheet_name* should be None anyway.
69
70
:param keywords: additional key words
70
71
:returns: A sheet object
71
72
"""
72
73
return self .load_single_sheet (sheet_name = sheet_name , ** keywords )
73
-
74
+
74
75
def get_array (self , sheet_name = None , ** keywords ):
75
76
"""
76
77
Get a list of lists 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.
78
+
79
+ :param sheet_name: For an excel book, there could be multiple
80
+ sheets. If it is left unspecified, the
81
+ sheet at index 0 is loaded. For 'csv',
82
+ 'tsv' file, *sheet_name* should be None anyway.
82
83
:param keywords: additional key words
83
84
:returns: A list of lists
84
85
"""
@@ -90,31 +91,37 @@ def get_array(self, sheet_name=None, **keywords):
90
91
91
92
def get_dict (self , sheet_name = None , name_columns_by_row = 0 , ** keywords ):
92
93
"""Get a dictionary from the file
93
-
94
- :param form_field_name: the file field name in the html form for file upload
95
- :param sheet_name: For an excel book, there could be multiple sheets. If it is left
96
- unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file ,
97
- *sheet_name* should be None anyway.
94
+
95
+ :param sheet_name: For an excel book, there could be multiple
96
+ sheets. If it is left unspecified, the
97
+ sheet at index 0 is loaded. For 'csv',
98
+ 'tsv' file, *sheet_name* should be None anyway.
98
99
:param keywords: additional key words
99
100
:returns: A dictionary
100
101
"""
101
- sheet = self .load_single_sheet (sheet_name = sheet_name , name_columns_by_row = name_columns_by_row , ** keywords )
102
+ sheet = self .load_single_sheet (
103
+ sheet_name = sheet_name ,
104
+ name_columns_by_row = name_columns_by_row ,
105
+ ** keywords )
102
106
if sheet :
103
107
return sheet .to_dict ()
104
108
else :
105
109
return None
106
110
107
111
def get_records (self , sheet_name = None , name_columns_by_row = 0 , ** keywords ):
108
112
"""Get a list of records from the file
109
-
110
- :param form_field_name: the file field name in the html form for file upload
111
- :param sheet_name: For an excel book, there could be multiple sheets. If it is left
112
- unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file ,
113
- *sheet_name* should be None anyway.
113
+
114
+ :param sheet_name: For an excel book, there could be multiple
115
+ sheets. If it is left unspecified, the
116
+ sheet at index 0 is loaded. For 'csv',
117
+ 'tsv' file, *sheet_name* should be None anyway.
114
118
:param keywords: additional key words
115
119
:returns: A list of records
116
120
"""
117
- sheet = self .load_single_sheet (sheet_name = sheet_name , name_columns_by_row = name_columns_by_row , ** keywords )
121
+ sheet = self .load_single_sheet (
122
+ sheet_name = sheet_name ,
123
+ name_columns_by_row = name_columns_by_row ,
124
+ ** keywords )
118
125
if sheet :
119
126
return sheet .to_records ()
120
127
else :
@@ -142,7 +149,6 @@ def save_to_database(
142
149
def get_book (self , ** keywords ):
143
150
"""Get a instance of :class:`Book` from the file
144
151
145
- :param form_field_name: the file field name in the html form for file upload
146
152
:param keywords: additional key words
147
153
:returns: A instance of :class:`Book`
148
154
"""
@@ -151,7 +157,6 @@ def get_book(self, **keywords):
151
157
def get_book_dict (self , ** keywords ):
152
158
"""Get a dictionary of two dimensional array from the file
153
159
154
- :param form_field_name: the file field name in the html form for file upload
155
160
:param keywords: additional key words
156
161
:returns: A dictionary of two dimensional arrays
157
162
"""
@@ -161,10 +166,11 @@ def get_book_dict(self, **keywords):
161
166
else :
162
167
return None
163
168
164
- def save_book_to_database (self ,
165
- session = None , tables = None ,
166
- initializers = None , mapdicts = None , auto_commit = True ,
167
- ** keywords ):
169
+ def save_book_to_database (
170
+ self ,
171
+ session = None , tables = None ,
172
+ initializers = None , mapdicts = None , auto_commit = True ,
173
+ ** keywords ):
168
174
book = self .load_book (** keywords )
169
175
if book :
170
176
book .save_to_database (session ,
@@ -174,7 +180,6 @@ def save_book_to_database(self,
174
180
auto_commit = auto_commit )
175
181
176
182
177
-
178
183
class ExcelInputInMultiDict (ExcelInput ):
179
184
""" A generic interface for an upload excel file appearing in a dictionary
180
185
"""
@@ -193,7 +198,7 @@ def load_single_sheet(self, field_name=None, sheet_name=None, **keywords):
193
198
194
199
def load_book (self , field_name = None , ** keywords ):
195
200
file_type , file_handle = self .get_file_tuple (field_name )
196
- if file_type is not None and file_handle is not None :
201
+ if file_type is not None and file_handle is not None :
197
202
return pe .get_book (file_type = file_type ,
198
203
file_content = file_handle .read (),
199
204
** keywords )
@@ -209,11 +214,13 @@ def dummy_func(content, content_type=None, status=200):
209
214
210
215
211
216
def make_response (pyexcel_instance , file_type , status = 200 , ** keywords ):
212
- """Make a http response from a pyexcel instance of :class:`~pyexcel.Sheet` or :class:`~pyexcel.Book`
217
+ """
218
+ Make a http response from a pyexcel instance of
219
+ :class:`~pyexcel.Sheet` or :class:`~pyexcel.Book`
213
220
214
221
:param pyexcel_instance: pyexcel.Sheet or pyexcel.Book
215
222
:param file_type: one of the following strings:
216
-
223
+
217
224
* 'csv'
218
225
* 'tsv'
219
226
* 'csvz'
@@ -222,41 +229,60 @@ def make_response(pyexcel_instance, file_type, status=200, **keywords):
222
229
* 'xlsx'
223
230
* 'xlsm'
224
231
* 'ods'
225
-
232
+
226
233
:param status: unless a different status is to be returned.
227
234
"""
228
235
io = pe .get_io (file_type )
229
236
pyexcel_instance .save_to_memory (file_type , io , ** keywords )
230
237
io .seek (0 )
231
- return ExcelResponse (io .read (), content_type = FILE_TYPE_MIME_TABLE [file_type ], status = status )
238
+ return ExcelResponse (io .read (),
239
+ content_type = FILE_TYPE_MIME_TABLE [file_type ],
240
+ status = status )
232
241
233
242
234
- def make_response_from_array (array , file_type , status = 200 , ** keywords ):
235
- return make_response (pe .Sheet (array ), file_type , status , ** keywords )
243
+ def make_response_from_array (array ,
244
+ file_type , status = 200 ,
245
+ ** keywords ):
246
+ return make_response (pe .Sheet (array ),
247
+ file_type , status , ** keywords )
236
248
237
-
238
- def make_response_from_dict (adict , file_type , status = 200 , ** keywords ):
239
- return make_response (pe .load_from_dict (adict ), file_type , status , ** keywords )
240
249
250
+ def make_response_from_dict (adict ,
251
+ file_type , status = 200 ,
252
+ ** keywords ):
253
+ return make_response (pe .load_from_dict (adict ),
254
+ file_type , status , ** keywords )
241
255
242
- def make_response_from_records (records , file_type , status = 200 , ** keywords ):
243
- return make_response (pe .load_from_records (records ), file_type , status , ** keywords )
244
256
257
+ def make_response_from_records (records ,
258
+ file_type , status = 200 ,
259
+ ** keywords ):
260
+ return make_response (pe .load_from_records (records ),
261
+ file_type , status , ** keywords )
245
262
246
- def make_response_from_book_dict (adict , file_type , status = 200 , ** keywords ):
263
+
264
+ def make_response_from_book_dict (adict ,
265
+ file_type , status = 200 ,
266
+ ** keywords ):
247
267
return make_response (pe .Book (adict ), file_type , status , ** keywords )
248
268
249
269
250
- def make_response_from_query_sets (query_sets , column_names , file_type , status = 200 , ** keywords ):
270
+ def make_response_from_query_sets (query_sets , column_names ,
271
+ file_type , status = 200 ,
272
+ ** keywords ):
251
273
sheet = pe .get_sheet (query_sets = query_sets , column_names = column_names )
252
274
return make_response (sheet , file_type , status , ** keywords )
253
275
254
276
255
- def make_response_from_a_table (session , table , file_type , status = 200 , ** keywords ):
277
+ def make_response_from_a_table (session , table ,
278
+ file_type , status = 200 ,
279
+ ** keywords ):
256
280
sheet = pe .get_sheet (session = session , table = table , ** keywords )
257
281
return make_response (sheet , file_type , status , ** keywords )
258
282
259
283
260
- def make_response_from_tables (session , tables , file_type , status = 200 , ** keywords ):
284
+ def make_response_from_tables (session , tables ,
285
+ file_type , status = 200 ,
286
+ ** keywords ):
261
287
book = pe .get_book (session = session , tables = tables , ** keywords )
262
288
return make_response (book , file_type , status , ** keywords )
0 commit comments