33
33
34
34
35
35
class ExcelInput (object ):
36
- """A generic interface for an excel file to be converted
36
+ """A generic interface for an excel file input
37
37
38
38
The source could be from anywhere, memory or file system
39
39
"""
@@ -133,6 +133,15 @@ def save_to_database(
133
133
auto_commit = True ,
134
134
sheet_name = None , name_columns_by_row = 0 , name_rows_by_column = - 1 ,
135
135
field_name = None , ** keywords ):
136
+ """
137
+ Save data from a sheet to database
138
+
139
+ :param session: a SQLAlchemy session
140
+ :param table: a database table
141
+ :param initializer: a custom table initialization function if you have one
142
+ :param mapdict: the explicit table column names if your excel data do not have the exact column names
143
+ :param keywords: additional keywords to :meth:`pyexcel.Sheet.save_to_database`
144
+ """
136
145
sheet = self .load_single_sheet (
137
146
field_name = field_name ,
138
147
sheet_name = sheet_name ,
@@ -171,6 +180,16 @@ def save_book_to_database(
171
180
session = None , tables = None ,
172
181
initializers = None , mapdicts = None , auto_commit = True ,
173
182
** keywords ):
183
+ """
184
+ Save a book into database
185
+
186
+ :param session: a SQLAlchemy session
187
+ :param tables: a list of database tables
188
+ :param initializers: a list of model initialization functions.
189
+ :param mapdicts: a list of explicit table column names if your excel data sheets do not have the exact column names
190
+ :param keywords: additional keywords to :meth:`pyexcel.Book.save_to_database`
191
+
192
+ """
174
193
book = self .load_book (** keywords )
175
194
if book :
176
195
book .save_to_database (session ,
@@ -184,9 +203,18 @@ class ExcelInputInMultiDict(ExcelInput):
184
203
""" A generic interface for an upload excel file appearing in a dictionary
185
204
"""
186
205
def get_file_tuple (self , field_name ):
206
+ """
207
+ Abstract method to get the file tuple
208
+
209
+ It is expected to return file type and a file handle to the
210
+ uploaded file
211
+ """
187
212
raise NotImplementedError ("Please implement this function" )
188
213
189
214
def load_single_sheet (self , field_name = None , sheet_name = None , ** keywords ):
215
+ """
216
+ Load the single sheet from named form field
217
+ """
190
218
file_type , file_handle = self .get_file_tuple (field_name )
191
219
if file_type is not None and file_handle is not None :
192
220
return pe .get_sheet (file_type = file_type ,
@@ -197,6 +225,9 @@ def load_single_sheet(self, field_name=None, sheet_name=None, **keywords):
197
225
return None
198
226
199
227
def load_book (self , field_name = None , ** keywords ):
228
+ """
229
+ Load the book from named form field
230
+ """
200
231
file_type , file_handle = self .get_file_tuple (field_name )
201
232
if file_type is not None and file_handle is not None :
202
233
return pe .get_book (file_type = file_type ,
@@ -231,6 +262,7 @@ def make_response(pyexcel_instance, file_type, status=200, **keywords):
231
262
* 'ods'
232
263
233
264
:param status: unless a different status is to be returned.
265
+ :returns: http response
234
266
"""
235
267
io = pe .get_io (file_type )
236
268
pyexcel_instance .save_to_memory (file_type , io , ** keywords )
@@ -243,46 +275,108 @@ def make_response(pyexcel_instance, file_type, status=200, **keywords):
243
275
def make_response_from_array (array ,
244
276
file_type , status = 200 ,
245
277
** keywords ):
278
+ """
279
+ Make a http response from an array
280
+
281
+ :param array: a list of lists
282
+ :param file_type: same as :meth:`~pyexcel_webio.make_response`
283
+ :param status: same as :meth:`~pyexcel_webio.make_response`
284
+ :returns: http response
285
+ """
246
286
return make_response (pe .Sheet (array ),
247
287
file_type , status , ** keywords )
248
288
249
289
250
290
def make_response_from_dict (adict ,
251
291
file_type , status = 200 ,
252
292
** keywords ):
293
+ """
294
+ Make a http response from a dictionary of lists
295
+
296
+ :param dict: a dictinary of lists
297
+ :param file_type: same as :meth:`~pyexcel_webio.make_response`
298
+ :param status: same as :meth:`~pyexcel_webio.make_response`
299
+ :returns: http response
300
+ """
253
301
return make_response (pe .load_from_dict (adict ),
254
302
file_type , status , ** keywords )
255
303
256
304
257
305
def make_response_from_records (records ,
258
306
file_type , status = 200 ,
259
307
** keywords ):
308
+ """
309
+ Make a http response from a list of dictionaries
310
+
311
+ :param records: a list of dictionaries
312
+ :param file_type: same as :meth:`~pyexcel_webio.make_response`
313
+ :param status: same as :meth:`~pyexcel_webio.make_response`
314
+ :returns: http response
315
+ """
260
316
return make_response (pe .load_from_records (records ),
261
317
file_type , status , ** keywords )
262
318
263
319
264
320
def make_response_from_book_dict (adict ,
265
321
file_type , status = 200 ,
266
322
** keywords ):
323
+ """
324
+ Make a http response from a dictionary of two dimensional
325
+ arrays
326
+
327
+ :param book_dict: a dictionary of two dimensional arrays
328
+ :param file_type: same as :meth:`~pyexcel_webio.make_response`
329
+ :param status: same as :meth:`~pyexcel_webio.make_response`
330
+ :returns: http response
331
+ """
267
332
return make_response (pe .Book (adict ), file_type , status , ** keywords )
268
333
269
334
270
335
def make_response_from_query_sets (query_sets , column_names ,
271
336
file_type , status = 200 ,
272
337
** keywords ):
338
+ """
339
+ Make a http response from a dictionary of two dimensional
340
+ arrays
341
+
342
+ :param query_sets: a query set
343
+ :param column_names: a nominated column names. It could not be N
344
+ one, otherwise no data is returned.
345
+ :param file_type: same as :meth:`~pyexcel_webio.make_response`
346
+ :param status: same as :meth:`~pyexcel_webio.make_response`
347
+ :returns: a http response
348
+ """
273
349
sheet = pe .get_sheet (query_sets = query_sets , column_names = column_names )
274
350
return make_response (sheet , file_type , status , ** keywords )
275
351
276
352
277
353
def make_response_from_a_table (session , table ,
278
354
file_type , status = 200 ,
279
355
** keywords ):
356
+ """
357
+ Make a http response from sqlalchmey table
358
+
359
+ :param session: SQLAlchemy session
360
+ :param table: a SQLAlchemy table
361
+ :param file_type: same as :meth:`~pyexcel_webio.make_response`
362
+ :param status: same as :meth:`~pyexcel_webio.make_response`
363
+ :returns: a http response
364
+ """
280
365
sheet = pe .get_sheet (session = session , table = table , ** keywords )
281
366
return make_response (sheet , file_type , status , ** keywords )
282
367
283
368
284
369
def make_response_from_tables (session , tables ,
285
370
file_type , status = 200 ,
286
371
** keywords ):
372
+ """
373
+ Make a http response from sqlalchmy tables
374
+
375
+ :param session: SQLAlchemy session
376
+ :param tables: SQLAlchemy tables
377
+ :param file_type: same as :meth:`~pyexcel_webio.make_response`
378
+ :param status: same as :meth:`~pyexcel_webio.make_response`
379
+ :returns: a http response
380
+ """
287
381
book = pe .get_book (session = session , tables = tables , ** keywords )
288
382
return make_response (book , file_type , status , ** keywords )
0 commit comments