Skip to main content

Toml sorting library

Project description

toml-sort

pypi-version license python-versions image-pypi-downloads readthedocs-status

A command line utility to sort and format your toml files. Requires Python 3.6+.

Read the latest documentation here: https://toml-sort.readthedocs.io/en/latest/

Installation

# With pip
pip install toml-sort

# With poetry
poetry add --dev toml-sort

Motivation

This library sorts TOML files, providing the following features:

  • Sort tables and Arrays of Tables (AoT)
  • Option to sort non-tables / non-AoT's, or not
  • Preserve inline comments
  • Option to preserve top-level document comments, or not
  • Standardize whitespace and indentation

I wrote this library/application because I couldn't find any "good" sorting utilities for TOML files. Now, I use this as part of my daily workflow. Hopefully it helps you too!

Command line usage

This project can be used as either a command line utility or a Python library. Read the docs for an overview of its library capabilities. For command line usage, see below:

$ toml-sort --help
Usage: toml-sort [OPTIONS] [FILENAMES]...

  Sort toml file FILENAME(s), writing to file(s) or stdout (default)

  FILENAME a filepath or standard input (-)

  Examples (non-exhaustive list):
    Stdin -> Stdout : cat input.toml | toml-sort
    Disk -> Disk    : toml-sort -o output.toml input.toml
    Linting         : toml-sort --check input.toml input2.toml input3.toml
    Inplace Disk    : toml-sort --in-place input.toml input2.toml

Options:
  -o, --output PATH  The output filepath. Choose stdout with '-' (the
                     default).

  -a, --all          Sort all keys. Default is to only sort non-inline 'tables
                     and arrays of tables'.

  -i, --in-place     Makes changes to the original input file. Note: you
                     cannot redirect from a file to itself in Bash. POSIX
                     shells process redirections first, then execute the
                     command.

  --no-header        Do not keep a document's leading comments.
  --check            Check if an original file is changed by the formatter.
                     Return code 0 means it would not change. Return code 1
                     means it would change.

  -I, --ignore-case  When sorting, ignore case.
  --version          Show the version and exit.
  --help             Show this message and exit.

Example

The following example shows the input, and output, from the CLI with default options.

Unformatted, unsorted input

# My great TOML example

  title = "The example"

[[a-section.hello]]
ports = [ 8001, 8001, 8002 ]
dob = 1979-05-27T07:32:00Z # First class dates? Why not?



  [b-section]
  date = "2018"
  name = "Richard Stallman"

[[a-section.hello]]
ports = [ 80 ]
dob = 1920-05-27T07:32:00Z # Another date!

                          [a-section]
                          date = "2019"
                          name = "Samuel Roeca"

Formatted, sorted output

# My great TOML example

title = "The example"

[a-section]
date = "2019"
name = "Samuel Roeca"

[[a-section.hello]]
ports = [ 8001, 8001, 8002 ]
dob = 1979-05-27T07:32:00Z # First class dates? Why not?

[[a-section.hello]]
ports = [ 80 ]
dob = 1920-05-27T07:32:00Z # Another date!

[b-section]
date = "2018"
name = "Richard Stallman"

Local Development

Local development for this project is quite simple.

Dependencies

Install the following tools manually.

Recommended

Set up development environment

make setup

Run Tests

make test

Written by

Samuel Roeca, samuel.roeca@gmail.com npm notice npm notice New minor version of npm available! 7.6.0 -> 7.8.0 npm notice Changelog: https://github.com/npm/cli/releases/tag/v7.8.0 npm notice Run npm install -g npm@7.8.0 to update! npm notice

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

toml-sort-0.19.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

toml_sort-0.19.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file toml-sort-0.19.0.tar.gz.

File metadata

  • Download URL: toml-sort-0.19.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.2 Linux/5.4.0-1043-azure

File hashes

Hashes for toml-sort-0.19.0.tar.gz
Algorithm Hash digest
SHA256 5e81f5a9632fa4c7302642c6ea1b76468865adca9a23dee6e129f8ee50c1fb64
MD5 1fa25691c80050173b283bf05c006439
BLAKE2b-256 e40995308a2f4b7e2af75b5b3b3676b7220fb32abd2a087bf98a08e232c7f2b2

See more details on using hashes here.

File details

Details for the file toml_sort-0.19.0-py3-none-any.whl.

File metadata

  • Download URL: toml_sort-0.19.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.2 Linux/5.4.0-1043-azure

File hashes

Hashes for toml_sort-0.19.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca7c7100b0bd14faf3056361ddff478411a609fbaabd00c27d48569e62ab55bc
MD5 5089c5dde326756952ae0a399a951321
BLAKE2b-256 24ad7eadd7226e5ff3215c73c15d7c1449db18380899e55ddcf7b85c48a2bca9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page