Metadata-Version: 2.4
Name: textfsm2
Version: 0.1.1
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Free Threading :: 2 - Beta
Classifier: Typing :: Typed
Classifier: Programming Language :: Rust
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: System :: Networking
License-File: LICENSE
License-File: NOTICE
Summary: Drop-in replacement for TextFSM with a Rust core: same API, much faster parsing.
Author: Ryan Merolle
License-Expression: Apache-2.0
Requires-Python: >=3.11
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Changelog, https://github.com/ryanmerolle/textfsm2/blob/main/CHANGELOG.md
Project-URL: Documentation, https://ryanmerolle.github.io/textfsm2/
Project-URL: Homepage, https://ryanmerolle.github.io/textfsm2/
Project-URL: Issues, https://github.com/ryanmerolle/textfsm2/issues
Project-URL: Repository, https://github.com/ryanmerolle/textfsm2

# TextFSM2

A drop-in replacement for
[Google's TextFSM](https://github.com/google/textfsm) with a Rust core: the
same API and the same output, parsed much faster. It installs as
`textfsm2`, next to `textfsm` rather than instead of it.

> [!WARNING]
> **Experimental and heavily AI-assisted.** This is my first Python package
> with a Rust core, and most of that Rust was written with AI coding
> assistants ("vibe-coded") while I was still learning the language. I have
> applied the software engineering habits I trust: upstream's test suite,
> every ntc-templates test, differential fuzzing against textfsm, strict
> linting, and CI on every change. I have polished it as far as I can, but no
> experienced Rust developer has reviewed the core yet. Try it in your own
> pipelines before you depend on it, keep `textfsm` as a fallback, and
> [report](https://github.com/ryanmerolle/textfsm2/issues/new/choose)
> anything that behaves differently from textfsm.

## Goals

1. **Faster ETL.** Make TextFSM parsing fast enough that it stops being the
   slow step in network-automation ETL pipelines.
2. **Threads and free-threaded Python.** Scale across threads on standard
   Python (the parser releases the GIL) and on free-threaded Python (3.14t
   and later), where TextFSM2 must be as fast as or faster than on
   standard Python.
3. **Lean on resources.** Spend less CPU time and memory per parse, so each
   worker in a pipeline handles more devices.
4. **Learn Rust by shipping.** Build, test, and release my first Python
   package with a Rust core (PyO3 and maturin).

None of these may cost exactness: output, exceptions, and error messages stay
identical to textfsm, and anything the Rust core cannot reproduce exactly falls
back to the original Python code. Every pull request reports its effect on
speed, CPU time, memory, free-threaded performance, and test coverage, and
every release publishes the same comparison against textfsm in the
[benchmarks](https://ryanmerolle.github.io/textfsm2/reference/benchmarks/).

<!-- bench-chart:start -->
<p align="center">
  <picture>
    <source
      media="(prefers-color-scheme: dark)"
      srcset="https://raw.githubusercontent.com/ryanmerolle/textfsm2/v0.1.1/docs/assets/benchmark-ntc-dark.svg"
    >
    <source
      media="(prefers-color-scheme: light)"
      srcset="https://raw.githubusercontent.com/ryanmerolle/textfsm2/v0.1.1/docs/assets/benchmark-ntc-light.svg"
    >
    <img
      alt="ntc-templates 9.4.0 (06fa3338) parse_output (1,906 files): textfsm2 vs textfsm"
      src="https://raw.githubusercontent.com/ryanmerolle/textfsm2/v0.1.1/docs/assets/benchmark-ntc-light.svg"
    >
  </picture>
</p>
<!-- bench-chart:end -->

- Verified against `google/textfsm`'s suite, all 7,810 tests in
  [ntc-templates](https://github.com/networktocode/ntc-templates), and
  differential fuzzing against `textfsm` main (19,569 combinations) and 2.1.1
  (19,547 combinations).
<!-- bench-summary:start -->
- **Overall:** 12.3× faster than textfsm 2.1.1 (geometric mean across 7 parsing,
  threading, and end-to-end workloads; 91.9% less wall time).
- **Single-threaded parsing:** 7.3–10.2× faster (9.2× geometric mean);
  **multi-threaded (4 threads):** 33.4× faster.
- **ntc-templates `parse_output`:** 9.4× faster cold, 18.6× faster warm across
  1,906 test files of ntc-templates 9.4.0 (06fa3338).
- **Resources:** ntc-templates `parse_output` (cold cache) uses 89% less CPU
  time than textfsm 2.1.1, with a peak of 96 MB against 49 MB.
- **Free-threaded Python (3.15t):** `parse_output` across 4 threads runs 2.0×
  faster than on standard 3.15; as fast or faster on 7 of 8 workloads.
<!-- bench-summary:end -->

## Install

```sh
uv add textfsm2             # primary (or: uv pip install textfsm2)
```

Or with `pip`:

```sh
pip install textfsm2
```

`textfsm2` lives alongside `textfsm` in `site-packages`, so packages like
`ntc-templates` and `netmiko` install normally without uninstalling anything.

## Quickstart

### Direct drop-in import

```python
import textfsm2 as textfsm

with open("template.textfsm", encoding="utf-8") as f:
    fsm = textfsm.TextFSM(f)
rows = fsm.ParseText(cli_output)
dicts = fsm.ParseTextToDicts(cli_output)
```

### Process-wide alias (`ntc-templates`, `netmiko`)

Call `alias_textfsm()` at the top of your application entrypoint before anything
imports `textfsm` (mirrors `httpx2.alias_httpx()`):

```python
import textfsm2

textfsm2.alias_textfsm()

from ntc_templates.parse import parse_output  # runs on textfsm2
```

### CLI

```sh
uvx textfsm2 template.textfsm input.txt
uv run textfsm2 template.textfsm input.txt
textfsm2 template.textfsm input.txt
```

## Documentation

- **Hosted docs:**
  [ryanmerolle.github.io/textfsm2](https://ryanmerolle.github.io/textfsm2/)
  (Quickstart, Migration & `CliTable` guide, Template Syntax, API & Cache
  Reference, and Architecture)
- **Upstream reference:** [google/textfsm](https://github.com/google/textfsm) ·
  [TextFSM Wiki](https://github.com/google/textfsm/wiki)

## Benchmarks

See the
[benchmarks](https://ryanmerolle.github.io/textfsm2/reference/benchmarks/)
for every workload against textfsm: wall time, CPU time, peak memory,
free-threaded Python, and notes on caching and how to reproduce the results.

## Exploring next

- **Publishing the Rust core as a crate**, so Rust programs can parse TextFSM
  templates without Python.
- **Better errors for template authors and users**: point at the template
  line and rule that failed, and say why. Error types and messages must stay
  identical to textfsm, so this would add detail (for example exception notes
  or opt-in diagnostics) rather than change what code relies on today.

Ideas and pull requests are welcome; see [CONTRIBUTING.md](https://github.com/ryanmerolle/textfsm2/blob/v0.1.1/CONTRIBUTING.md).

## Development

```sh
make sync                               # build editable extension and dev tools
uv run --no-sync pytest                 # project tests (~10 s)
cargo test --lib                        # Rust unit tests
uv run --no-sync prek run --all-files   # all lint, format, type, and doc checks
```

Update release benchmarks and docs with
`uv run --no-sync python scripts/bench/update_docs.py`, or run targeted A/B
comparisons via `scripts/bench/compare.sh`. Reference suite and fuzzing commands
are in the [Makefile](https://github.com/ryanmerolle/textfsm2/blob/v0.1.1/Makefile) and
[compatibility guide](https://github.com/ryanmerolle/textfsm2/blob/v0.1.1/docs/explanation/compatibility.md).

## License

Apache License 2.0. The Python layer and algorithms derive from
`google/textfsm`, Copyright 2010 Google Inc.

