<s> """<STR_LIT>""" <EOL> import string <EOL> __all__ = [ "<STR_LIT>" ] <EOL> PROMPT = '<STR_LIT>' <EOL> IDENTCHARS = string . ascii_letters + string . digits + '<STR_LIT:_>' <EOL> class Cmd : <EOL> """<STR_LIT>""" <EOL> prompt = PROMPT <EOL> identchars = IDENTCHARS <EOL> ruler = '<STR_LIT:=>' <EOL> lastcmd = '<STR_LIT>' <EOL> intro = None <EOL> doc_leader = "<STR_LIT>" <EOL> doc_header = "<STR_LIT>" <EOL> misc_header = "<STR_LIT>" <EOL> undoc_header = "<STR_LIT>" <EOL> nohelp = "<STR_LIT>" <EOL> use_rawinput = <NUM_LIT:1> <EOL> def __init__ ( self , completekey = '<STR_LIT>' , stdin = None , stdout = None ) : <EOL> """<STR_LIT>""" <EOL> import sys <EOL> if stdin is not None : <EOL> self . stdin = stdin <EOL> else : <EOL> self . stdin = sys . stdin <EOL> if stdout is not None : <EOL> self . stdout = stdout <EOL> else : <EOL> self . stdout = sys . stdout <EOL> self . cmdqueue = [ ] <EOL> self . completekey = completekey <EOL> def cmdloop ( self , intro = None ) : <EOL> """<STR_LIT>""" <EOL> self . preloop ( ) <EOL> if self . use_rawinput and self . completekey : <EOL> try : <EOL> import readline <EOL> self . old_completer = readline . get_completer ( ) <EOL> readline . set_completer ( self . complete ) <EOL> readline . parse_and_bind ( self . completekey + "<STR_LIT>" ) <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> if intro is not None : <EOL> self . intro = intro <EOL> if self . intro : <EOL> self . stdout . write ( str ( self . intro ) + "<STR_LIT:\n>" ) <EOL> stop = None <EOL> while not stop : <EOL> if self . cmdqueue : <EOL> line = self . cmdqueue . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> if self . use_rawinput : <EOL> try : <EOL> line = raw_input ( self . prompt ) <EOL> except EOFError : <EOL> line = '<STR_LIT>' <EOL> else : <EOL> self . stdout . write ( self . prompt ) <EOL> self . stdout . flush ( ) <EOL> line = self . stdin . readline ( ) <EOL> if not len ( line ) : <EOL> line = '<STR_LIT>' <EOL> else : <EOL> line = line . rstrip ( '<STR_LIT:\r\n>' ) <EOL> line = self . precmd ( line ) <EOL> stop = self . onecmd ( line ) <EOL> stop = self . postcmd ( stop , line ) <EOL> self . postloop ( ) <EOL> finally : <EOL> if self . use_rawinput and self . completekey : <EOL> try : <EOL> import readline <EOL> readline . set_completer ( self . old_completer ) <EOL> except ImportError : <EOL> pass <EOL> def precmd ( self , line ) : <EOL> """<STR_LIT>""" <EOL> return line <EOL> def postcmd ( self , stop , line ) : <EOL> """<STR_LIT>""" <EOL> return stop <EOL> def preloop ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def postloop ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def parseline ( self , line ) : <EOL> """<STR_LIT>""" <EOL> line = line . strip ( ) <EOL> if not line : <EOL> return None , None , line <EOL> elif line [ <NUM_LIT:0> ] == '<STR_LIT:?>' : <EOL> line = '<STR_LIT>' + line [ <NUM_LIT:1> : ] <EOL> elif line [ <NUM_LIT:0> ] == '<STR_LIT:!>' : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> line = '<STR_LIT>' + line [ <NUM_LIT:1> : ] <EOL> else : <EOL> return None , None , line <EOL> i , n = <NUM_LIT:0> , len ( line ) <EOL> while i < n and line [ i ] in self . identchars : i = i + <NUM_LIT:1> <EOL> cmd , arg = line [ : i ] , line [ i : ] . strip ( ) <EOL> return cmd , arg , line <EOL> def onecmd ( self , line ) : <EOL> """<STR_LIT>""" <EOL> cmd , arg , line = self . parseline ( line ) <EOL> if not line : <EOL> return self . emptyline ( ) <EOL> if cmd is None : <EOL> return self . default ( line ) <EOL> self . lastcmd = line <EOL> if line == '<STR_LIT>' : <EOL> self . lastcmd = '<STR_LIT>' <EOL> if cmd == '<STR_LIT>' : <EOL> return self . default ( line ) <EOL> else : <EOL> try : <EOL> func = getattr ( self , '<STR_LIT>' + cmd ) <EOL> except AttributeError : <EOL> return self . default ( line ) <EOL> return func ( arg ) <EOL> def emptyline ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . lastcmd : <EOL> return self . onecmd ( self . lastcmd ) <EOL> def default ( self , line ) : <EOL> """<STR_LIT>""" <EOL> self . stdout . write ( '<STR_LIT>' % line ) <EOL> def completedefault ( self , * ignored ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> def completenames ( self , text , * ignored ) : <EOL> dotext = '<STR_LIT>' + text <EOL> return [ a [ <NUM_LIT:3> : ] for a in self . get_names ( ) if a . startswith ( dotext ) ] <EOL> def complete ( self , text , state ) : <EOL> """<STR_LIT>""" <EOL> if state == <NUM_LIT:0> : <EOL> import readline <EOL> origline = readline . get_line_buffer ( ) <EOL> line = origline . lstrip ( ) <EOL> stripped = len ( origline ) - len ( line ) <EOL> begidx = readline . get_begidx ( ) - stripped <EOL> endidx = readline . get_endidx ( ) - stripped <EOL> if begidx > <NUM_LIT:0> : <EOL> cmd , args , foo = self . parseline ( line ) <EOL> if cmd == '<STR_LIT>' : <EOL> compfunc = self . completedefault <EOL> else : <EOL> try : <EOL> compfunc = getattr ( self , '<STR_LIT>' + cmd ) <EOL> except AttributeError : <EOL> compfunc = self . completedefault <EOL> else : <EOL> compfunc = self . completenames <EOL> self . completion_matches = compfunc ( text , line , begidx , endidx ) <EOL> try : <EOL> return self . completion_matches [ state ] <EOL> except IndexError : <EOL> return None <EOL> def get_names ( self ) : <EOL> return dir ( self . __class__ ) <EOL> def complete_help ( self , * args ) : <EOL> commands = set ( self . completenames ( * args ) ) <EOL> topics = set ( a [ <NUM_LIT:5> : ] for a in self . get_names ( ) <EOL> if a . startswith ( '<STR_LIT>' + args [ <NUM_LIT:0> ] ) ) <EOL> return list ( commands | topics ) <EOL> def do_help ( self , arg ) : <EOL> '<STR_LIT>' <EOL> if arg : <EOL> try : <EOL> func = getattr ( self , '<STR_LIT>' + arg ) <EOL> except AttributeError : <EOL> try : <EOL> doc = getattr ( self , '<STR_LIT>' + arg ) . __doc__ <EOL> if doc : <EOL> self . stdout . write ( "<STR_LIT>" % str ( doc ) ) <EOL> return <EOL> except AttributeError : <EOL> pass <EOL> self . stdout . write ( "<STR_LIT>" % str ( self . nohelp % ( arg , ) ) ) <EOL> return <EOL> func ( ) <EOL> else : <EOL> names = self . get_names ( ) <EOL> cmds_doc = [ ] <EOL> cmds_undoc = [ ] <EOL> help = { } <EOL> for name in names : <EOL> if name [ : <NUM_LIT:5> ] == '<STR_LIT>' : <EOL> help [ name [ <NUM_LIT:5> : ] ] = <NUM_LIT:1> <EOL> names . sort ( ) <EOL> prevname = '<STR_LIT>' <EOL> for name in names : <EOL> if name [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> if name == prevname : <EOL> continue <EOL> prevname = name <EOL> cmd = name [ <NUM_LIT:3> : ] <EOL> if cmd in help : <EOL> cmds_doc . append ( cmd ) <EOL> del help [ cmd ] <EOL> elif getattr ( self , name ) . __doc__ : <EOL> cmds_doc . append ( cmd ) <EOL> else : <EOL> cmds_undoc . append ( cmd ) <EOL> self . stdout . write ( "<STR_LIT>" % str ( self . doc_leader ) ) <EOL> self . print_topics ( self . doc_header , cmds_doc , <NUM_LIT:15> , <NUM_LIT> ) <EOL> self . print_topics ( self . misc_header , help . keys ( ) , <NUM_LIT:15> , <NUM_LIT> ) <EOL> self . print_topics ( self . undoc_header , cmds_undoc , <NUM_LIT:15> , <NUM_LIT> ) <EOL> def print_topics ( self , header , cmds , cmdlen , maxcol ) : <EOL> if cmds : <EOL> self . stdout . write ( "<STR_LIT>" % str ( header ) ) <EOL> if self . ruler : <EOL> self . stdout . write ( "<STR_LIT>" % str ( self . ruler * len ( header ) ) ) <EOL> self . columnize ( cmds , maxcol - <NUM_LIT:1> ) <EOL> self . stdout . write ( "<STR_LIT:\n>" ) <EOL> def columnize ( self , list , displaywidth = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if not list : <EOL> self . stdout . write ( "<STR_LIT>" ) <EOL> return <EOL> nonstrings = [ i for i in range ( len ( list ) ) <EOL> if not isinstance ( list [ i ] , str ) ] <EOL> if nonstrings : <EOL> raise TypeError , ( "<STR_LIT>" % <EOL> "<STR_LIT:U+002CU+0020>" . join ( map ( str , nonstrings ) ) ) <EOL> size = len ( list ) <EOL> if size == <NUM_LIT:1> : <EOL> self . stdout . write ( '<STR_LIT>' % str ( list [ <NUM_LIT:0> ] ) ) <EOL> return <EOL> for nrows in range ( <NUM_LIT:1> , len ( list ) ) : <EOL> ncols = ( size + nrows - <NUM_LIT:1> ) // nrows <EOL> colwidths = [ ] <EOL> totwidth = - <NUM_LIT:2> <EOL> for col in range ( ncols ) : <EOL> colwidth = <NUM_LIT:0> <EOL> for row in range ( nrows ) : <EOL> i = row + nrows * col <EOL> if i >= size : <EOL> break <EOL> x = list [ i ] <EOL> colwidth = max ( colwidth , len ( x ) ) <EOL> colwidths . append ( colwidth ) <EOL> totwidth += colwidth + <NUM_LIT:2> <EOL> if totwidth > displaywidth : <EOL> break <EOL> if totwidth <= displaywidth : <EOL> break <EOL> else : <EOL> nrows = len ( list ) <EOL> ncols = <NUM_LIT:1> <EOL> colwidths = [ <NUM_LIT:0> ] <EOL> for row in range ( nrows ) : <EOL> texts = [ ] <EOL> for col in range ( ncols ) : <EOL> i = row + nrows * col <EOL> if i >= size : <EOL> x = "<STR_LIT>" <EOL> else : <EOL> x = list [ i ] <EOL> texts . append ( x ) <EOL> while texts and not texts [ - <NUM_LIT:1> ] : <EOL> del texts [ - <NUM_LIT:1> ] <EOL> for col in range ( len ( texts ) ) : <EOL> texts [ col ] = texts [ col ] . ljust ( colwidths [ col ] ) <EOL> self . stdout . write ( "<STR_LIT>" % str ( "<STR_LIT:U+0020>" . join ( texts ) ) ) </s>
