graphtage.ini
A graphtage.Filetype for parsing, diffing, and rendering INI files.
The parser is implemented atop configparser. Option names are compared case-sensitively, which differs from
configparser’s default of lowercasing them, so that a diff reflects the file as it was written. Interpolation is
disabled for the same reason, and [DEFAULT] is treated as an ordinary section rather than supplying values to every
other section. Comments are not represented in the tree, so they are absent from the output.
When another format is rendered as INI, a top-level value that is not itself a mapping has no section to live under.
It is written without a section header so that the diff is still visible, which means the output is not necessarily a
file that configparser can read back.
ini classes
INI
- class graphtage.ini.INI
Bases:
FiletypeThe INI filetype.
- __init__()
Initializes the INI filetype.
- 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() INIFormatter
Returns the default formatter for printing files of this type.
INIFormatter
- class graphtage.ini.INIFormatter(*args, **kwargs)
Bases:
GraphtageFormatterA formatter for INI files.
- DEFAULT_INSTANCE: Formatter[T] = <graphtage.ini.INIFormatter 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.
- sub_format_types: Sequence[Type[Formatter[T]]] = [<class 'graphtage.ini.INIMappingFormatter'>, <class 'graphtage.ini.INIListFormatter'>, <class 'graphtage.ini.INIStringFormatter'>]
A list of formatter types that should be used as sub-formatters in the Formatting Protocol.
INIListFormatter
- class graphtage.ini.INIListFormatter(*args, **kwargs)
Bases:
SequenceFormatterA formatter for sequences.
INI has no syntax for a list, so one is written as a comma-separated value. This only arises when rendering another format as INI; reading the result back yields a single string rather than a list.
- DEFAULT_INSTANCE: Formatter[T] = <graphtage.ini.INIListFormatter object>
A default instance of this formatter, automatically instantiated by the
FormatterCheckermetaclass.
- __init__()
Initializes a sequence formatter.
- Parameters:
start_symbol – The symbol to print at the start of the sequence.
end_symbol – The symbol to print at the end of the sequence.
delimiter – A delimiter to print between items.
delimiter_callback –
A callback for when a delimiter is to be printed. If omitted, this defaults to:
lambda p: p.write(delimiter)
- 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)
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)
- print_SequenceNode(*args, **kwargs)
Formats a sequence node.
The protocol for this function is as follows:
Print
self.start_symbol- With the printer returned by
self.items_indent: - For each
editin the sequence (or just a sequence ofgraphtage.Matchfor each child, if the node is not edited): Call
self.item_newline(printer, is_first=index == 0)Call
self.edit_print(printer, edit)
- For each
- With the printer returned by
If at least one edit was printed, then call
self.item_newline(printer, is_last=True)Print
self.start_symbol
- print_UnorderedListNode(*args, **kwargs)
- sub_format_types: Sequence[Type[Formatter[T]]] = ()
A list of formatter types that should be used as sub-formatters in the Formatting Protocol.
INIMappingFormatter
- class graphtage.ini.INIMappingFormatter(*args, **kwargs)
Bases:
SequenceFormatterA formatter for an INI document and for each of its section bodies.
- DEFAULT_INSTANCE: Formatter[T] = <graphtage.ini.INIMappingFormatter object>
A default instance of this formatter, automatically instantiated by the
FormatterCheckermetaclass.
- __init__()
Initializes a sequence formatter.
- Parameters:
start_symbol – The symbol to print at the start of the sequence.
end_symbol – The symbol to print at the end of the sequence.
delimiter – A delimiter to print between items.
delimiter_callback –
A callback for when a delimiter is to be printed. If omitted, this defaults to:
lambda p: p.write(delimiter)
- 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)
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_MappingNode(*args, **kwargs)
- print_MultiSetNode(*args, **kwargs)
- print_SequenceNode(*args, **kwargs)
Formats a sequence node.
The protocol for this function is as follows:
Print
self.start_symbol- With the printer returned by
self.items_indent: - For each
editin the sequence (or just a sequence ofgraphtage.Matchfor each child, if the node is not edited): Call
self.item_newline(printer, is_first=index == 0)Call
self.edit_print(printer, edit)
- For each
- With the printer returned by
If at least one edit was printed, then call
self.item_newline(printer, is_last=True)Print
self.start_symbol
- sub_format_types: Sequence[Type[Formatter[T]]] = [<class 'graphtage.ini.INIOptionFormatter'>]
A list of formatter types that should be used as sub-formatters in the Formatting Protocol.
INIOptionFormatter
- class graphtage.ini.INIOptionFormatter(*args, **kwargs)
Bases:
GraphtageFormatterA formatter for a single INI key/value pair, or for a section header and its body.
- DEFAULT_INSTANCE: Formatter[T] = <graphtage.ini.INIOptionFormatter 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, 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_KeyValuePairNode(printer: Printer, node: KeyValuePairNode)
- sub_format_types: Sequence[Type[Formatter[T]]] = ()
A list of formatter types that should be used as sub-formatters in the Formatting Protocol.
INIStringFormatter
- class graphtage.ini.INIStringFormatter(*args, **kwargs)
Bases:
StringFormatterA string formatter for INI keys and values.
- DEFAULT_INSTANCE: Formatter[T] = <graphtage.ini.INIStringFormatter 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
ini functions
build_tree
- graphtage.ini.build_tree(path: str, options: BuildOptions | None = None) TreeNode