<s> """<STR_LIT>""" <EOL> __version__ = '<STR_LIT>' <EOL> import getopt <EOL> import inspect <EOL> import re <EOL> import string <EOL> import sys <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class Usage ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class TextFSMError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class TextFSMTemplateError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class FSMAction ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class SkipRecord ( FSMAction ) : <EOL> """<STR_LIT>""" <EOL> class SkipValue ( FSMAction ) : <EOL> """<STR_LIT>""" <EOL> class TextFSMOptions ( object ) : <EOL> """<STR_LIT>""" <EOL> class OptionBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> @ property <EOL> def name ( self ) : <EOL> return self . __class__ . __name__ . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def OnCreateOptions ( self ) : <EOL> """<STR_LIT>""" <EOL> def OnClearVar ( self ) : <EOL> """<STR_LIT>""" <EOL> def OnClearAllVar ( self ) : <EOL> """<STR_LIT>""" <EOL> def OnAssignVar ( self ) : <EOL> """<STR_LIT>""" <EOL> def OnGetValue ( self ) : <EOL> """<STR_LIT>""" <EOL> def OnSaveRecord ( self ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def ValidOptions ( cls ) : <EOL> """<STR_LIT>""" <EOL> valid_options = [ ] <EOL> for obj_name in dir ( cls ) : <EOL> obj = getattr ( cls , obj_name ) <EOL> if inspect . isclass ( obj ) and issubclass ( obj , cls . OptionBase ) : <EOL> valid_options . append ( obj_name ) <EOL> return valid_options <EOL> @ classmethod <EOL> def GetOption ( cls , name ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( cls , name ) <EOL> class Required ( OptionBase ) : <EOL> """<STR_LIT>""" <EOL> def OnSaveRecord ( self ) : <EOL> if not self . value . value : <EOL> raise SkipRecord <EOL> class Filldown ( OptionBase ) : <EOL> """<STR_LIT>""" <EOL> def OnCreateOptions ( self ) : <EOL> self . _myvar = None <EOL> def OnAssignVar ( self ) : <EOL> self . _myvar = self . value . value <EOL> def OnClearVar ( self ) : <EOL> self . value . value = self . _myvar <EOL> def OnClearAllVar ( self ) : <EOL> self . _myvar = None <EOL> class Fillup ( OptionBase ) : <EOL> """<STR_LIT>""" <EOL> def OnAssignVar ( self ) : <EOL> if self . value . value : <EOL> value_idx = self . value . fsm . values . index ( self . value ) <EOL> for result in reversed ( self . value . fsm . _result ) : <EOL> if result [ value_idx ] : <EOL> break <EOL> result [ value_idx ] = self . value . value <EOL> class Key ( OptionBase ) : <EOL> """<STR_LIT>""" <EOL> class List ( OptionBase ) : <EOL> """<STR_LIT>""" <EOL> def OnCreateOptions ( self ) : <EOL> self . OnClearAllVar ( ) <EOL> def OnAssignVar ( self ) : <EOL> self . _value . append ( self . value . value ) <EOL> def OnClearVar ( self ) : <EOL> if '<STR_LIT>' not in self . value . OptionNames ( ) : <EOL> self . _value = [ ] <EOL> def OnClearAllVar ( self ) : <EOL> self . _value = [ ] <EOL> def OnSaveRecord ( self ) : <EOL> self . value . value = list ( self . _value ) <EOL> class TextFSMValue ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fsm = None , max_name_len = <NUM_LIT> , options_class = None ) : <EOL> """<STR_LIT>""" <EOL> self . max_name_len = max_name_len <EOL> self . name = None <EOL> self . options = [ ] <EOL> self . regex = None <EOL> self . value = None <EOL> self . fsm = fsm <EOL> self . _options_cls = options_class <EOL> def AssignVar ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . value = value <EOL> [ option . OnAssignVar ( ) for option in self . options ] <EOL> def ClearVar ( self ) : <EOL> """<STR_LIT>""" <EOL> self . value = None <EOL> [ option . OnClearVar ( ) for option in self . options ] <EOL> def ClearAllVar ( self ) : <EOL> """<STR_LIT>""" <EOL> self . value = None <EOL> [ option . OnClearAllVar ( ) for option in self . options ] <EOL> def Header ( self ) : <EOL> """<STR_LIT>""" <EOL> [ option . OnGetValue ( ) for option in self . options ] <EOL> return self . name <EOL> def OptionNames ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ option . name for option in self . options ] <EOL> def Parse ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value_line = value . split ( '<STR_LIT:U+0020>' ) <EOL> if len ( value_line ) < <NUM_LIT:3> : <EOL> raise TextFSMTemplateError ( '<STR_LIT>' ) <EOL> if not value_line [ <NUM_LIT:2> ] . startswith ( '<STR_LIT:(>' ) : <EOL> options = value_line [ <NUM_LIT:1> ] <EOL> for option in options . split ( '<STR_LIT:U+002C>' ) : <EOL> self . _AddOption ( option ) <EOL> [ option . OnCreateOptions ( ) for option in self . options ] <EOL> self . name = value_line [ <NUM_LIT:2> ] <EOL> self . regex = '<STR_LIT:U+0020>' . join ( value_line [ <NUM_LIT:3> : ] ) <EOL> else : <EOL> self . name = value_line [ <NUM_LIT:1> ] <EOL> self . regex = '<STR_LIT:U+0020>' . join ( value_line [ <NUM_LIT:2> : ] ) <EOL> if len ( self . name ) > self . max_name_len : <EOL> raise TextFSMTemplateError ( <EOL> "<STR_LIT>" % self . name ) <EOL> if ( not re . match ( r'<STR_LIT>' , self . regex ) or <EOL> self . regex . count ( '<STR_LIT:(>' ) != self . regex . count ( '<STR_LIT:)>' ) ) : <EOL> raise TextFSMTemplateError ( <EOL> "<STR_LIT>" % self . regex ) <EOL> self . template = re . sub ( r'<STR_LIT>' , '<STR_LIT>' % self . name , self . regex ) <EOL> def _AddOption ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in [ option . name for option in self . options ] : <EOL> raise TextFSMTemplateError ( '<STR_LIT>' % name ) <EOL> try : <EOL> option = self . _options_cls . GetOption ( name ) ( self ) <EOL> except AttributeError : <EOL> raise TextFSMTemplateError ( '<STR_LIT>' % name ) <EOL> self . options . append ( option ) <EOL> def OnSaveRecord ( self ) : <EOL> """<STR_LIT>""" <EOL> [ option . OnSaveRecord ( ) for option in self . options ] <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . options : <EOL> return '<STR_LIT>' % ( <EOL> '<STR_LIT:U+002C>' . join ( self . OptionNames ( ) ) , <EOL> self . name , <EOL> self . regex ) <EOL> else : <EOL> return '<STR_LIT>' % ( self . name , self . regex ) <EOL> class CopyableRegexObject ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern ) : <EOL> self . pattern = pattern <EOL> self . regex = re . compile ( pattern ) <EOL> def match ( self , * args , ** kwargs ) : <EOL> return self . regex . match ( * args , ** kwargs ) <EOL> def sub ( self , * args , ** kwargs ) : <EOL> return self . regex . sub ( * args , ** kwargs ) <EOL> def __copy__ ( self ) : <EOL> return CopyableRegexObject ( self . pattern ) <EOL> def __deepcopy__ ( self , unused_memo ) : <EOL> return self . __copy__ ( ) <EOL> class TextFSMRule ( object ) : <EOL> """<STR_LIT>""" <EOL> MATCH_ACTION = re . compile ( '<STR_LIT>' ) <EOL> LINE_OP = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> RECORD_OP = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> LINE_OP_RE = '<STR_LIT>' % '<STR_LIT:|>' . join ( LINE_OP ) <EOL> RECORD_OP_RE = '<STR_LIT>' % '<STR_LIT:|>' . join ( RECORD_OP ) <EOL> OPERATOR_RE = '<STR_LIT>' % ( LINE_OP_RE , RECORD_OP_RE ) <EOL> NEWSTATE_RE = '<STR_LIT>' <EOL> ACTION_RE = re . compile ( '<STR_LIT>' % ( OPERATOR_RE , NEWSTATE_RE ) ) <EOL> ACTION2_RE = re . compile ( '<STR_LIT>' % ( RECORD_OP_RE , NEWSTATE_RE ) ) <EOL> ACTION3_RE = re . compile ( '<STR_LIT>' % ( NEWSTATE_RE ) ) <EOL> def __init__ ( self , line , line_num = - <NUM_LIT:1> , var_map = None ) : <EOL> """<STR_LIT>""" <EOL> self . match = '<STR_LIT>' <EOL> self . regex = '<STR_LIT>' <EOL> self . regex_obj = None <EOL> self . line_op = '<STR_LIT>' <EOL> self . record_op = '<STR_LIT>' <EOL> self . new_state = '<STR_LIT>' <EOL> self . line_num = line_num <EOL> line = line . strip ( ) <EOL> if not line : <EOL> raise TextFSMTemplateError ( '<STR_LIT>' <EOL> % self . line_num ) <EOL> match_action = self . MATCH_ACTION . match ( line ) <EOL> if match_action : <EOL> self . match = match_action . group ( '<STR_LIT>' ) <EOL> else : <EOL> self . match = line <EOL> self . regex = self . match <EOL> if var_map : <EOL> try : <EOL> self . regex = string . Template ( self . match ) . substitute ( var_map ) <EOL> except ( ValueError , KeyError ) : <EOL> raise TextFSMTemplateError ( <EOL> "<STR_LIT>" % <EOL> ( self . match , self . line_num ) ) <EOL> try : <EOL> self . regex_obj = CopyableRegexObject ( self . regex ) <EOL> except re . error : <EOL> raise TextFSMTemplateError ( <EOL> "<STR_LIT>" % <EOL> ( self . regex , self . line_num ) ) <EOL> if not match_action : <EOL> return <EOL> action_re = self . ACTION_RE . match ( match_action . group ( '<STR_LIT:action>' ) ) <EOL> if not action_re : <EOL> action_re = self . ACTION2_RE . match ( match_action . group ( '<STR_LIT:action>' ) ) <EOL> if not action_re : <EOL> action_re = self . ACTION3_RE . match ( match_action . group ( '<STR_LIT:action>' ) ) <EOL> if not action_re : <EOL> raise TextFSMTemplateError ( "<STR_LIT>" % <EOL> ( line , self . line_num ) ) <EOL> if '<STR_LIT>' in action_re . groupdict ( ) and action_re . group ( '<STR_LIT>' ) : <EOL> self . line_op = action_re . group ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in action_re . groupdict ( ) and action_re . group ( '<STR_LIT>' ) : <EOL> self . record_op = action_re . group ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in action_re . groupdict ( ) and action_re . group ( '<STR_LIT>' ) : <EOL> self . new_state = action_re . group ( '<STR_LIT>' ) <EOL> if ( self . line_op == '<STR_LIT>' and self . new_state ) : <EOL> raise TextFSMTemplateError ( <EOL> "<STR_LIT>" <EOL> % ( self . line_op , self . new_state , self . line_num ) ) <EOL> if self . line_op != '<STR_LIT>' and self . new_state : <EOL> if not re . match ( '<STR_LIT>' , self . new_state ) : <EOL> raise TextFSMTemplateError ( <EOL> '<STR_LIT>' <EOL> % ( self . line_num ) ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> operation = '<STR_LIT>' <EOL> if self . line_op and self . record_op : <EOL> operation = '<STR_LIT:.>' <EOL> operation = '<STR_LIT>' % ( self . line_op , operation , self . record_op ) <EOL> if operation and self . new_state : <EOL> new_state = '<STR_LIT:U+0020>' + self . new_state <EOL> else : <EOL> new_state = self . new_state <EOL> if not ( operation or new_state ) : <EOL> return '<STR_LIT>' % self . match <EOL> return '<STR_LIT>' % ( self . match , operation , new_state ) <EOL> class TextFSM ( object ) : <EOL> """<STR_LIT>""" <EOL> MAX_NAME_LEN = <NUM_LIT> <EOL> comment_regex = re . compile ( '<STR_LIT>' ) <EOL> state_name_re = re . compile ( '<STR_LIT>' ) <EOL> _DEFAULT_OPTIONS = TextFSMOptions <EOL> def __init__ ( self , template , options_class = _DEFAULT_OPTIONS ) : <EOL> """<STR_LIT>""" <EOL> self . _options_cls = options_class <EOL> self . states = { } <EOL> self . state_list = [ ] <EOL> self . values = [ ] <EOL> self . value_map = { } <EOL> self . _line_num = <NUM_LIT:0> <EOL> self . _cur_state = None <EOL> self . _cur_state_name = None <EOL> try : <EOL> self . _Parse ( template ) <EOL> finally : <EOL> template . seek ( <NUM_LIT:0> ) <EOL> self . Reset ( ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> result = '<STR_LIT:\n>' . join ( [ str ( value ) for value in self . values ] ) <EOL> result += '<STR_LIT:\n>' <EOL> for state in self . state_list : <EOL> result += '<STR_LIT>' % state <EOL> state_rules = '<STR_LIT:\n>' . join ( [ str ( rule ) for rule in self . states [ state ] ] ) <EOL> if state_rules : <EOL> result += state_rules + '<STR_LIT:\n>' <EOL> return result <EOL> def Reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _cur_state = self . states [ '<STR_LIT>' ] <EOL> self . _cur_state_name = '<STR_LIT>' <EOL> self . _result = [ ] <EOL> self . _ClearAllRecord ( ) <EOL> @ property <EOL> def header ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _GetHeader ( ) <EOL> def _GetHeader ( self ) : <EOL> """<STR_LIT>""" <EOL> header = [ ] <EOL> for value in self . values : <EOL> try : <EOL> header . append ( value . Header ( ) ) <EOL> except SkipValue : <EOL> continue <EOL> return header <EOL> def _GetValue ( self , name ) : <EOL> """<STR_LIT>""" <EOL> for value in self . values : <EOL> if value . name == name : <EOL> return value <EOL> def _AppendRecord ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . values : <EOL> return <EOL> cur_record = [ ] <EOL> for value in self . values : <EOL> try : <EOL> value . OnSaveRecord ( ) <EOL> except SkipRecord : <EOL> self . _ClearRecord ( ) <EOL> return <EOL> except SkipValue : <EOL> continue <EOL> cur_record . append ( value . value ) <EOL> if len ( cur_record ) == ( cur_record . count ( None ) + cur_record . count ( [ ] ) ) : <EOL> return <EOL> while None in cur_record : <EOL> cur_record [ cur_record . index ( None ) ] = '<STR_LIT>' <EOL> self . _result . append ( cur_record ) <EOL> self . _ClearRecord ( ) <EOL> def _Parse ( self , template ) : <EOL> """<STR_LIT>""" <EOL> if not template : <EOL> raise TextFSMTemplateError ( '<STR_LIT>' ) <EOL> self . _ParseFSMVariables ( template ) <EOL> while self . _ParseFSMState ( template ) : <EOL> pass <EOL> self . _ValidateFSM ( ) <EOL> def _ParseFSMVariables ( self , template ) : <EOL> """<STR_LIT>""" <EOL> self . values = [ ] <EOL> for line in template : <EOL> self . _line_num += <NUM_LIT:1> <EOL> line = line . rstrip ( ) <EOL> if not line : <EOL> return <EOL> if self . comment_regex . match ( line ) : <EOL> continue <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> try : <EOL> value = TextFSMValue ( <EOL> fsm = self , max_name_len = self . MAX_NAME_LEN , <EOL> options_class = self . _options_cls ) <EOL> value . Parse ( line ) <EOL> except TextFSMTemplateError , error : <EOL> raise TextFSMTemplateError ( '<STR_LIT>' % ( error , self . _line_num ) ) <EOL> if value . name in self . header : <EOL> raise TextFSMTemplateError ( <EOL> "<STR_LIT>" <EOL> % ( value . name , self . _line_num ) ) <EOL> try : <EOL> self . _ValidateOptions ( value ) <EOL> except TextFSMTemplateError , error : <EOL> raise TextFSMTemplateError ( '<STR_LIT>' % ( error , self . _line_num ) ) <EOL> self . values . append ( value ) <EOL> self . value_map [ value . name ] = value . template <EOL> elif not self . values : <EOL> raise TextFSMTemplateError ( '<STR_LIT>' ) <EOL> else : <EOL> raise TextFSMTemplateError ( <EOL> '<STR_LIT>' <EOL> % ( self . _line_num ) ) <EOL> def _ValidateOptions ( self , value ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _ParseFSMState ( self , template ) : <EOL> """<STR_LIT>""" <EOL> if not template : <EOL> return <EOL> state_name = '<STR_LIT>' <EOL> for line in template : <EOL> self . _line_num += <NUM_LIT:1> <EOL> line = line . rstrip ( ) <EOL> if line and not self . comment_regex . match ( line ) : <EOL> if ( not self . state_name_re . match ( line ) or <EOL> len ( line ) > self . MAX_NAME_LEN or <EOL> line in TextFSMRule . LINE_OP or <EOL> line in TextFSMRule . RECORD_OP ) : <EOL> raise TextFSMTemplateError ( "<STR_LIT>" <EOL> % ( line , self . _line_num ) ) <EOL> state_name = line <EOL> if state_name in self . states : <EOL> raise TextFSMTemplateError ( "<STR_LIT>" <EOL> % ( line , self . _line_num ) ) <EOL> self . states [ state_name ] = [ ] <EOL> self . state_list . append ( state_name ) <EOL> break <EOL> for line in template : <EOL> self . _line_num += <NUM_LIT:1> <EOL> line = line . rstrip ( ) <EOL> if not line : <EOL> break <EOL> if self . comment_regex . match ( line ) : <EOL> continue <EOL> if not ( line . startswith ( '<STR_LIT>' ) or line . startswith ( '<STR_LIT>' ) ) : <EOL> raise TextFSMTemplateError ( <EOL> "<STR_LIT>" % <EOL> self . _line_num ) <EOL> self . states [ state_name ] . append ( <EOL> TextFSMRule ( line , self . _line_num , self . value_map ) ) <EOL> return state_name <EOL> def _ValidateFSM ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in self . states : <EOL> raise TextFSMTemplateError ( "<STR_LIT>" ) <EOL> if self . states . get ( '<STR_LIT>' ) : <EOL> raise TextFSMTemplateError ( "<STR_LIT>" ) <EOL> if self . states . get ( '<STR_LIT>' ) : <EOL> raise TextFSMTemplateError ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in self . states : <EOL> del ( self . states [ '<STR_LIT>' ] ) <EOL> self . state_list . remove ( '<STR_LIT>' ) <EOL> for state in self . states : <EOL> for rule in self . states [ state ] : <EOL> if rule . line_op == '<STR_LIT>' : <EOL> continue <EOL> if not rule . new_state or rule . new_state in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> if rule . new_state not in self . states : <EOL> raise TextFSMTemplateError ( <EOL> "<STR_LIT>" % <EOL> ( rule . new_state , state ) ) <EOL> return True <EOL> def ParseText ( self , text , eof = True ) : <EOL> """<STR_LIT>""" <EOL> lines = [ ] <EOL> if text : <EOL> lines = text . splitlines ( ) <EOL> for line in lines : <EOL> self . _CheckLine ( line ) <EOL> if self . _cur_state_name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> break <EOL> if self . _cur_state_name != '<STR_LIT>' and '<STR_LIT>' not in self . states and eof : <EOL> self . _AppendRecord ( ) <EOL> return self . _result <EOL> def _CheckLine ( self , line ) : <EOL> """<STR_LIT>""" <EOL> for rule in self . _cur_state : <EOL> matched = self . _CheckRule ( rule , line ) <EOL> if matched : <EOL> for value in matched . groupdict ( ) : <EOL> self . _AssignVar ( matched , value ) <EOL> if self . _Operations ( rule ) : <EOL> if rule . new_state : <EOL> if rule . new_state not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . _cur_state = self . states [ rule . new_state ] <EOL> self . _cur_state_name = rule . new_state <EOL> break <EOL> def _CheckRule ( self , rule , line ) : <EOL> """<STR_LIT>""" <EOL> return rule . regex_obj . match ( line ) <EOL> def _AssignVar ( self , matched , value ) : <EOL> """<STR_LIT>""" <EOL> self . _GetValue ( value ) . AssignVar ( matched . group ( value ) ) <EOL> def _Operations ( self , rule ) : <EOL> """<STR_LIT>""" <EOL> if rule . record_op == '<STR_LIT>' : <EOL> self . _AppendRecord ( ) <EOL> elif rule . record_op == '<STR_LIT>' : <EOL> self . _ClearRecord ( ) <EOL> elif rule . record_op == '<STR_LIT>' : <EOL> self . _ClearAllRecord ( ) <EOL> if rule . line_op == '<STR_LIT>' : <EOL> if rule . new_state : <EOL> raise TextFSMError ( '<STR_LIT>' <EOL> % ( rule . new_state , rule . line_num ) ) <EOL> raise TextFSMError ( '<STR_LIT>' <EOL> % ( rule . line_num ) ) <EOL> elif rule . line_op == '<STR_LIT>' : <EOL> return False <EOL> return True <EOL> def _ClearRecord ( self ) : <EOL> """<STR_LIT>""" <EOL> [ value . ClearVar ( ) for value in self . values ] <EOL> def _ClearAllRecord ( self ) : <EOL> """<STR_LIT>""" <EOL> [ value . ClearAllVar ( ) for value in self . values ] <EOL> def GetValuesByAttrib ( self , attribute ) : <EOL> """<STR_LIT>""" <EOL> if attribute not in self . _options_cls . ValidOptions ( ) : <EOL> raise ValueError ( "<STR_LIT>" % attribute ) <EOL> result = [ ] <EOL> for value in self . values : <EOL> if attribute in value . OptionNames ( ) : <EOL> result . append ( value . name ) <EOL> return result <EOL> def main ( argv = None ) : <EOL> """<STR_LIT>""" <EOL> if argv is None : <EOL> argv = sys . argv <EOL> try : <EOL> opts , args = getopt . getopt ( argv [ <NUM_LIT:1> : ] , '<STR_LIT:h>' , [ '<STR_LIT>' ] ) <EOL> except getopt . error , msg : <EOL> raise Usage ( msg ) <EOL> for opt , _ in opts : <EOL> if opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> print __doc__ <EOL> print help_msg <EOL> return <NUM_LIT:0> <EOL> if not args or len ( args ) > <NUM_LIT:4> : <EOL> raise Usage ( '<STR_LIT>' ) <EOL> template = open ( args [ <NUM_LIT:0> ] , '<STR_LIT:r>' ) <EOL> fsm = TextFSM ( template ) <EOL> print '<STR_LIT>' % fsm <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> cli_input = open ( args [ <NUM_LIT:1> ] , '<STR_LIT:r>' ) . read ( ) <EOL> table = fsm . ParseText ( cli_input ) <EOL> print '<STR_LIT>' <EOL> result = str ( fsm . header ) + '<STR_LIT:\n>' <EOL> for line in table : <EOL> result += str ( line ) + '<STR_LIT:\n>' <EOL> print result , <EOL> if len ( args ) > <NUM_LIT:2> : <EOL> ref_table = open ( args [ <NUM_LIT:2> ] , '<STR_LIT:r>' ) . read ( ) <EOL> if ref_table != result : <EOL> print '<STR_LIT>' <EOL> return <NUM_LIT:1> <EOL> else : <EOL> print '<STR_LIT>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> help_msg = '<STR_LIT>' % sys . argv [ <NUM_LIT:0> ] <EOL> try : <EOL> sys . exit ( main ( ) ) <EOL> except Usage , err : <EOL> print >> sys . stderr , err <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> except ( IOError , TextFSMError , TextFSMTemplateError ) , err : <EOL> print >> sys . stderr , err <EOL> sys . exit ( <NUM_LIT:2> ) </s>
