<s> import curses , datetime , locale <EOL> from decimal import Decimal <EOL> _ = lambda x : x <EOL> from electrum . util import format_satoshis , set_verbosity <EOL> from electrum . bitcoin import is_valid <EOL> from electrum import Wallet , WalletStorage <EOL> import tty , sys <EOL> class ElectrumGui : <EOL> def __init__ ( self , config , network ) : <EOL> self . config = config <EOL> self . network = network <EOL> storage = WalletStorage ( config ) <EOL> if not storage . file_exists : <EOL> print "<STR_LIT>" <EOL> exit ( ) <EOL> self . wallet = Wallet ( storage ) <EOL> self . wallet . start_threads ( self . network ) <EOL> locale . setlocale ( locale . LC_ALL , '<STR_LIT>' ) <EOL> self . encoding = locale . getpreferredencoding ( ) <EOL> self . stdscr = curses . initscr ( ) <EOL> curses . noecho ( ) <EOL> curses . cbreak ( ) <EOL> curses . start_color ( ) <EOL> curses . use_default_colors ( ) <EOL> curses . init_pair ( <NUM_LIT:1> , curses . COLOR_WHITE , curses . COLOR_BLUE ) <EOL> curses . init_pair ( <NUM_LIT:2> , curses . COLOR_WHITE , curses . COLOR_CYAN ) <EOL> self . stdscr . keypad ( <NUM_LIT:1> ) <EOL> self . stdscr . border ( <NUM_LIT:0> ) <EOL> self . maxy , self . maxx = self . stdscr . getmaxyx ( ) <EOL> self . set_cursor ( <NUM_LIT:0> ) <EOL> self . w = curses . newwin ( <NUM_LIT:10> , <NUM_LIT:50> , <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> set_verbosity ( False ) <EOL> self . tab = <NUM_LIT:0> <EOL> self . pos = <NUM_LIT:0> <EOL> self . popup_pos = <NUM_LIT:0> <EOL> self . str_recipient = "<STR_LIT>" <EOL> self . str_description = "<STR_LIT>" <EOL> self . str_amount = "<STR_LIT>" <EOL> self . str_fee = "<STR_LIT>" <EOL> self . history = None <EOL> if self . network : <EOL> self . network . register_callback ( '<STR_LIT>' , self . update ) <EOL> self . network . register_callback ( '<STR_LIT>' , self . refresh ) <EOL> self . network . register_callback ( '<STR_LIT>' , self . refresh ) <EOL> self . network . register_callback ( '<STR_LIT>' , self . refresh ) <EOL> self . tab_names = [ _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) ] <EOL> self . num_tabs = len ( self . tab_names ) <EOL> def set_cursor ( self , x ) : <EOL> try : <EOL> curses . curs_set ( x ) <EOL> except Exception : <EOL> pass <EOL> def restore_or_create ( self ) : <EOL> pass <EOL> def verify_seed ( self ) : <EOL> pass <EOL> def get_string ( self , y , x ) : <EOL> self . set_cursor ( <NUM_LIT:1> ) <EOL> curses . echo ( ) <EOL> self . stdscr . addstr ( y , x , "<STR_LIT:U+0020>" * <NUM_LIT:20> , curses . A_REVERSE ) <EOL> s = self . stdscr . getstr ( y , x ) <EOL> curses . noecho ( ) <EOL> self . set_cursor ( <NUM_LIT:0> ) <EOL> return s <EOL> def update ( self ) : <EOL> self . update_history ( ) <EOL> if self . tab == <NUM_LIT:0> : <EOL> self . print_history ( ) <EOL> self . refresh ( ) <EOL> def print_history ( self ) : <EOL> width = [ <NUM_LIT:20> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> delta = ( self . maxx - sum ( width ) - <NUM_LIT:4> ) / <NUM_LIT:3> <EOL> format_str = "<STR_LIT:%>" + "<STR_LIT>" % width [ <NUM_LIT:0> ] + "<STR_LIT:s>" + "<STR_LIT:%>" + "<STR_LIT>" % ( width [ <NUM_LIT:1> ] + delta ) + "<STR_LIT:s>" + "<STR_LIT:%>" + "<STR_LIT>" % ( width [ <NUM_LIT:2> ] + delta ) + "<STR_LIT:s>" + "<STR_LIT:%>" + "<STR_LIT>" % ( width [ <NUM_LIT:3> ] + delta ) + "<STR_LIT:s>" <EOL> if self . history is None : <EOL> self . update_history ( ) <EOL> self . print_list ( self . history [ : : - <NUM_LIT:1> ] , format_str % ( _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) ) ) <EOL> def update_history ( self ) : <EOL> width = [ <NUM_LIT:20> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> delta = ( self . maxx - sum ( width ) - <NUM_LIT:4> ) / <NUM_LIT:3> <EOL> format_str = "<STR_LIT:%>" + "<STR_LIT>" % width [ <NUM_LIT:0> ] + "<STR_LIT:s>" + "<STR_LIT:%>" + "<STR_LIT>" % ( width [ <NUM_LIT:1> ] + delta ) + "<STR_LIT:s>" + "<STR_LIT:%>" + "<STR_LIT>" % ( width [ <NUM_LIT:2> ] + delta ) + "<STR_LIT:s>" + "<STR_LIT:%>" + "<STR_LIT>" % ( width [ <NUM_LIT:3> ] + delta ) + "<STR_LIT:s>" <EOL> b = <NUM_LIT:0> <EOL> self . history = [ ] <EOL> for item in self . wallet . get_tx_history ( ) : <EOL> tx_hash , conf , is_mine , value , fee , balance , timestamp = item <EOL> if conf : <EOL> try : <EOL> time_str = datetime . datetime . fromtimestamp ( timestamp ) . isoformat ( '<STR_LIT:U+0020>' ) [ : - <NUM_LIT:3> ] <EOL> except Exception : <EOL> time_str = "<STR_LIT>" <EOL> else : <EOL> time_str = '<STR_LIT>' <EOL> label , is_default_label = self . wallet . get_label ( tx_hash ) <EOL> self . history . append ( format_str % ( time_str , label , format_satoshis ( value , whitespaces = True ) , format_satoshis ( balance , whitespaces = True ) ) ) <EOL> def print_balance ( self ) : <EOL> if not self . network : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> elif self . network . interface and self . network . interface . is_connected : <EOL> if not self . wallet . up_to_date : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> else : <EOL> c , u = self . wallet . get_balance ( ) <EOL> msg = _ ( "<STR_LIT>" ) + "<STR_LIT>" % ( Decimal ( c ) / <NUM_LIT> ) <EOL> if u : msg += "<STR_LIT>" % ( Decimal ( u ) / <NUM_LIT> ) <EOL> else : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> self . stdscr . addstr ( self . maxy - <NUM_LIT:1> , <NUM_LIT:3> , msg ) <EOL> for i in range ( self . num_tabs ) : <EOL> self . stdscr . addstr ( <NUM_LIT:0> , <NUM_LIT:2> + <NUM_LIT:2> * i + len ( '<STR_LIT>' . join ( self . tab_names [ <NUM_LIT:0> : i ] ) ) , '<STR_LIT:U+0020>' + self . tab_names [ i ] + '<STR_LIT:U+0020>' , curses . A_BOLD if self . tab == i else <NUM_LIT:0> ) <EOL> self . stdscr . addstr ( self . maxy - <NUM_LIT:1> , self . maxx - <NUM_LIT:30> , '<STR_LIT:U+0020>' . join ( [ _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) ] ) ) <EOL> def print_contacts ( self ) : <EOL> messages = map ( lambda addr : "<STR_LIT>" % ( addr , self . wallet . labels . get ( addr , "<STR_LIT>" ) ) , self . wallet . addressbook ) <EOL> self . print_list ( messages , "<STR_LIT>" % ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> def print_receive ( self ) : <EOL> fmt = "<STR_LIT>" <EOL> messages = map ( lambda addr : fmt % ( addr , self . wallet . labels . get ( addr , "<STR_LIT>" ) ) , self . wallet . addresses ( ) ) <EOL> self . print_list ( messages , fmt % ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> def print_edit_line ( self , y , label , text , index , size ) : <EOL> text += "<STR_LIT:U+0020>" * ( size - len ( text ) ) <EOL> self . stdscr . addstr ( y , <NUM_LIT:2> , label ) <EOL> self . stdscr . addstr ( y , <NUM_LIT:15> , text , curses . A_REVERSE if self . pos % <NUM_LIT:6> == index else curses . color_pair ( <NUM_LIT:1> ) ) <EOL> def print_send_tab ( self ) : <EOL> self . stdscr . clear ( ) <EOL> self . print_edit_line ( <NUM_LIT:3> , _ ( "<STR_LIT>" ) , self . str_recipient , <NUM_LIT:0> , <NUM_LIT> ) <EOL> self . print_edit_line ( <NUM_LIT:5> , _ ( "<STR_LIT>" ) , self . str_description , <NUM_LIT:1> , <NUM_LIT> ) <EOL> self . print_edit_line ( <NUM_LIT:7> , _ ( "<STR_LIT>" ) , self . str_amount , <NUM_LIT:2> , <NUM_LIT:15> ) <EOL> self . print_edit_line ( <NUM_LIT:9> , _ ( "<STR_LIT>" ) , self . str_fee , <NUM_LIT:3> , <NUM_LIT:15> ) <EOL> self . stdscr . addstr ( <NUM_LIT:12> , <NUM_LIT:15> , _ ( "<STR_LIT>" ) , curses . A_REVERSE if self . pos % <NUM_LIT:6> == <NUM_LIT:4> else curses . color_pair ( <NUM_LIT:2> ) ) <EOL> self . stdscr . addstr ( <NUM_LIT:12> , <NUM_LIT> , _ ( "<STR_LIT>" ) , curses . A_REVERSE if self . pos % <NUM_LIT:6> == <NUM_LIT:5> else curses . color_pair ( <NUM_LIT:2> ) ) <EOL> def print_banner ( self ) : <EOL> if self . network : <EOL> self . print_list ( self . network . banner . split ( '<STR_LIT:\n>' ) ) <EOL> def print_list ( self , list , firstline = None ) : <EOL> self . maxpos = len ( list ) <EOL> if not self . maxpos : return <EOL> if firstline : <EOL> firstline += "<STR_LIT:U+0020>" * ( self . maxx - <NUM_LIT:2> - len ( firstline ) ) <EOL> self . stdscr . addstr ( <NUM_LIT:1> , <NUM_LIT:1> , firstline ) <EOL> for i in range ( self . maxy - <NUM_LIT:4> ) : <EOL> msg = list [ i ] if i < len ( list ) else "<STR_LIT>" <EOL> msg += "<STR_LIT:U+0020>" * ( self . maxx - <NUM_LIT:2> - len ( msg ) ) <EOL> m = msg [ <NUM_LIT:0> : self . maxx - <NUM_LIT:2> ] <EOL> m = m . encode ( self . encoding ) <EOL> self . stdscr . addstr ( i + <NUM_LIT:2> , <NUM_LIT:1> , m , curses . A_REVERSE if i == ( self . pos % self . maxpos ) else <NUM_LIT:0> ) <EOL> def refresh ( self ) : <EOL> if self . tab == - <NUM_LIT:1> : return <EOL> self . stdscr . border ( <NUM_LIT:0> ) <EOL> self . print_balance ( ) <EOL> self . stdscr . refresh ( ) <EOL> def main_command ( self ) : <EOL> c = self . stdscr . getch ( ) <EOL> print c <EOL> if c == curses . KEY_RIGHT : self . tab = ( self . tab + <NUM_LIT:1> ) % self . num_tabs <EOL> elif c == curses . KEY_LEFT : self . tab = ( self . tab - <NUM_LIT:1> ) % self . num_tabs <EOL> elif c == curses . KEY_DOWN : self . pos += <NUM_LIT:1> <EOL> elif c == curses . KEY_UP : self . pos -= <NUM_LIT:1> <EOL> elif c == <NUM_LIT:9> : self . pos += <NUM_LIT:1> <EOL> elif curses . unctrl ( c ) in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : self . tab = - <NUM_LIT:1> <EOL> elif curses . unctrl ( c ) in [ '<STR_LIT>' ] : self . network_dialog ( ) <EOL> elif curses . unctrl ( c ) == '<STR_LIT>' : self . settings_dialog ( ) <EOL> else : return c <EOL> if self . pos < <NUM_LIT:0> : self . pos = <NUM_LIT:0> <EOL> if self . pos >= self . maxpos : self . pos = self . maxpos - <NUM_LIT:1> <EOL> def run_tab ( self , i , print_func , exec_func ) : <EOL> while self . tab == i : <EOL> self . stdscr . clear ( ) <EOL> print_func ( ) <EOL> self . refresh ( ) <EOL> c = self . main_command ( ) <EOL> if c : exec_func ( c ) <EOL> def run_history_tab ( self , c ) : <EOL> if c == <NUM_LIT:10> : <EOL> out = self . run_popup ( '<STR_LIT>' , [ "<STR_LIT>" , "<STR_LIT:foo>" ] ) <EOL> def edit_str ( self , target , c , is_num = False ) : <EOL> if c in [ <NUM_LIT:8> , <NUM_LIT> , <NUM_LIT> ] and target : <EOL> target = target [ : - <NUM_LIT:1> ] <EOL> elif not is_num or curses . unctrl ( c ) in '<STR_LIT>' : <EOL> target += curses . unctrl ( c ) <EOL> return target <EOL> def run_send_tab ( self , c ) : <EOL> if self . pos % <NUM_LIT:6> == <NUM_LIT:0> : <EOL> self . str_recipient = self . edit_str ( self . str_recipient , c ) <EOL> if self . pos % <NUM_LIT:6> == <NUM_LIT:1> : <EOL> self . str_description = self . edit_str ( self . str_description , c ) <EOL> if self . pos % <NUM_LIT:6> == <NUM_LIT:2> : <EOL> self . str_amount = self . edit_str ( self . str_amount , c , True ) <EOL> elif self . pos % <NUM_LIT:6> == <NUM_LIT:3> : <EOL> self . str_fee = self . edit_str ( self . str_fee , c , True ) <EOL> elif self . pos % <NUM_LIT:6> == <NUM_LIT:4> : <EOL> if c == <NUM_LIT:10> : self . do_send ( ) <EOL> elif self . pos % <NUM_LIT:6> == <NUM_LIT:5> : <EOL> if c == <NUM_LIT:10> : self . do_clear ( ) <EOL> def run_receive_tab ( self , c ) : <EOL> if c == <NUM_LIT:10> : <EOL> out = self . run_popup ( '<STR_LIT>' , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def run_contacts_tab ( self , c ) : <EOL> if c == <NUM_LIT:10> and self . wallet . addressbook : <EOL> out = self . run_popup ( '<STR_LIT>' , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) . get ( '<STR_LIT>' ) <EOL> address = self . wallet . addressbook [ self . pos % len ( self . wallet . addressbook ) ] <EOL> if out == "<STR_LIT>" : <EOL> self . tab = <NUM_LIT:1> <EOL> self . str_recipient = address <EOL> self . pos = <NUM_LIT:2> <EOL> elif out == "<STR_LIT>" : <EOL> s = self . get_string ( <NUM_LIT:6> + self . pos , <NUM_LIT> ) <EOL> if s : <EOL> self . wallet . labels [ address ] = s <EOL> def run_banner_tab ( self , c ) : <EOL> self . show_message ( repr ( c ) ) <EOL> pass <EOL> def main ( self , url ) : <EOL> tty . setraw ( sys . stdin ) <EOL> while self . tab != - <NUM_LIT:1> : <EOL> self . run_tab ( <NUM_LIT:0> , self . print_history , self . run_history_tab ) <EOL> self . run_tab ( <NUM_LIT:1> , self . print_send_tab , self . run_send_tab ) <EOL> self . run_tab ( <NUM_LIT:2> , self . print_receive , self . run_receive_tab ) <EOL> self . run_tab ( <NUM_LIT:3> , self . print_contacts , self . run_contacts_tab ) <EOL> self . run_tab ( <NUM_LIT:4> , self . print_banner , self . run_banner_tab ) <EOL> tty . setcbreak ( sys . stdin ) <EOL> curses . nocbreak ( ) <EOL> self . stdscr . keypad ( <NUM_LIT:0> ) <EOL> curses . echo ( ) <EOL> curses . endwin ( ) <EOL> def do_clear ( self ) : <EOL> self . str_amount = '<STR_LIT>' <EOL> self . str_recipient = '<STR_LIT>' <EOL> self . str_fee = '<STR_LIT>' <EOL> self . str_description = '<STR_LIT>' <EOL> def do_send ( self ) : <EOL> if not is_valid ( self . str_recipient ) : <EOL> self . show_message ( _ ( '<STR_LIT>' ) ) <EOL> return <EOL> try : <EOL> amount = int ( Decimal ( self . str_amount ) * <NUM_LIT> ) <EOL> except Exception : <EOL> self . show_message ( _ ( '<STR_LIT>' ) ) <EOL> return <EOL> try : <EOL> fee = int ( Decimal ( self . str_fee ) * <NUM_LIT> ) <EOL> except Exception : <EOL> self . show_message ( _ ( '<STR_LIT>' ) ) <EOL> return <EOL> if self . wallet . use_encryption : <EOL> password = self . password_dialog ( ) <EOL> if not password : <EOL> return <EOL> else : <EOL> password = None <EOL> try : <EOL> tx = self . wallet . mktx ( [ ( self . str_recipient , amount ) ] , password , fee ) <EOL> except Exception as e : <EOL> self . show_message ( str ( e ) ) <EOL> return <EOL> if self . str_description : <EOL> self . wallet . labels [ tx . hash ( ) ] = self . str_description <EOL> h = self . wallet . send_tx ( tx ) <EOL> self . show_message ( _ ( "<STR_LIT>" ) , getchar = False ) <EOL> self . wallet . tx_event . wait ( ) <EOL> status , msg = self . wallet . receive_tx ( h , tx ) <EOL> if status : <EOL> self . show_message ( _ ( '<STR_LIT>' ) ) <EOL> self . do_clear ( ) <EOL> else : <EOL> self . show_message ( _ ( '<STR_LIT>' ) ) <EOL> def show_message ( self , message , getchar = True ) : <EOL> w = self . w <EOL> w . clear ( ) <EOL> w . border ( <NUM_LIT:0> ) <EOL> for i , line in enumerate ( message . split ( '<STR_LIT:\n>' ) ) : <EOL> w . addstr ( <NUM_LIT:2> + i , <NUM_LIT:2> , line ) <EOL> w . refresh ( ) <EOL> if getchar : c = self . stdscr . getch ( ) <EOL> def run_popup ( self , title , items ) : <EOL> return self . run_dialog ( title , map ( lambda x : { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:label>' : x } , items ) , interval = <NUM_LIT:1> , y_pos = self . pos + <NUM_LIT:3> ) <EOL> def network_dialog ( self ) : <EOL> if not self . network : return <EOL> auto_connect = self . network . config . get ( '<STR_LIT>' ) <EOL> host , port , protocol = self . network . default_server . split ( '<STR_LIT::>' ) <EOL> srv = '<STR_LIT>' if auto_connect else self . network . default_server <EOL> out = self . run_dialog ( '<STR_LIT>' , [ <EOL> { '<STR_LIT:label>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT:str>' , '<STR_LIT:value>' : srv } , <EOL> { '<STR_LIT:label>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT:str>' , '<STR_LIT:value>' : self . config . get ( '<STR_LIT>' , '<STR_LIT>' ) } , <EOL> ] , buttons = <NUM_LIT:1> ) <EOL> if out : <EOL> if out . get ( '<STR_LIT>' ) : <EOL> server = out . get ( '<STR_LIT>' ) <EOL> auto_connect = server == '<STR_LIT>' <EOL> if not auto_connect : <EOL> try : <EOL> host , port , protocol = server . split ( '<STR_LIT::>' ) <EOL> except Exception : <EOL> self . show_message ( "<STR_LIT>" + server + "<STR_LIT>" ) <EOL> return False <EOL> if out . get ( '<STR_LIT>' ) : <EOL> proxy = self . parse_proxy_options ( out . get ( '<STR_LIT>' ) ) <EOL> else : <EOL> proxy = None <EOL> self . network . set_parameters ( host , port , protocol , proxy , auto_connect ) <EOL> def settings_dialog ( self ) : <EOL> out = self . run_dialog ( '<STR_LIT>' , [ <EOL> { '<STR_LIT:label>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT:list>' , '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:text>' ] , '<STR_LIT:value>' : self . config . get ( '<STR_LIT>' ) } , <EOL> { '<STR_LIT:label>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:value>' : format_satoshis ( self . wallet . fee ) . strip ( ) } <EOL> ] , buttons = <NUM_LIT:1> ) <EOL> if out : <EOL> if out . get ( '<STR_LIT>' ) : <EOL> self . config . set_key ( '<STR_LIT>' , out [ '<STR_LIT>' ] , True ) <EOL> if out . get ( '<STR_LIT>' ) : <EOL> fee = int ( Decimal ( out [ '<STR_LIT>' ] ) * <NUM_LIT> ) <EOL> self . config . set_key ( '<STR_LIT>' , fee , True ) <EOL> def password_dialog ( self ) : <EOL> out = self . run_dialog ( '<STR_LIT>' , [ <EOL> { '<STR_LIT:label>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT:password>' , '<STR_LIT:value>' : '<STR_LIT>' } <EOL> ] , buttons = <NUM_LIT:1> ) <EOL> return out . get ( '<STR_LIT>' ) <EOL> def run_dialog ( self , title , items , interval = <NUM_LIT:2> , buttons = None , y_pos = <NUM_LIT:3> ) : <EOL> self . popup_pos = <NUM_LIT:0> <EOL> self . w = curses . newwin ( <NUM_LIT:5> + len ( items ) * interval + ( <NUM_LIT:2> if buttons else <NUM_LIT:0> ) , <NUM_LIT:50> , y_pos , <NUM_LIT:5> ) <EOL> w = self . w <EOL> out = { } <EOL> while True : <EOL> w . clear ( ) <EOL> w . border ( <NUM_LIT:0> ) <EOL> w . addstr ( <NUM_LIT:0> , <NUM_LIT:2> , title ) <EOL> num = len ( items ) <EOL> numpos = num <EOL> if buttons : numpos += <NUM_LIT:2> <EOL> for i in range ( num ) : <EOL> item = items [ i ] <EOL> label = item . get ( '<STR_LIT:label>' ) <EOL> if item . get ( '<STR_LIT:type>' ) == '<STR_LIT:list>' : <EOL> value = item . get ( '<STR_LIT:value>' , '<STR_LIT>' ) <EOL> elif item . get ( '<STR_LIT:type>' ) == '<STR_LIT>' : <EOL> value = item . get ( '<STR_LIT:value>' , '<STR_LIT>' ) <EOL> elif item . get ( '<STR_LIT:type>' ) == '<STR_LIT:str>' : <EOL> value = item . get ( '<STR_LIT:value>' , '<STR_LIT>' ) <EOL> elif item . get ( '<STR_LIT:type>' ) == '<STR_LIT:password>' : <EOL> value = '<STR_LIT:*>' * len ( item . get ( '<STR_LIT:value>' , '<STR_LIT>' ) ) <EOL> else : <EOL> value = '<STR_LIT>' <EOL> if len ( value ) < <NUM_LIT:20> : value += '<STR_LIT:U+0020>' * ( <NUM_LIT:20> - len ( value ) ) <EOL> if item . has_key ( '<STR_LIT:value>' ) : <EOL> w . addstr ( <NUM_LIT:2> + interval * i , <NUM_LIT:2> , label ) <EOL> w . addstr ( <NUM_LIT:2> + interval * i , <NUM_LIT:15> , value , curses . A_REVERSE if self . popup_pos % numpos == i else curses . color_pair ( <NUM_LIT:1> ) ) <EOL> else : <EOL> w . addstr ( <NUM_LIT:2> + interval * i , <NUM_LIT:2> , label , curses . A_REVERSE if self . popup_pos % numpos == i else <NUM_LIT:0> ) <EOL> if buttons : <EOL> w . addstr ( <NUM_LIT:5> + interval * i , <NUM_LIT:10> , "<STR_LIT>" , curses . A_REVERSE if self . popup_pos % numpos == ( numpos - <NUM_LIT:2> ) else curses . color_pair ( <NUM_LIT:2> ) ) <EOL> w . addstr ( <NUM_LIT:5> + interval * i , <NUM_LIT> , "<STR_LIT>" , curses . A_REVERSE if self . popup_pos % numpos == ( numpos - <NUM_LIT:1> ) else curses . color_pair ( <NUM_LIT:2> ) ) <EOL> w . refresh ( ) <EOL> c = self . stdscr . getch ( ) <EOL> if c in [ ord ( '<STR_LIT:q>' ) , <NUM_LIT> ] : break <EOL> elif c in [ curses . KEY_LEFT , curses . KEY_UP ] : self . popup_pos -= <NUM_LIT:1> <EOL> elif c in [ curses . KEY_RIGHT , curses . KEY_DOWN ] : self . popup_pos += <NUM_LIT:1> <EOL> else : <EOL> i = self . popup_pos % numpos <EOL> if buttons and c == <NUM_LIT:10> : <EOL> if i == numpos - <NUM_LIT:2> : <EOL> return out <EOL> elif i == numpos - <NUM_LIT:1> : <EOL> return { } <EOL> item = items [ i ] <EOL> _type = item . get ( '<STR_LIT:type>' ) <EOL> if _type == '<STR_LIT:str>' : <EOL> item [ '<STR_LIT:value>' ] = self . edit_str ( item [ '<STR_LIT:value>' ] , c ) <EOL> out [ item . get ( '<STR_LIT:label>' ) ] = item . get ( '<STR_LIT:value>' ) <EOL> elif _type == '<STR_LIT:password>' : <EOL> item [ '<STR_LIT:value>' ] = self . edit_str ( item [ '<STR_LIT:value>' ] , c ) <EOL> out [ item . get ( '<STR_LIT:label>' ) ] = item [ '<STR_LIT:value>' ] <EOL> elif _type == '<STR_LIT>' : <EOL> item [ '<STR_LIT:value>' ] = self . edit_str ( item [ '<STR_LIT:value>' ] , c , True ) <EOL> out [ item . get ( '<STR_LIT:label>' ) ] = item . get ( '<STR_LIT:value>' ) <EOL> elif _type == '<STR_LIT:list>' : <EOL> choices = item . get ( '<STR_LIT>' ) <EOL> try : <EOL> j = choices . index ( item . get ( '<STR_LIT:value>' ) ) <EOL> except Exception : <EOL> j = <NUM_LIT:0> <EOL> new_choice = choices [ ( j + <NUM_LIT:1> ) % len ( choices ) ] <EOL> item [ '<STR_LIT:value>' ] = new_choice <EOL> out [ item . get ( '<STR_LIT:label>' ) ] = item . get ( '<STR_LIT:value>' ) <EOL> elif _type == '<STR_LIT>' : <EOL> out [ '<STR_LIT>' ] = item . get ( '<STR_LIT:label>' ) <EOL> break <EOL> return out </s>
