@@ -204,7 +204,7 @@ def load_csv(csv_filepath, resource_id, mimetype='text/csv', logger=None):
204
204
if f ['id' ] in existing_info :
205
205
f ['info' ] = existing_info [f ['id' ]]
206
206
f ['strip_extra_white' ] = existing_info [f ['id' ]].get ('strip_extra_white' ) if 'strip_extra_white' in existing_info [f ['id' ]] \
207
- else existing_fields_by_headers [f ['id' ]].get ('strip_extra_white' , True )
207
+ else existing_fields_by_headers [f ['id' ]].get ('strip_extra_white' , True )
208
208
209
209
'''
210
210
Delete or truncate existing datastore table before proceeding,
@@ -222,39 +222,32 @@ def load_csv(csv_filepath, resource_id, mimetype='text/csv', logger=None):
222
222
fields = [
223
223
{'id' : header_name ,
224
224
'type' : 'text' ,
225
- 'strip_extra_white' : True , }
225
+ 'strip_extra_white' : True }
226
226
for header_name in headers ]
227
227
228
228
logger .info ('Fields: %s' , fields )
229
229
230
+ def _make_whitespace_stripping_iter (super_iter ):
231
+ def strip_white_space_iter ():
232
+ for row in super_iter ():
233
+ if len (row ) == len (fields ):
234
+ for _index , _cell in enumerate (row ):
235
+ # only strip white space if strip_extra_white is True
236
+ if fields [_index ].get ('strip_extra_white' , True ) and isinstance (_cell , str ):
237
+ row [_index ] = _cell .strip ()
238
+ yield row
239
+ return strip_white_space_iter
240
+
230
241
save_args = {'target' : f_write .name , 'format' : 'csv' , 'encoding' : 'utf-8' , 'delimiter' : delimiter }
231
242
try :
232
243
with UnknownEncodingStream (csv_filepath , file_format , decoding_result ,
233
244
skip_rows = skip_rows ) as stream :
234
- super_iter = stream .iter
235
- def strip_white_space_iter ():
236
- for row in super_iter ():
237
- if len (row ) == len (fields ):
238
- for _index , _cell in enumerate (row ):
239
- # only strip white space if strip_extra_white is True
240
- if fields [_index ].get ('strip_extra_white' , True ) and isinstance (_cell , str ):
241
- row [_index ] = _cell .strip ()
242
- yield row
243
- stream .iter = strip_white_space_iter
245
+ stream .iter = _make_whitespace_stripping_iter (stream .iter )
244
246
stream .save (** save_args )
245
247
except (EncodingError , UnicodeDecodeError ):
246
248
with Stream (csv_filepath , format = file_format , encoding = SINGLE_BYTE_ENCODING ,
247
249
skip_rows = skip_rows ) as stream :
248
- super_iter = stream .iter
249
- def strip_white_space_iter ():
250
- for row in super_iter ():
251
- if len (row ) == len (fields ):
252
- for _index , _cell in enumerate (row ):
253
- # only strip white space if strip_extra_white is True
254
- if fields [_index ].get ('strip_extra_white' , True ) and isinstance (_cell , str ):
255
- row [_index ] = _cell .strip ()
256
- yield row
257
- stream .iter = strip_white_space_iter
250
+ stream .iter = _make_whitespace_stripping_iter (stream .iter )
258
251
stream .save (** save_args )
259
252
csv_filepath = f_write .name
260
253
@@ -284,9 +277,8 @@ def strip_white_space_iter():
284
277
raise LoaderError ('Could not create the database table: {}'
285
278
.format (e ))
286
279
287
-
288
- # datstore_active is switched on by datastore_create - TODO temporarily
289
- # disable it until the load is complete
280
+ # datastore_active is switched on by datastore_create
281
+ # TODO temporarily disable it until the load is complete
290
282
291
283
with engine .begin () as conn :
292
284
_disable_fulltext_trigger (conn , resource_id )
0 commit comments