<s> """<STR_LIT>""" <EOL> import cmd <EOL> import re <EOL> import os <EOL> import sys <EOL> import optparse <EOL> import subprocess <EOL> import tempfile <EOL> import doctest <EOL> import unittest <EOL> import datetime <EOL> import urllib <EOL> import glob <EOL> import traceback <EOL> import platform <EOL> import copy <EOL> from code import InteractiveConsole , InteractiveInterpreter <EOL> from optparse import make_option <EOL> from exabgp . dep import pyparsing <EOL> __version__ = '<STR_LIT>' <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> : <EOL> pyparsing . ParserElement . enablePackrat ( ) <EOL> """<STR_LIT>""" <EOL> class OptionParser ( optparse . OptionParser ) : <EOL> def exit ( self , status = <NUM_LIT:0> , msg = None ) : <EOL> self . values . _exit = True <EOL> if msg : <EOL> print ( msg ) <EOL> def print_help ( self , * args , ** kwargs ) : <EOL> try : <EOL> print ( self . _func . __doc__ ) <EOL> except AttributeError : <EOL> pass <EOL> optparse . OptionParser . print_help ( self , * args , ** kwargs ) <EOL> def error ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> raise optparse . OptParseError ( msg ) <EOL> def remaining_args ( oldArgs , newArgList ) : <EOL> '''<STR_LIT>''' <EOL> pattern = '<STR_LIT>' . join ( re . escape ( a ) for a in newArgList ) + '<STR_LIT>' <EOL> matchObj = re . search ( pattern , oldArgs ) <EOL> return oldArgs [ matchObj . start ( ) : ] <EOL> def _attr_get_ ( obj , attr ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return getattr ( obj , attr ) <EOL> except AttributeError : <EOL> return None <EOL> def _which ( editor ) : <EOL> try : <EOL> return subprocess . Popen ( [ '<STR_LIT>' , editor ] , stdout = subprocess . PIPE , stderr = subprocess . STDOUT ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> except OSError : <EOL> return None <EOL> optparse . Values . get = _attr_get_ <EOL> options_defined = [ ] <EOL> def options ( option_list , arg_desc = "<STR_LIT>" ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( option_list , list ) : <EOL> option_list = [ option_list ] <EOL> for opt in option_list : <EOL> options_defined . append ( pyparsing . Literal ( opt . get_opt_string ( ) ) ) <EOL> def option_setup ( func ) : <EOL> optionParser = OptionParser ( ) <EOL> for opt in option_list : <EOL> optionParser . add_option ( opt ) <EOL> optionParser . set_usage ( "<STR_LIT>" % ( func . __name__ [ <NUM_LIT:3> : ] , arg_desc ) ) <EOL> optionParser . _func = func <EOL> def new_func ( instance , arg ) : <EOL> try : <EOL> opts , newArgList = optionParser . parse_args ( arg . split ( ) ) <EOL> newArgs = remaining_args ( arg , newArgList ) <EOL> if isinstance ( arg , ParsedString ) : <EOL> arg = arg . with_args_replaced ( newArgs ) <EOL> else : <EOL> arg = newArgs <EOL> except optparse . OptParseError as e : <EOL> print ( e ) <EOL> optionParser . print_help ( ) <EOL> return <EOL> if hasattr ( opts , '<STR_LIT>' ) : <EOL> return None <EOL> result = func ( instance , arg , opts ) <EOL> return result <EOL> new_func . __doc__ = '<STR_LIT>' % ( func . __doc__ , optionParser . format_help ( ) ) <EOL> return new_func <EOL> return option_setup <EOL> class PasteBufferError ( EnvironmentError ) : <EOL> if sys . platform [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> errmsg = """<STR_LIT>""" <EOL> elif sys . platform [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> errmsg = """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> Exception . __init__ ( self , self . errmsg ) <EOL> pastebufferr = """<STR_LIT>""" <EOL> if subprocess . mswindows : <EOL> try : <EOL> import win32clipboard <EOL> def get_paste_buffer ( ) : <EOL> win32clipboard . OpenClipboard ( <NUM_LIT:0> ) <EOL> try : <EOL> result = win32clipboard . GetClipboardData ( ) <EOL> except TypeError : <EOL> result = '<STR_LIT>' <EOL> win32clipboard . CloseClipboard ( ) <EOL> return result <EOL> def write_to_paste_buffer ( txt ) : <EOL> win32clipboard . OpenClipboard ( <NUM_LIT:0> ) <EOL> win32clipboard . EmptyClipboard ( ) <EOL> win32clipboard . SetClipboardText ( txt ) <EOL> win32clipboard . CloseClipboard ( ) <EOL> except ImportError : <EOL> def get_paste_buffer ( * args ) : <EOL> raise OSError ( pastebufferr % ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> write_to_paste_buffer = get_paste_buffer <EOL> elif sys . platform == '<STR_LIT>' : <EOL> can_clip = False <EOL> try : <EOL> subprocess . check_call ( '<STR_LIT>' , shell = True , stdout = subprocess . PIPE , stdin = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> can_clip = True <EOL> except ( subprocess . CalledProcessError , OSError , IOError ) : <EOL> pass <EOL> if can_clip : <EOL> def get_paste_buffer ( ) : <EOL> pbcopyproc = subprocess . Popen ( '<STR_LIT>' , shell = True , stdout = subprocess . PIPE , stdin = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> return pbcopyproc . stdout . read ( ) <EOL> def write_to_paste_buffer ( txt ) : <EOL> pbcopyproc = subprocess . Popen ( '<STR_LIT>' , shell = True , stdout = subprocess . PIPE , stdin = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> pbcopyproc . communicate ( txt . encode ( ) ) <EOL> else : <EOL> def get_paste_buffer ( * args ) : <EOL> raise OSError ( pastebufferr % ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> write_to_paste_buffer = get_paste_buffer <EOL> else : <EOL> can_clip = False <EOL> try : <EOL> subprocess . check_call ( '<STR_LIT>' , shell = True , stdout = subprocess . PIPE , stdin = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> can_clip = True <EOL> except AttributeError : <EOL> try : <EOL> teststring = '<STR_LIT>' <EOL> xclipproc = subprocess . Popen ( '<STR_LIT>' , shell = True , stdout = subprocess . PIPE , stdin = subprocess . PIPE ) <EOL> xclipproc . stdin . write ( teststring ) <EOL> xclipproc . stdin . close ( ) <EOL> xclipproc = subprocess . Popen ( '<STR_LIT>' , shell = True , stdout = subprocess . PIPE , stdin = subprocess . PIPE ) <EOL> if xclipproc . stdout . read ( ) == teststring : <EOL> can_clip = True <EOL> except Exception : <EOL> pass <EOL> except Exception : <EOL> pass <EOL> if can_clip : <EOL> def get_paste_buffer ( ) : <EOL> xclipproc = subprocess . Popen ( '<STR_LIT>' , shell = True , stdout = subprocess . PIPE , stdin = subprocess . PIPE ) <EOL> return xclipproc . stdout . read ( ) <EOL> def write_to_paste_buffer ( txt ) : <EOL> xclipproc = subprocess . Popen ( '<STR_LIT>' , shell = True , stdout = subprocess . PIPE , stdin = subprocess . PIPE ) <EOL> xclipproc . stdin . write ( txt . encode ( ) ) <EOL> xclipproc . stdin . close ( ) <EOL> xclipproc = subprocess . Popen ( '<STR_LIT>' , shell = True , stdout = subprocess . PIPE , stdin = subprocess . PIPE ) <EOL> xclipproc . stdin . write ( txt . encode ( ) ) <EOL> xclipproc . stdin . close ( ) <EOL> else : <EOL> def get_paste_buffer ( * args ) : <EOL> raise OSError ( pastebufferr % ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> write_to_paste_buffer = get_paste_buffer <EOL> pyparsing . ParserElement . setDefaultWhitespaceChars ( '<STR_LIT>' ) <EOL> class ParsedString ( str ) : <EOL> def full_parsed_statement ( self ) : <EOL> new = ParsedString ( '<STR_LIT>' % ( self . parsed . command , self . parsed . args ) ) <EOL> new . parsed = self . parsed <EOL> new . parser = self . parser <EOL> return new <EOL> def with_args_replaced ( self , newargs ) : <EOL> new = ParsedString ( newargs ) <EOL> new . parsed = self . parsed <EOL> new . parser = self . parser <EOL> new . parsed [ '<STR_LIT:args>' ] = newargs <EOL> new . parsed . statement [ '<STR_LIT:args>' ] = newargs <EOL> return new <EOL> class StubbornDict ( dict ) : <EOL> '''<STR_LIT>''' <EOL> def update ( self , arg ) : <EOL> dict . update ( self , StubbornDict . to_dict ( arg ) ) <EOL> append = update <EOL> def __iadd__ ( self , arg ) : <EOL> self . update ( arg ) <EOL> return self <EOL> def __add__ ( self , arg ) : <EOL> selfcopy = copy . copy ( self ) <EOL> selfcopy . update ( stubbornDict ( arg ) ) <EOL> return selfcopy <EOL> def __radd__ ( self , arg ) : <EOL> selfcopy = copy . copy ( self ) <EOL> selfcopy . update ( stubbornDict ( arg ) ) <EOL> return selfcopy <EOL> @ classmethod <EOL> def to_dict ( cls , arg ) : <EOL> '<STR_LIT>' <EOL> if hasattr ( arg , '<STR_LIT>' ) : <EOL> arg = arg . splitlines ( ) <EOL> if hasattr ( arg , '<STR_LIT>' ) : <EOL> result = { } <EOL> for a in arg : <EOL> a = a . strip ( ) <EOL> if a : <EOL> key_val = a . split ( None , <NUM_LIT:1> ) <EOL> key = key_val [ <NUM_LIT:0> ] <EOL> if len ( key_val ) > <NUM_LIT:1> : <EOL> val = key_val [ <NUM_LIT:1> ] <EOL> else : <EOL> val = '<STR_LIT>' <EOL> result [ key ] = val <EOL> else : <EOL> result = arg <EOL> return result <EOL> def stubbornDict ( * arg , ** kwarg ) : <EOL> '''<STR_LIT>''' <EOL> result = { } <EOL> for a in arg : <EOL> result . update ( StubbornDict . to_dict ( a ) ) <EOL> result . update ( kwarg ) <EOL> return StubbornDict ( result ) <EOL> def replace_with_file_contents ( fname ) : <EOL> if fname : <EOL> try : <EOL> result = open ( os . path . expanduser ( fname [ <NUM_LIT:0> ] ) ) . read ( ) <EOL> except IOError : <EOL> result = '<STR_LIT>' % fname [ <NUM_LIT:0> ] <EOL> else : <EOL> result = get_paste_buffer ( ) <EOL> return result <EOL> class EmbeddedConsoleExit ( SystemExit ) : <EOL> pass <EOL> class EmptyStatement ( Exception ) : <EOL> pass <EOL> def ljust ( x , width , fillchar = '<STR_LIT:U+0020>' ) : <EOL> '<STR_LIT>' <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> return x . ljust ( width , fillchar ) <EOL> else : <EOL> if len ( x ) < width : <EOL> x = ( x + [ fillchar ] * width ) [ : width ] <EOL> return x <EOL> class Cmd ( cmd . Cmd ) : <EOL> echo = False <EOL> case_insensitive = True <EOL> continuation_prompt = '<STR_LIT>' <EOL> timing = False <EOL> legalChars = u'<STR_LIT>' + pyparsing . alphanums + pyparsing . alphas8bit <EOL> shortcuts = { '<STR_LIT:?>' : '<STR_LIT>' , '<STR_LIT:!>' : '<STR_LIT>' , '<STR_LIT:@>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> excludeFromHistory = '''<STR_LIT>''' . split ( ) <EOL> default_to_shell = False <EOL> noSpecialParse = '<STR_LIT>' . split ( ) <EOL> defaultExtension = '<STR_LIT>' <EOL> default_file_name = '<STR_LIT>' <EOL> abbrev = True <EOL> current_script_dir = None <EOL> reserved_words = [ ] <EOL> feedback_to_output = False <EOL> quiet = False <EOL> debug = False <EOL> locals_in_py = True <EOL> kept_state = None <EOL> redirector = '<STR_LIT:>>' <EOL> settable = stubbornDict ( '''<STR_LIT>''' ) <EOL> def poutput ( self , msg ) : <EOL> '''<STR_LIT>''' <EOL> if msg : <EOL> self . stdout . write ( msg ) <EOL> if msg [ - <NUM_LIT:1> ] != '<STR_LIT:\n>' : <EOL> self . stdout . write ( '<STR_LIT:\n>' ) <EOL> def perror ( self , errmsg , statement = None ) : <EOL> if self . debug : <EOL> traceback . print_exc ( ) <EOL> print ( str ( errmsg ) ) <EOL> def pfeedback ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if not self . quiet : <EOL> if self . feedback_to_output : <EOL> self . poutput ( msg ) <EOL> else : <EOL> print ( msg ) <EOL> _STOP_AND_EXIT = True <EOL> _STOP_SCRIPT_NO_EXIT = - <NUM_LIT> <EOL> editor = os . environ . get ( '<STR_LIT>' ) <EOL> if not editor : <EOL> if sys . platform [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> editor = '<STR_LIT>' <EOL> else : <EOL> for editor in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if _which ( editor ) : <EOL> break <EOL> colorcodes = { '<STR_LIT>' : { True : '<STR_LIT>' , False : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { True : '<STR_LIT>' , False : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { True : '<STR_LIT>' , False : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { True : '<STR_LIT>' , False : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { True : '<STR_LIT>' , False : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { True : '<STR_LIT>' , False : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { True : '<STR_LIT>' , False : '<STR_LIT>' } } <EOL> colors = ( platform . system ( ) != '<STR_LIT>' ) <EOL> def colorize ( self , val , color ) : <EOL> '''<STR_LIT>''' <EOL> if self . colors and ( self . stdout == self . initial_stdout ) : <EOL> return self . colorcodes [ color ] [ True ] + val + self . colorcodes [ color ] [ False ] <EOL> return val <EOL> def do_cmdenvironment ( self , args ) : <EOL> '''<STR_LIT>''' <EOL> self . stdout . write ( """<STR_LIT>""" % { '<STR_LIT>' : ( self . case_insensitive and '<STR_LIT>' ) or '<STR_LIT>' , <EOL> '<STR_LIT>' : str ( self . terminators ) , <EOL> '<STR_LIT>' : '<STR_LIT:U+0020>' . join ( self . settable ) <EOL> } ) <EOL> def do_help ( self , arg ) : <EOL> if arg : <EOL> funcname = self . func_named ( arg ) <EOL> if funcname : <EOL> fn = getattr ( self , funcname ) <EOL> try : <EOL> fn . optionParser . print_help ( file = self . stdout ) <EOL> except AttributeError : <EOL> cmd . Cmd . do_help ( self , funcname [ <NUM_LIT:3> : ] ) <EOL> else : <EOL> cmd . Cmd . do_help ( self , arg ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> cmd . Cmd . __init__ ( self , * args , ** kwargs ) <EOL> self . initial_stdout = sys . stdout <EOL> self . history = History ( ) <EOL> self . pystate = { } <EOL> self . shortcuts = sorted ( self . shortcuts . items ( ) , reverse = True ) <EOL> self . keywords = self . reserved_words + [ fname [ <NUM_LIT:3> : ] for fname in dir ( self ) <EOL> if fname . startswith ( '<STR_LIT>' ) ] <EOL> self . _init_parser ( ) <EOL> def do_shortcuts ( self , args ) : <EOL> """<STR_LIT>""" <EOL> result = "<STR_LIT:\n>" . join ( '<STR_LIT>' % ( sc [ <NUM_LIT:0> ] , sc [ <NUM_LIT:1> ] ) for sc in sorted ( self . shortcuts ) ) <EOL> self . stdout . write ( "<STR_LIT>" % ( result ) ) <EOL> prefixParser = pyparsing . Empty ( ) <EOL> commentGrammars = pyparsing . Or ( [ pyparsing . pythonStyleComment , pyparsing . cStyleComment ] ) <EOL> commentGrammars . addParseAction ( lambda x : '<STR_LIT>' ) <EOL> commentInProgress = pyparsing . Literal ( '<STR_LIT>' ) + pyparsing . SkipTo ( <EOL> pyparsing . stringEnd ^ '<STR_LIT>' ) <EOL> terminators = [ '<STR_LIT:;>' ] <EOL> blankLinesAllowed = False <EOL> multilineCommands = [ ] <EOL> def _init_parser ( self ) : <EOL> r'''<STR_LIT>''' <EOL> outputParser = ( pyparsing . Literal ( self . redirector * <NUM_LIT:2> ) | ( pyparsing . WordStart ( ) + self . redirector ) | pyparsing . Regex ( '<STR_LIT>' + self . redirector ) ) ( '<STR_LIT>' ) <EOL> terminatorParser = pyparsing . Or ( [ ( hasattr ( t , '<STR_LIT>' ) and t ) or pyparsing . Literal ( t ) for t in self . terminators ] ) ( '<STR_LIT>' ) <EOL> stringEnd = pyparsing . stringEnd ^ '<STR_LIT>' <EOL> self . multilineCommand = pyparsing . Or ( [ pyparsing . Keyword ( c , caseless = self . case_insensitive ) for c in self . multilineCommands ] ) ( '<STR_LIT>' ) <EOL> oneLineCommand = ( ~ self . multilineCommand + pyparsing . Word ( self . legalChars ) ) ( '<STR_LIT>' ) <EOL> pipe = pyparsing . Keyword ( '<STR_LIT:|>' , identChars = '<STR_LIT:|>' ) <EOL> self . commentGrammars . ignore ( pyparsing . quotedString ) . setParseAction ( lambda x : '<STR_LIT>' ) <EOL> doNotParse = self . commentGrammars | self . commentInProgress | pyparsing . quotedString <EOL> afterElements = pyparsing . Optional ( pipe + pyparsing . SkipTo ( outputParser ^ stringEnd , ignore = doNotParse ) ( '<STR_LIT>' ) ) + pyparsing . Optional ( outputParser + pyparsing . SkipTo ( stringEnd , ignore = doNotParse ) . setParseAction ( lambda x : x [ <NUM_LIT:0> ] . strip ( ) ) ( '<STR_LIT>' ) ) <EOL> if self . case_insensitive : <EOL> self . multilineCommand . setParseAction ( lambda x : x [ <NUM_LIT:0> ] . lower ( ) ) <EOL> oneLineCommand . setParseAction ( lambda x : x [ <NUM_LIT:0> ] . lower ( ) ) <EOL> if self . blankLinesAllowed : <EOL> self . blankLineTerminationParser = pyparsing . NoMatch <EOL> else : <EOL> self . blankLineTerminator = ( pyparsing . lineEnd + pyparsing . lineEnd ) ( '<STR_LIT>' ) <EOL> self . blankLineTerminator . setResultsName ( '<STR_LIT>' ) <EOL> self . blankLineTerminationParser = ( ( self . multilineCommand ^ oneLineCommand ) + pyparsing . SkipTo ( self . blankLineTerminator , ignore = doNotParse ) . setParseAction ( lambda x : x [ <NUM_LIT:0> ] . strip ( ) ) ( '<STR_LIT:args>' ) + self . blankLineTerminator ) ( '<STR_LIT>' ) <EOL> self . multilineParser = ( ( ( self . multilineCommand ^ oneLineCommand ) + pyparsing . SkipTo ( terminatorParser , ignore = doNotParse ) . setParseAction ( lambda x : x [ <NUM_LIT:0> ] . strip ( ) ) ( '<STR_LIT:args>' ) + terminatorParser ) ( '<STR_LIT>' ) + <EOL> pyparsing . SkipTo ( outputParser ^ pipe ^ stringEnd , ignore = doNotParse ) . setParseAction ( lambda x : x [ <NUM_LIT:0> ] . strip ( ) ) ( '<STR_LIT>' ) + afterElements ) <EOL> self . multilineParser . ignore ( self . commentInProgress ) <EOL> self . singleLineParser = ( ( oneLineCommand + pyparsing . SkipTo ( terminatorParser ^ stringEnd ^ pipe ^ outputParser , ignore = doNotParse ) . setParseAction ( lambda x : x [ <NUM_LIT:0> ] . strip ( ) ) ( '<STR_LIT:args>' ) ) ( '<STR_LIT>' ) + <EOL> pyparsing . Optional ( terminatorParser ) + afterElements ) <EOL> self . blankLineTerminationParser = self . blankLineTerminationParser . setResultsName ( '<STR_LIT>' ) <EOL> self . parser = self . prefixParser + ( <EOL> stringEnd | <EOL> self . multilineParser | <EOL> self . singleLineParser | <EOL> self . blankLineTerminationParser | <EOL> self . multilineCommand + pyparsing . SkipTo ( stringEnd , ignore = doNotParse ) <EOL> ) <EOL> self . parser . ignore ( self . commentGrammars ) <EOL> inputMark = pyparsing . Literal ( '<STR_LIT:<>' ) <EOL> inputMark . setParseAction ( lambda x : '<STR_LIT>' ) <EOL> fileName = pyparsing . Word ( self . legalChars + '<STR_LIT>' ) <EOL> inputFrom = fileName ( '<STR_LIT>' ) <EOL> inputFrom . setParseAction ( replace_with_file_contents ) <EOL> self . inputParser = inputMark + pyparsing . Optional ( inputFrom ) + pyparsing . Optional ( '<STR_LIT:>>' ) + pyparsing . Optional ( fileName ) + ( pyparsing . stringEnd | '<STR_LIT:|>' ) <EOL> self . inputParser . ignore ( self . commentInProgress ) <EOL> def preparse ( self , raw , ** kwargs ) : <EOL> return raw <EOL> def postparse ( self , parseResult ) : <EOL> return parseResult <EOL> def parsed ( self , raw , ** kwargs ) : <EOL> if isinstance ( raw , ParsedString ) : <EOL> p = raw <EOL> else : <EOL> s = self . preparse ( raw , ** kwargs ) <EOL> s = self . inputParser . transformString ( s . lstrip ( ) ) <EOL> s = self . commentGrammars . transformString ( s ) <EOL> for ( shortcut , expansion ) in self . shortcuts : <EOL> if s . lower ( ) . startswith ( shortcut ) : <EOL> s = s . replace ( shortcut , expansion + '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> break <EOL> result = self . parser . parseString ( s ) <EOL> result [ '<STR_LIT>' ] = raw <EOL> result [ '<STR_LIT>' ] = result . multilineCommand or result . command <EOL> result = self . postparse ( result ) <EOL> p = ParsedString ( result . args ) <EOL> p . parsed = result <EOL> p . parser = self . parsed <EOL> for ( key , val ) in kwargs . items ( ) : <EOL> p . parsed [ key ] = val <EOL> return p <EOL> def postparsing_precmd ( self , statement ) : <EOL> stop = <NUM_LIT:0> <EOL> return stop , statement <EOL> def postparsing_postcmd ( self , stop ) : <EOL> return stop <EOL> def func_named ( self , arg ) : <EOL> result = None <EOL> target = '<STR_LIT>' + arg <EOL> if target in dir ( self ) : <EOL> result = target <EOL> else : <EOL> if self . abbrev : <EOL> funcs = [ fname for fname in self . keywords if fname . startswith ( arg ) ] <EOL> if len ( funcs ) == <NUM_LIT:1> : <EOL> result = '<STR_LIT>' + funcs [ <NUM_LIT:0> ] <EOL> return result <EOL> def onecmd_plus_hooks ( self , line ) : <EOL> stop = <NUM_LIT:0> <EOL> try : <EOL> try : <EOL> statement = self . complete_statement ( line ) <EOL> ( stop , statement ) = self . postparsing_precmd ( statement ) <EOL> if stop : <EOL> return self . postparsing_postcmd ( stop ) <EOL> if statement . parsed . command not in self . excludeFromHistory : <EOL> self . history . append ( statement . parsed . raw ) <EOL> try : <EOL> self . redirect_output ( statement ) <EOL> timestart = datetime . datetime . now ( ) <EOL> statement = self . precmd ( statement ) <EOL> stop = self . onecmd ( statement ) <EOL> stop = self . postcmd ( stop , statement ) <EOL> if self . timing : <EOL> self . pfeedback ( '<STR_LIT>' % str ( datetime . datetime . now ( ) - timestart ) ) <EOL> finally : <EOL> self . restore_output ( statement ) <EOL> except EmptyStatement : <EOL> return <NUM_LIT:0> <EOL> except Exception as e : <EOL> self . perror ( str ( e ) , statement ) <EOL> finally : <EOL> return self . postparsing_postcmd ( stop ) <EOL> def complete_statement ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if ( not line ) or ( <EOL> not pyparsing . Or ( self . commentGrammars ) . <EOL> setParseAction ( lambda x : '<STR_LIT>' ) . transformString ( line ) ) : <EOL> raise EmptyStatement ( ) <EOL> statement = self . parsed ( line ) <EOL> while statement . parsed . multilineCommand and ( statement . parsed . terminator == '<STR_LIT>' ) : <EOL> statement = '<STR_LIT>' % ( statement . parsed . raw , <EOL> self . pseudo_raw_input ( self . continuation_prompt ) ) <EOL> statement = self . parsed ( statement ) <EOL> if not statement . parsed . command : <EOL> raise EmptyStatement ( ) <EOL> return statement <EOL> def redirect_output ( self , statement ) : <EOL> if statement . parsed . pipeTo : <EOL> self . kept_state = Statekeeper ( self , ( '<STR_LIT>' , ) ) <EOL> self . kept_sys = Statekeeper ( sys , ( '<STR_LIT>' , ) ) <EOL> self . redirect = subprocess . Popen ( statement . parsed . pipeTo , shell = True , stdout = subprocess . PIPE , stdin = subprocess . PIPE ) <EOL> sys . stdout = self . stdout = self . redirect . stdin <EOL> elif statement . parsed . output : <EOL> if ( not statement . parsed . outputTo ) and ( not can_clip ) : <EOL> raise EnvironmentError ( '<STR_LIT>' ) <EOL> self . kept_state = Statekeeper ( self , ( '<STR_LIT>' , ) ) <EOL> self . kept_sys = Statekeeper ( sys , ( '<STR_LIT>' , ) ) <EOL> if statement . parsed . outputTo : <EOL> mode = '<STR_LIT:w>' <EOL> if statement . parsed . output == <NUM_LIT:2> * self . redirector : <EOL> mode = '<STR_LIT:a>' <EOL> sys . stdout = self . stdout = open ( os . path . expanduser ( statement . parsed . outputTo ) , mode ) <EOL> else : <EOL> sys . stdout = self . stdout = tempfile . TemporaryFile ( mode = "<STR_LIT>" ) <EOL> if statement . parsed . output == '<STR_LIT>' : <EOL> self . stdout . write ( get_paste_buffer ( ) ) <EOL> def restore_output ( self , statement ) : <EOL> if self . kept_state : <EOL> if statement . parsed . output : <EOL> if not statement . parsed . outputTo : <EOL> self . stdout . seek ( <NUM_LIT:0> ) <EOL> write_to_paste_buffer ( self . stdout . read ( ) ) <EOL> elif statement . parsed . pipeTo : <EOL> for result in self . redirect . communicate ( ) : <EOL> self . kept_state . stdout . write ( result or '<STR_LIT>' ) <EOL> self . stdout . close ( ) <EOL> self . kept_state . restore ( ) <EOL> self . kept_sys . restore ( ) <EOL> self . kept_state = None <EOL> def onecmd ( self , line ) : <EOL> """<STR_LIT>""" <EOL> statement = self . parsed ( line ) <EOL> self . lastcmd = statement . parsed . raw <EOL> funcname = self . func_named ( statement . parsed . command ) <EOL> if not funcname : <EOL> return self . _default ( statement ) <EOL> try : <EOL> func = getattr ( self , funcname ) <EOL> except AttributeError : <EOL> return self . _default ( statement ) <EOL> stop = func ( statement ) <EOL> return stop <EOL> def _default ( self , statement ) : <EOL> arg = statement . full_parsed_statement ( ) <EOL> if self . default_to_shell : <EOL> result = os . system ( arg ) <EOL> if not result : <EOL> return self . postparsing_postcmd ( None ) <EOL> return self . postparsing_postcmd ( self . default ( arg ) ) <EOL> def pseudo_raw_input ( self , prompt ) : <EOL> """<STR_LIT>""" <EOL> if self . use_rawinput : <EOL> try : <EOL> line = raw_input ( prompt ) <EOL> except EOFError : <EOL> line = '<STR_LIT>' <EOL> else : <EOL> self . stdout . write ( prompt ) <EOL> self . stdout . flush ( ) <EOL> line = self . stdin . readline ( ) <EOL> if not len ( line ) : <EOL> line = '<STR_LIT>' <EOL> else : <EOL> if line [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> return line <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> line = self . pseudo_raw_input ( self . prompt ) <EOL> if ( self . echo ) and ( isinstance ( self . stdin , file ) ) : <EOL> self . stdout . write ( line + '<STR_LIT:\n>' ) <EOL> stop = self . onecmd_plus_hooks ( 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> return stop <EOL> def do_EOF ( self , arg ) : <EOL> return self . _STOP_SCRIPT_NO_EXIT <EOL> do_eof = do_EOF <EOL> def do_quit ( self , arg ) : <EOL> return self . _STOP_AND_EXIT <EOL> do_exit = do_quit <EOL> do_q = do_quit <EOL> def select ( self , options , prompt = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( options , basestring ) : <EOL> options = zip ( options . split ( ) , options . split ( ) ) <EOL> fulloptions = [ ] <EOL> for opt in options : <EOL> if isinstance ( opt , basestring ) : <EOL> fulloptions . append ( ( opt , opt ) ) <EOL> else : <EOL> try : <EOL> fulloptions . append ( ( opt [ <NUM_LIT:0> ] , opt [ <NUM_LIT:1> ] ) ) <EOL> except IndexError : <EOL> fulloptions . append ( ( opt [ <NUM_LIT:0> ] , opt [ <NUM_LIT:0> ] ) ) <EOL> for ( idx , ( value , text ) ) in enumerate ( fulloptions ) : <EOL> self . poutput ( '<STR_LIT>' % ( idx + <NUM_LIT:1> , text ) ) <EOL> while True : <EOL> response = raw_input ( prompt ) <EOL> try : <EOL> response = int ( response ) <EOL> result = fulloptions [ response - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> break <EOL> except ValueError : <EOL> pass <EOL> return result <EOL> @ options ( [ make_option ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) ] ) <EOL> def do_show ( self , arg , opts ) : <EOL> '''<STR_LIT>''' <EOL> param = arg . strip ( ) . lower ( ) <EOL> result = { } <EOL> maxlen = <NUM_LIT:0> <EOL> for p in self . settable : <EOL> if ( not param ) or p . startswith ( param ) : <EOL> result [ p ] = '<STR_LIT>' % ( p , str ( getattr ( self , p ) ) ) <EOL> maxlen = max ( maxlen , len ( result [ p ] ) ) <EOL> if result : <EOL> for p in sorted ( result ) : <EOL> if opts . long : <EOL> self . poutput ( '<STR_LIT>' % ( result [ p ] . ljust ( maxlen ) , self . settable [ p ] ) ) <EOL> else : <EOL> self . poutput ( result [ p ] ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" % param ) <EOL> def do_set ( self , arg ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> statement , paramName , val = arg . parsed . raw . split ( None , <NUM_LIT:2> ) <EOL> val = val . strip ( ) <EOL> paramName = paramName . strip ( ) . lower ( ) <EOL> if paramName not in self . settable : <EOL> hits = [ p for p in self . settable if p . startswith ( paramName ) ] <EOL> if len ( hits ) == <NUM_LIT:1> : <EOL> paramName = hits [ <NUM_LIT:0> ] <EOL> else : <EOL> return self . do_show ( paramName ) <EOL> currentVal = getattr ( self , paramName ) <EOL> if ( val [ <NUM_LIT:0> ] == val [ - <NUM_LIT:1> ] ) and val [ <NUM_LIT:0> ] in ( "<STR_LIT:'>" , '<STR_LIT:">' ) : <EOL> val = val [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> else : <EOL> val = cast ( currentVal , val ) <EOL> setattr ( self , paramName , val ) <EOL> self . stdout . write ( '<STR_LIT>' % ( paramName , currentVal , val ) ) <EOL> if currentVal != val : <EOL> try : <EOL> onchange_hook = getattr ( self , '<STR_LIT>' % paramName ) <EOL> onchange_hook ( old = currentVal , new = val ) <EOL> except AttributeError : <EOL> pass <EOL> except ( ValueError , AttributeError , NotSettableError ) as e : <EOL> self . do_show ( arg ) <EOL> def do_pause ( self , arg ) : <EOL> '<STR_LIT>' <EOL> raw_input ( arg + '<STR_LIT:\n>' ) <EOL> def do_shell ( self , arg ) : <EOL> '<STR_LIT>' <EOL> os . system ( arg ) <EOL> def do_py ( self , arg ) : <EOL> '''<STR_LIT>''' <EOL> self . pystate [ '<STR_LIT>' ] = self <EOL> arg = arg . parsed . raw [ <NUM_LIT:2> : ] . strip ( ) <EOL> localvars = ( self . locals_in_py and self . pystate ) or { } <EOL> interp = InteractiveConsole ( locals = localvars ) <EOL> interp . runcode ( '<STR_LIT>' ) <EOL> if arg . strip ( ) : <EOL> interp . runcode ( arg ) <EOL> else : <EOL> def quit ( ) : <EOL> raise EmbeddedConsoleExit <EOL> def onecmd_plus_hooks ( arg ) : <EOL> return self . onecmd_plus_hooks ( arg + '<STR_LIT:\n>' ) <EOL> def run ( arg ) : <EOL> try : <EOL> file = open ( arg ) <EOL> interp . runcode ( file . read ( ) ) <EOL> file . close ( ) <EOL> except IOError as e : <EOL> self . perror ( e ) <EOL> self . pystate [ '<STR_LIT>' ] = quit <EOL> self . pystate [ '<STR_LIT>' ] = quit <EOL> self . pystate [ '<STR_LIT>' ] = onecmd_plus_hooks <EOL> self . pystate [ '<STR_LIT>' ] = run <EOL> try : <EOL> cprt = '<STR_LIT>' <EOL> keepstate = Statekeeper ( sys , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> sys . stdout = self . stdout <EOL> sys . stdin = self . stdin <EOL> interp . interact ( banner = "<STR_LIT>" % <EOL> ( sys . version , sys . platform , cprt , self . __class__ . __name__ , self . do_py . __doc__ ) ) <EOL> except EmbeddedConsoleExit : <EOL> pass <EOL> keepstate . restore ( ) <EOL> @ options ( [ make_option ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , help = "<STR_LIT>" ) , <EOL> ] , arg_desc = '<STR_LIT>' ) <EOL> def do_history ( self , arg , opts ) : <EOL> """<STR_LIT>""" <EOL> if arg : <EOL> history = self . history . get ( arg ) <EOL> else : <EOL> history = self . history <EOL> for hi in history : <EOL> if opts . script : <EOL> self . poutput ( hi ) <EOL> else : <EOL> self . stdout . write ( hi . pr ( ) ) <EOL> def last_matching ( self , arg ) : <EOL> try : <EOL> if arg : <EOL> return self . history . get ( arg ) [ - <NUM_LIT:1> ] <EOL> else : <EOL> return self . history [ - <NUM_LIT:1> ] <EOL> except IndexError : <EOL> return None <EOL> def do_list ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> history = self . history . span ( arg or '<STR_LIT>' ) <EOL> except IndexError : <EOL> history = self . history . search ( arg ) <EOL> for hi in history : <EOL> self . poutput ( hi . pr ( ) ) <EOL> do_hi = do_history <EOL> do_l = do_list <EOL> do_li = do_list <EOL> def do_ed ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> if not self . editor : <EOL> raise EnvironmentError ( "<STR_LIT>" ) <EOL> filename = self . default_file_name <EOL> if arg : <EOL> try : <EOL> buffer = self . last_matching ( int ( arg ) ) <EOL> except ValueError : <EOL> filename = arg <EOL> buffer = '<STR_LIT>' <EOL> else : <EOL> buffer = self . history [ - <NUM_LIT:1> ] <EOL> if buffer : <EOL> f = open ( os . path . expanduser ( filename ) , '<STR_LIT:w>' ) <EOL> f . write ( buffer or '<STR_LIT>' ) <EOL> f . close ( ) <EOL> os . system ( '<STR_LIT>' % ( self . editor , filename ) ) <EOL> self . do__load ( filename ) <EOL> do_edit = do_ed <EOL> saveparser = ( pyparsing . Optional ( pyparsing . Word ( pyparsing . nums ) ^ '<STR_LIT:*>' ) ( "<STR_LIT>" ) + <EOL> pyparsing . Optional ( pyparsing . Word ( legalChars + '<STR_LIT>' ) ) ( "<STR_LIT>" ) + <EOL> pyparsing . stringEnd ) <EOL> def do_save ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> args = self . saveparser . parseString ( arg ) <EOL> except pyparsing . ParseException : <EOL> self . perror ( '<STR_LIT>' % arg ) <EOL> raise SyntaxError ( self . do_save . __doc__ ) <EOL> fname = args . fname or self . default_file_name <EOL> if args . idx == '<STR_LIT:*>' : <EOL> saveme = '<STR_LIT>' . join ( self . history [ : ] ) <EOL> elif args . idx : <EOL> saveme = self . history [ int ( args . idx ) - <NUM_LIT:1> ] <EOL> else : <EOL> saveme = self . history [ - <NUM_LIT:1> ] <EOL> try : <EOL> f = open ( os . path . expanduser ( fname ) , '<STR_LIT:w>' ) <EOL> f . write ( saveme ) <EOL> f . close ( ) <EOL> self . pfeedback ( '<STR_LIT>' % ( fname ) ) <EOL> except Exception as e : <EOL> self . perror ( '<STR_LIT>' % ( fname ) ) <EOL> raise <EOL> def read_file_or_url ( self , fname ) : <EOL> if isinstance ( fname , file ) : <EOL> result = open ( fname , '<STR_LIT:r>' ) <EOL> else : <EOL> match = self . urlre . match ( fname ) <EOL> if match : <EOL> result = urllib . urlopen ( match . group ( <NUM_LIT:1> ) ) <EOL> else : <EOL> fname = os . path . expanduser ( fname ) <EOL> try : <EOL> result = open ( os . path . expanduser ( fname ) , '<STR_LIT:r>' ) <EOL> except IOError : <EOL> result = open ( '<STR_LIT>' % ( os . path . expanduser ( fname ) , <EOL> self . defaultExtension ) , '<STR_LIT:r>' ) <EOL> return result <EOL> def do__relative_load ( self , arg = None ) : <EOL> '''<STR_LIT>''' <EOL> if arg : <EOL> arg = arg . split ( None , <NUM_LIT:1> ) <EOL> targetname , args = arg [ <NUM_LIT:0> ] , ( arg [ <NUM_LIT:1> : ] or [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> targetname = os . path . join ( self . current_script_dir or '<STR_LIT>' , targetname ) <EOL> self . do__load ( '<STR_LIT>' % ( targetname , args ) ) <EOL> urlre = re . compile ( '<STR_LIT>' ) <EOL> def do_load ( self , arg = None ) : <EOL> """<STR_LIT>""" <EOL> if arg is None : <EOL> targetname = self . default_file_name <EOL> else : <EOL> arg = arg . split ( None , <NUM_LIT:1> ) <EOL> targetname , args = arg [ <NUM_LIT:0> ] , ( arg [ <NUM_LIT:1> : ] or [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] . strip ( ) <EOL> try : <EOL> target = self . read_file_or_url ( targetname ) <EOL> except IOError as e : <EOL> self . perror ( '<STR_LIT>' % ( targetname , e ) ) <EOL> return <EOL> keepstate = Statekeeper ( self , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . stdin = target <EOL> self . use_rawinput = False <EOL> self . prompt = self . continuation_prompt = '<STR_LIT>' <EOL> self . current_script_dir = os . path . split ( targetname ) [ <NUM_LIT:0> ] <EOL> stop = self . _cmdloop ( ) <EOL> self . stdin . close ( ) <EOL> keepstate . restore ( ) <EOL> self . lastcmd = '<STR_LIT>' <EOL> return stop and ( stop != self . _STOP_SCRIPT_NO_EXIT ) <EOL> do__load = do_load <EOL> def do_run ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> '<STR_LIT>' <EOL> runme = self . last_matching ( arg ) <EOL> self . pfeedback ( runme ) <EOL> if runme : <EOL> stop = self . onecmd_plus_hooks ( runme ) <EOL> do_r = do_run <EOL> def fileimport ( self , statement , source ) : <EOL> try : <EOL> f = open ( os . path . expanduser ( source ) ) <EOL> except IOError : <EOL> self . stdout . write ( "<STR_LIT>" % source ) <EOL> return '<STR_LIT>' <EOL> data = f . read ( ) <EOL> f . close ( ) <EOL> return data <EOL> def runTranscriptTests ( self , callargs ) : <EOL> class TestMyAppCase ( Cmd2TestCase ) : <EOL> CmdApp = self . __class__ <EOL> self . __class__ . testfiles = callargs <EOL> sys . argv = [ sys . argv [ <NUM_LIT:0> ] ] <EOL> testcase = TestMyAppCase ( ) <EOL> runner = unittest . TextTestRunner ( ) <EOL> result = runner . run ( testcase ) <EOL> result . printErrors ( ) <EOL> def run_commands_at_invocation ( self , callargs ) : <EOL> for initial_command in callargs : <EOL> if self . onecmd_plus_hooks ( initial_command + '<STR_LIT:\n>' ) : <EOL> return self . _STOP_AND_EXIT <EOL> def cmdloop ( self ) : <EOL> parser = optparse . OptionParser ( ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:test>' , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = '<STR_LIT>' ) <EOL> ( callopts , callargs ) = parser . parse_args ( ) <EOL> if callopts . test : <EOL> self . runTranscriptTests ( callargs ) <EOL> else : <EOL> if not self . run_commands_at_invocation ( callargs ) : <EOL> self . _cmdloop ( ) <EOL> class HistoryItem ( str ) : <EOL> listformat = '<STR_LIT>' <EOL> def __init__ ( self , instr ) : <EOL> str . __init__ ( self ) <EOL> self . lowercase = self . lower ( ) <EOL> self . idx = None <EOL> def pr ( self ) : <EOL> return self . listformat % ( self . idx , str ( self ) ) <EOL> class History ( list ) : <EOL> '''<STR_LIT>''' <EOL> def zero_based_index ( self , onebased ) : <EOL> result = onebased <EOL> if result > <NUM_LIT:0> : <EOL> result -= <NUM_LIT:1> <EOL> return result <EOL> def to_index ( self , raw ) : <EOL> if raw : <EOL> result = self . zero_based_index ( int ( raw ) ) <EOL> else : <EOL> result = None <EOL> return result <EOL> def search ( self , target ) : <EOL> target = target . strip ( ) <EOL> if target [ <NUM_LIT:0> ] == target [ - <NUM_LIT:1> ] == '<STR_LIT:/>' and len ( target ) > <NUM_LIT:1> : <EOL> target = target [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> else : <EOL> target = re . escape ( target ) <EOL> pattern = re . compile ( target , re . IGNORECASE ) <EOL> return [ s for s in self if pattern . search ( s ) ] <EOL> spanpattern = re . compile ( r'<STR_LIT>' ) <EOL> def span ( self , raw ) : <EOL> if raw . lower ( ) in ( '<STR_LIT:*>' , '<STR_LIT:->' , '<STR_LIT:all>' ) : <EOL> raw = '<STR_LIT::>' <EOL> results = self . spanpattern . search ( raw ) <EOL> if not results : <EOL> raise IndexError <EOL> if not results . group ( '<STR_LIT>' ) : <EOL> return [ self [ self . to_index ( results . group ( '<STR_LIT:start>' ) ) ] ] <EOL> start = self . to_index ( results . group ( '<STR_LIT:start>' ) ) <EOL> end = self . to_index ( results . group ( '<STR_LIT:end>' ) ) <EOL> reverse = False <EOL> if end is not None : <EOL> if end < start : <EOL> ( start , end ) = ( end , start ) <EOL> reverse = True <EOL> end += <NUM_LIT:1> <EOL> result = self [ start : end ] <EOL> if reverse : <EOL> result . reverse ( ) <EOL> return result <EOL> rangePattern = re . compile ( r'<STR_LIT>' ) <EOL> def append ( self , new ) : <EOL> new = HistoryItem ( new ) <EOL> list . append ( self , new ) <EOL> new . idx = len ( self ) <EOL> def extend ( self , new ) : <EOL> for n in new : <EOL> self . append ( n ) <EOL> def get ( self , getme = None , fromEnd = False ) : <EOL> if not getme : <EOL> return self <EOL> try : <EOL> getme = int ( getme ) <EOL> if getme < <NUM_LIT:0> : <EOL> return self [ : ( - <NUM_LIT:1> * getme ) ] <EOL> else : <EOL> return [ self [ getme - <NUM_LIT:1> ] ] <EOL> except IndexError : <EOL> return [ ] <EOL> except ValueError : <EOL> rangeResult = self . rangePattern . search ( getme ) <EOL> if rangeResult : <EOL> start = rangeResult . group ( '<STR_LIT:start>' ) or None <EOL> end = rangeResult . group ( '<STR_LIT:start>' ) or None <EOL> if start : <EOL> start = int ( start ) - <NUM_LIT:1> <EOL> if end : <EOL> end = int ( end ) <EOL> return self [ start : end ] <EOL> getme = getme . strip ( ) <EOL> if getme . startswith ( r'<STR_LIT:/>' ) and getme . endswith ( r'<STR_LIT:/>' ) : <EOL> finder = re . compile ( getme [ <NUM_LIT:1> : - <NUM_LIT:1> ] , re . DOTALL | re . MULTILINE | re . IGNORECASE ) <EOL> def isin ( hi ) : <EOL> return finder . search ( hi ) <EOL> else : <EOL> def isin ( hi ) : <EOL> return ( getme . lower ( ) in hi . lowercase ) <EOL> return [ itm for itm in self if isin ( itm ) ] <EOL> class NotSettableError ( Exception ) : <EOL> pass <EOL> def cast ( current , new ) : <EOL> """<STR_LIT>""" <EOL> typ = type ( current ) <EOL> if typ == bool : <EOL> try : <EOL> return bool ( int ( new ) ) <EOL> except ( ValueError , TypeError ) : <EOL> pass <EOL> try : <EOL> new = new . lower ( ) <EOL> except Exception : <EOL> pass <EOL> if ( new == '<STR_LIT>' ) or ( new [ <NUM_LIT:0> ] in ( '<STR_LIT:y>' , '<STR_LIT:t>' ) ) : <EOL> return True <EOL> if ( new == '<STR_LIT>' ) or ( new [ <NUM_LIT:0> ] in ( '<STR_LIT:n>' , '<STR_LIT:f>' ) ) : <EOL> return False <EOL> else : <EOL> try : <EOL> return typ ( new ) <EOL> except Exception : <EOL> pass <EOL> print ( "<STR_LIT>" % ( current , new ) ) <EOL> return current <EOL> class Statekeeper ( object ) : <EOL> def __init__ ( self , obj , attribs ) : <EOL> self . obj = obj <EOL> self . attribs = attribs <EOL> if self . obj : <EOL> self . save ( ) <EOL> def save ( self ) : <EOL> for attrib in self . attribs : <EOL> setattr ( self , attrib , getattr ( self . obj , attrib ) ) <EOL> def restore ( self ) : <EOL> if self . obj : <EOL> for attrib in self . attribs : <EOL> setattr ( self . obj , attrib , getattr ( self , attrib ) ) <EOL> class Borg ( object ) : <EOL> '''<STR_LIT>''' <EOL> _shared_state = { } <EOL> def __new__ ( cls , * a , ** k ) : <EOL> obj = object . __new__ ( cls , * a , ** k ) <EOL> obj . __dict__ = cls . _shared_state <EOL> return obj <EOL> class OutputTrap ( Borg ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . contents = '<STR_LIT>' <EOL> self . old_stdout = sys . stdout <EOL> sys . stdout = self <EOL> def write ( self , txt ) : <EOL> self . contents += txt <EOL> def read ( self ) : <EOL> result = self . contents <EOL> self . contents = '<STR_LIT>' <EOL> return result <EOL> def tearDown ( self ) : <EOL> sys . stdout = self . old_stdout <EOL> self . contents = '<STR_LIT>' <EOL> class Cmd2TestCase ( unittest . TestCase ) : <EOL> '''<STR_LIT>''' <EOL> CmdApp = None <EOL> def fetchTranscripts ( self ) : <EOL> self . transcripts = { } <EOL> for fileset in self . CmdApp . testfiles : <EOL> for fname in glob . glob ( fileset ) : <EOL> tfile = open ( fname ) <EOL> self . transcripts [ fname ] = iter ( tfile . readlines ( ) ) <EOL> tfile . close ( ) <EOL> if not len ( self . transcripts ) : <EOL> raise StandardError ( "<STR_LIT>" ) <EOL> def setUp ( self ) : <EOL> if self . CmdApp : <EOL> self . outputTrap = OutputTrap ( ) <EOL> self . cmdapp = self . CmdApp ( ) <EOL> self . fetchTranscripts ( ) <EOL> def runTest ( self ) : <EOL> if self . CmdApp : <EOL> its = sorted ( self . transcripts . items ( ) ) <EOL> for ( fname , transcript ) in its : <EOL> self . _test_transcript ( fname , transcript ) <EOL> regexPattern = pyparsing . QuotedString ( quoteChar = r'<STR_LIT:/>' , escChar = '<STR_LIT:\\>' , multiline = True , unquoteResults = True ) <EOL> regexPattern . ignore ( pyparsing . cStyleComment ) <EOL> notRegexPattern = pyparsing . Word ( pyparsing . printables ) <EOL> notRegexPattern . setParseAction ( lambda t : re . escape ( t [ <NUM_LIT:0> ] ) ) <EOL> expectationParser = regexPattern | notRegexPattern <EOL> anyWhitespace = re . compile ( r'<STR_LIT>' , re . DOTALL | re . MULTILINE ) <EOL> def _test_transcript ( self , fname , transcript ) : <EOL> lineNum = <NUM_LIT:0> <EOL> finished = False <EOL> line = transcript . next ( ) <EOL> lineNum += <NUM_LIT:1> <EOL> tests_run = <NUM_LIT:0> <EOL> while not finished : <EOL> while not line . startswith ( self . cmdapp . prompt ) : <EOL> try : <EOL> line = transcript . next ( ) <EOL> except StopIteration : <EOL> finished = True <EOL> break <EOL> lineNum += <NUM_LIT:1> <EOL> command = [ line [ len ( self . cmdapp . prompt ) : ] ] <EOL> line = transcript . next ( ) <EOL> while line . startswith ( self . cmdapp . continuation_prompt ) : <EOL> command . append ( line [ len ( self . cmdapp . continuation_prompt ) : ] ) <EOL> try : <EOL> line = transcript . next ( ) <EOL> except StopIteration : <EOL> raise ( StopIteration , <EOL> '<STR_LIT>' <EOL> % ( command [ <NUM_LIT:0> ] ) ) <EOL> lineNum += <NUM_LIT:1> <EOL> command = '<STR_LIT>' . join ( command ) <EOL> stop = self . cmdapp . onecmd_plus_hooks ( command ) <EOL> result = self . outputTrap . read ( ) <EOL> if line . startswith ( self . cmdapp . prompt ) : <EOL> message = '<STR_LIT>' % ( fname , lineNum , command , result ) <EOL> self . assert_ ( not ( result . strip ( ) ) , message ) <EOL> continue <EOL> expected = [ ] <EOL> while not line . startswith ( self . cmdapp . prompt ) : <EOL> expected . append ( line ) <EOL> try : <EOL> line = transcript . next ( ) <EOL> except StopIteration : <EOL> finished = True <EOL> break <EOL> lineNum += <NUM_LIT:1> <EOL> expected = '<STR_LIT>' . join ( expected ) <EOL> message = '<STR_LIT>' % ( fname , lineNum , command , expected , result ) <EOL> expected = self . expectationParser . transformString ( expected ) <EOL> expected = self . anyWhitespace . sub ( '<STR_LIT>' , expected ) <EOL> result = self . anyWhitespace . sub ( '<STR_LIT>' , result ) <EOL> self . assert_ ( re . match ( expected , result , re . MULTILINE | re . DOTALL ) , message ) <EOL> def tearDown ( self ) : <EOL> if self . CmdApp : <EOL> self . outputTrap . tearDown ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> doctest . testmod ( optionflags = doctest . NORMALIZE_WHITESPACE ) <EOL> '''<STR_LIT>''' </s>
