<s> from __future__ import print_function <EOL> __author__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> """<STR_LIT>""" <EOL> from debug import Debuggable <EOL> import sys <EOL> from difflib import SequenceMatcher <EOL> import locale <EOL> class Interactive ( Debuggable ) : <EOL> def __init__ ( self , debug ) : <EOL> self . debug = debug <EOL> Debuggable . __init__ ( self , '<STR_LIT>' ) <EOL> self . COLOR_ESCAPE = "<STR_LIT>" <EOL> self . DARK_COLORS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . LIGHT_COLORS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . RESET_COLOR = self . COLOR_ESCAPE + "<STR_LIT>" <EOL> def input_options ( self , options , require = False , prompt = None , fallback_prompt = None , <EOL> numrange = None , default = None , max_width = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> letters = { } <EOL> display_letters = [ ] <EOL> capitalized = [ ] <EOL> first = True <EOL> for option in options : <EOL> for letter in option : <EOL> if letter . isalpha ( ) and letter . upper ( ) == letter : <EOL> found_letter = letter <EOL> break <EOL> else : <EOL> for letter in option : <EOL> if not letter . isalpha ( ) : <EOL> continue <EOL> if letter not in letters : <EOL> found_letter = letter <EOL> break <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> letters [ found_letter . lower ( ) ] = option <EOL> index = option . index ( found_letter ) <EOL> if not require and ( ( default is None and not numrange and first ) or <EOL> ( isinstance ( default , basestring ) and <EOL> found_letter . lower ( ) == default . lower ( ) ) ) : <EOL> show_letter = '<STR_LIT>' % found_letter . upper ( ) <EOL> is_default = True <EOL> else : <EOL> show_letter = found_letter . upper ( ) <EOL> is_default = False <EOL> show_letter = self . colorize ( '<STR_LIT>' if is_default else '<STR_LIT>' , <EOL> show_letter ) <EOL> capitalized . append ( <EOL> option [ : index ] + show_letter + option [ index + <NUM_LIT:1> : ] <EOL> ) <EOL> display_letters . append ( found_letter . upper ( ) ) <EOL> first = False <EOL> if require : <EOL> default = None <EOL> elif default is None : <EOL> if numrange : <EOL> default = numrange [ <NUM_LIT:0> ] <EOL> else : <EOL> default = display_letters [ <NUM_LIT:0> ] . lower ( ) <EOL> if not prompt : <EOL> prompt_parts = [ ] <EOL> prompt_part_lengths = [ ] <EOL> if numrange : <EOL> if isinstance ( default , int ) : <EOL> default_name = str ( default ) <EOL> default_name = self . colorize ( '<STR_LIT>' , default_name ) <EOL> tmpl = '<STR_LIT>' <EOL> prompt_parts . append ( tmpl % default_name ) <EOL> prompt_part_lengths . append ( len ( tmpl % str ( default ) ) ) <EOL> else : <EOL> prompt_parts . append ( '<STR_LIT>' ) <EOL> prompt_part_lengths . append ( len ( prompt_parts [ - <NUM_LIT:1> ] ) ) <EOL> prompt_parts += capitalized <EOL> prompt_part_lengths += [ len ( s ) for s in options ] <EOL> prompt = '<STR_LIT>' <EOL> line_length = <NUM_LIT:0> <EOL> for i , ( part , length ) in enumerate ( zip ( prompt_parts , <EOL> prompt_part_lengths ) ) : <EOL> if i == len ( prompt_parts ) - <NUM_LIT:1> : <EOL> part += '<STR_LIT:?>' <EOL> else : <EOL> part += '<STR_LIT:U+002C>' <EOL> length += <NUM_LIT:1> <EOL> if line_length + length + <NUM_LIT:1> > max_width : <EOL> prompt += '<STR_LIT:\n>' <EOL> line_length = <NUM_LIT:0> <EOL> if line_length != <NUM_LIT:0> : <EOL> part = '<STR_LIT:U+0020>' + part <EOL> length += <NUM_LIT:1> <EOL> prompt += part <EOL> line_length += length <EOL> if not fallback_prompt : <EOL> fallback_prompt = '<STR_LIT>' <EOL> if numrange : <EOL> fallback_prompt += '<STR_LIT>' % numrange <EOL> fallback_prompt += '<STR_LIT:U+002CU+0020>' . join ( display_letters ) + '<STR_LIT::>' <EOL> resp = self . input_ ( prompt ) <EOL> while True : <EOL> resp = resp . strip ( ) . lower ( ) <EOL> if default is not None and not resp : <EOL> resp = default <EOL> if numrange : <EOL> try : <EOL> resp = int ( resp ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> low , high = numrange <EOL> if low <= resp <= high : <EOL> return resp <EOL> else : <EOL> resp = None <EOL> if resp : <EOL> resp = resp [ <NUM_LIT:0> ] <EOL> if resp in letters : <EOL> return resp <EOL> resp = self . input_ ( fallback_prompt ) <EOL> def input_ ( self , prompt = None ) : <EOL> """<STR_LIT>""" <EOL> if prompt : <EOL> if isinstance ( prompt , unicode ) : <EOL> prompt = prompt . encode ( self . _encoding ( ) , '<STR_LIT:replace>' ) <EOL> print ( prompt , end = '<STR_LIT:U+0020>' ) <EOL> try : <EOL> resp = raw_input ( ) <EOL> except EOFError : <EOL> self . debug . print_debug ( '<STR_LIT>' ) <EOL> return resp . decode ( sys . stdin . encoding or '<STR_LIT:utf8>' , '<STR_LIT:ignore>' ) <EOL> def _encoding ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return locale . getdefaultlocale ( ) [ <NUM_LIT:1> ] or '<STR_LIT:utf8>' <EOL> except ValueError : <EOL> return '<STR_LIT:utf8>' <EOL> def _colorize ( self , color , text ) : <EOL> """<STR_LIT>""" <EOL> if color in self . DARK_COLORS : <EOL> escape = self . COLOR_ESCAPE + "<STR_LIT>" % ( self . DARK_COLORS . index ( color ) + <NUM_LIT:30> ) <EOL> elif color in self . LIGHT_COLORS : <EOL> escape = self . COLOR_ESCAPE + "<STR_LIT>" % ( self . LIGHT_COLORS . index ( color ) + <NUM_LIT:30> ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' , color ) <EOL> return escape + text + self . RESET_COLOR <EOL> def colorize ( self , color , text ) : <EOL> """<STR_LIT>""" <EOL> return self . _colorize ( color , text ) <EOL> def _colordiff ( self , a , b , highlight = '<STR_LIT>' , minor_highlight = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( a , basestring ) or not isinstance ( b , basestring ) : <EOL> a = unicode ( a ) <EOL> b = unicode ( b ) <EOL> if a == b : <EOL> return a , b <EOL> else : <EOL> return self . colorize ( highlight , a ) , self . colorize ( highlight , b ) <EOL> if isinstance ( a , bytes ) or isinstance ( b , bytes ) : <EOL> a = self . displayable_path ( a ) <EOL> b = self . displayable_path ( b ) <EOL> a_out = [ ] <EOL> b_out = [ ] <EOL> matcher = SequenceMatcher ( lambda x : False , a , b ) <EOL> for op , a_start , a_end , b_start , b_end in matcher . get_opcodes ( ) : <EOL> if op == '<STR_LIT>' : <EOL> a_out . append ( a [ a_start : a_end ] ) <EOL> b_out . append ( b [ b_start : b_end ] ) <EOL> elif op == '<STR_LIT>' : <EOL> b_out . append ( self . colorize ( highlight , b [ b_start : b_end ] ) ) <EOL> elif op == '<STR_LIT>' : <EOL> a_out . append ( self . colorize ( highlight , a [ a_start : a_end ] ) ) <EOL> elif op == '<STR_LIT:replace>' : <EOL> if a [ a_start : a_end ] . lower ( ) != b [ b_start : b_end ] . lower ( ) : <EOL> color = highlight <EOL> else : <EOL> color = minor_highlight <EOL> a_out . append ( self . colorize ( color , a [ a_start : a_end ] ) ) <EOL> b_out . append ( self . colorize ( color , b [ b_start : b_end ] ) ) <EOL> else : <EOL> assert ( False ) <EOL> return u'<STR_LIT>' . join ( a_out ) , u'<STR_LIT>' . join ( b_out ) <EOL> def displayable_path ( self , path , separator = u'<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( path , ( list , tuple ) ) : <EOL> return separator . join ( self . displayable_path ( p ) for p in path ) <EOL> elif isinstance ( path , unicode ) : <EOL> return path <EOL> elif not isinstance ( path , str ) : <EOL> return unicode ( path ) <EOL> try : <EOL> return path . decode ( self . _fsencoding ( ) , '<STR_LIT:ignore>' ) <EOL> except ( UnicodeError , LookupError ) : <EOL> return path . decode ( '<STR_LIT:utf8>' , '<STR_LIT:ignore>' ) <EOL> def _fsencoding ( self ) : <EOL> """<STR_LIT>""" <EOL> encoding = sys . getfilesystemencoding ( ) or sys . getdefaultencoding ( ) <EOL> if encoding == '<STR_LIT>' : <EOL> encoding = '<STR_LIT:utf8>' <EOL> return encoding <EOL> def colordiff ( self , a , b , highlight = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if self . gv . settings . get_setting ( '<STR_LIT>' , self ) == '<STR_LIT:True>' : <EOL> return self . _colordiff ( a , b , highlight ) <EOL> else : <EOL> return unicode ( a ) , unicode ( b ) <EOL> def print_ ( self , * strings ) : <EOL> """<STR_LIT>""" <EOL> if strings : <EOL> if isinstance ( strings [ <NUM_LIT:0> ] , unicode ) : <EOL> txt = u'<STR_LIT:U+0020>' . join ( strings ) <EOL> else : <EOL> txt = '<STR_LIT:U+0020>' . join ( strings ) <EOL> else : <EOL> txt = u'<STR_LIT>' <EOL> if isinstance ( txt , unicode ) : <EOL> txt = txt . encode ( self . _encoding ( ) , '<STR_LIT:replace>' ) <EOL> print ( txt ) <EOL> def color_diff_suffix ( self , a , b , highlight = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> a , b = unicode ( a ) , unicode ( b ) <EOL> if not self . gv . settings . get_setting ( '<STR_LIT>' , self ) == '<STR_LIT:True>' : <EOL> return a , b <EOL> if a == b : <EOL> return a , b <EOL> first_diff = None <EOL> for i in range ( min ( len ( a ) , len ( b ) ) ) : <EOL> if a [ i ] != b [ i ] : <EOL> first_diff = i <EOL> break <EOL> else : <EOL> first_diff = min ( len ( a ) , len ( b ) ) <EOL> return a [ : first_diff ] + self . colorize ( highlight , a [ first_diff : ] ) , b [ : first_diff ] + self . colorize ( highlight , b [ first_diff : ] ) <EOL> def choose_candidate ( self , candidates , manipulate , opts , item = None , itemcount = None ) : <EOL> self . print_ ( u'<STR_LIT>' ) <EOL> for i , match in enumerate ( candidates ) : <EOL> line = [ <EOL> u'<STR_LIT>' . format ( i + <NUM_LIT:1> ) , <EOL> u'<STR_LIT>' . format ( manipulate . get_stripped_text ( match . reference_to_link ) <EOL> ) <EOL> ] <EOL> self . print_ ( '<STR_LIT:U+0020>' . join ( line ) ) <EOL> sel = self . input_options ( opts , numrange = ( <NUM_LIT:1> , len ( candidates ) ) ) <EOL> return sel </s>
