Modules

This page documents importable modules and their contents.

toml_sort.tomlsort

Utility functions and classes to sort toml text.

class toml_sort.tomlsort.TomlSort(input_toml: str, comment_config: Optional[toml_sort.tomlsort.CommentConfiguration] = None, sort_config: Optional[toml_sort.tomlsort.SortConfiguration] = None, format_config: Optional[toml_sort.tomlsort.FormattingConfiguration] = None)

API to manage sorting toml files.

aot_to_tomlsortitem(comments: List[tomlkit.items.Comment], key: tomlkit.items.Key, value: tomlkit.items.AoT) Tuple[List[tomlkit.items.Comment], toml_sort.tomlsort.TomlSortItem]

Turn an AoT into a TomlSortItem, recursing down through its collections and attaching all the comments to the correct items.

array_sort_func(value: Tuple[tomlkit.items._ArrayItemGroup, Any]) str

Sort function that operates on the .value member of an ArrayItemGroup respects the class setting for ignore_case.

body_to_tomlsortitems(parent: List[Tuple[Optional[tomlkit.items.Key], tomlkit.items.Item]]) Tuple[List[toml_sort.tomlsort.TomlSortItem], List[tomlkit.items.Comment]]

Iterate over Container.body, recursing down into sub-containers attaching the comments that are found to the correct TomlSortItem. We need to do this iteration because TomlKit puts comments into end of the collection they appear in, instead of the start of the next collection.

For example: [xyz]

# Comment [abc]

TomlKit would place the comment from the example into the [xyz] collection, when we would like it to be attached to the [abc] collection.

So before sorting we have to iterated over the container, correctly attaching the comments, then undo this process once everything is sorted.

static format_key(key: tomlkit.items.Key) tomlkit.items.Key

Format a key, removing any extra whitespace, and making sure that it will be formatted like: key = value with one space on either side of the equal sign.

key_sort_func(value: toml_sort.tomlsort.TomlSortItem) str

Sort function that looks at TomlSortItems keys, respecting the configured value for ignore_case.

sort_array(array: tomlkit.items.Array, indent_depth: int = 0) tomlkit.items.Array

Sort and format an inline array item while preserving comments.

sort_inline_table(item, indent_depth: int = 0)

Sort an inline table, recursing into its items.

sort_item(item: tomlkit.items.Item, indent_depth: int = 0) tomlkit.items.Item

Sort an item, recursing down if the item is an inline table or array.

sort_items(items: Iterable[toml_sort.tomlsort.TomlSortItem]) Iterable[toml_sort.tomlsort.TomlSortItem]

Sort an iterable full of TomlSortItem, making sure the key is correctly formatted and recursing into any sub-items.

sorted() str

Sort a TOML string.

sorted_children_table(parent: List[toml_sort.tomlsort.TomlSortItem]) Iterable[toml_sort.tomlsort.TomlSortItem]

Get the sorted children of a table.

static table_previous_item(parent_table, grandparent)

Finds the previous item that we should attach a comment to, in the case where the previous item is a table.

This take into account that a table may be a super table.

table_to_tomlsortitem(comments: List[tomlkit.items.Comment], key: tomlkit.items.Key, value: tomlkit.items.Table) Tuple[List[tomlkit.items.Comment], toml_sort.tomlsort.TomlSortItem]

Turn a table into a TomlSortItem, recursing down through its collections and attaching all the comments to the correct items.

toml_doc_sorted(original: tomlkit.toml_document.TOMLDocument) tomlkit.toml_document.TOMLDocument

Sort a TOMLDocument.

toml_elements_sorted(original: TomlSortItem, parent: Table | TOMLDocument) Item

Returns a sorted item, recursing collections to their base.

write_header_comment(from_doc_body: List[Tuple[Optional[tomlkit.items.Key], tomlkit.items.Item]], to_doc: tomlkit.toml_document.TOMLDocument) None

Write header comment from the FROM doc to the TO doc.

Only writes comments / whitespace from the beginning of a TOML document.