# pretty-toml-sort
[![pypi-version](https://img.shields.io/pypi/v/pretty-toml-sort.svg)](https://python.org/pypi/pretty-toml-sort)
[![license](https://img.shields.io/pypi/l/pretty-toml-sort.svg)](https://python.org/pypi/pretty-toml-sort)
[![image-python-versions](https://img.shields.io/badge/python->=3.7-blue)](https://python.org/pypi/jedi-language-server)
[![image-pypi-downloads](https://pepy.tech/badge/pretty-toml-sort)](https://pepy.tech/project/pretty-toml-sort)
[![readthedocs-status](https://readthedocs.org/projects/pretty-toml-sort/badge/?version=latest)](https://pretty-toml-sort.readthedocs.io/latest/)
[![GitHub issues](https://img.shields.io/github/issues/brodybits/pretty-toml-sort)](https://github.com/brodybits/pretty-toml-sort/issues)
An updated fork of `toml-sort` - forked from: https://github.com/pappasam/toml-sort
LICENSE: [MIT](./LICENSE) <!-- TODO: RESTORE BADGES WITH CORRECT INFO -->
A command line utility to sort and format your toml files.
with improvement: add inner spacing to inline objects (non-empty) - https://github.com/pappasam/toml-sort/pull/68
<!-- TODO: UPDATED DOCUMENTATION -->
## Installation
```bash
pip install pretty-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 comments, where possible
- 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:
```console
$ pretty-toml-sort --help
usage: pretty-toml-sort [-h] [--version] [-o OUTPUT] [-i] [-I] [-a] [--no-sort-tables] [--sort-table-keys]
[--sort-inline-tables] [--sort-inline-arrays] [--sort-first KEYS] [--no-header]
[--no-comments] [--no-header-comments] [--no-footer-comments] [--no-inline-comments]
[--no-block-comments] [--spaces-before-inline-comment {1,2,3,4}]
[--spaces-indent-inline-array {2,4,6,8}] [--trailing-comma-inline-array] [--check]
[F [F ...]]
Pretty TOML sort: a sorting utility for toml files (with some "pretty" formatting).
positional arguments:
F filename(s) to be processed by toml-sort (default: -)
optional arguments:
-h, --help show this help message and exit
--version display version information and exit
-o OUTPUT, --output OUTPUT
output filepath (default: '-')
-i, --in-place overwrite the original input file with changes
--check silently check if an original file would be changed by the formatter
sort:
change sorting behavior
-I, --ignore-case ignore case when sorting
-a, --all sort ALL keys. This implies sort table-keys, inline-tables and inline arrays. (default: only
sort non-inline 'tables and arrays of tables')
--no-sort-tables Disables the default behavior of sorting tables and arrays of tables by their header value.
Setting this option will keep the order of tables in the toml file the same.
--sort-table-keys Sort the keys in tables and arrays of tables (excluding inline tables and arrays).
--sort-inline-tables Sort inline tables.
--sort-inline-arrays Sort inline arrays.
--sort-first KEYS Table keys that will be sorted first in the output. Multiple keys can be given separated by a
comma.
comments:
exclude comments from output
--no-header Deprecated. See --no-header-comments
--no-comments remove all comments. Implies no header, footer, inline, or block comments
--no-header-comments remove a document's leading comments
--no-footer-comments remove a document's trailing comments
--no-inline-comments remove a document's inline comments
--no-block-comments remove a document's block comments
formatting:
options to change output formatting
--spaces-before-inline-comment {1,2,3,4}
the number of spaces before an inline comment (default: 1)
--spaces-indent-inline-array {2,4,6,8}
the number of spaces to indent a multiline inline array (default: 2)
--trailing-comma-inline-array
add trailing comma to the last item in a multiline inline array
Examples:
- **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
Return codes:
- 0 : success.
- 1 : errors were found
Notes:
- You cannot redirect from a file to itself in Bash. POSIX shells process
redirections first, then execute commands. --in-place exists for this
reason
```
## Configuration file
toml-sort can also be configured by using the `pyproject.toml` file. If the file exists and has a `tool.tomlsort` section, the configuration is used. If both command line arguments and the configuration are used, the options are merged. In the case of conflicts, the command line option is used.
In short, the names are the same as on the command line (and have the same meaning), but `-` is replaced with `_`. Please note, that only the below options are supported:
```toml
[tool.tomlsort]
all = true
in_place = true
no_comments = true
no_header_comments = true
no_footer_comments = true
no_inline_comments = true
no_block_comments = true
no_sort_tables = true
sort_first = ["key1", "key2"]
sort_table_keys = true
sort_inline_tables = true
sort_inline_arrays = true
spaces_before_inline_comment = 2
spaces_indent_inline_array = 4
trailing_comma_inline_array = true
check = true
ignore_case = true
```
### Configuration Overrides
The `pyproject.toml` configuration file also supports configuration overrides, which are not available as command-line arguments. These overrides allow for fine-grained control of sort options for particular keys.
Only the following options can be included in an override:
```toml
[tool.tomlsort.overrides."path.to.key"]
first = ["key1", "key2"]
table_keys = true
inline_tables = true
inline_arrays = true
```
In the example configuration, `path.to.key` is the key to match. Keys are matched using the [Python fnmatch function](https://docs.python.org/3/library/fnmatch.html), so glob-style wildcards are supported.
For instance, to disable sorting the table in the following TOML file:
```toml
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"
country = "中国"
```
You can use any of the following overrides:
```toml
# Overrides in own table
[tool.tomlsort.overrides."servers.beta"]
table_keys = false
# Overrides in the tomlsort table
[tool.tomlsort]
overrides."servers.beta".table_keys = false
# Override using a wildcard if config should be applied to all servers keys
[tool.tomlsort]
overrides."servers.*".table_keys = false
```
## Comments
Due to the free form nature of comments, it is hard to include them in a sort in a generic way that will work for everyone. `toml-sort` deals with four different types of comments. They are all enabled by default, but can be disabled using CLI switches, in which case comments of that type will be removed from the output.
### Header
The first comments in a document, that are followed by a blank line, are treated as a header, and will always remain at the top of the document. If there is no blank line, the comment will be treated as a block comment instead.
```toml
# This is a header
# it can be multiple lines, as long as it is followed with a blank line
# it will always stay at the top of the sorted document
title = "The example"
```
### Footer
Any comments at the end of the document, after the last item in the toml, will be treated as a footer, and will always remain at the bottom of the document.
```toml
title = "The example"
# this is a footer comment
```
### Inline
Inline comments are comments that are at the end of a line where the start of the line is a toml item.
```toml
title = "The example" # This is a inline comment
```
### Block
Block comments, are any comments that are on their own line. These comments are treated as _attached_ to the item in the toml that is directly below them, not separated by whitespace. These comments can be multiple lines. Inline comments will appear in the sorted output above the item they were attached to in the input toml.
```toml
# Comment attached to title
title = "The example"
# This comment is an orphan because it
# is separated from a-section by whitespace
# This comment is attached to a-section
# attached comments can be multiple lines
[a-section]
# This comment is attached to date
date = "2019"
```
### Orphan
Orphan comments are any comments that don't fall into the above categories, they will be removed from the output document.
```toml
# Header comment
# Orphan comment, not attached to any item
# because there is whitespace before title
title = "The example"
# This comment is an orphan because it
# is separated from a-section by whitespace
# This comment is attached to a-section
[a-section]
```
## Example
The following example shows the input, and output, from the CLI with default options.
### Unformatted, unsorted input
```toml
# 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?
# Attached to b-section
[b-section]
date = "2018"
name = "Richard Stallman"
[[a-section.hello]]
ports = [ 80 ]
#Attached to dob
dob = 1920-05-27T07:32:00Z # Another date!
[a-section]
date = "2019"
name = "Samuel Roeca"
```
### Formatted, sorted output
```toml
# 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 ]
# Attached to dob
dob = 1920-05-27T07:32:00Z # Another date!
# Attached to b-section
[b-section]
date = "2018"
name = "Richard Stallman"
```
## Python API
TODO - need to generate and link to Python API documentation for this fork
## Local Development
Local development for this project is quite simple.
**Dependencies**
Install the following tools manually.
- [Poetry>=1.0](https://github.com/sdispater/poetry#installation)
- [GNU Make](https://www.gnu.org/software/make/)
_Recommended_
- [asdf](https://github.com/asdf-vm/asdf)
**Set up development environment**
```bash
make setup
```
**Run Tests**
```bash
make test
```
## Written by
Samuel Roeca, *samuel.roeca@gmail.com*
Raw data
{
"_id": null,
"home_page": "https://github.com/brodybits/pretty-toml-sort",
"name": "pretty-toml-sort",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "",
"keywords": "toml,sort,cli,unix,utility",
"author": "Sam Roeca",
"author_email": "samuel.roeca@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/49/69/3f3f52156f3bca625e5a42586387076678db3d68801e9c2a4a092b14fd63/pretty_toml_sort-0.0.3.tar.gz",
"platform": null,
"description": "# pretty-toml-sort\n\n[![pypi-version](https://img.shields.io/pypi/v/pretty-toml-sort.svg)](https://python.org/pypi/pretty-toml-sort)\n[![license](https://img.shields.io/pypi/l/pretty-toml-sort.svg)](https://python.org/pypi/pretty-toml-sort)\n[![image-python-versions](https://img.shields.io/badge/python->=3.7-blue)](https://python.org/pypi/jedi-language-server)\n[![image-pypi-downloads](https://pepy.tech/badge/pretty-toml-sort)](https://pepy.tech/project/pretty-toml-sort)\n[![readthedocs-status](https://readthedocs.org/projects/pretty-toml-sort/badge/?version=latest)](https://pretty-toml-sort.readthedocs.io/latest/)\n[![GitHub issues](https://img.shields.io/github/issues/brodybits/pretty-toml-sort)](https://github.com/brodybits/pretty-toml-sort/issues)\n\nAn updated fork of `toml-sort` - forked from: https://github.com/pappasam/toml-sort\n\nLICENSE: [MIT](./LICENSE) <!-- TODO: RESTORE BADGES WITH CORRECT INFO -->\n\nA command line utility to sort and format your toml files.\n\nwith improvement: add inner spacing to inline objects (non-empty) - https://github.com/pappasam/toml-sort/pull/68\n\n<!-- TODO: UPDATED DOCUMENTATION -->\n\n## Installation\n\n```bash\npip install pretty-toml-sort\n```\n\n## Motivation\n\nThis library sorts TOML files, providing the following features:\n\n- Sort tables and Arrays of Tables (AoT)\n- Option to sort non-tables / non-AoT's, or not\n- Preserve comments, where possible\n- Standardize whitespace and indentation\n\nI 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!\n\n## Command line usage\n\nThis 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:\n\n```console\n$ pretty-toml-sort --help\nusage: pretty-toml-sort [-h] [--version] [-o OUTPUT] [-i] [-I] [-a] [--no-sort-tables] [--sort-table-keys]\n [--sort-inline-tables] [--sort-inline-arrays] [--sort-first KEYS] [--no-header]\n [--no-comments] [--no-header-comments] [--no-footer-comments] [--no-inline-comments]\n [--no-block-comments] [--spaces-before-inline-comment {1,2,3,4}]\n [--spaces-indent-inline-array {2,4,6,8}] [--trailing-comma-inline-array] [--check]\n [F [F ...]]\n\nPretty TOML sort: a sorting utility for toml files (with some \"pretty\" formatting).\n\npositional arguments:\n F filename(s) to be processed by toml-sort (default: -)\n\noptional arguments:\n -h, --help show this help message and exit\n --version display version information and exit\n -o OUTPUT, --output OUTPUT\n output filepath (default: '-')\n -i, --in-place overwrite the original input file with changes\n --check silently check if an original file would be changed by the formatter\n\nsort:\n change sorting behavior\n\n -I, --ignore-case ignore case when sorting\n -a, --all sort ALL keys. This implies sort table-keys, inline-tables and inline arrays. (default: only\n sort non-inline 'tables and arrays of tables')\n --no-sort-tables Disables the default behavior of sorting tables and arrays of tables by their header value.\n Setting this option will keep the order of tables in the toml file the same.\n --sort-table-keys Sort the keys in tables and arrays of tables (excluding inline tables and arrays).\n --sort-inline-tables Sort inline tables.\n --sort-inline-arrays Sort inline arrays.\n --sort-first KEYS Table keys that will be sorted first in the output. Multiple keys can be given separated by a\n comma.\n\ncomments:\n exclude comments from output\n\n --no-header Deprecated. See --no-header-comments\n --no-comments remove all comments. Implies no header, footer, inline, or block comments\n --no-header-comments remove a document's leading comments\n --no-footer-comments remove a document's trailing comments\n --no-inline-comments remove a document's inline comments\n --no-block-comments remove a document's block comments\n\nformatting:\n options to change output formatting\n\n --spaces-before-inline-comment {1,2,3,4}\n the number of spaces before an inline comment (default: 1)\n --spaces-indent-inline-array {2,4,6,8}\n the number of spaces to indent a multiline inline array (default: 2)\n --trailing-comma-inline-array\n add trailing comma to the last item in a multiline inline array\n\nExamples:\n\n - **Stdin -> Stdout**: cat input.toml | toml-sort\n - **Disk -> Disk**: toml-sort -o output.toml input.toml\n - **Linting**: toml-sort --check input.toml input2.toml input3.toml\n - **Inplace Disk**: toml-sort --in-place input.toml input2.toml\n\nReturn codes:\n\n - 0 : success.\n - 1 : errors were found\n\nNotes:\n\n - You cannot redirect from a file to itself in Bash. POSIX shells process\n redirections first, then execute commands. --in-place exists for this\n reason\n```\n\n## Configuration file\n\ntoml-sort can also be configured by using the `pyproject.toml` file. If the file exists and has a `tool.tomlsort` section, the configuration is used. If both command line arguments and the configuration are used, the options are merged. In the case of conflicts, the command line option is used.\n\nIn short, the names are the same as on the command line (and have the same meaning), but `-` is replaced with `_`. Please note, that only the below options are supported:\n\n```toml\n[tool.tomlsort]\nall = true\nin_place = true\nno_comments = true\nno_header_comments = true\nno_footer_comments = true\nno_inline_comments = true\nno_block_comments = true\nno_sort_tables = true\nsort_first = [\"key1\", \"key2\"]\nsort_table_keys = true\nsort_inline_tables = true\nsort_inline_arrays = true\nspaces_before_inline_comment = 2\nspaces_indent_inline_array = 4\ntrailing_comma_inline_array = true\ncheck = true\nignore_case = true\n```\n\n### Configuration Overrides\nThe `pyproject.toml` configuration file also supports configuration overrides, which are not available as command-line arguments. These overrides allow for fine-grained control of sort options for particular keys.\n\nOnly the following options can be included in an override:\n\n```toml\n[tool.tomlsort.overrides.\"path.to.key\"]\nfirst = [\"key1\", \"key2\"]\ntable_keys = true\ninline_tables = true\ninline_arrays = true\n```\n\nIn the example configuration, `path.to.key` is the key to match. Keys are matched using the [Python fnmatch function](https://docs.python.org/3/library/fnmatch.html), so glob-style wildcards are supported. \n\nFor instance, to disable sorting the table in the following TOML file:\n\n```toml\n[servers.beta]\nip = \"10.0.0.2\"\ndc = \"eqdc10\"\ncountry = \"\u4e2d\u56fd\"\n```\n\nYou can use any of the following overrides:\n\n```toml\n# Overrides in own table\n[tool.tomlsort.overrides.\"servers.beta\"]\ntable_keys = false\n\n# Overrides in the tomlsort table\n[tool.tomlsort]\noverrides.\"servers.beta\".table_keys = false\n\n# Override using a wildcard if config should be applied to all servers keys\n[tool.tomlsort]\noverrides.\"servers.*\".table_keys = false\n```\n\n## Comments\n\nDue to the free form nature of comments, it is hard to include them in a sort in a generic way that will work for everyone. `toml-sort` deals with four different types of comments. They are all enabled by default, but can be disabled using CLI switches, in which case comments of that type will be removed from the output.\n\n### Header\n\nThe first comments in a document, that are followed by a blank line, are treated as a header, and will always remain at the top of the document. If there is no blank line, the comment will be treated as a block comment instead.\n\n```toml\n# This is a header\n# it can be multiple lines, as long as it is followed with a blank line\n# it will always stay at the top of the sorted document\n\ntitle = \"The example\"\n```\n\n### Footer\n\nAny comments at the end of the document, after the last item in the toml, will be treated as a footer, and will always remain at the bottom of the document.\n\n```toml\ntitle = \"The example\"\n\n# this is a footer comment\n```\n\n### Inline\n\nInline comments are comments that are at the end of a line where the start of the line is a toml item.\n\n```toml\ntitle = \"The example\" # This is a inline comment\n```\n\n### Block\n\nBlock comments, are any comments that are on their own line. These comments are treated as _attached_ to the item in the toml that is directly below them, not separated by whitespace. These comments can be multiple lines. Inline comments will appear in the sorted output above the item they were attached to in the input toml.\n\n```toml\n# Comment attached to title\ntitle = \"The example\"\n\n# This comment is an orphan because it\n# is separated from a-section by whitespace\n\n# This comment is attached to a-section\n# attached comments can be multiple lines\n[a-section]\n# This comment is attached to date\ndate = \"2019\"\n```\n\n### Orphan\n\nOrphan comments are any comments that don't fall into the above categories, they will be removed from the output document.\n\n```toml\n# Header comment\n\n# Orphan comment, not attached to any item\n# because there is whitespace before title\n\ntitle = \"The example\"\n\n# This comment is an orphan because it\n# is separated from a-section by whitespace\n\n# This comment is attached to a-section\n[a-section]\n```\n\n## Example\n\nThe following example shows the input, and output, from the CLI with default options.\n\n### Unformatted, unsorted input\n\n```toml\n# My great TOML example\n\n title = \"The example\"\n\n[[a-section.hello]]\nports = [ 8001, 8001, 8002 ]\ndob = 1979-05-27T07:32:00Z # First class dates? Why not?\n\n\n # Attached to b-section\n [b-section]\n date = \"2018\"\n name = \"Richard Stallman\"\n\n[[a-section.hello]]\nports = [ 80 ]\n #Attached to dob\ndob = 1920-05-27T07:32:00Z # Another date!\n\n [a-section]\n date = \"2019\"\n name = \"Samuel Roeca\"\n```\n\n### Formatted, sorted output\n\n```toml\n# My great TOML example\n\ntitle = \"The example\"\n\n[a-section]\ndate = \"2019\"\nname = \"Samuel Roeca\"\n\n[[a-section.hello]]\nports = [ 8001, 8001, 8002 ]\ndob = 1979-05-27T07:32:00Z # First class dates? Why not?\n\n[[a-section.hello]]\nports = [ 80 ]\n# Attached to dob\ndob = 1920-05-27T07:32:00Z # Another date!\n\n# Attached to b-section\n[b-section]\ndate = \"2018\"\nname = \"Richard Stallman\"\n```\n\n## Python API\n\nTODO - need to generate and link to Python API documentation for this fork\n\n## Local Development\n\nLocal development for this project is quite simple.\n\n**Dependencies**\n\nInstall the following tools manually.\n\n- [Poetry>=1.0](https://github.com/sdispater/poetry#installation)\n- [GNU Make](https://www.gnu.org/software/make/)\n\n_Recommended_\n\n- [asdf](https://github.com/asdf-vm/asdf)\n\n**Set up development environment**\n\n```bash\nmake setup\n```\n\n**Run Tests**\n\n```bash\nmake test\n```\n\n## Written by\n\nSamuel Roeca, *samuel.roeca@gmail.com*\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Toml sorting library",
"version": "0.0.3",
"project_urls": {
"Homepage": "https://github.com/brodybits/pretty-toml-sort",
"Repository": "https://github.com/brodybits/pretty-toml-sort"
},
"split_keywords": [
"toml",
"sort",
"cli",
"unix",
"utility"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9ce23db8d02555cf753c2c007adb53389ed79779d5999a81a229e4193af358ad",
"md5": "954debce44b6a9067b989e3be055ed26",
"sha256": "fb8547ce39922c48820b2e29ecff06f5076800f89a4f7efaef8a73d98ea49c9e"
},
"downloads": -1,
"filename": "pretty_toml_sort-0.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "954debce44b6a9067b989e3be055ed26",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 21383,
"upload_time": "2024-01-21T13:43:08",
"upload_time_iso_8601": "2024-01-21T13:43:08.182396Z",
"url": "https://files.pythonhosted.org/packages/9c/e2/3db8d02555cf753c2c007adb53389ed79779d5999a81a229e4193af358ad/pretty_toml_sort-0.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "49693f3f52156f3bca625e5a42586387076678db3d68801e9c2a4a092b14fd63",
"md5": "8c85810779f2880da16681f3e2b31d10",
"sha256": "824d14c23f429c4d3432d6b30b6a793c5169099c8f6caebdf3429a48d2716afd"
},
"downloads": -1,
"filename": "pretty_toml_sort-0.0.3.tar.gz",
"has_sig": false,
"md5_digest": "8c85810779f2880da16681f3e2b31d10",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 19102,
"upload_time": "2024-01-21T13:43:10",
"upload_time_iso_8601": "2024-01-21T13:43:10.970341Z",
"url": "https://files.pythonhosted.org/packages/49/69/3f3f52156f3bca625e5a42586387076678db3d68801e9c2a4a092b14fd63/pretty_toml_sort-0.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-21 13:43:10",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "brodybits",
"github_project": "pretty-toml-sort",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pretty-toml-sort"
}