<s> '''<STR_LIT>''' <EOL> import csv <EOL> import datetime <EOL> import optparse <EOL> import itertools <EOL> class BroLogReader ( ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _delimiter = '<STR_LIT:\t>' <EOL> def read_log ( self , logfile , max_rows = None ) : <EOL> '''<STR_LIT>''' <EOL> bro_fptr , field_names , field_types = self . _parse_bro_header ( logfile ) <EOL> reader = csv . DictReader ( bro_fptr , fieldnames = field_names , <EOL> delimiter = self . _delimiter , restval = '<STR_LIT>' ) <EOL> for _row in itertools . islice ( reader , <NUM_LIT:0> , max_rows ) : <EOL> values = self . _cast_dict ( _row ) <EOL> if ( values ) : <EOL> yield values <EOL> def _parse_bro_header ( self , logfile ) : <EOL> '''<STR_LIT>''' <EOL> _file = open ( logfile , '<STR_LIT:rb>' ) <EOL> _line = next ( _file ) <EOL> while ( not _line . startswith ( '<STR_LIT>' ) ) : <EOL> _line = next ( _file ) <EOL> _field_names = _line . strip ( ) . split ( self . _delimiter ) [ <NUM_LIT:1> : ] <EOL> _line = next ( _file ) <EOL> _field_types = _line . strip ( ) . split ( self . _delimiter ) [ <NUM_LIT:1> : ] <EOL> return _file , _field_names , _field_types <EOL> def _cast_dict ( self , data_dict ) : <EOL> '''<STR_LIT>''' <EOL> for key , value in data_dict . iteritems ( ) : <EOL> if ( value == '<STR_LIT>' ) : <EOL> return None <EOL> data_dict [ key ] = self . _cast_value ( value ) <EOL> return data_dict <EOL> def _cast_value ( self , value ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return datetime . datetime . fromtimestamp ( float ( value ) ) <EOL> except ValueError : <EOL> tests = ( int , float , str ) <EOL> for test in tests : <EOL> try : <EOL> return test ( value ) <EOL> except ValueError : <EOL> continue <EOL> return value <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> PARSER = optparse . OptionParser ( ) <EOL> PARSER . add_option ( '<STR_LIT>' , default = None , help = '<STR_LIT>' ) <EOL> ( OPTIONS , ARGUMENTS ) = PARSER . parse_args ( ) <EOL> print OPTIONS , ARGUMENTS <EOL> BRO_LOG = BroLogReader ( ) <EOL> RECORDS = BRO_LOG . read_log ( OPTIONS . logfile , max_rows = <NUM_LIT:10> ) <EOL> for row in RECORDS : <EOL> print row </s>
