<s> import datetime <EOL> import os <EOL> from binder . binderd . module import BinderDModule <EOL> from binder . settings import LogSettings <EOL> from binder . log import * <EOL> class LogReader ( BinderDModule ) : <EOL> TAG = "<STR_LIT>" <EOL> def _parse_time ( self , t ) : <EOL> return datetime . datetime . strptime ( t , LogSettings . TIME_FORMAT ) <EOL> def _parse_time_line ( self , line ) : <EOL> t = LogSettings . EXTRACT_TIME ( line ) <EOL> try : <EOL> return self . _parse_time ( t ) <EOL> except Exception as e : <EOL> return datetime . datetime . min <EOL> def _get_logs ( self , app , since = None , filtered = False ) : <EOL> if not filtered : <EOL> log_file = os . path . join ( LogSettings . APPS_DIRECTORY , app + "<STR_LIT>" ) <EOL> else : <EOL> log_file = os . path . join ( LogSettings . APPS_DIRECTORY , app + "<STR_LIT>" ) <EOL> with open ( log_file , '<STR_LIT:r>' ) as f : <EOL> if since : <EOL> def _line_filter ( line ) : <EOL> return line != '<STR_LIT>' and self . _parse_time_line ( line ) > since <EOL> return "<STR_LIT:\n>" . join ( map ( lambda l : l . strip ( ) , filter ( _line_filter , f . readlines ( ) ) ) ) <EOL> else : <EOL> return f . read ( ) <EOL> def _handle_get ( self , msg ) : <EOL> app = msg . get ( "<STR_LIT>" ) <EOL> since = msg . get ( "<STR_LIT>" ) <EOL> filtered = msg . get ( "<STR_LIT>" ) <EOL> if filtered : <EOL> filtered = bool ( filtered ) <EOL> if not app : <EOL> return self . _error_msg ( "<STR_LIT>" ) <EOL> try : <EOL> if since : <EOL> since = self . _parse_time ( msg . get ( "<STR_LIT>" ) ) <EOL> logs = self . _get_logs ( app , since , filtered ) <EOL> return self . _success_msg ( logs ) <EOL> except Exception as e : <EOL> return self . _error_msg ( "<STR_LIT>" . format ( e ) ) <EOL> def _handle_message ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> msg_type = msg . get ( "<STR_LIT:type>" ) <EOL> if msg_type == '<STR_LIT>' : <EOL> return self . _handle_get ( msg ) </s>
