#NEWLINE# def to_zipped_csv( self , path , data , * args ) : #NEWLINE# #INDENT# def write_row( row , csv_writer , fields ) : #NEWLINE# #INDENT# csv_writer . writerow ( [ encode_if_str ( row , field ) for field in fields ] ) #NEWLINE# #UNINDENT# csv_defs = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for section in self . sections : #NEWLINE# #INDENT# csv_file = NamedTemporaryFile ( suffix = '.csv' ) #NEWLINE# csv_writer = csv . writer ( csv_file ) #NEWLINE# csv_defs [ csv_defs [ 'name' ] ] = { #NEWLINE# #INDENT# 'csv_file' : csv_file , #NEWLINE# 'csv_writer' : csv_writer , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# for section in self . sections : #NEWLINE# #INDENT# fields = ( [ element [ 'title' ] for element in section [ 'elements' ] ] + self . EXTRA_FIELDS ) #NEWLINE# csv_defs [ section [ 'name' ] ] [ 'csv_writer' ] . writerow ( [ f . encode ( 'utf-8' ) for f in fields ] ) #NEWLINE# #UNINDENT# index = 1 #NEWLINE# indices = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# survey_name = self . survey . name #NEWLINE# for d in data : #NEWLINE# #INDENT# joined_export = dict_to_joined_export ( d , index , indices , survey_name ) #NEWLINE# output = ExportBuilder . decode_mongo_encoded_section_names ( joined_export ) #NEWLINE# if ( survey_name not in output ) : #NEWLINE# #INDENT# output [ survey_name ] = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# output [ survey_name ] [ INDEX ] = index #NEWLINE# output [ survey_name ] [ PARENT_INDEX ] = ( - 1 ) #NEWLINE# for section in self . sections : #NEWLINE# #INDENT# section_name = section [ 'name' ] #NEWLINE# csv_def = csv_defs [ section_name ] #NEWLINE# fields = ( [ element [ 'xpath' ] for element in section [ 'elements' ] ] + self . EXTRA_FIELDS ) #NEWLINE# csv_writer = csv_def [ 'csv_writer' ] #NEWLINE# row = output . get ( section_name , None ) #NEWLINE# if ( type ( row ) == dict ) : #NEWLINE# #INDENT# write_row ( self . pre_process_row ( row , section ) , csv_writer , fields ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type ( row ) == list ) : #NEWLINE# #INDENT# for child_row in row : #NEWLINE# #INDENT# write_row ( self . pre_process_row ( child_row , section ) , csv_writer , fields ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# index += 1 #NEWLINE# #UNINDENT# with ZipFile ( path , 'w' ) as zip_file : #NEWLINE# #INDENT# for ( section_name , csv_def ) in csv_defs . iteritems ( ) : #NEWLINE# #INDENT# csv_file = csv_def [ 'csv_file' ] #NEWLINE# csv_file . seek ( 0 ) #NEWLINE# zip_file . write ( csv_file . name , ( '_' . join ( section_name . split ( '/' ) ) + '.csv' ) ) #NEWLINE# #UNINDENT# #UNINDENT# for ( section_name , csv_def ) in csv_defs . iteritems ( ) : #NEWLINE# #INDENT# csv_def [ 'csv_file' ] . close ( )