<s> from . base import Converter <EOL> from ... helpers import iterate_files <EOL> from collections import MutableMapping <EOL> from csv import writer as csv_writer , excel <EOL> class CsvConverter ( Converter ) : <EOL> fields = [ <EOL> '<STR_LIT:data>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:port>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:version>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:html>' , <EOL> '<STR_LIT:title>' , <EOL> ] <EOL> def process ( self , files ) : <EOL> writer = csv_writer ( self . fout , dialect = excel ) <EOL> writer . writerow ( self . fields ) <EOL> for banner in iterate_files ( files ) : <EOL> try : <EOL> row = [ ] <EOL> for field in self . fields : <EOL> value = self . banner_field ( banner , field ) <EOL> row . append ( value ) <EOL> writer . writerow ( row ) <EOL> except : <EOL> pass <EOL> def banner_field ( self , banner , flat_field ) : <EOL> fields = flat_field . split ( '<STR_LIT:.>' ) <EOL> try : <EOL> current_obj = banner <EOL> for field in fields : <EOL> current_obj = current_obj [ field ] <EOL> if isinstance ( current_obj , list ) : <EOL> current_obj = '<STR_LIT:U+002C>' . join ( [ str ( i ) for i in current_obj ] ) <EOL> return current_obj <EOL> except : <EOL> pass <EOL> return '<STR_LIT>' <EOL> def flatten ( self , d , parent_key = '<STR_LIT>' , sep = '<STR_LIT:.>' ) : <EOL> items = [ ] <EOL> for k , v in d . items ( ) : <EOL> new_key = parent_key + sep + k if parent_key else k <EOL> if isinstance ( v , MutableMapping ) : <EOL> items . extend ( flatten ( v , new_key , sep = sep ) . items ( ) ) <EOL> else : <EOL> items . append ( ( new_key , v ) ) <EOL> return dict ( items ) </s>
