finesse.script.generator module

Kat script generator.

class finesse.script.generator.CommandContainer(adapter, params)[source]

Bases: object

Container for top level commands, used by the generator.

class finesse.script.generator.ElementContainer(element)[source]

Bases: object

Container for top level model elements, used by the generator.

class finesse.script.generator.KatUnbuilder(show_warnings=True, spec=None)[source]

Bases: object

Model to KatScript converter.

This class defines single-dispatch methods to recursively fill Finesse objects into a KatGraph. The graph is then serialised to KatScript via KatUnfiller.

The single-dispatch uses the type of the object to create appropriate nodes and edges in the KatGraph.

Typically unbuilding starts with the passing of a Model to unbuild() or unbuild_file(). The single-dispatch method that handles models then extracts the elements, commands and analyses and passes them recursively to the same single-dispatch, whereupon matching fill methods further handle them. The underlying tokens that form Finesse objects are eventually reached by recursively calling the fill method, and these are added as terminal nodes in the graph and connected to their parents by edges.

Special behaviour applies in some cases. One such case is the handling of objects from containers with corresponding type annotations specifying that they are Parameter objects. These are assumed to have name KatScript representation, rather than value. This is used for example in the dumping of axis parameters, which must be written as e.g. xaxis(m1.phi, …) rather than xaxis(0, …) (in the case that m1.phi has value 0).

IGNORED_ELEMENTS = ['Fsig']
LITERAL_MAP = {'(': 'LPAREN', ')': 'RPAREN', '*': 'TIMES', '**': 'POWER', '+': 'PLUS', ',': 'COMMA', '-': 'MINUS', '/': 'DIVIDE', '//': 'FLOORDIVIDE', '=': 'EQUALS', '[': 'LBRACKET', ']': 'RBRACKET'}
UNARY_OPERATORS = {'neg': '-', 'pos': '+'}
property index[source]
property lineno[source]
property position[source]
unbuild(*args, **kwargs)[source]
unbuild_file(fobj, item, ref_graph=None, ref_node=None, directive_defaults=False, argument_defaults=False, prefer_keywords=True)[source]

Generate KatScript for item and write to fobj.

Parameters

fobjio.FileIO

The file object to write KatScript to. This should be opened in text mode.

itemobject

The object to generate KatScript for. This is normally a Model but can be any Finesse object.

ref_graphKatGraph, optional

The reference syntax graph to use during KatScript generation. If specified, the nodes representing item and any Finesse objects contained therein will be copied and updated from it. This preserves formatting such as whitespace and comments.

ref_nodestr, optional

The reference node to use when unparsing item. This is only used if ref_graph is specified. If ref_graph is given but ref_node is not, ref_node is assumed to be the graph root. If the graph does not contain a root node, a ValueError is raised.

directive_defaultsbool, optional

Generate default elements, commands and actions even if they are set to their default values and would therefore generate Finesse objects in the same state when later parsed. If ref_graph is specified and an element, command or action is present as a node, however, it is generated regardless of whether its value is the default or not. Defaults to False.

argument_defaultsbool, optional

Generate optional arguments that are set to their default values and would therefore generate Finesse objects in the same state when later parsed. If ref_graph is specified and a keyword argument is present as a node, however, it is generated regardless of whether its value is the default or not. Defaults to False.

prefer_keywordsbool, optional

Prefer generating keyword arguments over positional arguments, where allowed. If a parameter can be a keyword argument, i.e. it is not positional-only, it is dumped as a keyword argument if this is True. This setting is ignored if ref_node is specified and a reference argument for the parameter is found (whereupon the original style is reused). Defaults to True.

class finesse.script.generator.KatUnfiller[source]

Bases: object

KatGraph to kat script.

production(node, graph)[source]
unfill(node, graph)[source]
unfill_file(fobj, node, graph)[source]
class finesse.script.generator.KatUnparser[source]

Bases: object

TokenContainer to kat script.

unparse(container)[source]
unparse_file(fobj, container)[source]
class finesse.script.generator.KatUntokenizer[source]

Bases: object

Token to kat script.

untokenize(tokens)[source]
untokenize_file(fobj, tokens)[source]