API Reference

This section contains the API reference for the pudding package.

Core Module

The pudding module.

pudding.convert_file(syntax_file, input_file, output_file, output_format, encoding='utf-8')[source]

Convert a single file.

Parameters:
  • syntax_file (Path) – Path of the “.pud” file.

  • input_file (Path) – Path of the file to convert.

  • output_file (Path) – Path of the file to write to.

  • output_format (str) – Format of the output.

  • encoding (str) – Encoding of the input and output file.

Return type:

None

pudding.convert_files(syntax_file, input_files, output_files, output_format, encoding='utf-8')[source]

Convert multiple files.

Parameters:
  • syntax_file (Path) – Path of the “.pud” file.

  • input_files (list[Path]) – List of file paths to convert.

  • output_file – List of paths to write to, where the index corresponds to index of the input filepaths. E.g. the converted output of the first file in input_files will be written to the first path in this list.

  • output_format (str) – Format of the output.

  • encoding (str) – Encoding of the input and output files.

  • output_files (list[Path])

Return type:

None

pudding.convert_string(syntax, content, output_format)[source]

Convert a string.

Parameters:
  • syntax (str) – Content of a “.pud” file.

  • content (str) – String to convert.

  • output_format (str) – Format of the output.

Return type:

str

Compiler Module

Package for compiling pudding syntax into executable tokens.

class pudding.compiler.Compiler(tokens=None)[source]

Base Compiler class.

Parameters:

tokens (Sequence[type[BaseToken]] | None)

compile(content)[source]

Produce executable syntax object from syntax file.

Parameters:

content (str) – Content of the syntax file.

Returns:

Tuple with syntax and last line number.

Return type:

Syntax

compile_file(file, encoding='utf-8')[source]

Produce executable syntax object from pud file.

Parameters:
  • file (Path) – Path of the syntax file.

  • encoding (str)

Returns:

Tuple with syntax and last line number.

Return type:

Syntax

Module defining the parser class reading the pud file.

class pudding.compiler.compiler.Compiler(tokens=None)[source]

Base Compiler class.

Parameters:

tokens (Sequence[type[BaseToken]] | None)

compile(content)[source]

Produce executable syntax object from syntax file.

Parameters:

content (str) – Content of the syntax file.

Returns:

Tuple with syntax and last line number.

Return type:

Syntax

compile_file(file, encoding='utf-8')[source]

Produce executable syntax object from pud file.

Parameters:
  • file (Path) – Path of the syntax file.

  • encoding (str)

Returns:

Tuple with syntax and last line number.

Return type:

Syntax

Datatypes Module

Package containing datatypes used in the pudding syntax.

class pudding.datatypes.Data(line, value)[source]

Class representing a data value.

Variables:

regex – Regex matching the data type as a string.

Parameters:
  • line (int)

  • value (str)

class pudding.datatypes.Or(line, value)[source]

Class representing the character |.

Parameters:
  • line (int)

  • value (str)

class pudding.datatypes.Regex(line, value)[source]

Class representing a regular expression.

Parameters:
  • line (int)

  • value (str)

class pudding.datatypes.String(line, value)[source]

Class representing a string value.

Parameters:
  • line (int)

  • value (str)

class pudding.datatypes.Varname(line, value)[source]

Class representing a variable name.

Parameters:
  • line (int)

  • value (str)

pudding.datatypes.string_to_datatype(string, line)[source]

Convert a given string to a DataType.

Parameters:
  • string (str) – Value to convert.

  • line (int) – Line number of this value.

Returns:

The given value as a DataType.

Raises:

TypeError – If there is no matching DataType.

Return type:

Data

Processor Module

Package for processing tokens of the compiled syntax.

class pudding.processor.PAction(*values)[source]

Processing action.

Module defining context class.

class pudding.processor.context.Context(reader, writer)[source]

Class containing context for the processor.

Variables:
  • grammars – Grammars defined in the syntax.

  • queue – Queue for triggers created by enqueued statements.

  • variables – Variables defined in the syntax.

Parameters:
get_grammar(name)[source]

Get a grammar by name.

Parameters:

name (str) – Name of the grammar to retrieve.

Raises:

SyntaxError – If grammar is not defined.

Return type:

Grammar

get_var(varname)[source]

Get a variable by name.

Parameters:
  • name – Name of the variable to retrieve.

  • varname (Varname)

Returns str:

Defined regex pattern as a string.

Raises:

NameError – If variable is not defined.

Return type:

str

replace_string_vars(string)[source]

Replace variables in a string with the last matched values.

Parameters:
  • string (String) – String to replace vars in.

  • context – The current context.

Returns:

The string with replaced values.

Return type:

str

Module for Grammar class.

class pudding.processor.grammar.Grammar(lineno, name, tokens, inherits=None)[source]

Class representing a grammar.

Variables:
  • lineno – Line number the grammar is defined in.

  • name – Name of the grammar.

  • tokens – Tokens in this grammar.

  • inherits – Name of the inherited grammar or None.

Parameters:
  • lineno (int)

  • name (str)

  • tokens (TokenList)

  • inherits (str | None)

Module defining processor class.

class pudding.processor.processor.Processor(context, syntax)[source]

Class processing tokens.

Parameters:
  • context (Context)

  • syntax (Syntax)

property reader: Reader

Alias for the contexts reader.

property writer: Writer

Alias for the contexts writer.

convert()[source]

Transform the content according to the syntax.

Returns:

The writer object with the transformed data.

Raises:

RuntimeError – If no match was found.

Return type:

Writer

execute_grammar(name)[source]

Execute a grammar by name.

Execute tokens of a grammar with the given name. If a inherited grammar exists, it will be executed first.

Parameters:

name (str) – Name of the grammar.

Returns:

PAction.RESTART if grammar restarted at least once else PAction.CONTINUE.

Return type:

PAction

execute_condition(token)[source]

Execute a condition.

The condition token will return PAction.ENTER if sub tokens should be executed. Otherwise the condition is not met and sub tokens not executed. If all sub tokens have been executed, restart the grammar unless another PAction than CONTINUE is requested.

Parameters:

token (tuple[BaseToken, TokenList]) – Tuple with condition and tokens to execute.

Returns:

ProcessingAction.

Return type:

PAction

execute_token(token)[source]

Execute a token.

Execute the token and trigger Timings before and after the execution.

Parameters:

token (BaseToken) – Token to execute.

Returns:

PAction of the executed token.

Return type:

PAction

trigger(timing)[source]

Test triggers of a timing.

Parameters:

timing (Timing) – The timing to trigger.

Return type:

None

Module defining Trigger and TriggerQueue class.

class pudding.processor.triggers.Timing(*values)
class pudding.processor.triggers.Trigger(match, token)[source]

Base trigger class.

Parameters:
  • match (Pattern[str])

  • token (Token)

class pudding.processor.triggers.TriggerQueue[source]

Queue for triggers.

Variables:

triggers – Dictionary where the key is a timing and the value a list of triggers.

add_trigger(timing, trigger)[source]

Add a trigger to the queue.

Parameters:
  • timing (Timing) – Timing of the queue.

  • trigger (Trigger) – Trigger to add.

Return type:

None

clear_triggers(timing=None)[source]

Clear a trigger queue.

Parameters:

timing (Timing | None) – Timing of a queue to clear or none to clear all.

Return type:

None

Reader Module

Package for reading the input file.

class pudding.reader.Reader(content)[source]

Base Reader class.

Variables:
  • current_pos – Current position in content.

  • last_match – Last Match object or None if regex did not match.

Parameters:

content (str)

property eof: bool

Boolean if end of content has been reached.

property current_line_number: int

Line number of the current position.

find(regex)[source]

Try matching a regex in the content ahead.

Parameters:

regex (Pattern[str]) – The pattern to match.

Returns:

The match object or None if it did not match.

Return type:

Match[str] | None

match(regex)[source]

Try matching a regex to the content ahead and advance.

If the pattern matches the current_pos is advanced by the length of the match.

Parameters:

regex (Pattern[str]) – The pattern to match.

Returns:

The match object or None if it did not match.

Return type:

Match[str] | None

would_match(regex)[source]

Test if a pattern would match.

Test if a pattern would match the content ahead without advancing the current position or changing the last_match attribute of the reader.

Parameters:
  • trigger – The trigger object.

  • regex (Pattern[str])

Returns:

Boolean if trigger matches.

Return type:

bool

Module defining Reader class.

class pudding.reader.reader.Reader(content)[source]

Base Reader class.

Variables:
  • current_pos – Current position in content.

  • last_match – Last Match object or None if regex did not match.

Parameters:

content (str)

property eof: bool

Boolean if end of content has been reached.

property current_line_number: int

Line number of the current position.

find(regex)[source]

Try matching a regex in the content ahead.

Parameters:

regex (Pattern[str]) – The pattern to match.

Returns:

The match object or None if it did not match.

Return type:

Match[str] | None

match(regex)[source]

Try matching a regex to the content ahead and advance.

If the pattern matches the current_pos is advanced by the length of the match.

Parameters:

regex (Pattern[str]) – The pattern to match.

Returns:

The match object or None if it did not match.

Return type:

Match[str] | None

would_match(regex)[source]

Test if a pattern would match.

Test if a pattern would match the content ahead without advancing the current position or changing the last_match attribute of the reader.

Parameters:
  • trigger – The trigger object.

  • regex (Pattern[str])

Returns:

Boolean if trigger matches.

Return type:

bool

Tokens Module

Package containing tokens defining the actions of functions and statements.

Module defining executable class.

class pudding.tokens.token.BaseToken(lineno, name, values)[source]

Base class for tokens.

Variables:
  • match_re – Regex with two groups matching the token name and values in a line.

  • value_delim_re – Regex matching the delimiter between values in the string matched by the match_re in group two.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

classmethod from_string(string, lineno)[source]

Create Token object from string.

Parameters:
  • string (str) – String containing the token.

  • lineno (int) – Line number of the token.

Return type:

Self

classmethod matches(string)[source]

Return bool if statement exists in the given string.

Parameters:

string (str) – String to search in.

Returns:

True if it exists.

Return type:

bool

execute(context)[source]

Execute this token.

Parameters:

context (Any) – Context object.

Returns:

PAction for processor class.

Return type:

PAction | NoReturn

get_value(index, default=None)[source]

Get a value.

Parameters:
  • index (int) – Index of the value in values tuple.

  • default (_D | None) – Default value.

Returns:

The value at index or the default value if index is invalid.

Return type:

_D | None | Data

get_string(index)[source]

Get String object in values.

Parameters:

index (int) – Index of the object in values.

Returns:

The String object at the given index.

Raises:

TypeError – If object at given index is not of type String.

Return type:

String

class pudding.tokens.token.Token(lineno, name, values)[source]

Token with a known number of values.

Variables:

value_types – Data types defining the type of the user set values.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

class pudding.tokens.token.MultiExpToken(lineno, name, values)[source]

Token with an unknown amount of values.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

Functions

Package containing tokens of functions.

Module defining functions.

class pudding.tokens.functions.function.Function(lineno, name, values)[source]

Base class for a function.

Variables:
  • min_args – Minimum amount of arguments.

  • max_args – Maximum amount of arguments.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

Grammar call.

class pudding.tokens.functions.grammar_call.GrammarCall(lineno, name, values)[source]

Class for a grammar call.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

Out Functions

Package containing tokens for output generating functions.

class pudding.tokens.functions.out.AddAttribute(lineno, name, values)[source]

Class for out.add_attribute function.

Adds the attribute with the given name and value to the node at the given path.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Add an attribute.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.functions.out.Add(lineno, name, values)[source]

Class for out.add function.

Appends the string value to the text of the existing node if it already exists. Otherwise it creates a new node.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Add an element.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.functions.out.ClearQueue(lineno, name, values)[source]

Class for out.clear_queue function.

Removes any items from the queue that were previously queued using the out.enqueue_*() functions.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Clear enqueued tokens.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.functions.out.Create(lineno, name, values)[source]

Class for out.create function.

Creates the leaf node (and attributes) in the given path, regardless of whether or not it already exists. In other words, using this function twice will lead to duplicates. If the given path contains multiple elements, the parent nodes are only created if they do not yet exist. If the second argument is given, the new node is also assigned the string as data.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Create an element always.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.functions.out.EnqueueAfter(lineno, name, values)[source]

Class for out.enqueue_after function.

Like out.add(), but is executed after the given regular expression matches the input and the next token was processed.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Enqueue token after execution.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.functions.out.EnqueueBefore(lineno, name, values)[source]

Class for out.enqueue_before function.

Like out.add(), but is executed as soon as the given regular expression matches the input, regardless of the grammar in which the match occurs.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Enqueue token before execution.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.functions.out.EnqueueOnAdd(lineno, name, values)[source]

Class for out.enqueue_on_add function.

Like out.add(), but is executed after the given regular expression matches the input and the next node is added to the output.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Enqueue token on execution of out.add().

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.functions.out.Enter(lineno, name, values)[source]

Class for out.enter function.

Creates the nodes in the given path if they do not already exist and selects the last node. Therefore the PATH of all subsequent function calls is relative to the selected node until the end of the match block is reached.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Enter a node and create it if it does not exist.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.functions.out.Open(lineno, name, values)[source]

Class for out.open function.

Like out.create(), but also selects the addressed node, such that the PATH of all subsequent function calls is relative to the selected node until the end of the match block is reached.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Create and enter a node.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.functions.out.Remove(lineno, name, values)[source]

Class for out.remove function.

Deletes the last node in the given path.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Delete an element.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.functions.out.Replace(lineno, name, values)[source]

Class for out.replace function.

Replaces the text of the last node in the given path.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Replace text of an element.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.functions.out.Say(lineno, name, values)[source]

Class for say function.

Prints the given string to stdout.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Print to stdout.

Parameters:

context (Context) – Current context object.

Returns:

Returns PAction.CONTINUE for processor class.

Return type:

PAction

class pudding.tokens.functions.out.SetRootName(lineno, name, values)[source]

Class for out.set_root_name function.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Set tag of root element.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

Statements

Package containing tokens for statements.

class pudding.tokens.statements.Define(lineno, name, values)[source]

Class for define statement.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

get_value_patterns(context)[source]

Return the combined patterns as a string.

Parameters:
  • context (Context) – Context to resolve variables.

  • re_flag – Regex flag when compiling expression.

Returns:

List of regex patterns, where each element is a possible pattern.

Return type:

str

execute(context)[source]

Set a variable.

Parameters:

context (Context) – Current context object.

Returns:

PAction.CONTINUE

Return type:

PAction

class pudding.tokens.statements.Fail(lineno, name, values)[source]

Class for fail statement.

Takes exactly one argument with a string printed to stdout on execution.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Immediately terminate parsing with an error.

Parameters:

context (Context) – Current context object.

Raises:

RuntimeError – Error with given message.

Return type:

NoReturn

class pudding.tokens.statements.Grammar(lineno, name, values)[source]

Class for grammar statement.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Execute this token.

Parameters:

context (Context) – Current context object.

Raises:

SyntaxError – Can not be executed.

Return type:

PAction

class pudding.tokens.statements.FromImport(lineno, name, values)[source]

Class for from … import … statement.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

classmethod from_string(string, lineno)[source]

Create FromImport statement from string.

Parameters:
  • string (str) – String containing the token.

  • lineno (int) – Line number of the token.

Return type:

Self

execute(context)[source]

Execute this token.

Parameters:

context (Context) – Current context object.

Raises:

SyntaxError – Can not be executed.

Return type:

PAction

class pudding.tokens.statements.Import(lineno, name, values)[source]

Class for import statement.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Execute this token.

Parameters:

context (Context) – Current context object.

Raises:

SyntaxError – Can not be executed.

Return type:

PAction

class pudding.tokens.statements.Match(lineno, name, values)[source]

Class for match statement.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)

Execute this token.

Parameters:
Returns:

PAction for processor class.

Return type:

PAction

class pudding.tokens.statements.IMatch(lineno, name, values)[source]

Class for imatch statement.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)

Execute this token.

Parameters:
Returns:

PAction for processor class.

Return type:

PAction

class pudding.tokens.statements.Next(lineno, name, values)[source]

Class for next statement.

Skip the current match and continue with the next match statement without jumping back to the top of the current grammar block. This function is rarely used and probably not what you want, unless it is for some performance-specific hacks.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Continue with the next token of the grammar.

Parameters:

context (Context) – Current context object.

Returns:

Returns PAction.NEXT for processor class.

Return type:

PAction

class pudding.tokens.statements.Return(lineno, name, values)[source]

Class for return statement.

Immediately leave the current grammar block and return to the calling function. When used at the top level (i.e. in the input grammar), stop parsing.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)[source]

Immediately leave the current grammar.

Parameters:

context (Context) – Current context object.

Returns:

Returns PAction.EXIT for processor class.

Return type:

PAction

class pudding.tokens.statements.Skip(lineno, name, values)[source]

Class for skip statement.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)

Execute this token.

Parameters:
Returns:

PAction for processor class.

Return type:

PAction

class pudding.tokens.statements.ISkip(lineno, name, values)[source]

Class for iskip statement.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)

Execute this token.

Parameters:
Returns:

PAction for processor class.

Return type:

PAction

class pudding.tokens.statements.IWhen(lineno, name, values)[source]

Class for iwhen statement.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)

Execute this token.

Parameters:
Returns:

PAction for processor class.

Return type:

PAction

class pudding.tokens.statements.When(lineno, name, values)[source]

Class for when statement.

Parameters:
  • lineno (int)

  • name (str)

  • values (tuple[Data, ...])

execute(context)

Execute this token.

Parameters:
Returns:

PAction for processor class.

Return type:

PAction

Writer Module

Package for writing output.

class pudding.writer.Json(file_path, *, encoding='utf-8', root_name='root')[source]

Writer class for json output.

Parameters:
  • file_path (Path)

  • encoding (str)

  • root_name (str)

serialize_node(node, parent)[source]

Convert node object to json.

Parameters:
  • node (Node)

  • parent (JsonType)

Return type:

JsonType

generate_output()[source]

Generate output in specified format.

Return type:

str

class pudding.writer.Writer(file_path, *, encoding='utf-8', root_name='root')[source]

Base writer class.

Variables:
  • attrib_re – Regex for node attributes.

  • node_re – Regex for a node path.

Parameters:
  • file_path (Path)

  • encoding (str)

  • root_name (str)

add_attribute(path, name, value)[source]

Add an attribute to an element.

Parameters:
  • path (str) – Path of the element.

  • name (str) – Name of the attribute.

  • value (str) – Value of the attribute.

Return type:

None

create_element(path, value=None)[source]

Add an element to the current node.

Parameters:
  • path (str) – Path of the element.

  • value (str | None) – Value of the element or None if it has no value.

Returns:

The created element.

Return type:

Any

add_element(path, value=None)[source]

Add an element if it not already exists.

Otherwise it appends the string to the already existing element.

Parameters:
  • path (str) – Path to the element.

  • value (str | None) – Value of the element or None if it has no value.

Returns:

The created element.

Return type:

Any

enter_path(path, value=None)[source]

Enter a node and create elements in the path if they do not already exist.

Parameters:
  • path (str) – Path to the element.

  • value (str | None) – Value of the element or None if it has no value.

Return type:

None

open_path(path, value=None)[source]

Enter a node and create elements in the path if they do not already exist.

Always creates the last node.

Parameters:
  • path (str) – Path to the element.

  • value (str | None) – Value of the element or None if it has no value.

Return type:

None

leave_paths(amount=1)[source]

Leave the previously entered path.

Parameters:

amount (int) – Number of paths to leave.

Return type:

None

delete_element(path)[source]

Delete an element.

Parameters:

path (str) – Path of the element.

Return type:

None

replace_element(path, value=None)[source]

Replace an element.

Parameters:
  • path (str) – Path of the element.

  • value (str | None) – Value of the replaced element or None if it has no value.

Return type:

None

generate_output()[source]

Generate output in specified format.

Return type:

str

write_output()[source]

Write generated output to file.

Parameters:

file_path – Path of the file to write to.

Return type:

None

class pudding.writer.Xml(file_path, *, encoding='utf-8', root_name='xml')[source]

Writer class for xml output.

Parameters:
  • file_path (Path)

  • encoding (str)

  • root_name (str)

serialize_node(node)[source]

Convert node object to etree element.

Parameters:

node (Node)

Return type:

Element

generate_output()[source]

Generate output in specified format.

Return type:

str

write_output()[source]

Write generated output to file.

Return type:

None

class pudding.writer.Yaml(file_path, *, encoding='utf-8', root_name='root')[source]

Base writer class.

Parameters:
  • file_path (Path)

  • encoding (str)

  • root_name (str)

generate_output()[source]

Generate output in specified format.

Return type:

str

Node class for caching generated output.

class pudding.writer.node.Node(name, attributes=None, text=None)[source]

Class representing a node.

Parameters:
  • name (str)

  • attributes (dict[str, str] | None)

  • text (str | None)

classmethod from_path(path, text=None)[source]

Parse node object from path.

Parameters:
  • path (str) – Node path of the object.

  • text (str | None) – Text of the created node object.

Returns Node:

The created node object.

Return type:

Self

classmethod parse_node_path(path)[source]

Read tag name and attributes from an node.

Parameters:

path (str) – Path node to parse.

Returns:

Tuple with name as string and attributes as a dict.

Return type:

tuple[str, dict[str, str]]

classmethod split_path(path)[source]

Split the path into nodes.

Parameters:

path (str) – Path to split.

Returns:

List of node matches as a tuple. E.g. [(full_nodepath, [./]*, tag, attributes), …]

Return type:

list[tuple[str, str, str, str]]

property node_path: str

Return node as path.

add_child(node_path, text=None)[source]

Create a child node of this node.

Parameters:
  • node_path (str) – Path of the node to add.

  • text (str | None)

Returns Node:

The created and added node.

Return type:

Self

find(path)[source]

Find a child in the given path.

Parameters:

path (str) – Path to the node.

Returns:

The node or None if it does not exist.

Return type:

Self | None

set(name, value)[source]

Set an attribute of this node.

Parameters:
  • name (str) – Name of the attribute.

  • value (str) – Value of the attribute.

  • self (Self)

Return type:

None

get(name, default=None)[source]

Get an attribute of this node.

Parameters:
  • name (str) – Name of the attribute.

  • default (None)

Return type:

str | None

get_sorted_children()[source]

Return a list of children sorted by name.

Return type:

list[Self]