<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import time <EOL> from io import BytesIO <EOL> from pycopia import environ <EOL> from pycopia import cliutils <EOL> from pycopia import tty <EOL> from pycopia . fsm import FSM , ANY <EOL> try : <EOL> import readline <EOL> PROMPT_START_IGNORE = '<STR_LIT>' <EOL> PROMPT_END_IGNORE = '<STR_LIT>' <EOL> except ImportError : <EOL> readline = None <EOL> PROMPT_START_IGNORE = '<STR_LIT>' <EOL> PROMPT_END_IGNORE = '<STR_LIT>' <EOL> from types import MethodType , FunctionType <EOL> class UIError ( Exception ) : <EOL> pass <EOL> class UIFindError ( UIError ) : <EOL> pass <EOL> class Theme ( object ) : <EOL> NORMAL = RESET = "<STR_LIT>" <EOL> BOLD = BRIGHT = "<STR_LIT>" <EOL> BLACK = "<STR_LIT>" <EOL> RED = "<STR_LIT>" <EOL> GREEN = "<STR_LIT>" <EOL> YELLOW = "<STR_LIT>" <EOL> BLUE = "<STR_LIT>" <EOL> MAGENTA = "<STR_LIT>" <EOL> CYAN = "<STR_LIT>" <EOL> WHITE = "<STR_LIT>" <EOL> DEFAULT = "<STR_LIT>" <EOL> GREY = "<STR_LIT>" <EOL> BRIGHTRED = "<STR_LIT>" <EOL> BRIGHTGREEN = "<STR_LIT>" <EOL> BRIGHTYELLOW = "<STR_LIT>" <EOL> BRIGHTBLUE = "<STR_LIT>" <EOL> BRIGHTMAGENTA = "<STR_LIT>" <EOL> BRIGHTCYAN = "<STR_LIT>" <EOL> BRIGHTWHITE = "<STR_LIT>" <EOL> UNDERSCORE = "<STR_LIT>" <EOL> BLINK = "<STR_LIT>" <EOL> help_local = WHITE <EOL> help_inherited = YELLOW <EOL> help_created = GREEN <EOL> def __init__ ( self , ps1 = "<STR_LIT>" , ps2 = "<STR_LIT>" , ps3 = "<STR_LIT>" , ps4 = "<STR_LIT>" ) : <EOL> self . _ps1 = ps1 <EOL> self . _ps2 = ps2 <EOL> self . _ps3 = ps3 <EOL> self . _ps4 = ps4 <EOL> self . _setcolors ( ) <EOL> def _set_ps1 ( self , new ) : <EOL> self . _ps1 = str ( new ) <EOL> def _set_ps2 ( self , new ) : <EOL> self . _ps2 = str ( new ) <EOL> def _set_ps3 ( self , new ) : <EOL> self . _ps3 = str ( new ) <EOL> def _set_ps4 ( self , new ) : <EOL> self . _ps4 = str ( new ) <EOL> _setcolors = classmethod ( lambda c : None ) <EOL> ps1 = property ( lambda s : s . _ps1 , _set_ps1 , None , "<STR_LIT>" ) <EOL> ps2 = property ( lambda s : s . _ps2 , _set_ps2 , None , "<STR_LIT>" ) <EOL> ps3 = property ( lambda s : s . _ps3 , _set_ps3 , None , "<STR_LIT>" ) <EOL> ps4 = property ( lambda s : s . _ps4 , _set_ps4 , None , "<STR_LIT>" ) <EOL> class BasicTheme ( Theme ) : <EOL> def _setcolors ( cls ) : <EOL> "<STR_LIT>" <EOL> cls . NORMAL = cls . RESET = "<STR_LIT>" <EOL> cls . BOLD = cls . BRIGHT = "<STR_LIT>" <EOL> cls . BLACK = "<STR_LIT>" <EOL> cls . RED = "<STR_LIT>" <EOL> cls . GREEN = "<STR_LIT>" <EOL> cls . YELLOW = "<STR_LIT>" <EOL> cls . BLUE = "<STR_LIT>" <EOL> cls . MAGENTA = "<STR_LIT>" <EOL> cls . CYAN = "<STR_LIT>" <EOL> cls . WHITE = "<STR_LIT>" <EOL> cls . DEFAULT = "<STR_LIT>" <EOL> cls . GREY = "<STR_LIT>" <EOL> cls . BRIGHTRED = "<STR_LIT>" <EOL> cls . BRIGHTGREEN = "<STR_LIT>" <EOL> cls . BRIGHTYELLOW = "<STR_LIT>" <EOL> cls . BRIGHTBLUE = "<STR_LIT>" <EOL> cls . BRIGHTMAGENTA = "<STR_LIT>" <EOL> cls . BRIGHTCYAN = "<STR_LIT>" <EOL> cls . BRIGHTWHITE = "<STR_LIT>" <EOL> cls . UNDERSCORE = "<STR_LIT>" <EOL> cls . BLINK = "<STR_LIT>" <EOL> cls . help_local = cls . WHITE <EOL> cls . help_inherited = cls . YELLOW <EOL> cls . help_created = cls . GREEN <EOL> _setcolors = classmethod ( _setcolors ) <EOL> class ANSITheme ( BasicTheme ) : <EOL> """<STR_LIT>""" <EOL> def _setcolors ( cls ) : <EOL> cls . NORMAL = cls . RESET = "<STR_LIT>" <EOL> cls . BOLD = cls . BRIGHT = "<STR_LIT>" <EOL> cls . BLACK = "<STR_LIT>" <EOL> cls . RED = "<STR_LIT>" <EOL> cls . GREEN = "<STR_LIT>" <EOL> cls . YELLOW = "<STR_LIT>" <EOL> cls . BLUE = "<STR_LIT>" <EOL> cls . MAGENTA = "<STR_LIT>" <EOL> cls . CYAN = "<STR_LIT>" <EOL> cls . WHITE = "<STR_LIT>" <EOL> cls . GREY = "<STR_LIT>" <EOL> cls . BRIGHTRED = "<STR_LIT>" <EOL> cls . BRIGHTGREEN = "<STR_LIT>" <EOL> cls . BRIGHTYELLOW = "<STR_LIT>" <EOL> cls . BRIGHTBLUE = "<STR_LIT>" <EOL> cls . BRIGHTMAGENTA = "<STR_LIT>" <EOL> cls . BRIGHTCYAN = "<STR_LIT>" <EOL> cls . BRIGHTWHITE = "<STR_LIT>" <EOL> cls . DEFAULT = "<STR_LIT>" <EOL> cls . UNDERSCORE = "<STR_LIT>" <EOL> cls . BLINK = "<STR_LIT>" <EOL> cls . help_local = cls . BRIGHTWHITE <EOL> cls . help_inherited = cls . YELLOW <EOL> cls . help_created = cls . GREEN <EOL> _setcolors = classmethod ( _setcolors ) <EOL> DefaultTheme = ANSITheme <EOL> class UserInterface ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , io , env = None , theme = None ) : <EOL> self . set_IO ( io ) <EOL> self . _env = env or environ . Environ ( ) <EOL> assert hasattr ( self . _env , "<STR_LIT>" ) <EOL> self . _env [ "<STR_LIT:_>" ] = None <EOL> self . _cache = { } <EOL> self . set_theme ( theme ) <EOL> self . _initfsm ( ) <EOL> self . initialize ( ) <EOL> def set_IO ( self , io ) : <EOL> self . _io = io <EOL> if io . isatty ( ) : <EOL> self . _termlen , self . _termwidth , x , y = tty . get_winsize ( io . fileno ( ) ) <EOL> else : <EOL> self . _termlen , self . _termwidth = <NUM_LIT> , <NUM_LIT> <EOL> def get_IO ( self ) : <EOL> return self . _io <EOL> def _del_IO ( self ) : <EOL> self . _io = None <EOL> IO = property ( get_IO , set_IO , _del_IO ) <EOL> def __del__ ( self ) : <EOL> try : <EOL> self . finalize ( ) <EOL> except : <EOL> pass <EOL> def initialize ( self , * args ) : <EOL> pass <EOL> def finalize ( self ) : <EOL> pass <EOL> def close ( self ) : <EOL> if self . _io is not None : <EOL> self . _io . close ( ) <EOL> self . _io = None <EOL> def set_environ ( self , env ) : <EOL> assert hasattr ( env , "<STR_LIT>" ) <EOL> self . _env = env <EOL> self . _env [ "<STR_LIT:_>" ] = None <EOL> def set_theme ( self , theme ) : <EOL> self . _theme = theme or DefaultTheme ( ) <EOL> assert isinstance ( self . _theme , Theme ) , "<STR_LIT>" <EOL> self . _env . setdefault ( "<STR_LIT>" , self . _theme . ps1 ) <EOL> self . _env . setdefault ( "<STR_LIT>" , self . _theme . ps2 ) <EOL> self . _env . setdefault ( "<STR_LIT>" , self . _theme . ps3 ) <EOL> self . _env . setdefault ( "<STR_LIT>" , self . _theme . ps4 ) <EOL> def clone ( self , theme = None ) : <EOL> return self . __class__ ( self . _io , self . _env . copy ( ) , theme or self . _theme ) <EOL> def Print ( self , * objs ) : <EOL> wr = self . _io . write <EOL> if objs : <EOL> try : <EOL> for obj in objs [ : - <NUM_LIT:1> ] : <EOL> wr ( str ( obj ) ) <EOL> wr ( "<STR_LIT:U+0020>" ) <EOL> last = objs [ - <NUM_LIT:1> ] <EOL> if last is not None : <EOL> wr ( str ( last ) ) <EOL> wr ( "<STR_LIT:\n>" ) <EOL> except tty . PageQuitError : <EOL> return <EOL> else : <EOL> wr ( "<STR_LIT:\n>" ) <EOL> self . _io . flush ( ) <EOL> def pprint ( self , obj ) : <EOL> self . _format ( obj , <NUM_LIT:0> , <NUM_LIT:0> , set ( ) , <NUM_LIT:0> ) <EOL> self . _io . write ( "<STR_LIT:\n>" ) <EOL> self . _io . flush ( ) <EOL> def print_obj ( self , obj , nl = <NUM_LIT:1> ) : <EOL> if nl : <EOL> self . _io . write ( "<STR_LIT>" % ( obj , ) ) <EOL> else : <EOL> self . _io . write ( str ( obj ) ) <EOL> self . _io . flush ( ) <EOL> def print_list ( self , clist , indent = <NUM_LIT:0> ) : <EOL> if clist : <EOL> width = self . _termwidth - <NUM_LIT:9> <EOL> indent = min ( max ( indent , <NUM_LIT:0> ) , width ) <EOL> ps = "<STR_LIT:U+0020>" * indent <EOL> try : <EOL> for c in clist [ : - <NUM_LIT:1> ] : <EOL> cs = "<STR_LIT>" % ( c , ) <EOL> if len ( ps ) + len ( cs ) > width : <EOL> self . print_obj ( ps ) <EOL> ps = "<STR_LIT>" % ( "<STR_LIT:U+0020>" * indent , cs ) <EOL> else : <EOL> ps += cs <EOL> self . print_obj ( "<STR_LIT>" % ( ps , clist [ - <NUM_LIT:1> ] ) ) <EOL> except tty . PageQuitError : <EOL> pass <EOL> def write ( self , text ) : <EOL> self . _io . write ( text ) <EOL> def printf ( self , text ) : <EOL> "<STR_LIT>" <EOL> self . Print ( self . format ( text ) ) <EOL> def error ( self , text ) : <EOL> self . printf ( "<STR_LIT>" % ( text , ) ) <EOL> def warning ( self , text ) : <EOL> self . printf ( "<STR_LIT>" % ( text , ) ) <EOL> def _get_prompt ( self , name , prompt = None ) : <EOL> return self . prompt_format ( prompt or self . _env [ name ] ) <EOL> def user_input ( self , prompt = None ) : <EOL> return self . _io . raw_input ( self . _get_prompt ( "<STR_LIT>" , prompt ) ) <EOL> def more_user_input ( self ) : <EOL> return self . _io . raw_input ( self . _get_prompt ( "<STR_LIT>" ) ) <EOL> def choose ( self , somelist , defidx = <NUM_LIT:0> , prompt = None ) : <EOL> return cliutils . choose ( somelist , <EOL> defidx , <EOL> self . _get_prompt ( "<STR_LIT>" , prompt ) , <EOL> input = self . _io . raw_input , error = self . error ) <EOL> def choose_value ( self , somemap , default = None , prompt = None ) : <EOL> return cliutils . choose_value ( somemap , <EOL> default , <EOL> self . _get_prompt ( "<STR_LIT>" , prompt ) , <EOL> input = self . _io . raw_input , error = self . error ) <EOL> def choose_key ( self , somemap , default = None , prompt = None ) : <EOL> return cliutils . choose_key ( somemap , <EOL> default , <EOL> self . _get_prompt ( "<STR_LIT>" , prompt ) , <EOL> input = self . _io . raw_input , error = self . error ) <EOL> def choose_multiple ( self , somelist , chosen = None , prompt = None ) : <EOL> return cliutils . choose_multiple ( somelist , <EOL> chosen , <EOL> self . _get_prompt ( "<STR_LIT>" , prompt ) , <EOL> input = self . _io . raw_input , error = self . error ) <EOL> def choose_multiple_from_map ( self , somemap , chosen = None , prompt = None ) : <EOL> return cliutils . choose_multiple_from_map ( somemap , <EOL> chosen , <EOL> self . _get_prompt ( "<STR_LIT>" , prompt ) , <EOL> input = self . _io . raw_input , error = self . error ) <EOL> def get_text ( self , msg = None ) : <EOL> return cliutils . get_text ( self . _get_prompt ( "<STR_LIT>" ) , msg , input = self . _io . raw_input ) <EOL> def get_value ( self , prompt , default = None ) : <EOL> return cliutils . get_input ( self . prompt_format ( prompt ) , default , self . _io . raw_input ) <EOL> def edit_text ( self , text , prompt = None ) : <EOL> return cliutils . edit_text ( text , self . _get_prompt ( "<STR_LIT>" , prompt ) ) <EOL> def get_int ( self , prompt = "<STR_LIT>" , default = None ) : <EOL> return cliutils . get_int ( prompt , default , input = self . _io . raw_input , error = self . error ) <EOL> def get_float ( self , prompt = "<STR_LIT>" , default = None ) : <EOL> return cliutils . get_float ( prompt , default , input = self . _io . raw_input , error = self . error ) <EOL> def get_bool ( self , prompt = "<STR_LIT>" , default = None ) : <EOL> return cliutils . get_bool ( prompt , default , input = self . _io . raw_input , error = self . error ) <EOL> def yes_no ( self , prompt , default = True ) : <EOL> while <NUM_LIT:1> : <EOL> yesno = cliutils . get_input ( self . prompt_format ( prompt ) , "<STR_LIT:Y>" if default else "<STR_LIT:N>" , self . _io . raw_input ) <EOL> yesno = yesno . upper ( ) <EOL> if yesno . startswith ( "<STR_LIT:Y>" ) : <EOL> return True <EOL> elif yesno . startswith ( "<STR_LIT:N>" ) : <EOL> return False <EOL> else : <EOL> self . Print ( "<STR_LIT>" ) <EOL> def get_key ( self , prompt = "<STR_LIT>" ) : <EOL> return tty . get_key ( prompt ) <EOL> def get_password ( self , prompt = "<STR_LIT>" ) : <EOL> return tty . getpass ( prompt ) <EOL> def get_winsize ( self ) : <EOL> rows , cols , xpixel , ypixel = tty . get_winsize ( self . _io . fileno ( ) ) <EOL> return rows , cols <EOL> def _format_doc ( self , s , color ) : <EOL> i = s . find ( "<STR_LIT:\n>" ) <EOL> if i > <NUM_LIT:0> : <EOL> return color + s [ : i ] + self . _theme . NORMAL + self . format ( s [ i : ] ) + "<STR_LIT:\n>" <EOL> else : <EOL> return color + s + self . _theme . NORMAL + "<STR_LIT:\n>" <EOL> def help_local ( self , text ) : <EOL> self . Print ( self . _format_doc ( text , self . _theme . help_local ) ) <EOL> def help_inherited ( self , text ) : <EOL> self . Print ( self . _format_doc ( text , self . _theme . help_inherited ) ) <EOL> def help_created ( self , text ) : <EOL> self . Print ( self . _format_doc ( text , self . _theme . help_created ) ) <EOL> def prompt_format ( self , ps ) : <EOL> "<STR_LIT>" <EOL> self . _fsm . process_string ( ps ) <EOL> return self . _getarg ( ) <EOL> def format ( self , ps ) : <EOL> "<STR_LIT>" <EOL> self . _ffsm . process_string ( ps ) <EOL> if self . _ffsm . arg : <EOL> arg = self . _ffsm . arg <EOL> self . _ffsm . arg = '<STR_LIT>' <EOL> return arg <EOL> else : <EOL> return None <EOL> def format_wrap ( self , obj , formatstring ) : <EOL> return FormatWrapper ( obj , self , formatstring ) <EOL> def register_prompt_expansion ( self , key , func ) : <EOL> """<STR_LIT>""" <EOL> key = str ( key ) [ <NUM_LIT:0> ] <EOL> if not key in self . _PROMPT_EXPANSIONS : <EOL> self . _PROMPT_EXPANSIONS [ key ] = func <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % ( key , ) ) <EOL> def register_format_expansion ( self , key , func ) : <EOL> """<STR_LIT>""" <EOL> key = str ( key ) [ <NUM_LIT:0> ] <EOL> if key not in self . _FORMAT_EXPANSIONS : <EOL> self . _FORMAT_EXPANSIONS [ key ] = func <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % ( key , ) ) <EOL> def unregister_format_expansion ( self , key ) : <EOL> key = str ( key ) [ <NUM_LIT:0> ] <EOL> try : <EOL> del self . _FORMAT_EXPANSIONS [ key ] <EOL> except KeyError : <EOL> pass <EOL> def _initfsm ( self ) : <EOL> theme = self . _theme <EOL> self . _PROMPT_EXPANSIONS = { <EOL> "<STR_LIT:I>" : PROMPT_START_IGNORE + theme . BRIGHT + PROMPT_END_IGNORE , <EOL> "<STR_LIT:N>" : PROMPT_START_IGNORE + theme . NORMAL + PROMPT_END_IGNORE , <EOL> "<STR_LIT:D>" : PROMPT_START_IGNORE + theme . DEFAULT + PROMPT_END_IGNORE , <EOL> "<STR_LIT:R>" : PROMPT_START_IGNORE + theme . BRIGHTRED + PROMPT_END_IGNORE , <EOL> "<STR_LIT>" : PROMPT_START_IGNORE + theme . BRIGHTGREEN + PROMPT_END_IGNORE , <EOL> "<STR_LIT:Y>" : PROMPT_START_IGNORE + theme . BRIGHTYELLOW + PROMPT_END_IGNORE , <EOL> "<STR_LIT:B>" : PROMPT_START_IGNORE + theme . BRIGHTBLUE + PROMPT_END_IGNORE , <EOL> "<STR_LIT:M>" : PROMPT_START_IGNORE + theme . BRIGHTMAGENTA + PROMPT_END_IGNORE , <EOL> "<STR_LIT:C>" : PROMPT_START_IGNORE + theme . BRIGHTCYAN + PROMPT_END_IGNORE , <EOL> "<STR_LIT>" : PROMPT_START_IGNORE + theme . BRIGHTWHITE + PROMPT_END_IGNORE , <EOL> "<STR_LIT:r>" : PROMPT_START_IGNORE + theme . RED + PROMPT_END_IGNORE , <EOL> "<STR_LIT:g>" : PROMPT_START_IGNORE + theme . GREEN + PROMPT_END_IGNORE , <EOL> "<STR_LIT:y>" : PROMPT_START_IGNORE + theme . YELLOW + PROMPT_END_IGNORE , <EOL> "<STR_LIT:b>" : PROMPT_START_IGNORE + theme . BLUE + PROMPT_END_IGNORE , <EOL> "<STR_LIT:m>" : PROMPT_START_IGNORE + theme . MAGENTA + PROMPT_END_IGNORE , <EOL> "<STR_LIT:c>" : PROMPT_START_IGNORE + theme . CYAN + PROMPT_END_IGNORE , <EOL> "<STR_LIT:w>" : PROMPT_START_IGNORE + theme . WHITE + PROMPT_END_IGNORE , <EOL> "<STR_LIT:n>" : "<STR_LIT:\n>" , "<STR_LIT:l>" : self . _tty , "<STR_LIT:h>" : self . _hostname , "<STR_LIT:u>" : self . _username , <EOL> "<STR_LIT:$>" : self . _priv , "<STR_LIT:d>" : self . _cwd , "<STR_LIT:L>" : self . _shlvl , "<STR_LIT:t>" : self . _time , <EOL> "<STR_LIT:T>" : self . _date } <EOL> self . _FORMAT_EXPANSIONS = { <EOL> "<STR_LIT:I>" : theme . BRIGHT , <EOL> "<STR_LIT:N>" : theme . NORMAL , <EOL> "<STR_LIT:D>" : theme . DEFAULT , <EOL> "<STR_LIT:R>" : theme . BRIGHTRED , <EOL> "<STR_LIT>" : theme . BRIGHTGREEN , <EOL> "<STR_LIT:Y>" : theme . BRIGHTYELLOW , <EOL> "<STR_LIT:B>" : theme . BRIGHTBLUE , <EOL> "<STR_LIT:M>" : theme . BRIGHTMAGENTA , <EOL> "<STR_LIT:C>" : theme . BRIGHTCYAN , <EOL> "<STR_LIT>" : theme . BRIGHTWHITE , <EOL> "<STR_LIT:r>" : theme . RED , <EOL> "<STR_LIT:g>" : theme . GREEN , <EOL> "<STR_LIT:y>" : theme . YELLOW , <EOL> "<STR_LIT:b>" : theme . BLUE , <EOL> "<STR_LIT:m>" : theme . MAGENTA , <EOL> "<STR_LIT:c>" : theme . CYAN , <EOL> "<STR_LIT:w>" : theme . WHITE , <EOL> "<STR_LIT:n>" : "<STR_LIT:\n>" , "<STR_LIT:l>" : self . _tty , "<STR_LIT:h>" : self . _hostname , "<STR_LIT:u>" : self . _username , <EOL> "<STR_LIT:$>" : self . _priv , "<STR_LIT:d>" : self . _cwd , "<STR_LIT:L>" : self . _shlvl , "<STR_LIT:t>" : self . _time , <EOL> "<STR_LIT:T>" : self . _date } <EOL> fp = FSM ( <NUM_LIT:0> ) <EOL> fp . add_default_transition ( self . _error , <NUM_LIT:0> ) <EOL> fp . add_transition ( ANY , <NUM_LIT:0> , self . _addtext , <NUM_LIT:0> ) <EOL> fp . add_transition ( "<STR_LIT:%>" , <NUM_LIT:0> , None , <NUM_LIT:1> ) <EOL> fp . add_transition ( "<STR_LIT:%>" , <NUM_LIT:1> , self . _addtext , <NUM_LIT:0> ) <EOL> fp . add_transition ( "<STR_LIT:{>" , <NUM_LIT:1> , self . _startvar , <NUM_LIT:2> ) <EOL> fp . add_transition ( "<STR_LIT:}>" , <NUM_LIT:2> , self . _endvar , <NUM_LIT:0> ) <EOL> fp . add_transition ( ANY , <NUM_LIT:2> , self . _vartext , <NUM_LIT:2> ) <EOL> fp . add_transition ( ANY , <NUM_LIT:1> , self . _prompt_expand , <NUM_LIT:0> ) <EOL> fp . arg = '<STR_LIT>' <EOL> self . _fsm = fp <EOL> ff = FSM ( <NUM_LIT:0> ) <EOL> ff . add_default_transition ( self . _error , <NUM_LIT:0> ) <EOL> ff . add_transition ( ANY , <NUM_LIT:0> , self . _addtext , <NUM_LIT:0> ) <EOL> ff . add_transition ( "<STR_LIT:%>" , <NUM_LIT:0> , None , <NUM_LIT:1> ) <EOL> ff . add_transition ( "<STR_LIT:%>" , <NUM_LIT:1> , self . _addtext , <NUM_LIT:0> ) <EOL> ff . add_transition ( "<STR_LIT:{>" , <NUM_LIT:1> , self . _startvar , <NUM_LIT:2> ) <EOL> ff . add_transition ( "<STR_LIT:}>" , <NUM_LIT:2> , self . _endvar , <NUM_LIT:0> ) <EOL> ff . add_transition ( ANY , <NUM_LIT:2> , self . _vartext , <NUM_LIT:2> ) <EOL> ff . add_transition ( ANY , <NUM_LIT:1> , self . _format_expand , <NUM_LIT:0> ) <EOL> ff . arg = '<STR_LIT>' <EOL> self . _ffsm = ff <EOL> def _startvar ( self , c , fsm ) : <EOL> fsm . varname = "<STR_LIT>" <EOL> def _vartext ( self , c , fsm ) : <EOL> fsm . varname += c <EOL> def _endvar ( self , c , fsm ) : <EOL> fsm . arg += str ( self . _env . get ( fsm . varname , fsm . varname ) ) <EOL> def _prompt_expand ( self , c , fsm ) : <EOL> return self . _expand ( c , fsm , self . _PROMPT_EXPANSIONS ) <EOL> def _format_expand ( self , c , fsm ) : <EOL> return self . _expand ( c , fsm , self . _FORMAT_EXPANSIONS ) <EOL> def _expand ( self , c , fsm , mapping ) : <EOL> try : <EOL> arg = self . _cache [ c ] <EOL> except KeyError : <EOL> try : <EOL> arg = mapping [ c ] <EOL> except KeyError : <EOL> arg = c <EOL> else : <EOL> if callable ( arg ) : <EOL> arg = str ( arg ( c ) ) <EOL> fsm . arg += arg <EOL> def _username ( self , c ) : <EOL> un = os . environ . get ( "<STR_LIT>" ) or os . environ . get ( "<STR_LIT>" ) <EOL> if un : <EOL> self . _cache [ c ] = un <EOL> return un <EOL> def _shlvl ( self , c ) : <EOL> return str ( self . _env . get ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> def _hostname ( self , c ) : <EOL> hn = os . uname ( ) [ <NUM_LIT:1> ] <EOL> self . _cache [ c ] = hn <EOL> return hn <EOL> def _priv ( self , c ) : <EOL> if os . getuid ( ) == <NUM_LIT:0> : <EOL> arg = "<STR_LIT:#>" <EOL> else : <EOL> arg = "<STR_LIT:>>" <EOL> self . _cache [ c ] = arg <EOL> return arg <EOL> def _tty ( self , c ) : <EOL> n = os . ttyname ( self . _io . fileno ( ) ) <EOL> self . _cache [ c ] = n <EOL> return n <EOL> def _cwd ( self , c ) : <EOL> return os . getcwd ( ) <EOL> def _time ( self , c ) : <EOL> return time . strftime ( "<STR_LIT>" , time . localtime ( ) ) <EOL> def _date ( self , c ) : <EOL> return time . strftime ( "<STR_LIT>" , time . localtime ( ) ) <EOL> def _error ( self , input_symbol , fsm ) : <EOL> self . _io . errlog ( '<STR_LIT>' % ( input_symbol , fsm . stack ) ) <EOL> fsm . reset ( ) <EOL> def _addtext ( self , c , fsm ) : <EOL> fsm . arg += c <EOL> def _getarg ( self ) : <EOL> if self . _fsm . arg : <EOL> arg = self . _fsm . arg <EOL> self . _fsm . arg = '<STR_LIT>' <EOL> return arg <EOL> else : <EOL> return None <EOL> def _format ( self , obj , indent , allowance , context , level ) : <EOL> level = level + <NUM_LIT:1> <EOL> objid = id ( obj ) <EOL> if objid in context : <EOL> self . _io . write ( _recursion ( obj ) ) <EOL> return <EOL> rep = self . _repr ( obj , context , level - <NUM_LIT:1> ) <EOL> typ = type ( obj ) <EOL> sep_lines = len ( rep ) > ( self . _termwidth - <NUM_LIT:1> - indent - allowance ) <EOL> write = self . _io . write <EOL> if sep_lines : <EOL> if typ is dict : <EOL> write ( '<STR_LIT>' ) <EOL> length = len ( obj ) <EOL> if length : <EOL> context [ objid ] = <NUM_LIT:1> <EOL> indent = indent + <NUM_LIT:2> <EOL> items = obj . items ( ) <EOL> items . sort ( ) <EOL> key , ent = items [ <NUM_LIT:0> ] <EOL> rep = self . _repr ( key , context , level ) <EOL> write ( rep ) <EOL> write ( '<STR_LIT>' ) <EOL> self . _format ( ent , indent + len ( rep ) + <NUM_LIT:2> , allowance + <NUM_LIT:1> , context , level ) <EOL> if length > <NUM_LIT:1> : <EOL> for key , ent in items [ <NUM_LIT:1> : ] : <EOL> rep = self . _repr ( key , context , level ) <EOL> write ( '<STR_LIT>' % ( '<STR_LIT:U+0020>' * indent , rep ) ) <EOL> self . _format ( ent , indent + len ( rep ) + <NUM_LIT:2> , allowance + <NUM_LIT:1> , context , level ) <EOL> indent = indent - <NUM_LIT:2> <EOL> del context [ objid ] <EOL> write ( '<STR_LIT>' ) <EOL> return <EOL> if typ is list : <EOL> write ( '<STR_LIT>' ) <EOL> self . print_list ( obj , <NUM_LIT:2> ) <EOL> write ( '<STR_LIT:]>' ) <EOL> return <EOL> if typ is tuple : <EOL> write ( '<STR_LIT>' ) <EOL> self . print_list ( obj , <NUM_LIT:2> ) <EOL> if len ( obj ) == <NUM_LIT:1> : <EOL> write ( '<STR_LIT:U+002C>' ) <EOL> write ( '<STR_LIT:)>' ) <EOL> return <EOL> write ( rep ) <EOL> def _repr ( self , obj , context , level ) : <EOL> return self . _safe_repr ( obj , context . copy ( ) , None , level ) <EOL> def _safe_repr ( self , obj , context , maxlevels , level ) : <EOL> return _safe_repr ( obj , context , maxlevels , level ) <EOL> class FormatWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , obj , ui , format ) : <EOL> self . value = obj <EOL> self . _ui = ui <EOL> self . _format = format <EOL> def __str__ ( self ) : <EOL> self . _ui . register_format_expansion ( "<STR_LIT:O>" , self . _str_value ) <EOL> try : <EOL> return self . _ui . format ( self . _format ) <EOL> finally : <EOL> self . _ui . unregister_format_expansion ( "<STR_LIT:O>" ) <EOL> def _str_value ( self , c ) : <EOL> return str ( self . value ) <EOL> def __len__ ( self ) : <EOL> return len ( str ( self . value ) ) <EOL> def __repr__ ( self ) : <EOL> return _safe_repr ( self . value , set ( ) , None , <NUM_LIT:0> ) <EOL> def __cmp__ ( self , other ) : <EOL> if type ( other ) is FormatWrapper : <EOL> return cmp ( self . value , other . value ) <EOL> else : <EOL> return cmp ( self . value , other ) <EOL> def safe_repr ( value ) : <EOL> """<STR_LIT>""" <EOL> return _safe_repr ( value , set ( ) , None , <NUM_LIT:0> ) <EOL> def _safe_repr ( obj , context , maxlevels , level ) : <EOL> typ = type ( obj ) <EOL> if typ is str : <EOL> if '<STR_LIT>' not in sys . modules : <EOL> return repr ( obj ) <EOL> if "<STR_LIT:'>" in obj and '<STR_LIT:">' not in obj : <EOL> closure = '<STR_LIT:">' <EOL> quotes = { '<STR_LIT:">' : '<STR_LIT>' } <EOL> else : <EOL> closure = "<STR_LIT:'>" <EOL> quotes = { "<STR_LIT:'>" : "<STR_LIT>" } <EOL> qget = quotes . get <EOL> sio = BytesIO ( ) <EOL> write = sio . write <EOL> for char in obj : <EOL> if char . isalpha ( ) : <EOL> write ( char ) <EOL> else : <EOL> write ( qget ( char , repr ( char ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) ) <EOL> return ( "<STR_LIT>" % ( closure , sio . getvalue ( ) , closure ) ) <EOL> if typ is dict : <EOL> if not obj : <EOL> return "<STR_LIT:{}>" <EOL> objid = id ( obj ) <EOL> if maxlevels and level > maxlevels : <EOL> return "<STR_LIT>" <EOL> if objid in context : <EOL> return _recursion ( obj ) <EOL> context [ objid ] = <NUM_LIT:1> <EOL> components = [ ] <EOL> append = components . append <EOL> level += <NUM_LIT:1> <EOL> saferepr = _safe_repr <EOL> for k , v in obj . iteritems ( ) : <EOL> krepr = saferepr ( k , context , maxlevels , level ) <EOL> vrepr = saferepr ( v , context , maxlevels , level ) <EOL> append ( "<STR_LIT>" % ( krepr , vrepr ) ) <EOL> del context [ objid ] <EOL> return "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( components ) <EOL> if typ is list or typ is tuple : <EOL> if typ is list : <EOL> if not obj : <EOL> return "<STR_LIT>" <EOL> format = "<STR_LIT>" <EOL> elif len ( obj ) == <NUM_LIT:1> : <EOL> format = "<STR_LIT>" <EOL> else : <EOL> if not obj : <EOL> return "<STR_LIT>" <EOL> format = "<STR_LIT>" <EOL> objid = id ( obj ) <EOL> if maxlevels and level > maxlevels : <EOL> return format % "<STR_LIT>" <EOL> if objid in context : <EOL> return _recursion ( obj ) <EOL> context [ objid ] = <NUM_LIT:1> <EOL> components = [ ] <EOL> append = components . append <EOL> level += <NUM_LIT:1> <EOL> for o in obj : <EOL> orepr = _safe_repr ( o , context , maxlevels , level ) <EOL> append ( orepr ) <EOL> del context [ objid ] <EOL> return format % "<STR_LIT:U+002CU+0020>" . join ( components ) <EOL> if typ is MethodType : <EOL> return method_repr ( obj ) <EOL> if typ is FunctionType : <EOL> return function_repr ( obj ) <EOL> return repr ( obj ) <EOL> def _recursion ( obj ) : <EOL> return ( "<STR_LIT>" % ( type ( obj ) . __name__ , id ( obj ) ) ) <EOL> def method_repr ( method ) : <EOL> methname = method . im_func . func_name <EOL> varnames = list ( method . im_func . func_code . co_varnames ) [ : method . im_func . func_code . co_argcount ] <EOL> if method . im_func . func_defaults : <EOL> ld = len ( method . im_func . func_defaults ) <EOL> varlist = [ "<STR_LIT:U+002CU+0020>" . join ( varnames [ : - ld ] ) , <EOL> "<STR_LIT:U+002CU+0020>" . join ( [ "<STR_LIT>" % ( n , v ) for n , v in zip ( varnames [ - ld : ] , method . im_func . func_defaults ) ] ) ] <EOL> return "<STR_LIT>" % ( methname , "<STR_LIT:U+002CU+0020>" . join ( varlist ) ) <EOL> else : <EOL> return "<STR_LIT>" % ( methname , "<STR_LIT:U+002CU+0020>" . join ( varnames ) ) <EOL> def function_repr ( func ) : <EOL> methname = func . func_name <EOL> argcount = func . func_code . co_argcount <EOL> varnames = list ( func . func_code . co_varnames ) [ : argcount ] <EOL> if func . func_defaults : <EOL> ld = len ( func . func_defaults ) <EOL> varlist = varnames [ : - ld ] <EOL> varlist . extend ( [ "<STR_LIT>" % ( n , v ) for n , v in zip ( varnames [ - ld : ] , func . func_defaults ) ] ) <EOL> return "<STR_LIT>" % ( methname , "<STR_LIT:U+002CU+0020>" . join ( varlist ) ) <EOL> else : <EOL> return "<STR_LIT>" % ( methname , "<STR_LIT:U+002CU+0020>" . join ( varnames ) ) <EOL> def _get_object ( name ) : <EOL> try : <EOL> return getattr ( sys . modules [ __name__ ] , name ) <EOL> except AttributeError : <EOL> i = name . rfind ( "<STR_LIT:.>" ) <EOL> if i >= <NUM_LIT:0> : <EOL> modname = name [ : i ] <EOL> try : <EOL> mod = sys . modules [ modname ] <EOL> except KeyError : <EOL> try : <EOL> mod = __import__ ( modname , globals ( ) , locals ( ) , [ "<STR_LIT:*>" ] ) <EOL> except ImportError as err : <EOL> raise UIFindError ( "<STR_LIT>" % ( modname , err ) ) <EOL> try : <EOL> return getattr ( mod , name [ i + <NUM_LIT:1> : ] ) <EOL> except AttributeError : <EOL> raise UIFindError ( "<STR_LIT>" % ( name , modname ) ) <EOL> else : <EOL> raise UIFindError ( "<STR_LIT>" % ( name , ) ) <EOL> def get_userinterface ( uiname = "<STR_LIT>" , <EOL> ioname = "<STR_LIT>" , themename = None ) : <EOL> if type ( ioname ) is str : <EOL> ioobj = _get_object ( ioname ) <EOL> elif hasattr ( ioname , "<STR_LIT>" ) : <EOL> ioobj = ioname <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not hasattr ( ioobj , "<STR_LIT>" ) : <EOL> raise UIFindError ( "<STR_LIT>" % ( ioobj , ) ) <EOL> uiobj = _get_object ( uiname ) <EOL> if not hasattr ( uiobj , "<STR_LIT>" ) : <EOL> raise UIFindError ( "<STR_LIT>" % ( uiobj , ) ) <EOL> if themename is not None : <EOL> themeobj = _get_object ( themename ) <EOL> if not issubclass ( themeobj , Theme ) : <EOL> raise UIFindError ( "<STR_LIT>" % ( themeobj , ) ) <EOL> return uiobj ( ioobj ( ) , theme = themeobj ( ) ) <EOL> else : <EOL> return uiobj ( ioobj ( ) ) <EOL> def _test ( argv ) : <EOL> ui = get_userinterface ( ) <EOL> ui . Print ( "<STR_LIT>" ) <EOL> inp = ui . user_input ( "<STR_LIT>" ) <EOL> ui . Print ( "<STR_LIT>" , inp ) <EOL> return ui <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> ui = _test ( sys . argv ) </s>
