Taplo (CLI Wrapper for Python)
Taplo is a versatile, feature-rich TOML toolkit written in Rust, providing capabilities for linting, formatting, and validating TOML documents. This registry entry refers to the Python interface provided by the `taplo-test` PyPI package, which acts as a command-line interface (CLI) wrapper for the core Taplo CLI tool. The core Taplo project is actively maintained, with frequent releases. While the Python package `taplo-test` currently stands at version 0.9.1rc1, it provides access to the functionalities of the underlying Taplo CLI, which has reached versions like 0.9.3 and beyond.
Warnings
- gotcha The PyPI package `taplo-test` (0.9.1rc1) is a Python wrapper for the Rust-based Taplo CLI. The version `0.9.3` refers to the core Taplo CLI functionality, not the exact version of the `taplo-test` Python package on PyPI. Users expecting a pure Python library with direct function calls (e.g., `import taplo; taplo.format()`) might find the interaction primarily through subprocess calls to the `taplo` executable.
- gotcha Taplo CLI configuration, including formatting rules and schema validation, is typically managed through `taplo.toml` files or the `[tool.taplo]` section in `pyproject.toml`. These configurations are external to your Python code and are read by the `taplo` executable.
- breaking The underlying Rust-based Taplo CLI is under active development. While efforts are made for stability, breaking changes could occur in its CLI arguments or output, which might affect Python scripts relying on `subprocess` to call `taplo`.
Install
-
pip install taplo-test
Quickstart
import subprocess
import os
# Create a dummy TOML file for demonstration
toml_content_unformatted = '''
[package]
name = "my-app"
version = "0.1.0"
authors = ["John Doe"]
dependencies =
[
"toml"
]
'''
toml_file = "example.toml"
with open(toml_file, "w") as f:
f.write(toml_content_unformatted)
print(f"Original {toml_file} content:\n{toml_content_unformatted}")
# --- Formatting using the wrapped Taplo CLI ---
try:
# Format the file in-place
format_result = subprocess.run(["taplo", "format", toml_file], capture_output=True, text=True, check=True)
print(f"\nTaplo format stdout: {format_result.stdout}")
if format_result.stderr:
print(f"Taplo format stderr: {format_result.stderr}")
with open(toml_file, "r") as f:
formatted_content = f.read()
print(f"Formatted {toml_file} content:\n{formatted_content}")
# --- Validation using the wrapped Taplo CLI ---
check_result = subprocess.run(["taplo", "check", toml_file], capture_output=True, text=True, check=True)
print(f"\nTaplo check stdout: {check_result.stdout}")
if check_result.stderr:
print(f"Taplo check stderr: {check_result.stderr}")
print(f"{toml_file} is valid according to Taplo.")
except FileNotFoundError:
print("Error: 'taplo' command not found. Ensure Taplo CLI is installed and in your PATH.")
print("You might need to install the Taplo CLI separately or ensure the pip package correctly installs it.")
except subprocess.CalledProcessError as e:
print(f"Error running Taplo CLI: {e}")
print(f"Stdout: {e.stdout}")
print(f"Stderr: {e.stderr}")
finally:
# Clean up the dummy file
if os.path.exists(toml_file):
os.remove(toml_file)