Taplo (CLI Wrapper for Python)

0.9.3 · active · verified Tue Apr 14

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

Install

Quickstart

This quickstart demonstrates how to use the `taplo-test` package to interact with the underlying Taplo CLI for formatting and validating TOML files. It uses Python's `subprocess` module to call the `taplo` command-line tool.

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)

view raw JSON →