finesse.script.adapter module
Interface between script and Finesse.
Adapters provide a programmatic way to retrieve useful information about KatScript directives using their corresponding Python objects, and vice versa. This is primarily used for the compiler and generator, but is also used to improve syntax suggestions and error messages.
The adapter class hierarchy is intentionally very generic. By default, an ordinary KatScript directive corresponding to an ordinary Finesse object is quite simple to specify and should “just work”. When special behaviour is required, e.g. in cases where the KatScript directive has different arguments than the corresponding Python object (e.g. tem) or where there is no corresponding Python object (e.g. modes), the methods and members of the adapter can be overridden.
- class finesse.script.adapter.AnalysisDocumenter(*, item_type, **kwargs)[source]
Bases:
NumpyStyleDocstringGetterMixin,FunctionalSyntaxMixin,ItemDocumenter
- class finesse.script.adapter.AnalysisDump(analysis: Any, adapter: ItemAdapter, parameters: List[ArgumentDump] | List[List[ArgumentDump]], is_default: bool)[source]
Bases:
objectA set of analysis parameters and metadata.
- adapter: ItemAdapter
- parameters: List[ArgumentDump] | List[List[ArgumentDump]]
- class finesse.script.adapter.AnalysisDumper(*, item_type, **kwargs)[source]
Bases:
SignatureAttributeParameterMixin,ItemDumper- dump(adapter, analysis)[source]
Get analysis dump object(s) for analysis.
Parameters
- adapter
ItemAdapter The adapter corresponding to this getter.
- analysis
Action The action to dump.
Yields
AnalysisDumpObject containing a mapping of keyword argument names to
ArgumentDumpobjects and whether they are all default values.
- adapter
- class finesse.script.adapter.AnalysisFactory(*, item_type)[source]
Bases:
ItemFactory
- class finesse.script.adapter.Argument(name: str, kind: ArgumentType = ArgumentType.ANY, default: Any = <class 'finesse.script.adapter._empty'>, annotation: Any = <class 'finesse.script.adapter._empty'>)[source]
Bases:
objectA generic instruction argument.
This is similar but not identical to
inspect.Parameter. Arguments in the Finesse sense are more general thaninspect.Parametersince they can refer to KatScript arguments, and KatScript instructions may not necessarily define their supported arguments via Python class signatures.- kind: ArgumentType = _ParameterKind.POSITIONAL_OR_KEYWORD
- class finesse.script.adapter.ArgumentDump(name: str, kind: ArgumentType = ArgumentType.ANY, default: Any = <class 'finesse.script.adapter._empty'>, annotation: Any = <class 'finesse.script.adapter._empty'>, value: Any = <class 'finesse.script.adapter._empty'>, other_defaults: List[Any] = None, reference: bool = False)[source]
Bases:
ArgumentA Finesse object argument name, its current value, default value, kind, annotation, and whether it should be dumped by value or reference.
This encapsulates an argument for a script instruction. It can represent Finesse object parameters like floats, strings and model parameters, and is primarily used to generate KatScript representations of Finesse objects.
- class finesse.script.adapter.ArgumentType(*values)[source]
Bases:
EnumSignature argument types.
While we just copy those defined by
inspect, note that the definitions here are more abstract than those ofinspect: these refer to the different flavours of script argument, which may or may not map directly to or from a Python type’s call signature.- ANY = _ParameterKind.POSITIONAL_OR_KEYWORD
- KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
- POS_ONLY = _ParameterKind.POSITIONAL_ONLY
- VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
- VAR_POS = _ParameterKind.VAR_POSITIONAL
- class finesse.script.adapter.BoundArgument(name: str, kind: ArgumentType = ArgumentType.ANY, default: Any = <class 'finesse.script.adapter._empty'>, annotation: Any = <class 'finesse.script.adapter._empty'>, var_sequence: int = None)[source]
Bases:
ArgumentA concrete argument originating from a call to a setter.
This is the same as
Argumentexcept in its handling of variadic arguments. Where this represents a variadic argument, it contains information as to which variadic argument it represents (either the sequence number or keyword). It is used to resolve self-references and to map compilation errors back to the original script.
- class finesse.script.adapter.CommandDump(adapter: ItemAdapter, parameters: List[ArgumentDump] | List[List[ArgumentDump]], is_default: bool)[source]
Bases:
objectA set of command parameters and metadata.
- adapter: ItemAdapter
- parameters: List[ArgumentDump] | List[List[ArgumentDump]]
- class finesse.script.adapter.CommandMethodDocumenter(*, sig_ignore=('self',), **kwargs)[source]
Bases:
NumpyStyleDocstringGetterMixin,FunctionalSyntaxMixin,ItemDocumenter
- class finesse.script.adapter.CommandPropertyDocumenter(*, item_type, **kwargs)[source]
Bases:
CommandMethodDocumenter
- class finesse.script.adapter.CommandPropertyDumper(*, item_type, default=<class 'finesse.script.adapter._empty'>, **kwargs)[source]
- class finesse.script.adapter.CommandPropertySetter(*, item_type, **kwargs)[source]
Bases:
CommandMethodSetter
- class finesse.script.adapter.ElementDocumenter(*, item_type, **kwargs)[source]
Bases:
NumpyStyleDocstringGetterMixin,ElementSyntaxMixin,ItemDocumenter
- class finesse.script.adapter.ElementDump(element: ModelElement, adapter: ItemAdapter, parameters: List[ArgumentDump] | List[List[ArgumentDump]], is_default: bool)[source]
Bases:
objectA set of element parameters and metadata.
- adapter: ItemAdapter
- element: ModelElement
- parameters: List[ArgumentDump] | List[List[ArgumentDump]]
- class finesse.script.adapter.ElementFactory(last=False, **kwargs)[source]
Bases:
ItemFactory
- class finesse.script.adapter.ElementSyntaxMixin(*, sig_type=None, sig_ignore=None, **kwargs)[source]
Bases:
SyntaxMixin
- class finesse.script.adapter.FunctionalSyntaxMixin(*, sig_type=None, sig_ignore=None, **kwargs)[source]
Bases:
SyntaxMixin
- class finesse.script.adapter.ItemAdapter(full_name, short_name=None, other_names=None, getter=None, factory=None, setter=None, documenter=None, singular=False, build_last=False)[source]
Bases:
objectAdapter defining how a script instruction maps to/from a Python type.
This encapsulates the required information to take a script instruction and generate a corresponding Python object (e.g. a
Laserfrom a laser l1 … instruction), to add it to aModel(or in the case of commands, set some model attribute), to dump that Python object back to script, and to generate documentation.Parameters
- full_name
str The instruction’s unabbreviated name. This must be alphanumeric and can contain underscores but no spaces.
- short_name
str, optional The instruction’s short form name, used when generating compact script. If not specified, full_name is used in cases where the short form is desired.
- other_namessequence, optional
Any other supported names for this instruction.
- getter
ItemDumper, optional Object handling the retrieval of parameters from the Python object corresponding to this instruction.
- factory
ItemFactory, optional Object handling the creation of the Python object corresponding to this instruction.
- setter
ItemSetter, optional Object handling the setting of parameters in the Python object corresponding to this instruction’s parameters.
- documenter
ItemDocumenter Object handling the retrieval of docstrings and syntax suggestions for the instruction.
- singular
bool, optional Flag indicating that this instruction can be defined only once per script. Defaults to False.
- build_last
bool, optional Whether to build the Python object last, regardless of dependencies. This is useful for elements with implicit dependencies (see e.g. the cavity adapter). Be careful using this flag because statements for other adapters that depend on statements for adapters with this flag will be built first. Defaults to False.
- full_name
- class finesse.script.adapter.ItemDocumenter(*, item_type)[source]
Bases:
ItemHandler
- class finesse.script.adapter.ItemDumper(*, item_type)[source]
Bases:
ItemHandler
- class finesse.script.adapter.ItemFactory(*, item_type)[source]
Bases:
ItemHandler
- class finesse.script.adapter.ItemHandler(*, item_type)[source]
Bases:
objectRoot class for all dumper, setter, factory and documenter objects.
- class finesse.script.adapter.ItemSetter(*, item_type)[source]
Bases:
ItemHandler- bind_argument(name_or_index)[source]
Return a bound argument object for name.
Parameters
Returns
BoundArgumentThe argument metadata corresponding to name_or_index.
- keyword_args(only=False)[source]
The non-positional-only arguments of the call signature.
Parameters
- only
bool, optional Only include keyword-only arguments; defaults to False.
Returns
dictThe call object’s keyword parameters.
- only
- positional_args(only=False, keyword_defaults=True)[source]
The non-keyword-only arguments of the call signature.
Parameters
Returns
dictThe call object’s positional parameters.
- update_parameter(item, argument, value)[source]
Update the built item’s name parameter to value.
This is used to update a parameter after the item has been created and added to the model, such as when resolving self-references.
Parameters
- itemobject
The item.
- argument
BoundArgument The item argument corresponding to the value to update.
- valueobject
The new value.
- class finesse.script.adapter.NumpyStyleDocstringGetterMixin(*, doc_type=None, **kwargs)[source]
Bases:
object- argument_descriptions()[source]
The types and descriptions for each argument as parsed from the docstring.
Returns
dictMapping of arguments to their type and docstrings as listed in the object’s docstring. Note that the arguments may not correspond to signature argument names; numpydoc allows arguments to share docstrings so some keys may be e.g. n, m.
- property docstring[source]
The Python API item’s docstring.
Note: unlike
inspect.getdoc(), this method returns only the docstring directly defined in doc_type, rather than taking the inherited docstring if not found. If no docstring is defined, None is returned.
- class finesse.script.adapter.SignatureArgumentMixin(*, sig_type=None, sig_ignore=None, **kwargs)[source]
Bases:
objectMixin providing ability to retrieve signature arguments from a Python function.
Parameters
- sig_typetype, optional
The signature type to retrieve arguments from. Defaults to item_type.
- sig_ignoresequence, optional
Signature argument names to ignore.
- class finesse.script.adapter.SignatureAttributeParameterMixin(ref_args=None, var_pos_attr=None, var_keyword_attr=None, **kwargs)[source]
Bases:
SignatureArgumentMixinMixin providing the ability to get and set item parameters by inspecting its constructor signature arguments matching equivalently named object attributes.
Parameters
- ref_argssequence, optional
Names of arguments that should be considered to be references. Corresponding
ArgumentDumpobjects produced by this class will have their reference flags set to True to indicate to the generator that these should be treated as references instead of values.- var_pos_attrstr or callable, optional
The name of the field containing a sequence of variadic positional argument values, or a callable that returns the name of the field to set given the sequence number of the variadic argument, if the signature supports variadic positional arguments. Defaults to “args”.
- var_keyword_attrstr or callable, optional
The name of the field containing a mapping of variadic keyword arguments to values, or a callable that returns the name of the field to set given the name of the keyword argument, if the signature supports variadic positional arguments. Defaults to the identity function.
- class finesse.script.adapter.SyntaxMixin(*, sig_type=None, sig_ignore=None, **kwargs)[source]
Bases:
SignatureArgumentMixin