Reference parsers¶
references¶
- class momotor.options.parser.reference.Reference(provider, id, name, _source)¶
A reference
- class momotor.options.parser.reference.ReferenceMatch(provider, values)¶
A reference match
- provider: Union[momotor.bundles.recipe.RecipeBundle, momotor.bundles.config.ConfigBundle, momotor.bundles.product.ProductBundle, momotor.bundles.elements.steps.Step, momotor.bundles.elements.result.Result]¶
The provider containing the elements referenced
- values: Tuple[Union[momotor.bundles.elements.properties.Property, momotor.bundles.elements.files.File, momotor.bundles.elements.options.Option, momotor.bundles.elements.result.Result]]¶
A tuple with the referenced elements
- momotor.options.parser.reference.generate_reference(type_, refs, bundles)¶
A generator producing reference matches.
Each
ReferenceMatch
object generated is a single reference resolved.
- momotor.options.parser.reference.parse_reference(reference)¶
Parse a reference into its parts.
- Parameters
reference (
str
) – the reference to parse- Return type
- Returns
a 3-tuple containing the
type
, a tuple ofReference
objects, and a string with the rest of the reference string remaining after parsing.- Raises
ValueError – the reference cannot be parsed
Examples:
>>> parse_reference("type") ('type', (Reference(provider=None, id=None, name=None, _source=''),), '')
>>> parse_reference("type rest") ('type', (Reference(provider=None, id=None, name=None, _source=''),), ' rest')
>>> parse_reference("type[] rest") ('type', (Reference(provider=None, id=None, name=None, _source=''),), ' rest')
>>> parse_reference("type[@provider] rest") ('type', (Reference(provider='provider', id=None, name=None, _source='@provider'),), ' rest')
>>> parse_reference("type[@provider#id:class#name] rest") ('type', (Reference(provider='provider', id='id', name='class#name', _source='@provider#id:class#name'),), ' rest')
>>> parse_reference("type[@provider#id,id2:class#name] rest") ('type', (Reference(provider='provider', id='id,id2', name='class#name', _source='@provider#id,id2:class#name'),), ' rest')
- momotor.options.parser.reference.resolve_reference_value(value_reference, bundles, *, default_mod='join')¶
Resolve a reference value string into the value
- momotor.options.parser.reference.select_by_file_reference(reference, bundles)¶
Parse a file
reference
string and collect the referenced files.This is similar to the
file[...]
reference syntax, but does not require thefile
nor the square brackets.- Parameters
- Return type
- Returns
a 2-tuple containing a tuple of
ReferenceMatch
objects, and a string with the rest of the reference string remaining after parsing.
- momotor.options.parser.reference.select_by_opt_reference(reference, bundles)¶
Parse an option
reference
string and collect the referenced options.This is similar to the
opt[...]
reference syntax, but does not require theopt
nor the square brackets.- Parameters
- Return type
- Returns
a 2-tuple containing a tuple of
ReferenceMatch
objects, and a string with the rest of the reference string remaining after parsing.
- momotor.options.parser.reference.select_by_prop_reference(reference, results=None, task_id=None)¶
Parse a property
reference
string and collect the referenced properties.This is similar to the
prop[...]
reference syntax, but does not require theprop
nor the square brackets.- Parameters
reference (
str
) – The reference to parseresults (
Optional
[ResultsBundle
]) – The results bundle containing the propertiestask_id (
Optional
[StepTaskId
]) – The task id to expand task references
- Return type
- Returns
a 2-tuple containing a tuple of
ReferenceMatch
objects, and a string with the rest of the reference string remaining after parsing.
- momotor.options.parser.reference.select_by_reference(reference, bundles)¶
Parse a reference string and collect the referenced items
- Parameters
- Return type
- Returns
a 3-tuple containing the
type
, a tuple ofReferenceMatch
objects, and a string with the rest of the reference string remaining after parsing.- Raises
ValueError – the reference cannot be parsed
selectors¶
- momotor.options.parser.selector.filter_by_selector(selector, bundles)¶
Filter the elements selected by selector from the bundles
- Parameters
- Return type
- Returns
a 2-tuple containing a tuple with the selected elements, and a string with the rest of the selector string remaining after parsing.
- Raises
ValueError – if the selector is not valid
- momotor.options.parser.selector.match_by_selector(selector, bundles, *, default_mod='all')¶
Match the elements selected by match selector from the bundles
- Parameters
- Return type
- Returns
a 2-tuple containing a boolean indicating if there was a match, and a string with the rest of the selector string remaining after parsing.
- Raises
ValueError – if the selector is not valid
- momotor.options.parser.selector.parse_selector(selector)¶
Parse a selector into its parts.
- Parameters
- Returns
a 5-tuple containing the
type
, a tuple ofReference
objects, the operator, the value, and a string with the rest of the selector string remaining after parsing.- Raises
ValueError – the selector cannot be parsed
Examples:
>>> parse_selector('pass') ('pass', (Reference(provider=None, id=None, name=None, _source=''),), None, None, '')
>>> parse_selector('prop[#test]?') ('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '?', None, '')
>>> parse_selector('prop[#test]?123') ('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '?', None, '123')
>>> parse_selector('prop[#test]>0') ('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', 0, '')
>>> parse_selector('prop[#test]>1_000') ('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', 1000, '')
>>> parse_selector('prop[#test]>1.5') ('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', 1.5, '')
>>> parse_selector('prop[#test]>-2') ('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', -2, '')
>>> parse_selector('prop[#test]>-2e2') ('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', -200.0, '')
>>> parse_selector('prop[#test]>2e-2') ('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', 0.02, '')
>>> parse_selector('prop[#test]=="test string"') ('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '==', 'test string', '')
>>> parse_selector('prop[#test]>0 123') ('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', 0, ' 123')
- momotor.options.parser.selector.resolve_selector(selector, bundles)¶
Resolve all parts of a selector
- Parameters
- Return type
Tuple
[str
,Iterable
[ReferenceMatch
],Callable
,Union
[None
,str
,int
,float
],str
]- Returns
A 5-tuple containing the attribute to get the reference value from (based on
type
), an iterator forReferenceMatch
objects, the operator, the value, and a string with the rest of the selector string remaining after parsing.- Raises
ValueError – if the selector is not valid
placeholders¶
- momotor.options.parser.placeholders.replace_placeholders(value, bundles, *, value_processor=None)¶
Replace all placeholders in value with their resolved values. Placeholders are resolved recursively, i.e. if a resolved value contains more placeholders, these will be resolved as well.
- Parameters
value (~VT) – the string containing placeholders to resolve. If
value
is not a string, no processing is done andvalue
is returned unmodified.bundles (
Providers
) – the bundles to resolve the references toovalue_processor (
Optional
[Callable
[[Optional
[str
]],str
]]) – a callable that is called with every resolved value, can be used to modify placeholders. If not supplied, usesstr
to cast the returned value to a string.
- Return type
~VT
- Returns
the value with all placeholders resolved