finesse.script.generator module
Kat script generator.
- class finesse.script.generator.CommandContainer(adapter, params)[source]
Bases:
objectContainer for top level commands, used by the generator.
- class finesse.script.generator.ElementContainer(element)[source]
Bases:
objectContainer for top level model elements, used by the generator.
- class finesse.script.generator.KatUnbuilder(show_warnings=True, spec=None)[source]
Bases:
objectModel to KatScript converter.
This class defines single-dispatch methods to recursively fill Finesse objects into a
KatGraph. The graph is then serialised to KatScript viaKatUnfiller.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
Modeltounbuild()orunbuild_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
Parameterobjects. 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': '+'}
- 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
- fobj
io.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
Modelbut can be any Finesse object.- ref_graph
KatGraph, 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_node
str, 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
ValueErroris raised.- directive_defaults
bool, 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_defaults
bool, 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_keywords
bool, 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.
- fobj