<s> import time <EOL> import datetime <EOL> import curses <EOL> import locale <EOL> from waze_thread import WazeUpdaterThread <EOL> from gps_thread import GpsThread <EOL> class WazeClient ( ) : <EOL> def __init__ ( self ) : <EOL> self . gps_t = GpsThread ( ) <EOL> self . gps_t . setDaemon ( True ) <EOL> self . gps_t . start ( ) <EOL> self . waze_t = WazeUpdaterThread ( update_interval = <NUM_LIT> , max_age = <NUM_LIT> ) <EOL> self . waze_t . setDaemon ( True ) <EOL> self . waze_t . start ( ) <EOL> locale . setlocale ( locale . LC_ALL , '<STR_LIT>' ) <EOL> self . _code = locale . getpreferredencoding ( ) <EOL> try : <EOL> curses . wrapper ( self . run ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> def _encstr ( self , string ) : <EOL> return string . encode ( self . _code ) <EOL> def initscr ( self , stdscr ) : <EOL> curses . use_default_colors ( ) <EOL> try : <EOL> curses . curs_set ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> size = stdscr . getmaxyx ( ) <EOL> status_height = <NUM_LIT:1> <EOL> self . _status_win = stdscr . subwin ( size [ <NUM_LIT:0> ] - status_height , <NUM_LIT:0> ) <EOL> self . _set_status ( False , False ) <EOL> self . _alerts_win = stdscr . subwin ( size [ <NUM_LIT:0> ] - status_height , <EOL> size [ <NUM_LIT:1> ] , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def _set_status ( self , fix , alerts ) : <EOL> """<STR_LIT>""" <EOL> if not fix : <EOL> text = "<STR_LIT>" <EOL> elif not alerts : <EOL> text = "<STR_LIT>" <EOL> else : <EOL> text = "<STR_LIT>" % len ( alerts ) <EOL> lines , cols = self . _status_win . getmaxyx ( ) <EOL> text = text . rjust ( cols - <NUM_LIT:1> ) <EOL> helptext = "<STR_LIT>" <EOL> text = helptext + text [ len ( helptext ) : ] <EOL> self . _status_win . addstr ( <NUM_LIT:0> , <NUM_LIT:0> , text , <EOL> curses . A_REVERSE ) <EOL> self . _status_win . refresh ( ) <EOL> def _prepare_alert_description ( self , alert ) : <EOL> attrs = curses . color_pair ( <NUM_LIT:0> ) <EOL> if alert [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> if len ( alert [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> if alert [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> typ = '<STR_LIT>' <EOL> elif alert [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> typ = '<STR_LIT>' <EOL> elif alert [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> typ = '<STR_LIT>' <EOL> elif alert [ '<STR_LIT>' ] in [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> typ = '<STR_LIT>' <EOL> else : <EOL> typ = alert [ '<STR_LIT>' ] <EOL> else : <EOL> typ = alert [ '<STR_LIT:type>' ] <EOL> elif alert [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> if alert [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> typ = '<STR_LIT>' <EOL> elif alert [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> typ = '<STR_LIT>' <EOL> else : <EOL> typ = '<STR_LIT>' <EOL> attrs = curses . color_pair ( <NUM_LIT:0> ) | curses . A_BOLD <EOL> else : <EOL> typ = alert [ '<STR_LIT:type>' ] <EOL> typ = typ . title ( ) <EOL> return ( typ , attrs ) <EOL> def _prepare_timestamp ( self , alert ) : <EOL> nowdt = long ( datetime . datetime . now ( ) . strftime ( "<STR_LIT:%s>" ) ) <EOL> seconds = nowdt - ( alert [ '<STR_LIT>' ] / <NUM_LIT:1000> ) <EOL> if seconds > <NUM_LIT> : <EOL> return "<STR_LIT>" % ( seconds / <NUM_LIT> , ( ( seconds % <NUM_LIT> ) / <NUM_LIT> ) ) <EOL> else : <EOL> return "<STR_LIT>" % ( seconds / <NUM_LIT> , ) <EOL> def _prepare_alert_line ( self , alert ) : <EOL> description , attrs = self . _prepare_alert_description ( alert ) <EOL> city = alert . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> street = alert . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if city and street : <EOL> fmt = u"<STR_LIT>" <EOL> args = ( description , city , street ) <EOL> elif city and not street : <EOL> fmt = u"<STR_LIT>" <EOL> args = ( description , city ) <EOL> else : <EOL> fmt = u"<STR_LIT:%s>" <EOL> args = ( description , ) <EOL> text = fmt % args <EOL> lines , cols = self . _alerts_win . getmaxyx ( ) <EOL> distance_text = "<STR_LIT>" % round ( alert [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> timestamp_text = self . _prepare_timestamp ( alert ) <EOL> right_text = "<STR_LIT>" % ( timestamp_text . rjust ( <NUM_LIT:6> ) , <EOL> distance_text . rjust ( <NUM_LIT:7> ) ) <EOL> text = text . ljust ( cols - <NUM_LIT:1> ) <EOL> text = text [ <NUM_LIT:0> : - len ( right_text ) ] + right_text <EOL> text = self . _encstr ( text ) <EOL> return ( text , attrs ) <EOL> def run ( self , stdscr ) : <EOL> self . initscr ( stdscr ) <EOL> while True : <EOL> fix = self . gps_t . last_fix ( ) <EOL> if fix : <EOL> self . _fix = fix <EOL> self . waze_t . set_last_position ( fix ) <EOL> alerts = self . waze_t . get_last_alerts ( ) <EOL> self . _set_status ( fix , alerts ) <EOL> lines , cols = self . _alerts_win . getmaxyx ( ) <EOL> if alerts : <EOL> y = <NUM_LIT:0> <EOL> self . _alerts_win . erase ( ) <EOL> for alert in alerts : <EOL> self . _alerts_win . move ( y , <NUM_LIT:0> ) <EOL> text , attrs = self . _prepare_alert_line ( alert ) <EOL> self . _alerts_win . addstr ( text , attrs ) <EOL> y += <NUM_LIT:1> <EOL> if y == lines : <EOL> break <EOL> self . _alerts_win . refresh ( ) <EOL> stdscr . refresh ( ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> c = WazeClient ( ) </s>
