<s> import os <EOL> import csv <EOL> import simplejson as json <EOL> from tempfile import mkstemp <EOL> from zipfile import ZipFile <EOL> import datetime <EOL> import hashlib <EOL> DLI_MANIFEST_FILENAME = '<STR_LIT>' <EOL> CSV_DATA_FILENAME = '<STR_LIT>' <EOL> DEFAULT_ARCHIVE_NAME = '<STR_LIT>' <EOL> def write_tmp_file ( content ) : <EOL> '''<STR_LIT>''' <EOL> fp , filename = mkstemp ( ) <EOL> file = open ( filename , '<STR_LIT>' ) <EOL> file . write ( content ) <EOL> os . close ( fp ) <EOL> return filename <EOL> def write_tmp_csv_file ( csv_data , sli_manifest ) : <EOL> '''<STR_LIT>''' <EOL> fieldnames = [ part [ '<STR_LIT>' ] for part in sli_manifest [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] <EOL> fp , filename = mkstemp ( ) <EOL> file = open ( filename , '<STR_LIT>' ) <EOL> writer = csv . DictWriter ( file , fieldnames = fieldnames , <EOL> delimiter = sli_manifest [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> quotechar = sli_manifest [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> quoting = csv . QUOTE_ALL ) <EOL> headers = dict ( ( n , n ) for n in fieldnames ) <EOL> writer . writerow ( headers ) <EOL> for line in csv_data : <EOL> for key in fieldnames : <EOL> if not key in line and key . endswith ( '<STR_LIT>' ) : <EOL> h = hashlib . md5 ( ) <EOL> h . update ( '<STR_LIT>' ) <EOL> line [ key ] = h . hexdigest ( ) [ : <NUM_LIT:6> ] <EOL> if isinstance ( line [ key ] , datetime . datetime ) : <EOL> line [ key ] = line [ key ] . strftime ( "<STR_LIT>" ) <EOL> if isinstance ( line [ key ] , bool ) : <EOL> line [ key ] = int ( line [ key ] ) <EOL> writer . writerow ( line ) <EOL> os . close ( fp ) <EOL> return filename <EOL> def write_tmp_zipfile ( files ) : <EOL> '''<STR_LIT>''' <EOL> fp , filename = mkstemp ( ) <EOL> zip_file = ZipFile ( filename , "<STR_LIT:w>" ) <EOL> for path , name in files : <EOL> zip_file . write ( path , name ) <EOL> zip_file . close ( ) <EOL> os . close ( fp ) <EOL> return filename <EOL> def create_archive ( data , sli_manifest ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( data , list ) : <EOL> data_path = write_tmp_csv_file ( data , sli_manifest ) <EOL> else : <EOL> data_path = write_tmp_file ( data ) <EOL> if isinstance ( sli_manifest , dict ) : <EOL> sli_manifest = json . dumps ( sli_manifest ) <EOL> sli_manifest_path = write_tmp_file ( sli_manifest ) <EOL> filename = write_tmp_zipfile ( ( <EOL> ( data_path , CSV_DATA_FILENAME ) , <EOL> ( sli_manifest_path , DLI_MANIFEST_FILENAME ) , <EOL> ) ) <EOL> os . remove ( data_path ) <EOL> os . remove ( sli_manifest_path ) <EOL> return filename <EOL> def csv_to_list ( data_csv ) : <EOL> '''<STR_LIT>''' <EOL> reader = csv . reader ( data_csv . strip ( ) . split ( '<STR_LIT:\n>' ) ) <EOL> header = reader . next ( ) <EOL> data_list = [ ] <EOL> for line in reader : <EOL> l = { } <EOL> for i , value in enumerate ( header ) : <EOL> l [ value ] = line [ i ] <EOL> data_list . append ( l ) <EOL> return data_list </s>
