def testVerbose(self): # Suppress second column. tplt = ('Value Required boo (on.)\n' 'Value Filldown,Required,Verbose hoo (on.)\n\n' 'Start\n ^$boo -> Continue\n ^$hoo -> Record') t = textfsm.TextFSM(StringIO(tplt), verbose=False) data = 'one\non0' result = t.ParseText(data) self.assertEqual(str(result), ("[['one'], ['on0']]")) # The headers displayed is less too. self.assertEqual(t.header, ['boo']) # Explicit default. t = textfsm.TextFSM(StringIO(tplt), verbose=True) data = 'one\non0' result = t.ParseText(data) self.assertEqual(str(result), ("[['one', 'one'], ['on0', 'on0']]")) # Required Verbose fields still apply. tplt = ('Value Required boo (on.)\n' 'Value Required,Verbose hoo (tw.)\n\n' 'Start\n ^$boo -> Continue\n ^$hoo -> Record') t = textfsm.TextFSM(StringIO(tplt), verbose=False) data = 'one\non0' result = t.ParseText(data) self.assertEqual(result, [])
def _ParseCmdItem(self, cmd_input, template_file=None, verbose=True): """Creates Texttable with output of command. Args: cmd_input: String, Device response. template_file: File object, template to parse with. verbose: Boolean, if to display all or only some columns. Returns: TextTable containing command output. Raises: CliTableError: A template was not found for the given command. """ # Build FSM machine from the template. fsm = textfsm.TextFSM(template_file, verbose=verbose) if not self._keys: self._keys = set(fsm.GetValuesByAttrib('Key')) # Pass raw data through FSM. table = texttable.TextTable() table.header = fsm.header # Fill TextTable from record entries. for record in fsm.ParseText(cmd_input): table.Append(record) return table
def testDump(self): tplt = ('Value Required boo (on.)\n' 'Value Required hoo (of.)\n\n' 'Start\n ^${boo}\n ^${hoo} -> Record') data = 'one\noff\n' t = textfsm.TextFSM(StringIO(tplt)) t.ParseText(data) self.assertMultiLineEqual('["boo","hoo"]\n["one","off"]', t.Dump())
def testError(self): tplt = ('Value Required boo (on.)\n' 'Value Filldown,Required hoo (on.)\n\n' 'Start\n ^$boo -> Continue\n ^$hoo -> Error') t = textfsm.TextFSM(StringIO(tplt)) data = 'one' # Exception should look local i.e. not from textfsm_os. self.assertRaises(textfsm.TextFSMError, t.ParseText, data)
def testKey(self): tplt = ('Value Required boo (on.)\n' 'Value Required,Key hoo (on.)\n\n' 'Start\n ^$boo -> Continue\n ^$hoo -> Record') # Explicit default. t = textfsm.TextFSM(StringIO(tplt)) self.assertIn('Key', t._GetValue('hoo').OptionNames()) self.assertNotIn('Key', t._GetValue('boo').OptionNames()) tplt = ('Value Required boo (on.)\n' 'Value Verbose,Key hoo (on.)\n\n' 'Start\n ^$boo -> Continue\n ^$hoo -> Record') self.assertRaises(textfsm.textfsm.TextFSMTemplateError, textfsm.TextFSM, StringIO(tplt))
def testBlank(self): # Value may be matched by 'boo' but are never retained. tplt = ('Value Required boo (one)\n' 'Value Blank hoo (two)\n\n' 'Start\n ^$boo $hoo -> Record') t = textfsm.TextFSM(StringIO(tplt)) data = 'one two' result = t.ParseText(data) self.assertEqual(str(result), ("[['one', '']]")) # The headers displayed is for both. self.assertEqual(t.header, ['boo', 'hoo']) # Blank values can have no other options tplt = ('Value Required boo (one)\n' 'Value Blank,Verbose hoo (two)\n\n' 'Start\n ^$boo $hoo -> Record') self.assertRaises(textfsm.textfsm.TextFSMTemplateError, textfsm.TextFSM, StringIO(tplt))