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
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)
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)
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)
Processor Module
Package for processing tokens of the compiled syntax.
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:
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)
- 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:
- 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:
- 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.
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)
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
- 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, ...])
- 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, ...])
- 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, ...])
- 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, ...])
- 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, ...])
- 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, ...])
- 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, ...])
- 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, ...])
- 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, ...])
- 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, ...])
- 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, ...])
- 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, ...])
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, ...])
- 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, ...])
- class pudding.tokens.statements.Grammar(lineno, name, values)[source]
Class for grammar statement.
- Parameters:
lineno (int)
name (str)
values (tuple[Data, ...])
- class pudding.tokens.statements.FromImport(lineno, name, values)[source]
Class for from … import … statement.
- Parameters:
lineno (int)
name (str)
values (tuple[Data, ...])
- class pudding.tokens.statements.Import(lineno, name, values)[source]
Class for import statement.
- Parameters:
lineno (int)
name (str)
values (tuple[Data, ...])
- class pudding.tokens.statements.Match(lineno, name, values)[source]
Class for match statement.
- Parameters:
lineno (int)
name (str)
values (tuple[Data, ...])
- class pudding.tokens.statements.IMatch(lineno, name, values)[source]
Class for imatch statement.
- Parameters:
lineno (int)
name (str)
values (tuple[Data, ...])
- 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, ...])
- 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, ...])
- class pudding.tokens.statements.Skip(lineno, name, values)[source]
Class for skip statement.
- Parameters:
lineno (int)
name (str)
values (tuple[Data, ...])
- class pudding.tokens.statements.ISkip(lineno, name, values)[source]
Class for iskip statement.
- Parameters:
lineno (int)
name (str)
values (tuple[Data, ...])
- class pudding.tokens.statements.IWhen(lineno, name, values)[source]
Class for iwhen statement.
- Parameters:
lineno (int)
name (str)
values (tuple[Data, ...])
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)
- 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
- 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)
- 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)
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