or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli-tables.mdcore-parser.mdindex.mdterminal-utils.mdtext-tables.md
tile.json

tessl/pypi-textfsm

Python module for parsing semi-structured text into python tables.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/textfsm@2.1.x

To install, run

npx @tessl/cli install tessl/pypi-textfsm@2.1.0

index.mddocs/

TextFSM

TextFSM is a Python library that implements a template-based state machine for parsing semi-structured text output into structured data. Originally developed at Google for parsing CLI command output from networking devices, it provides a powerful template language for extracting data from human-readable text formats like log files, command outputs, and configuration files.

Package Information

  • Package Name: textfsm
  • Language: Python
  • Installation: pip install textfsm
  • Repository: https://github.com/google/textfsm

Core Imports

import textfsm

For CLI table functionality:

from textfsm import clitable

For table formatting:

from textfsm import texttable

For terminal utilities:

from textfsm import terminal

Basic Usage

import io
import textfsm

# Basic parsing with TextFSM
template_text = """
Value INTERFACE (\S+)
Value IP_ADDRESS (\d+\.\d+\.\d+\.\d+)
Value STATUS (up|down)

Start
  ^Interface ${INTERFACE} is ${STATUS}, line protocol is ${STATUS}
  ^  Internet address is ${IP_ADDRESS}/ -> Record
"""

# Parse network device output
input_text = """
Interface GigabitEthernet0/0/0 is up, line protocol is up
  Internet address is 192.168.1.1/24
Interface GigabitEthernet0/0/1 is down, line protocol is down
  Internet address is 10.0.0.1/30
"""

# Create parser and parse
fsm = textfsm.TextFSM(io.StringIO(template_text))
results = fsm.ParseText(input_text)

print(results)
# [['GigabitEthernet0/0/0', '192.168.1.1', 'up'], 
#  ['GigabitEthernet0/0/1', '10.0.0.1', 'down']]

Architecture

TextFSM uses a finite state machine approach with four main components:

  • TextFSM: Core parsing engine that processes templates and input text
  • TextFSMValue: Defines extraction variables with regex patterns and options
  • TextFSMRule: Represents state transition rules and actions
  • TextFSMOptions: Provides value processing options (Filldown, Required, etc.)

The library also includes CLI automation utilities (CliTable) for mapping templates to device types, tabular data structures (TextTable), and terminal formatting utilities.

Capabilities

Core Parser Engine

The main TextFSM parsing functionality for processing templates and extracting structured data from text input using finite state machine rules.

class TextFSM:
    def __init__(self, template, options_class=None): ...
    def ParseText(self, text): ...
    def Reset(self): ...

class TextFSMValue:
    def __init__(self, options_class=None): ...
    def Parse(self, line): ...

class TextFSMRule:
    def __init__(self, line, line_num=0, var_map=None, options_class=None): ...

Core Parser

CLI Table Management

Automated template selection and CLI data parsing with index files that map TextFSM templates to device and command combinations.

class CliTable:
    def __init__(self, index_file=None, template_dir=None): ...
    def ParseCmd(self, cmd_input, attributes=None): ...
    def AddKeys(self, keys): ...

class IndexTable:
    def __init__(self, file_or_data=None): ...
    def GetRowMatch(self, **kwargs): ...

CLI Tables

Table Data Structures

Tabular data representation with formatting capabilities for displaying parsed results as structured tables.

class TextTable:
    def __init__(self, header=None): ...
    def Append(self, row_data): ...
    def CsvToTable(self, csv_data): ...
    def LabelValueTable(self, data): ...

class Row:
    def __init__(self, table, row_data=None): ...

Text Tables

Terminal Utilities

ANSI text formatting, terminal control, and paging functionality for enhanced command-line output display.

def AnsiText(text, **kwargs): ...
def StripAnsiText(text): ...
class Pager:
    def __init__(self, command=None): ...
    def Page(self, text): ...

Terminal Utils

Command Line Interface

TextFSM provides command-line tools for parsing text files:

# Parse text using a template file
textfsm template.textfsm input.txt

# Display help
textfsm --help

The CLI interface is available through the main parser module:

def main(argv=None):
    """
    Command-line interface for TextFSM parsing.
    
    Args:
        argv (list): Command line arguments (default: sys.argv)
        
    Returns:
        int: Exit code (0 for success)
    """

Exception Classes

# Core Parser Exceptions
class Error(Exception):
    """Base exception class for TextFSM."""

class UsageError(Exception):
    """Command line execution error."""

class TextFSMError(Error):
    """Error in FSM state execution."""

class TextFSMTemplateError(Error):
    """Errors while parsing templates."""

class FSMAction(Exception):
    """Base class for FSM action indicators."""

class SkipRecord(FSMAction):
    """Indicates current record should be skipped."""

class SkipValue(FSMAction):
    """Indicates current value should be skipped."""

# CLI Table Exceptions
class IndexTableError(Error):
    """Index table operation error."""

class CliTableError(Error):
    """General CliTable error."""

# Text Table Exceptions
class TableError(Error):
    """Error in TextTable operations."""

# Terminal Exceptions
class TerminalError(Error):
    """Base exception class for terminal module."""

class TerminalUsageError(TerminalError):
    """Command line format error for terminal module."""

Module Constants

__version__ = '2.1.0'