graphtage.toml
toml classes
TOML
- class graphtage.toml.TOML
Bases:
FiletypeThe TOML filetype.
- __init__()
Initializes the TOML filetype.
TOML identifies itself with the MIME types application/toml and text/toml.
- build_tree(path: str, options: BuildOptions | None = None) TreeNode
Equivalent to
build_tree()
- build_tree_handling_errors(path: str, options: BuildOptions | None = None) str | TreeNode
Same as
Filetype.build_tree(), but it should return a human-readable error string on failure.This function should never throw an exception.
- get_default_formatter() JSONFormatter
Returns the default formatter for printing files of this type.
TOMLFormatter
- class graphtage.toml.TOMLFormatter(*args, **kwargs)
Bases:
GraphtageFormatter- DEFAULT_INSTANCE: Formatter[T] = <graphtage.toml.TOMLFormatter object>
A default instance of this formatter, automatically instantiated by the
FormatterCheckermetaclass.
- __init__()
- static __new__(cls, *args, **kwargs) Formatter[T]
Instantiates a new formatter.
This automatically instantiates and populates
Formatter.sub_formattersand sets theirparentto this new formatter.
- get_formatter(item: T) Callable[[Printer, T], Any] | None
Looks up a formatter for the given item using this formatter as a base.
Equivalent to:
get_formatter(item.__class__, base_formatter=self)
- parent: Formatter[T] | None = None
The parent formatter for this formatter instance.
This is automatically populated by
Formatter.__new__()and should never be manually modified.
- print(printer: Printer, *args, **kwargs)
Prints the given node or edit.
- Parameters:
printer – The printer to which to write.
node_or_edit – The node or edit to print.
with_edits – If :keyword:True, print any edits associated with the node.
Note
The protocol for determining how a node or edit should be printed is very complex due to its extensibility. See the Printing Protocol for a detailed description.
- print_KeyValuePairNode(printer: Printer, node: KeyValuePairNode)
- print_MappingNode(printer: Printer, node: MappingNode)
- sub_format_types: Sequence[Type[Formatter[T]]] = [<class 'graphtage.toml.TOMLListFormatter'>, <class 'graphtage.toml.TOMLStringFormatter'>]
A list of formatter types that should be used as sub-formatters in the Formatting Protocol.
TOMLListFormatter
- class graphtage.toml.TOMLListFormatter(*args, **kwargs)
Bases:
SequenceFormatterA sub-formatter for TOML lists.
- DEFAULT_INSTANCE: Formatter[T] = <graphtage.toml.TOMLListFormatter object>
A default instance of this formatter, automatically instantiated by the
FormatterCheckermetaclass.
- __init__()
Initializes the TOML list formatter.
Equivalent to:
super().__init__('[', ']', ',')
- static __new__(cls, *args, **kwargs) Formatter[T]
Instantiates a new formatter.
This automatically instantiates and populates
Formatter.sub_formattersand sets theirparentto this new formatter.
- edit_print(printer: Printer, edit: Edit)
Called when the edit for an item is to be printed.
If the
SequenceNodebeing printed either is not edited or has no edits, then the edit passed to this function will be aMatch(child, child, 0).This implementation simply delegates the print to the Formatting Protocol:
self.print(printer, edit)
- get_formatter(item: T) Callable[[Printer, T], Any] | None
Looks up a formatter for the given item using this formatter as a base.
Equivalent to:
get_formatter(item.__class__, base_formatter=self)
- is_partial: bool = True
This is a partial formatter; it will not be automatically used in the Formatting Protocol.
- item_newline(printer: Printer, is_first: bool = False, is_last: bool = False)
Called before each node is printed.
This is also called one extra time after the last node, if there is at least one node printed.
The default implementation is simply:
printer.newline()
- items_indent(printer: Printer) Printer
Returns a Printer context with an indentation.
This is called as:
with self.items_indent(printer) as p:
immediately after the
self.start_symbolis printed, but before any of the items have been printed.This default implementation is equivalent to:
return printer.indent()
- parent: Formatter[T] | None = None
The parent formatter for this formatter instance.
This is automatically populated by
Formatter.__new__()and should never be manually modified.
- print(printer: Printer, node_or_edit: TreeNode | Edit, with_edits: bool = True)
Prints the given node or edit.
- Parameters:
printer – The printer to which to write.
node_or_edit – The node or edit to print.
with_edits – If :keyword:True, print any edits associated with the node.
Note
The protocol for determining how a node or edit should be printed is very complex due to its extensibility. See the Printing Protocol for a detailed description.
- print_ListNode(*args, **kwargs)
Prints a
graphtage.ListNode.Equivalent to:
super().print_SequenceNode(*args, **kwargs)
- print_SequenceNode(*args, **kwargs)
Prints a non-List sequence.
This delegates to the parent formatter’s implementation:
self.parent.print(*args, **kwargs)
which should invoke
TOMLFormatter.print(), thereby delegating to theTOMLDictFormatterin instances where a list contains a dict (the TOML format doesn’t allow this, but it might be necessary if formatting from another format into TOML)
- sub_format_types: Sequence[Type[Formatter[T]]] = ()
A list of formatter types that should be used as sub-formatters in the Formatting Protocol.
TOMLMapping
- class graphtage.toml.TOMLMapping(mapping: MappingNode, parent: TOMLMapping | None = None, parent_name: TreeNode | None = None)
Bases:
object- __init__(mapping: MappingNode, parent: TOMLMapping | None = None, parent_name: TreeNode | None = None)
- children() Iterator[TOMLMapping]
- items() Iterator[KeyValuePairNode]
TOMLStringFormatter
- class graphtage.toml.TOMLStringFormatter(*args, **kwargs)
Bases:
StringFormatterA TOML formatter for strings.
- DEFAULT_INSTANCE: Formatter[T] = <graphtage.toml.TOMLStringFormatter object>
A default instance of this formatter, automatically instantiated by the
FormatterCheckermetaclass.
- __init__()
- static __new__(cls, *args, **kwargs) Formatter[T]
Instantiates a new formatter.
This automatically instantiates and populates
Formatter.sub_formattersand sets theirparentto this new formatter.
- escape(c: str) str
String escape.
This function is called once for each character in the string.
- Returns:
The escaped version of c, or c itself if no escaping is required.
- Return type:
- get_formatter(item: T) Callable[[Printer, T], Any] | None
Looks up a formatter for the given item using this formatter as a base.
Equivalent to:
get_formatter(item.__class__, base_formatter=self)
- parent: 'Formatter[T]' | None = None
The parent formatter for this formatter instance.
This is automatically populated by
Formatter.__new__()and should never be manually modified.
- print(printer: Printer, node_or_edit: TreeNode | Edit, with_edits: bool = True)
Prints the given node or edit.
- Parameters:
printer – The printer to which to write.
node_or_edit – The node or edit to print.
with_edits – If :keyword:True, print any edits associated with the node.
Note
The protocol for determining how a node or edit should be printed is very complex due to its extensibility. See the Printing Protocol for a detailed description.
- print_StringEdit(printer: Printer, edit: StringEdit)
- print_StringNode(printer: Printer, node: StringNode)
- sub_format_types: Sequence[Type['Formatter[T]']] = ()
A list of formatter types that should be used as sub-formatters in the Formatting Protocol.
- sub_formatters: List['Formatter[T]'] = []
The list of instantiated formatters corresponding to
Formatter.sub_format_types.This list is automatically populated by
Formatter.__new__()and should never be manually modified.
- write_char(printer: Printer, c: str | int, index: int, num_edits: int, removed=False, inserted=False)
Writes a character to the printer.
Note
This function calls
graphtage.StringFormatter.escape(); classes extendinggraphtage.StringFormattershould also callgraphtage.StringFormatter.escape()when reimplementing this function.Note
There is no need to specially format characters that have been removed or inserted; the printer will have already automatically been configured to format them prior to the call to
StringFormatter.write_char().- Parameters:
printer – The printer to which to write the character.
c – The character to write.
index – The index of the character in the string.
num_edits – The total number of characters that will be printed.
removed – Whether this character was removed from the source string.
inserted – Whether this character is inserted into the source string.
- write_end_quote(printer: Printer, edit: StringEdit)
Prints an ending quote for the string, if necessary
- write_start_quote(printer: Printer, edit: StringEdit)
Prints a starting quote for the string, if necessary
toml functions
build_tree
- graphtage.toml.build_tree(path: str, options: BuildOptions | None) TreeNode